2

I'm building my dApp by modifying the pet-shop example, however, I got stuck at the migration step. 1_initial_migration.js runs successfully but 2_deploy_contracts.js gives me the following error despite the fact I modified it accordingly.

I've read all the Migration Documentation and gone through a number of related post but I'm still having a hard time understanding how it relates to my project.

... 0x48c02fda96b9e71d1b219e4b09876181027c60aa2589c9ccd7f46f2970e4f34b Error encountered, bailing. Network state unknown. Review successful transactions manually. Error: VM Exception while processing transaction: invalid opcode at Object.InvalidResponse (C:\Users\charles.fried\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:43303:16) at C:\Users\charles.fried\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:331156:36 at C:\Users\charles.fried\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:175492:11 at C:\Users\charles.fried\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:314196:9 at XMLHttpRequest.request.onreadystatechange (C:\Users\charles.fried\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:329855:7) at XMLHttpRequestEventTarget.dispatchEvent (C:\Users\charles.fried\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:70159:18) at XMLHttpRequest._setReadyState (C:\Users\charles.fried\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:70449:12) at XMLHttpRequest._onHttpResponseEnd (C:\Users\charles.fried\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:70604:12) at IncomingMessage. (C:\Users\charles.fried\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:70564:24) at emitNone (events.js:91:20)

2_deploy_contracts.js:

var Plant = artifacts.require("Plant");

module.exports = function(deployer) {
  deployer.deploy(Plant);
};

Plant.sol:

pragma solidity ^0.4.18;

contract Plant {

    address public creator;

    // initial Variables
    uint public number_of_leaves;
    uint public remaining_leaves = 0; // has to start on 1 otherwise it cancels price calculation
    uint public initial_coins;
    uint public total_supply;
    uint public finney_balance; // 1 Ether is 1,000 finney - see this converter -> https://converter.murkin.me/
    uint public plant_price = 60; // Price is in Finney $20 if approx 60 Finney
    uint public number_of_plants;

    // Modififer to check if there's enough money for a new plant without wasting gas
    modifier onlyIfEnoughMoney() {
        require(finney_balance > plant_price); 
        _;
    }

    // only_creator can update price
    modifier onlyCreator() {
        require(msg.sender == creator);
        _;
    }

    // you need to send some money to initialise the contract
    modifier enoughToInitialise() {
        require(msg.value > 0);
        _;
    }

    function Plant(uint _number_of_leaves, uint _initial_coins) public payable enoughToInitialise {
        number_of_leaves = _number_of_leaves;
        remaining_leaves = number_of_leaves;
        initial_coins = _initial_coins;
        total_supply = initial_coins;
        finney_balance = msg.value/1000000000000000;
        creator = msg.sender;
        number_of_plants ++;
        depositedFunds(msg.value, finney_balance);
    }

    function buy_new_plant() private onlyIfEnoughMoney {

        uint number_plants_to_buy = finney_balance / plant_price;

        total_supply += (initial_coins*number_plants_to_buy);
        finney_balance -= (plant_price*number_plants_to_buy);
        number_of_plants += number_plants_to_buy; 
        plantWasPurchased(finney_balance, total_supply, number_of_plants);
    }

    function () payable public {
        finney_balance += msg.value/1000000000000000;
        buy_new_plant();
        depositedFunds(msg.value, finney_balance);
    }

    function terminate() private onlyCreator{
        selfdestruct(creator);
    }

    function leafPicked(uint numPicked) public onlyCreator returns (uint){
        require(numPicked < remaining_leaves);
        remaining_leaves -= numPicked;
        leafWasPicked(remaining_leaves, numPicked);
    }

    event leafWasPicked(uint leavesLeft, uint numberTaken);
    event plantWasPurchased(uint newBalance, uint newTotalSupply, uint newPlantNumber); // Event
    event depositedFunds(uint amount, uint newBalance); // Event

}

1 Answer 1

2

In the deployer call, you need to pass in the arguments of the Plant constructor. Otherwise, the constructor fails because the arguments are undefined.

module.exports = function(deployer, network, accounts) {
  deployer.deploy(Plant, <someNumberOfLeaves>, <someInitialCoin>, { from: accounts[0], value: 1e18 );
};

This is mentioned in this section: Deployer API

4
  • Much appreciated, the initialisation function is also payable and requires some ether to be sent. How should I do this?
    – Charles
    Nov 16, 2017 at 8:57
  • You can specify the value to send when deploying the contract in the parameters object following the arguments. See my revised answer. Note I also added parameters to the function (network & accounts) so you can specify which account the value is coming from
    – carlolm
    Nov 16, 2017 at 9:16
  • I can see accounts being used but not network, could briefly explain its role?
    – Charles
    Nov 16, 2017 at 9:19
  • networks is the object key / string (such as "development", "testnet", "mainnet") from your truffle.js config file of the network that deployer is deploying to (truffleframework.com/docs/getting_started/…). I included that there because the function argument syntax requires that there's a networks parameter before accounts (truffleframework.com/docs/getting_started/…)
    – carlolm
    Nov 16, 2017 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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