Here is my contract:

pragma solidity ^0.4.4;

import "./SafeMath.sol";

contract TestContract is SafeMath {
    struct Result {
        bytes32 name;
        uint256 balance;
        mapping (address => uint256) betBalances;

    function TestContract(bytes32[] _resultNames) {
        for (uint i = 0; i < _resultNames.length; i++) {
            name: _resultNames[i],
            balance: 0

    function getBetBalance(uint resultIndex) public validResultIndex(resultIndex) constant returns (uint256) {
        return results[resultIndex].betBalances[msg.sender];

    function bet(uint resultIndex) public hasNotEnded payable {
        Result storage result = results[resultIndex];
        result.balance = safeAdd(result.balance, msg.value);
        result.betBalances[msg.sender] = safeAdd(result.betBalances[msg.sender], msg.value);

Here is my testing code:

const web3 = global.web3;
const TestContract = artifacts.require("./TestContract.sol");

contract('TestContract', function(accounts) {
    const params = {
        _owner: accounts[0],
        _name: "test",
        _resultNames: ["first", "second", "third"],
        _bettingEndBlock: 1000

    let testContract;

    it("allows users to bet if the betting end block has not been reached", async function() {
        testContract = await TestContract.new(...Object.values(params));

        testContract.BetAccepted().watch((error, response) => {
            if (error) {
                console.log("Event Error: " + error);
            } else {
                console.log("Event Triggered: " + JSON.stringify(response.event));
                console.log("resultIndex: " + JSON.stringify(response.args._resultIndex));
                console.log("betAmount: " + JSON.stringify(response.args._betAmount));
                console.log("betBalance: " + JSON.stringify(response.args._betBalance));

        let initialBalance = web3.eth.getBalance(testContract.address).toNumber();
        let betAmount = web3.toWei(1, 'ether');
        let betResultIndex = 0;

        await testContract.bet(betResultIndex, { from: accounts[1], value: betAmount });
        let newBalance = web3.eth.getBalance(testContract.address).toNumber();
        let difference = newBalance - initialBalance;
        assert.equal(difference, betAmount, "New result balance does not match added bet.");

        let resultBalance = await testContract.getResultBalance(betResultIndex);
        assert.equal(resultBalance, betAmount, "Result balance does not match.");

        let betBalance = await testContract.getBetBalance(betResultIndex);
        assert.equal(betBalance.toString(), betAmount, "Bet balance does not match.");

And here is the console error:

        throw reason;
AssertionError: Bet balance does not match.: expected '0' to equal '1000000000000000000'
    at /Users/xxx/coding/xxx/test/topic.js:75:11
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

I have confirmed the bet method works by adding an Event and that the betBalance is as it should be (not included in this code). But it is returning as 0 on the testing side. Why is this happening?

I know that transactions aren't able to return values, but is my call to get the resultBalance considered a transaction since I am chaining the call?

  • Please either post all your solidity code, or narrow this down to a simpler demonstration of the error that others can reproduce.
    – dcorking
    Aug 29, 2017 at 7:04
  • Updated post to include simple testable code.
    – The Nomad
    Aug 29, 2017 at 7:11
  • Are you sure the bet method is working properly? You're not saving the local result object back to the main mapping (results[resultIndex] = result), so it may not be getting saved properly? Aug 29, 2017 at 11:37
  • @MidnightLightning Yes I am sure it is working properly. I have an Event that I use to return the value of results[resultIndex].betBalances[msg.sender] and it properly shows the correct balance.
    – The Nomad
    Aug 29, 2017 at 12:43

1 Answer 1


Your msg.sender is not the same. In the first call you use { from: accounts[1] .. In the second you don't. When you don't specify the account taken is accounts[0]

  • You are correct @decanus. Actually I figured this out right after I set the bounty lol. But, good catch! You earned it!
    – The Nomad
    Aug 31, 2017 at 15:53

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.