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;
    }
}
相关推荐
2401_8914092614 小时前
【无标题】
区块链
磐链科技1 天前
IM钱包开发核心技术拆解:DID去中心化身份与社交图谱的链上映射
去中心化·区块链
磐链科技2 天前
中心化钱包开发中的账户安全模型:Vitalik最新社交恢复构想解读
安全·区块链
磐链科技2 天前
交易所开发流动性解决方案:做市商接入与订单深度管理策略
区块链
Web3_Daisy2 天前
从流动性到执行:如何降低 MEV 对 Web3 市场
大数据·人工智能·web3·区块链
qq_33776320192 天前
仿博易大师内外盘国际期货软件源码全套开发:期货交易系统架构设计与核心技术解析
大数据·区块链
磐链科技3 天前
账户抽象+社交恢复:社交钱包开发的核心技术底座与落地实践
区块链
ZDGJ60994 天前
大恒指与小恒指的区别
区块链
MartinYeung55 天前
[bug 分析] Orchard 伪造漏洞: 深度代码剖析
安全·区块链·bug
ZDGJ60995 天前
大恒指与小恒指交易时间详解
区块链