简体   繁体   English

部署智能合约时获取“未定义”的参数数量无效

[英]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.我正在尝试在 testRPC 上部署我的第一个投票合约,下面是我的代码..出于某种原因,当我开始部署时它会抱怨。

The error seems to be from the arguments parameter.错误似乎来自 arguments 参数。 I tried passing an empty array and it said "Got 0 expected 1!".我尝试传递一个空数组,它说“得到 0 预期 1!”。 I tried passing just one name and it says "value.forEach" is not a function.我试着只传递一个名字,它说“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)
}

Any help would be much appreciated.任何帮助将不胜感激。 Thank you.谢谢。

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其中 A 是您的智能合约,arg1、arg2 等是参数

This is mentioned in the documentation: https://www.trufflesuite.com/docs/truffle/getting-started/running-migrations#deployer-deploy-contract-args-options-文档中提到了这一点: https : //www.trufflesuite.com/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: compiledByteCode });

It is this bytecodeWithParam that you paste into the "Byte Code" field.您将这个 bytecodeWithParam 粘贴到“字节码”字段中。 If you look at it in detail, you will see param1 and param2 32-byte packed at the end.仔细看,最后会看到 param1 和 param2 是 32 字节的压缩包。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Node.js 编译和部署以太坊智能合约时出错 - Getting error when compiling and deploying ethereum smart contract with Node.js 部署智能合约Web3无效的RPC响应 - Deploying smart contract web3 Invalid RPC response 在以太坊中部署智能合约时如何解决错误? - How to solve the error when deploying a smart contract in Ethereum? 在安全帽上部署合同或创建 NFT 时出现错误:ProviderError: invalid sender - On hardhat when deploying a contract or minting an NFT getting error: ProviderError: invalid sender 使用eosjs部署EOS智能合约时如何解决'missing setcode.vmtype (type=uint8)'错误? - How to solve 'missing setcode.vmtype (type=uint8)' error when deploying EOS Smart Contract using eosjs? 在Hyperledger Fabric NodeJS智能合约中,是否应将所有参数作为字符串传递? - In Hyperledger Fabric NodeJS smart contract, should all parameters be passed as a string? 使用Web 3连接到智能合约时出错 - Getting error while connecting to smart contract using web 3 在 Hedera 区块链中创建智能合约时出现错误“交易过大” - Getting error "Transaction Oversize" while creating a smart contract in Hedera blockchain 使用web3.js部署时如何获取智能合约地址 - How to get Smart Contract address when it is deployed with web3.js 执行智能合约功能 - execute a smart contract function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM