[英]Getting error when compiling and deploying ethereum smart contract with Node.js
[英]Getting Invalid number of parameters for “undefined” when deploying smart contract
我正在嘗試在 testRPC 上部署我的第一個投票合約,下面是我的代碼..出於某種原因,當我開始部署時它會抱怨。
錯誤似乎來自 arguments 參數。 我嘗試傳遞一個空數組,它說“得到 0 預期 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)
}
任何幫助將不勝感激。 謝謝。
在遷移中,在部署程序中添加您的參數。
// Deploy a single contract with constructor arguments
deployer.deploy(A, arg1, arg2, ...);
其中 A 是您的智能合約,arg1、arg2 等是參數
文檔中提到了這一點: https : //www.trufflesuite.com/docs/truffle/getting-started/running-migrations#deployer-deploy-contract-args-options-
對於需要構造函數參數的合約,嘗試這樣的操作
var bytecodeWithParam = MyContract.new.getData(
param1,
param2,
{ data: compiledByteCode });
您將這個 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.