Receive
Receive()
contract MyContract { // Receive Ether
receive() external payable { // Do something with the received ether }
}import "./token.sol"; // A standard ERC20 token
contract MyContract {
Token token;
constructor(address _tokenAddress) {
token = Token(_tokenAddress);
}
// Receive ERC20 tokens
receive() external payable {
uint256 amount = msg.value;
token.transferFrom(msg.sender, address(this), amount);
}
}Last updated