0

Community,

I've been banging my head over the following problem and hope you can spot my mistake. I deploy the following contract with Populus on Testrpc. This is pretty simple contract:

pragma solidity ^0.4.19;

contract ICO {
    address public owner;
    mapping(address => uint) public balances;
    address[] public buyers;

    uint constant public MULTIPLIER = 1000;

    function ICO() public {
        owner = msg.sender;
    }

    function getBuyersLength() public constant returns (uint) {
        return buyers.length;
    }

    function buy() public payable {
        uint coins = msg.value * MULTIPLIER;
        balances[msg.sender] += coins;
        buyers.push(msg.sender);
    }
}

I am calling the buy function with Web3js in the following way:

let contract;
let account;

window.addEventListener('load', function () {
    if (typeof web3 !== 'undefined') {
        console.log('Using MetaMask connection');
        w3 = new Web3(web3.currentProvider);
    } else {
        w3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
    }

    contract = w3.eth.contract(contractABI).at(contractAddress);
    account = w3.eth.defaultAccount;

    main()

});

function main() {
    let amount = w3.toWei(10, 'ether');
    contract.buy({'value': amount}, function (err, res) {
        if (err) {
            console.error('Error occurred: ' + err);
        } else {
            console.log('Transaction created: ' + res);
        }
    })
}

After waiting for the transaction to be included in a block (happens almost instantaneously since I run testrpc, I call the getBuyersLength() function like this:

function updateBalances() {
    contract.getBuyersLength(function (err, res) {
        if (err) {
            console.error('Error in getBuyers: ' + err);
        } else {
            console.log('Count of Buyers: ' + res);
        }
    }
}

However, the results is always 0, which means that the buyers.length stays the same. Can you spot a mistake I've made?

  • Let me know your feedback on my ans. If its resolved please mark as solved. It would be helpful for community to find ans easily – Jitendra Kumar. Balla Feb 18 '18 at 19:51
  • 1
    With testrpc/ganache-cli you can use github.com/trufflesuite/ganache that provides an interface to browse blocks/transactions. If the array didn't increase its length then buy() has failed. – Ismael Feb 19 '18 at 0:34
1

Just solved it. The problem was how I deployed the contract to TestRPC. I used populus deploy ICO -c testrpc to deploy the contract, but somehow, the contract was never saved on the test blockchain. I now deploy the contract from the frontend, which is not the best solution, but it works for now. Here's how:

w3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
account = w3.eth.defaultAccount;

let contractScheme = w3.eth.contract(contractABI);
let gasEstimate = w3.eth.estimateGas({data: contractBytecode});
contract = contractScheme.new({
    data: contractBytecode,
    from: account,
    gas: gasEstimate
}, function (err, res) {
    if (err) {
        console.error('Error occurred in Contract Creation: ' + err)
    } else {
        // Callback fires twice. Once upon reception of the TransactionHash and
        // once the contract receives an address.
        if (!res.address) {
            console.log('TransactionID for Contract Creation ' + res.transactionHash)
        } 
        if (res.address) {
            console.log('Contract Address: ' + res.address);
            main()
        }
    }
});
-1

Your contract seems to be ok. Might be issue in your Js main file w3.toWei function. I guess you didn't assign any web3 object to w3. So w3 is undefined, Javascript will throw error. Because no deification found for w3.toWei. So your statements will not execute, contract.buy() will not execute. If this method is not executed then there is no trans in Eth client. So when you call getBuyersLength() it will return 0 only.

function main() {
    let contract = web3.eth.contract(CONTRACT_ABI).at(CONTRACT_ADDR);
    let amount = web3.toWei(10, 'ether');
    contract.buy({'value': amount}, function (err, res) {
        if (err) {
            console.error('Error occurred: ' + err);
        } else {
            console.log('Transaction created: ' + res);
        }
    })
}

Replace your code with above mentioned code. If your problem not resolved then update your qus with Javascript logs.

  • Unfortunately this did not resolve my issue. I assign w3 to web3 earlier in my script. I'll added it to the question. But thank you! – Peter Feb 18 '18 at 20:31

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.