0

I have two smart contracts. A caller and a receiver.

My caller code looks like so:

// SPDX-License-Identifier: G.P.L 3.0

pragma solidity 0.8.3;

contract Caller {
    event Response(bool success, bytes data);

    function testCallFoo(address payable _addr) public payable {
        // You can send ether and specify a custom gas amount
        (bool success, bytes memory data) = _addr.call{value: msg.value, gas: 50000}(
            abi.encodeWithSignature("foo(string,uint256)", "call foo", 123)
        );

        emit Response(success, data);
    }
    
    function testNotThere(address payable _addr) public payable {
        // You can send ether and specify a custom gas amount
        (bool success, bytes memory data) = _addr.call{value: msg.value, gas: 50000}("");

        emit Response(success, data);
    }

}

and my receiver code looks like so:

contract Receiver {
    // 'empty' with no code inside it.
}

How can I send ether to the receiver contract? Is it possible? If no, why? Yet the contract is an address on the blockchain?

What is the minimum code required to send ether to a contract?

I am asking all this to understand what it takes for a contract to be able to receive ether.

2
1

By default, a contract without a payable function can't receive Ether. However, there are some exceptions, or tricks, on how it's possible: https://ethereum.stackexchange.com/a/63988/31933

But for normal usage, you need a receive payable (https://docs.soliditylang.org/en/v0.8.0/contracts.html#receive-ether-function) function in the contract. So the minimum contract would be:

contract Receiver {
    receive() external payable {
    }
}
2
  • In the link you have shared showing some exceptions, one ot the exceptions is when ether is sent to contract before it exists. How is this possible. How will you know the address to send to?
    – YulePale
    Apr 11 at 10:47
  • 1
    The address is created deterministically, based on the sender address and used nonce. Furthermore, nowadays that is even easier with CREATE2 opcode, where the nonce is not used. The bottom line is that the address can be calculated in advance. Apr 11 at 13:05

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