部署智能合约时获取 "undefined" 的参数数量无效
Getting Invalid number of parameters for "undefined" when deploying smart contract
我正在尝试在 testRPC 上部署我的第一个投票合约,下面是我的代码。出于某种原因,当我开始部署时它在抱怨。
错误似乎来自 arguments 参数。我尝试传递一个空数组,它说“Got 0 expected 1!”。我尝试只传递一个名称,它说“value.forEach”不是一个函数。
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)
}
为需要构造函数参数的合约尝试类似的方法
var bytecodeWithParam = MyContract.new.getData(
param1,
param2,
{ data: compiledByteCode });
您粘贴到 "Byte Code" 字段中的就是这个 bytecodeWithParam。仔细看的话,最后会看到param1和param2 32字节打包。
另一个例子
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
在迁移中,将您的参数添加到部署程序中。
// Deploy a single contract with constructor arguments
deployer.deploy(A, arg1, arg2, ...);
其中 A 是您的智能合约,arg1、arg2 等是参数
我正在尝试在 testRPC 上部署我的第一个投票合约,下面是我的代码。出于某种原因,当我开始部署时它在抱怨。
错误似乎来自 arguments 参数。我尝试传递一个空数组,它说“Got 0 expected 1!”。我尝试只传递一个名称,它说“value.forEach”不是一个函数。
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)
}
为需要构造函数参数的合约尝试类似的方法
var bytecodeWithParam = MyContract.new.getData(
param1,
param2,
{ data: compiledByteCode });
您粘贴到 "Byte Code" 字段中的就是这个 bytecodeWithParam。仔细看的话,最后会看到param1和param2 32字节打包。
另一个例子
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
在迁移中,将您的参数添加到部署程序中。
// Deploy a single contract with constructor arguments
deployer.deploy(A, arg1, arg2, ...);
其中 A 是您的智能合约,arg1、arg2 等是参数