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;
    }
}
相关推荐
Dream Algorithm3 小时前
Solana(SOL)链的生态。
区块链
CryptoRzz4 小时前
对接印度股票市场数据 (India api) 实时k线图表
java·开发语言·python·区块链·maven
Sui_Network4 小时前
BitGo 通过 LayerZero 将原生 WBTC 引入 Sui
大数据·人工智能·科技·去中心化·区块链
上海云盾-小余5 小时前
DDoS 攻击溯源:DNS 水印标记 + 区块链存证的双保险
区块链·php·ddos
Rockbean7 小时前
3分钟Solidity: 3.3 Enum枚举
web3·区块链·solidity
Rockbean8 小时前
3分钟Solidity: 3.2 Array数组
web3·区块链·solidity
明月出天山_9 小时前
【金融科技理论与实践】常见知识点汇总——北大软微期末考复习
分布式·科技·金融·区块链·智能合约
麻辣兔变形记9 小时前
永续合约杠杆逻辑全解析:前端、后端和保证金的关系
前端·后端·区块链·智能合约
谈笑也风生1 天前
浅谈:被称为新基建的区块链(四)
区块链
Dream Algorithm1 天前
杠杆和合约之间的区别
笔记·区块链