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++) {
results.push(Result({
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:
/Users/xxx/.config/yarn/global/node_modules/truffle/build/cli.bundled.js:213982
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?
bet
method is working properly? You're not saving the localresult
object back to the main mapping (results[resultIndex] = result
), so it may not be getting saved properly?Event
that I use to return the value ofresults[resultIndex].betBalances[msg.sender]
and it properly shows the correct balance.