solidity selfdestruct合约销毁
1、部署合约,附带value
2、获取合约地址的eth余额,余额为第一步附带的value
3、调用方法killSelf
,进行合约的销毁,并把剩余的ETH转给receiver
solidity
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.8.18;
contract Destroy {
address public owner;
constructor() payable {
owner = msg.sender;
}
event Receive(address indexed sender, uint256 amount, string data);
receive() external payable {
emit Receive(msg.sender, msg.value, "Receive Success");
}
function killSelf(address receiver) public {
require(msg.sender == owner, "Only owner can call this function");
// // 调用selfdestruct销毁合约,并把剩余的ETH转给receiver
selfdestruct(payable(receiver));
}
// 获取合约地址的eth余额
function getBalance() public view returns (uint256) {
return address(this).balance;
}
}