Search code examples
javascriptethereumblockchainsolidityweb3js

Deployed contract failed to execute


I have a contract here:

pragma solidity ^0.4.2;

contract Charity{

    mapping (address => uint) public coinBalanceOf;
    event CoinTransfer(address sender, address receiver, uint amount);

    function charity(uint supply){

        coinBalanceOf[msg.sender] = supply;
    }


    function sendCoin(address receiver, uint amount) returns (bool sufficient)
    {

        if (coinBalanceOf[msg.sender] < amount) return false;

        coinBalanceOf[msg.sender] -= amount;
        coinBalanceOf[receiver]   += amount;

        CoinTransfer(msg.sender, receiver, amount);

        return true;

    }

}

When I deploy using web3 1.0.0-beta with

import * as fs       from 'fs'       ;
import * as solc     from 'solc'     ;
import * as Web3     from 'web3'     ; 

var web3   = new Web3(new Web3.providers.WebsocketProvider('ws://localhost:8546'));


var contract_path : string = "../solidity/contracts/Charity.sol"
const input       = fs.readFileSync(contract_path)
const output      = solc.compile(input.toString(), 1);
var contract_name = ":" + pr.last(contract_path.split("/")).split(".")[0]
const bytecode    = output.contracts[contract_name].bytecode
const abi_        = JSON.parse(output.contracts[contract_name].interface);

web3.eth.getAccounts().then(accounts => {

    var coinbase = accounts[0];
    var receiver = accounts[1];

    // create contract
    var myContract = new web3.eth.Contract(abi_, coinbase,
        {
            from    : coinbase,
            gasPrice: "2000000"
        });


    var deployedContract = myContract.deploy({

        data: '0x' + bytecode,

    }).send({

        from: coinbase,
        gas : 1500000 ,
        gasPrice: '30000000000000'            

    }, (err, hash) => {

        if (err) { console.log("error on deployment: ", err) }
        console.log("Hash: ", hash)

    })


    myContract.methods.sendCoin(receiver, 7000000).send({ 

        from: coinbase,
        gas: 100000,
        gasPrice: '10000000'
    }, (err,val) => {
            if (err) { console.log(err) }
            else {
                console.log("sent coin: ", val)
            }
        })
    .then(console.log)  

});

However when I deployed it on a private blockchain that is mining, I see no changes in the receiver's balance.

I followed the answer below to pass the supply argument into the contract, however it is still not executing. It may be due to a timing issue, so I moved the sendCoin function to the callback as follows, but it is still not executing:

   var myContract = new web3.eth.Contract(abi_, coinbase,
        {
            from    : coinbase,
            gasPrice: "2000000",
        });

    /**
        maybe there should be a callback up there ----^ ???
    */

    var deployedContract = myContract.deploy({

        data: '0x' + bytecode,
        arguments: [2406927999999]  // this is not sending
        // web3.eth.getBalance(coinbase)] note the raw number gets error: number-to-bn
        // is it because it's too big?

    }).send({

        from: coinbase,
        gas : 1500000 ,
        gasPrice: '30000000000000'            

    }, (err, hash) => {

        if (err) { console.log("error on deployment: ", err) }


        console.log("contract deployed with Hash: [REDACTED]")


    }).then((v) => {

            /**
                maybe this should happen in the callback?

            */
            myContract.methods.sendCoin(receiver, 70000000000).send({ 

                from: coinbase,
                gas : 100000  ,
                gasPrice: '10000000'

            }, (err,val) => {
                    if (err) { console.log(err) }
                    else {
                        console.log("---------------------------------------")
                        console.log("sent coin: ", val)
                        console.log("---------------------------------------")

                    }
            })
            console.log(".then callback with value.options: ", v.options)

            console.log('=====================================================')



    })

});

Solution

  • You need to pass in the value for supply when initializing the contract. You can pass it in like this:

    var deployedContract = myContract.deploy({
      data: '0x' + bytecode,
      arguments: [999999999]
    }).send({
      from: coinbase,
      gas : 1500000 ,
      gasPrice: '30000000000000'            
    }, (err, hash) => {
      if (err) { console.log("error on deployment: ", err) }
      console.log("Hash: ", hash)
    })
    

    See the docs for deploy here to see how to pass parameters into the contract constructor.

    Also, make sure you capitalize Charity in your function name to match the name of the contract (otherwise it's not a constructor).