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;
    }
}
相关推荐
CryptoPP4 小时前
快速对接东京证券交易所API数据:实战指南与代码示例
开发语言·人工智能·windows·python·信息可视化·区块链
栗子~~6 小时前
ethers - 区块链变更链上状态与在确认(对账)说明
区块链
Jinkxs6 小时前
Java 跨域14-Java 与区块链(Hyperledger)集成
java·开发语言·区块链
cmes_love6 小时前
期货Level 2五档Tick历史数据详解
区块链
Man on the moon6 小时前
Solidity 零基础入门:从语法到实战,快速掌握智能合约开发
web3·区块链·智能合约
电报号dapp1197 小时前
DApp经济模型设计:2026年反泡沫完全指南
区块链·智能合约·哈希算法
CTA终结者9 小时前
期货开仓前保证金够吗:get_account 可用与占用字段对照
python·区块链
开源量化GO9 小时前
夜盘白盘衔接几分钟误下单:天勤交易时段与行情过滤
python·区块链
CTA量化套保13 小时前
一个账户跑多个期货策略:仓位与报单隔离思路
python·区块链
zhuhai_xigedian16 小时前
区块链技术加持:源网荷储系统的能源数据安全与溯源
大数据·区块链·能源