0

there is this mapping

mapping(address => uint256) public balances;

and these functions:

function hasEvenSubtrees(address _node) internal view returns (bool) {
        uint256 leftSubtrees = countSubtrees(nodes[_node].left);
        uint256 rightSubtrees = countSubtrees(nodes[_node].right);
        uint256 totalSubtrees = leftSubtrees + rightSubtrees + 1;
        return (totalSubtrees % 2 == 0);
    }

    function checkAndDistributeReward(address root) private {
        if (hasEvenSubtrees(root)) {
            uint256 rewardAmount = 2 ether;
            nodes[root].balance += rewardAmount;
            contractBalance -= rewardAmount;
            balances[root] += rewardAmount;
            emit RewardGiven(root, rewardAmount);
        }
    }

I call the checkAndDistributeReward(root) in a function called register so it has to save the balance in balances mapping. it won't. return value of balances(address) is always 0. i tried every way I knew and result is just the same.

1 Answer 1

1

Are you sure the if statement is passed?

contract Test {
mapping(address => uint256) public balances;

function checkAndDistributeReward(address root) public {
    // if (hasEvenSubtrees(root)) {
        uint256 rewardAmount = 2 ether;
        // nodes[root].balance += rewardAmount;
        // contractBalance -= rewardAmount;
        balances[root] += rewardAmount;
        // emit RewardGiven(root, rewardAmount);
    // }
}

}

Without the if (obviously) works for me. If you wrote unit tests you can try a hardhat test coverage to see if "if" is passed or not.

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.