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;
    }
}
相关推荐
marsh02065 小时前
57 openclaw与区块链:构建可信的去中心化应用
青少年编程·去中心化·区块链
木西6 小时前
Solidity 0.8.27 + OZ V5 实战:构建 AI 驱动的去中心化预测市场核心合约
web3·智能合约·solidity
多年小白7 小时前
开盘策略】2026年5月28日(周四)
大数据·人工智能·物联网·金融·区块链
m0_380167147 小时前
CoinGlass API、Tardis、Coinalyze:加密衍生品数据 API 怎么选?
人工智能·ai·区块链
m0_380167148 小时前
最适合交易机器人的加密数据 API:CoinGlass API 指南
人工智能·ai·区块链
华科大胡子13 小时前
ImToken智能合约交互
区块链
方向研究14 小时前
IC贴水现象
区块链
穗余1 天前
2026 AI x Web3 School共学营笔记-Day8-Agent Wallet
人工智能·web3·区块链
ZDTina1 天前
浅析金融领域 TRS:总收益互换
金融·区块链
松花皮蛋me1 天前
5月25号:科技成交额拥堵?同时带不动指数?
科技·区块链