Search code examples
javascriptnode.jsethereumsolidityweb3js

Getting Invalid number of parameters for "undefined" when deploying smart contract


I'm trying to deploy my first Voting contract on the testRPC and the below is my code.. for some reason it's complaining when I come to deploy.

The error seems to be from the arguments parameter. I tried passing an empty array and it said "Got 0 expected 1!". I tried passing just one name and it says "value.forEach" is not a function.

Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
sourceCode = fs.readFileSync('Voting.sol').toString()
solc = require('solc')
compiledCode = solc.compile(sourceCode)
abiDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface)
VotingContract = new web3.eth.Contract(abiDefinition)
byteCode = compiledCode.contracts[':Voting'].bytecode

VotingContract.deploy({
    data: byteCode, 
    arguments:['Joseph','Sean','Matthew']
}).send({
    from: '0x00D1AE0A6fC13B9ecdefA118B94cF95ac16D4ab0', 
    gas: 4700000
})
.on('error', function(error) {
    console.log(error);
}
.then(function(newContractInstance) {
    console.log(newContractInstance.options.address)
}

Solution

  • Try something like this for contracts that need constructor arguments

    var bytecodeWithParam = MyContract.new.getData(
        param1,
        param2,
        { data: compiledByteCode });
    

    It is this bytecodeWithParam that you paste into the "Byte Code" field. If you look at it in detail, you will see param1 and param2 32-byte packed at the end.

    Another Example

    var MyContract = web3.eth.contract(abiArray);
    
    // instantiate by address
    var contractInstance = MyContract.at(address);
    
    // deploy new contract
    var contractInstance = MyContract.new([constructorParam1] [, constructorParam2], {data: '0x12345...', from: myAccount, gas: 1000000});
    
    // Get the data to deploy the contract manually
    var contractData = MyContract.new.getData([constructorParam1] [, constructorParam2], {data: '0x12345...'});
    // contractData = '0x12345643213456000000000023434234'
    

    https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract