问题:以太坊合约部署失败,报错:TypeError: param.map is not a function 或者 Error: Invalid number of parameters for "undefined". Got 2 expected 1!
解决:官方文档写的比较特别,所以被误导了。web3使用的是1.7.0
方法:
官方文档:
const myContract = await new web3.eth.Contract(JSON.parse(interface));
myContract.deploy({
data: '0x12345...',
arguments: [123, 'My String']
})
.send({
from: '0x1234567890123456789012345678901234567891',
gas: 1500000,
gasPrice: '30000000000000'
}, function(error, transactionHash){ ... })
.on('error', function(error){ ... })
.on('transactionHash', function(transactionHash){ ... })
.on('receipt', function(receipt){
console.log(receipt.contractAddress) // contains the new contract address
})
.on('confirmation', function(confirmationNumber, receipt){ ... })
.then(function(newContractInstance){
console.log(newContractInstance.options.address) // instance with the new contract address
});
文档中arguments传递的是数组,是指【参数1, 参数2,参数3】
我的合约中是需要传递一个数组的,所以需要写成[['alice', 'bob']]
var result = await new web3.eth.Contract(JSON.parse(interface))
.deploy({ data: bytecode, arguments: [[web3.utils.asciiToHex('Alice'), web3.utils.asciiToHex('Bob')]] })
.send({ from: accounts[0], gas: 1000000 });
console.log(result.options.address);
参考:
https://ethereum.stackexchange.com/questions/55594/deploying-contract-with-parameters-using-web3-js-and-node-js
https://ethereum.stackexchange.com/questions/67429/how-to-pass-arguments-to-the-constructor-while-testing-an-contract-using-web3