0

I have a simple contract where I implemented a mechanism that sends ETH to contributors. When I call contribute function everything works great, but if I use something like that bulkSend(msg.value / 2) I have an error:

revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.

What's wrong?

pragma solidity >=0.4.22 <0.7.0;

contract Test {

    address payable[] public contributors;

    constructor() public {}

    function bulkSend(uint256 amount) public payable {
        uint256 share = amount / contributors.length;
        uint256 length = contributors.length;
        uint256 currentSum = 0;
        for (uint256 i = 0; i < length; i++) {
            require(share > 0);
            currentSum += share;
            require(currentSum <= msg.value);
            contributors[i].transfer(share);
        }
        require(currentSum == msg.value);
    }

    function contribute() public payable {
        contributors.push(msg.sender);
        bulkSend(msg.value);
    }
}
2

You have a guard in bulkSend: require(currentSum == msg.value).

If you send a transaction where amount != msg.value OR amount % contributors.length != 0 then this requirement will fail, because this requirement will throw.

A good practice is to include "error messages" in your require statements. This helps you debugging these transactions.

  • I think Solidity 0.5.x does not even compile the contracts anymore if they do not include a string reason for require(). – Mikko Ohtamaa May 8 '20 at 9:38

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.