admin管理员组

文章数量:1323524

I'm trying to deploy my first Voting contract on the testRPC and the below is my code.. for some reason it's plaining when I e 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')
piledCode = solcpile(sourceCode)
abiDefinition = JSON.parse(piledCode.contracts[':Voting'].interface)
VotingContract = new web3.eth.Contract(abiDefinition)
byteCode = piledCode.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)
}

I'm trying to deploy my first Voting contract on the testRPC and the below is my code.. for some reason it's plaining when I e 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')
piledCode = solc.pile(sourceCode)
abiDefinition = JSON.parse(piledCode.contracts[':Voting'].interface)
VotingContract = new web3.eth.Contract(abiDefinition)
byteCode = piledCode.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)
}
Share Improve this question edited May 30, 2022 at 19:53 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked Aug 19, 2017 at 20:06 OcirneOcirne 3231 gold badge6 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

In the migrations, add your args in the deployer.

// Deploy a single contract with constructor arguments
deployer.deploy(A, arg1, arg2, ...);

where A is your smart contract, arg1, arg2 etc are the arguments

This is mentioned in the documentation: https://www.trufflesuite./docs/truffle/getting-started/running-migrations#deployer-deploy-contract-args-options-

Try something like this for contracts that need constructor arguments

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

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./ethereum/wiki/wiki/JavaScript-API#web3ethcontract

本文标签: