solidity selfdestruct合约销毁

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;
    }
}
相关推荐
Web3VentureView2 小时前
X Space AMA回顾|预测熊市底部:当市场寻找价格,SYNBO正在构建未来
人工智能·物联网·金融·web3·区块链
devmoon1 天前
使用 Zombienet 运行平行链网络
web3·区块链·sdk·polkadot·测试网·跨链
qyresearch_1 天前
移动感应健身:全球市场扩张下的中国机遇与破局之道
大数据·人工智能·区块链
软件工程小施同学2 天前
区块链论文速读 CCF A--VLDB 2025 (2) 附pdf下载
pdf·区块链
MQLYES2 天前
26-总结
区块链
Rockbean2 天前
10分钟智能合约:进阶实战-3.3 拒绝服务攻击
web3·智能合约·solidity
MQLYES3 天前
25-ETH-美链
区块链
量化炼金 (CodeAlchemy)3 天前
【交易策略】低通滤波器策略:在小时图上捕捉中期动量
大数据·人工智能·机器学习·区块链
Rockbean3 天前
10分钟智能合约:进阶实战-3.2.2 跨函数重入
web3·智能合约·solidity
Rockbean3 天前
10分钟智能合约:进阶实战-3.2.3 跨合约重入
web3·智能合约·solidity