Reputation: 1
I'm following an Ethereum Dapp tutorial on Udemy. I seem to stumble upon an error somewhere early in the course. I was about to deploy my beginner's contract and test it with Mocha when the problem appeared.
I have found fixes online but does not fix mine. I suspect it's about the versioning of web3 or solc. I am using the latest versions of both packages. The tutorial I'm following uses older versions that are already depreciated.
Inbox.sol
pragma solidity >=0.4.0 <0.6.0;
contract Inbox{
string message;
function set(string memory initialMessage) public {
message = initialMessage;
}
function setMessage(string memory newMessage) public{
message = newMessage;
}
}
Compile.js
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8');
module.exports = solc.compile(source, 1).contracts[':Inbox'];
Inbox.test.js:
const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const {interface, bytecode} = require('../compile');
let accounts;
let inbox;
beforeEach(async ()=>{
accounts = await web3.eth.getAccounts();
inbox = await new web3.eth.Contract(JSON.parse(interface))
.deploy({
data: bytecode,
arguments:["Hi there!"]
})
.send({from:accounts[0] , gas: 1000000});
});
describe("Inbox", ()=>{
it("deploys a contract", () => {
console.log(inbox)
});
});
The error on my terminal says:
before each" hook for "deploys a contract":
Error: types/values length mismatch (count={"types":0,"values":1}, value={"types":[],"values":["Hi there!"]}, version=4.0.32)
at Object.throwError (node_modules\ethers\errors.js:76:17)
at AbiCoder.encode (node_modules\ethers\utils\abi-coder.js:922:20)
at AbiCoder.encodeParameters (node_modules\web3-eth-abi\dist\web3-eth-abi.cjs.js:45:34)
at MethodEncoder.encode (node_modules\web3-eth-contract\dist\web3-eth-contract.cjs.js:143:45)
at MethodsProxy.createMethod (node_modules\web3-eth-contract\dist\web3-eth-contract.cjs.js:556:57)
at MethodsProxy.executeMethod (node_modules\web3-eth-contract\dist\web3-eth-contract.cjs.js:534:23)
at Function.ContractMethod.send (node_modules\web3-eth-contract\dist\web3-eth-contract.cjs.js:505:29)
at Context.beforeEach (test\Inbox.test.js:19:6)
at process._tickCallback (internal/process/next_tick.js:68:7)
My package.json:
{
"name": "inbox",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"author": "Ryan Arcel Galendez",
"license": "ISC",
"dependencies": {
"ganache-cli": "^6.4.4",
"mocha": "^6.1.4",
"solc": "^0.4.26",
"web3": "^1.0.0-beta.55"
}
I expect my Mocha test to display "success".
Upvotes: 0
Views: 6143
Reputation: 456
The issue is that you don't have a constructor that accepts a single parameter, which is why when you deploy an instance of the contract and pass in an initial message of "Hi there!" it fails.
It looks like your set function should be the constructor based on the parameter name.
function set(string memory initialMessage) public {
message = initialMessage;
}
You should change function set
to constructor
pragma solidity >=0.4.0 <0.6.0;
contract Inbox{
string public message;
constructor(string memory initialMessage) public {
message = initialMessage;
}
function setMessage(string memory newMessage) public{
message = newMessage;
}
}
You may want to look at tools such as Truffle and/or ZeppelinOS to make smart contract development easier.
Upvotes: 1