0

A beginner smart contract developer here. This is my smart contract:

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;
            }
    }     
}

While compiling using Truffle compile I don't get any errors as below:

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

However, when I use truffle migrate --reset this happens:

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

It's running on PowerShell.

  • we need your migration file.. you contract expect a parameter when you deploy it. will look something like this await deployer.deploy(Ballot, <your parameter>); – Majd TL Apr 8 at 15:37
1

The code itself is fine and therefore compiles successfully. However when you try to deploy the contract in 2_deploy_contracts.js it gives an error.

Your contract requires one constructor argument uint8 _numProposals which you need to provide in 2_deploy_contracts.js. So instead of something like deployer.deploy(A) you need something like deployer.deploy(A, 8) where 8 is the argument.

You can read more about Truffle's constructor arguments syntax here: https://www.trufflesuite.com/docs/truffle/getting-started/running-migrations#deployer-deploy-contract-args-options-

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.