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;
    }
}
相关推荐
2601_9620725019 小时前
CBCX:电力需求增长考验电网容量
区块链
IvorySQL4 天前
PostgreSQL 日报|内核多项缺陷修复与数据校验补丁推进(8 月 27 日)
数据库·postgresql·区块链
zhanghaha13144 天前
Python进阶教程:19_量化交易 零基础超详细入门教程
区块链
mykj15514 天前
Web3开发系统不是炒币,是互联网的“对账本”逻辑
web3·区块链·rwa·钱包区块链系统开发
MartinYeung54 天前
Uniswap v4 许可池:重塑 DeFi 流动性管理的范式革命
人工智能·区块链
磐链科技5 天前
智能合约开发中的Layer2适配:Rollup、侧链与状态通道实践
区块链·智能合约
Richown6 天前
Three.js 供应链三维可视化:全球物流轨迹、库存热力图与数字孪生的交互呈现
区块链·react
小K讲AI营销6 天前
固态电池战局拆解:机器人为何先于汽车吃到红利
大数据·人工智能·区块链
来了就不要走7 天前
源达财源滚滚APP怎么样?日常炒股能用得上吗?
区块链
网安蟹佬霸7 天前
WebAssembly安全攻防实战:从WASM逆向到漏洞利用
前端·安全·自动化·区块链·智能合约·wasm