0

I am trying to transfer some Ether between two contracts but it is just not working for me. Here is my code:

pragma solidity ^0.6.0;

contract ContractA {
    uint public tips;

    /**
     * Function to collect the tips.
     */
    function collectTips(address payable _recipient) public {
        require(_recipient.send(tips));
        tips = 0;
    }
    
    /**
     * Receive function in order to receive tips. No calldata is set
     */
    receive () external payable {
        tips += msg.value;
    } 
}

contract ContractB {
    uint256 public tips;
    
    /**
     * Receive function in order to receive tips. No calldata is set
     */
    receive() external payable {
        tips += msg.value;
    }
}

Whenever I send a transaction to collectTips, giving the contract address of ContractB I get the following error in remix

transact to ContractA.collectTips errored: VM error: revert. 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.

I was referring to these docs: https://docs.soliditylang.org/en/v0.6.0/contracts.html#receive-ether-function

I am clueless. Anyone an idea what I am doing wrong?

I found also sort of the same question around here, but nothing helped. Either the Solidity version is too old or just not working for me :/

Best and thanks in advance

1 Answer 1

1

Sending Ether using send or transfer only forwards 23000 gas of which 21000 is consumed by the context switching and the remaining 2000 is not enough to write to storage as tips is being updated in the receive function of contractB. The reads/writes to storage which will require around 5500 gas plus the 21000 gas for context switching.

function collectTips(address payable _recipient) public {
        (bool success, ) = _recipient.call{value: tips, gas: 5500}("");
        require(success);
        tips = 0;
}

The above implementation will explicitly send 21000 + 5500 gas along with the Ether sending transaction and the transaction will not revert.

3
  • Have you tried your solution in remix? I had to change gas to 19000 in order to make it work. Did it work for you with the 5500 gas? Which Solidity version did you use? I am using 0.6.0
    – Donut
    Nov 26, 2020 at 6:21
  • yes, the first time it will cost 20000 gas to store a value into storage of contractB and if you send the second time it costs 5000 gas to update the storage in conbtractB hence the change. Nov 26, 2020 at 7:59
  • Thanks for the explanation :) I appreciate it :)
    – Donut
    Nov 26, 2020 at 12:23

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.