Solidity 中的自毁合约
登链社区
2024-06-27 18:00
订阅此专栏
收藏此文章

基本定义

在 Solidity 中,自毁合约(Self-Destruct Contract)是一种能够销毁自身并将剩余的以太币(Ether)发送到指定地址的智能合约。自毁合约通过调用 selfdestruct 函数实现,这个函数会删除合约的代码和存储,从而释放网络资源。

自毁合约的特点

  • 释放资源:自毁合约会从区块链上删除其代码和存储数据,释放存储空间。

  • 返还以太币:在合约自毁时,可以将合约中剩余的以太币返还给指定地址。

  • 不可逆性:一旦调用 selfdestruct,合约将永久销毁,无法恢复。

为什么需要自毁合约

  • 生命周期管理:当一个合约完成其使命后,可以通过自毁释放区块链资源。

  • 安全性:在某些情况下,销毁合约可以防止恶意攻击或漏洞利用。

  • 成本优化:减少不再需要的合约占用的存储空间,可以降低区块链的长期存储成本。

代码示例

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract SelfDestructExample {
address public owner;
uint public creationTime;
uint public constant SELF_DESTRUCT_TIMEOUT = 30 days;

constructor() payable {
owner = msg.sender;
creationTime = block.timestamp;
}

function destroyContract() public {
require(msg.sender == owner, "Only the owner can destroy this contract");
require(block.timestamp > creationTime + SELF_DESTRUCT_TIMEOUT, "Contract cannot be destroyed yet");
selfdestruct(payable(owner));
}

function getBalance() public view returns (uint) {
return address(this).balance;
}

receive() external payable {}
}

代码解释

1. 许可证声明和 Solidity 版本声明

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
  • // SPDX-License-Identifier: MIT 是许可证声明,表示该合约使用 MIT 许可证。
  • pragma solidity ^0.8.24; 指定了该合约使用的 Solidity 编译器版本为 0.8.24 及以上。

2. 合约定义

contract SelfDestructExample {

定义了一个名为 SelfDestructExample 的合约。

3. 状态变量

address public owner;
uint public creationTime;
uint public constant SELF_DESTRUCT_TIMEOUT = 30 days;
  • address public owner;:保存合约所有者的地址。
  • uint public creationTime;:保存合约的创建时间戳。
  • uint public constant SELF_DESTRUCT_TIMEOUT = 30 days;:定义一个常量,表示合约必须等待 30 天才能被销毁。

4. 构造函数

constructor() payable {
owner = msg.sender;
creationTime = block.timestamp;
}
  • constructor() payable:构造函数在合约部署时被调用,允许发送以太币到合约。
  • owner = msg.sender;:初始化合约所有者为部署合约的地址。
  • creationTime = block.timestamp;:初始化合约创建时间为当前区块的时间戳。

5. 自毁函数

function destroyContract() public {
require(msg.sender == owner, "Only the owner can destroy this contract");
require(block.timestamp > creationTime + SELF_DESTRUCT_TIMEOUT, "Contract cannot be destroyed yet");
selfdestruct(payable(owner));
}
  • require(msg.sender == owner, "Only the owner can destroy this contract");:确保只有合约所有者才能调用此函数。
  • require(block.timestamp > creationTime + SELF_DESTRUCT_TIMEOUT, "Contract cannot be destroyed yet");:确保当前时间大于创建时间加上 30 天,才允许销毁合约。
  • selfdestruct(payable(owner));:调用 selfdestruct 将合约中的所有以太币发送给所有者并销毁合约。

6. 获取合约余额函数

function getBalance() public view returns (uint) {
return address(this).balance;
}
  • function getBalance() public view returns (uint):定义一个公共视图函数,用于返回合约的以太币余额。
  • return address(this).balance;:返回合约地址的余额。

7. 接收以太币函数

receive() external payable {}
  • receive() external payable {}:定义一个 receive 函数,允许合约接收以太币。

总结

这个合约定义了一个自毁机制,但在自毁之前需要满足两个条件:

  1. 只能由合约所有者调用。
  2. 必须在合约创建 30 天后才能调用。

此外,合约还包括一个获取当前合约余额的函数和一个接收以太币的 receive 函数。

最后

写文章不易,如果文章对您有帮助,欢迎点个赞,您的支持是我写作的最大动力。

相关资料、源码已同步 github:https://github.com/MagicalBridge/Blog[1] 欢迎 star


参考资料
[1]

https://github.com/MagicalBridge/Blog: https://github.com/MagicalBridge/Blog


【免责声明】市场有风险,投资需谨慎。本文不构成投资建议,用户应考虑本文中的任何意见、观点或结论是否符合其特定状况。据此投资,责任自负。

登链社区
数据请求中
查看更多

推荐专栏

数据请求中
在 App 打开