Truffle
智能合約部署失敗 PowerShell
這裡是初學者的智能合約開發人員。這是我的智能合約:
pragma solidity ^0.5.16; contract Ballot { struct Voter { uint weight; bool voted; uint8 vote; // address delegate; } struct Proposal { uint voteCount; // could add other data about proposal } address chairperson; mapping(address => Voter) voters; Proposal[] proposals; /// Create a new ballot with $(_numProposals) different proposals. constructor(uint8 _numProposals) public { chairperson = msg.sender; voters[chairperson].weight = 2; proposals.length = _numProposals; } /// Give $(toVoter) the right to vote on this ballot. /// May only be called by $(chairperson). function register(address toVoter) public { if (msg.sender != chairperson || voters[toVoter].voted) return; voters[toVoter].weight = 1; voters[toVoter].voted = false; } /// Give a single vote to proposal $(toProposal). function vote(uint8 toProposal) public { Voter storage sender = voters[msg.sender]; if (sender.voted || toProposal >= proposals.length) return; sender.voted = true; sender.vote = toProposal; proposals[toProposal].voteCount += sender.weight; } function winningProposal() public view returns (uint8 _winningProposal,uint wvc) { uint256 winningVoteCount = 0; for (uint8 prop = 0; prop < proposals.length; prop++) if (proposals[prop].voteCount > winningVoteCount) { winningVoteCount = proposals[prop].voteCount; _winningProposal = prop; wvc=winningVoteCount; } } }
在使用 Truffle compile 進行編譯時,我沒有收到任何錯誤,如下所示:
Compiling your contracts... =========================== > Compiling .\contracts\Ballot.sol > Compiling .\contracts\Migrations.sol > Artifacts written to C:\Users\Aaryamann\Desktop\simpleAddress\build\contracts > Compiled successfully using: - solc: 0.5.16+commit.9c3226ce.Emscripten.clang
但是,當我使用
truffle migrate --reset
這種情況時:Compiling your contracts... =========================== > Compiling .\contracts\Ballot.sol > Compiling .\contracts\Migrations.sol > Artifacts written to C:\Users\Aaryamann\Desktop\simpleAddress\build\contracts > Compiled successfully using: - solc: 0.5.16+commit.9c3226ce.Emscripten.clang Starting migrations... ====================== > Network name: 'development' > Network id: 5777 > Block gas limit: 0x6691b7 1_initial_migration.js ====================== Replacing 'Migrations' ---------------------- > transaction hash: 0xee307d9e6aa87830ba37b60d5e5a5fc0de67ed6f307f23a86bf9fea715b4ecc0 > Blocks: 0 Seconds: 0 > contract address: 0xFd69eD8A7425792183a50B2433E5260efC68ad16 > block number: 1 > block timestamp: 1586275003 > account: 0xe266322C7F15539244BCcF0Ac2e8C0918fc9d5D7 > balance: 99.9967165 > gas used: 164175 > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.0032835 ETH > Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.0032835 ETH 2_deploy_contracts.js ===================== Deploying 'Ballot' ------------------ Error: *** Deployment Failed *** "Ballot" -- Invalid number of parameters for "undefined". Got 0 expected 1!. at C:\Users\Aaryamann\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\deployer\src\deployment.js:364:1 at processTicksAndRejections (internal/process/task_queues.js:97:5) at Migration._deploy (C:\Users\Aaryamann\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\migration.js:70:1) at Migration._load (C:\Users\Aaryamann\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\migration.js:57:1) at Migration.run (C:\Users\Aaryamann\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\migration.js:167:1) at Object.runMigrations (C:\Users\Aaryamann\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:148:1) at Object.runFrom (C:\Users\Aaryamann\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:110:1) at Object.runAll (C:\Users\Aaryamann\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:114:1) at Object.run (C:\Users\Aaryamann\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:79:1) at runMigrations (C:\Users\Aaryamann\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:253:1) at C:\Users\Aaryamann\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:218:1 Truffle v5.1.20 (core: 5.1.20) Node v13.12.0
它在 PowerShell 上執行。
程式碼本身很好,因此編譯成功。但是,當您嘗試在2_deploy_contracts.js中部署合約時,會出現錯誤。
您的合約需要一個建構子參數
uint8 _numProposals
,您需要在2_deploy_contracts.js中提供該參數。所以不是像deployer.deploy(A)
你需要的東西,比如論點deployer.deploy(A, 8)
在哪裡。8
您可以在此處閱讀有關 Truffle 的建構子參數語法的更多資訊:https ://www.trufflesuite.com/docs/truffle/getting-started/running-migrations#deployer-deploy-contract-args-options-