Selfdestruct: force another contract to receive your ethers.
Overview
Insert Lead paragraph here.
"Smart Contract receive ethers ONLY when contract provide
fallback()
orreceive()
function. "
Is this statement true or not?
[ ] true
[ ] false
1pragma solidity ^0.8.0;
2
3// SPDX-License-Identifier: MIT
4contract MyContract {
5 receive() external payable {}
6 fallback() external payable {}
7}
The answer is false.
Why?
A smart contract has a selfdestruct()
function to force transfer the contract's ethers to another address, whether the receiver address has receive ().
1// SPDX-License-Identifier: MIT
2pragma solidity ^0.8.0;
3
4contract ReceiveMyEth {
5
6 function deposit() public payable {}
7 function destroy(address _addr) public {
8 selfdestruct(payable(_addr));
9 }
10
11}
12
13// empty contract without receive() and fallback()
14contract MyContract {
15 // receive() external payable {}
16 // fallback() external payable {}
17}
You can deposit some ethers into ReceiveMyEth contract, then call destroy() function to send ethers to MyContract contract.