1

How can I write a contract that sends some of the ether it receives on to one wallet, and the rest of the ether back to the sender (or another wallet)

The following code works if I comment out either line 17 or 18 but doesn't when both are uncommented:

pragma solidity ^0.4.24;

contract Forwarder {

    address public destinationAddress;
    address constant public otherAddress = 0xf17f52151EbEF6C7334FAD080c5704D77216b732;

    event LogForwarded(address indexed sender, uint amount);
    event LogFlushed(address indexed sender, uint amount);

    function Forwarder() public {
        destinationAddress = msg.sender;
    }

    function() payable public {
        emit LogForwarded(msg.sender, msg.value);
        otherAddress.transfer(msg.value /3);        // 17
        destinationAddress.transfer(msg.value /3);  // 18
    }

    function flush() public {
        emit LogFlushed(msg.sender, address(this).balance);
        destinationAddress.transfer(address(this).balance);
    }

}
2
  • can you please provide the error log? Take note that the default gas limit for fallback is very small then may not enough gas to pay tnx fee 2 or more internal transfer tnx. Or you can increase gas limit when sending eth to the smart contract.
    – Tony Dang
    Commented Sep 5, 2018 at 10:27
  • Thanks for the reply, it seems like exactly that was my issue. Increasing the gas amount solved it. If you want to write it as an answer I can accept it if you like?
    – Kai
    Commented Sep 5, 2018 at 10:56

1 Answer 1

2

Take note that the default Gas Limit for fallback function is very small then may not enough gas to pay fee for 2 or more internal transfer transactions. You should increase Gas Limit when sending ETH to the smart contract.

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.