selfdestruct函数是智能合约自带的,它有两个功能
一是销毁合约,二是强制发送主币到一个地址;
源码:
pragma solidity ^0.8.7;
// selfdestruct
// - delete
// - force send Ether to any address
contract Kill {
constructor() payable {}
function kill() external {
selfdestruct(payable(msg.sender));
}
function testCall() external pure returns (uint) {
return 123;
}
}
contract Helper {
function getBalance() external view returns (uint) {
return address(this).balance;
}
function kill(Kill _kill) external {
_kill.kill();
}
}