there's something wrong with my code but I can't see what. I'm trying to create "pots" where you can add ETH inside.

mapping(uint => Pot) public pots;
mapping(uint => mapping(address => uint)) public contributionAmountForAddress; // Is a mapping(potIndex => mapping(0x0xxx => amountContributed))

struct Pot {
        string title;
        string description;
        address creator;
        PotStatus status;

But when someone try to add MORE FUNDS to an existing one pot (the guys has already contributed), contributeToPot, it looks like my function is replacing the new contribution instead of adding it to the already existing contribution:

function contributeToPot(uint256 _potIdx) public payable returns (bool) {
        require(msg.value > 0, "The amount must be more than 0");
        require(pots[_potIdx].status == PotStatus.RUNNING, "The pot is closed, you cannot add more funds");

        // Pot storage potToFund = pots[_potIdx];
        if (contributionAmountForAddress[_potIdx][msg.sender] > 0) {
            contributionAmountForAddress[_potIdx][msg.sender] = contributionAmountForAddress[_potIdx][msg.sender] + msg.value;
        } else {
            contributionAmountForAddress[_potIdx][msg.sender] = msg.value;

        return true;

Here are my tests:


it('should contribute to a pot', async () => {
            const contributor = accounts[2]
            const amount = web3.utils.toWei(web3.utils.toBN(FIRST_CONTRIBUTION_IN_ETH), 'ether')
            await PiggyBank.contributeToPot(lastPotIndex.toNumber(), { from: contributor, value: amount })

                (await PiggyBank.contributionAmountForAddress.call(lastPotIndex, contributor)).toString(),

it ('should contribute even more to a pot', async () => {
            const contributor = accounts[3]
            const amount = web3.utils.toWei(web3.utils.toBN(SECOND_CONTRIBUTION_IN_ETH), 'ether')
            await PiggyBank.contributeToPot(lastPotIndex.toNumber(), { from: contributor, value: amount })
            const totalContributed = web3.utils.toWei(web3.utils.toBN(FIRST_CONTRIBUTION_IN_ETH + SECOND_CONTRIBUTION_IN_ETH), 'ether')
            // console.log(
            //     await PiggyBank.contributionAmountForAddress.call()
            // )
                (await PiggyBank.contributionAmountForAddress.call(lastPotIndex, contributor)).toString(),

The first one is correct, the second one not :(

1) Contract: PiggyBank
       Contribute to a pot
         should contribute even more to a pot:

      AssertionError: expected '1000000000000000000' to equal '3000000000000000000'
      + expected - actual


Someone have some ideas why is my test not passing ? It looks like the condition if(contributionAmountForAddress[_potIdx][msg.sender] > 0) { is always false. I don't know how can I log something in the code to better handle my error. Maybe my code structure is totally wrong ? Thanks !

1 Answer 1


Rather add a mapping to check if a user has contributed to the pool. Then instead of using:

if(contributionAmountForAddress[_potIdx][msg.sender] > 0) 

rather check if the new contribution mapping is true for that specific pool.

  • Hi, I don't understand what you mean. Can you elaborate with an example please ?
    – Alxndr BRL
    Commented Aug 18, 2021 at 17:05

Your Answer

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

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