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;
    }
}
相关推荐
Richown8 小时前
React 高级模式:并发渲染下的状态机驱动架构——从 Finite State Machine 到生产级实现
区块链·react
ChannelLab13 小时前
BONK FAQ:关于生态、产品、社区与金融化的 24 个核心问题
金融·区块链
超级架构师20 小时前
【区块链技术】区块链101:比特币是什么?
人工智能·区块链·比特币
维克兜率天1 天前
4.3.2.1 日常监控:上线只是开始
服务器·数据库·python·区块链·php·量化
磐链科技3 天前
交易所开发核心:钱包提现引擎的设计——从风控拦截、多级审批到自动广播上链
web3·区块链
2601_962077983 天前
阿里二面,前端开发在web3.0中该如何应用,记录面经
区块链·前端开发·web3.0·技术趋势·学习准备
baopixiaoz3 天前
BeeQuant × BeeAgent:AI驱动智能交易
大数据·人工智能·python·区块链
ZDGJ60993 天前
恒指期货什么意思,恒指期货怎么看
区块链
综合简报4 天前
东南亚发稿多少钱一篇?预算怎么规划?如何选到高性价比服务商?
区块链
磐链科技4 天前
区块链开发入门实战:用 Hardhat 与 Ethers.js 搭建你的第一个 DApp
开发语言·javascript·区块链