OpenZeppelin:安全高效智能合约的秘密武器

本文介绍了OpenZeppelin,一个为以太坊和其他区块链平台提供安全、可重用智能合约库的公司和开源平台。文章概述了OpenZeppelin提供的关键产品和服务,并展示了如何通过npm安装OpenZeppelin,以及如何使用OpenZeppelin的ERC20代币合约。

OpenZeppelin 是一家公司和一个开源平台,在区块链和智能合约开发生态系统中广受尊敬。以下是一个全面的概述:

概述

OpenZeppelin 为 Ethereum 和其他区块链平台提供了一个安全、可重用的智能合约库。该公司成立于 2015 年,现已成为区块链安全领域最值得信赖的资源之一。

OpenZeppelin 提供的主要产品和服务

安装 OpenZeppilin 的终端命令

初始化包:

npm init -y

安装 OpenZeppelin:

npm install @openzeppelin/contracts

请访问 OpenZeppelin 官方网站以获得更好的理解:

|| https://github.com/OpenZeppelin/openzeppelin-contracts

在这里,我们可以看到在 OpenZeppelin/contracts 下

我们有很多文件夹,所有文件夹都包含不同类型的智能合约,用于不同的 useCase,我们可以在我们的项目中使用,这样我们就不必自己编写智能合约的整个函数。

它可以帮助我们提高时间效率和安全性,因为自己输入函数可能会导致逻辑错误,这会使我们的合约容易被黑客攻击......

在 token 文件夹下,我们可以看到像 ERC20、ERC721、ERC1155 这样的代币

在一个Token ERC20 下,我们可以看到许多合约,人们可以通过从文件夹导入智能合约来在他们的合约中使用,以使用他们想要的合约的任何特定函数。

||示例:我想在我的智能合约中导入 ERC20 代币,那么我必须以下列方式导入:

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

// Import specific contracts from OpenZeppelin
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/security/Pausable.sol";

// Create your contract by inheriting from OpenZeppelin contracts
contract MyToken is ERC20, Ownable, Pausable {
    // Inherits from three OpenZeppelin contracts: ERC20, Ownable, and Pausable
    // 继承自三个 OpenZeppelin 合约:ERC20、Ownable 和 Pausable

    // Import and use the SafeMath library directly
    using SafeMath for uint256;
    // Applies SafeMath library functions to all uint256 variables
    // 将 SafeMath 库函数应用于所有 uint256 变量

    uint256 public maxSupply;
    // Custom variable, not from OpenZeppelin

    // Constructor
    constructor(uint256 _initialSupply, uint256 _maxSupply) ERC20("My Token", "MTK") {
        // ERC20 constructor is called with "My Token" and "MTK" parameters
        // ERC20 构造函数使用“My Token”和“MTK”参数调用
        // Ownable constructor is implicitly called (sets msg.sender as owner)
        // Ownable 构造函数被隐式调用(将 msg.sender 设置为所有者)
        // Pausable constructor is implicitly called (sets _paused to false)
        // Pausable 构造函数被隐式调用(将 _paused 设置为 false)

        require(_maxSupply >= _initialSupply, "Max supply must be greater than initial supply");
        // Custom logic, not from OpenZeppelin

        maxSupply = _maxSupply;
        // Custom logic, not from OpenZeppelin

        // Mint initial tokens to the deployer (msg.sender)
        // 将初始代币铸造给部署者 (msg.sender)
        _mint(msg.sender, _initialSupply);
        // _mint is an internal function from ERC20
        // _mint 是 ERC20 的内部函数
    }

    // Using functions from Pausable
    // 使用 Pausable 中的函数
    function pause() public onlyOwner {
        // onlyOwner modifier is from Ownable
        // onlyOwner 修饰符来自 Ownable
        _pause();
        // _pause is an internal function from Pausable
        // _pause 是 Pausable 的内部函数
    }

    function unpause() public onlyOwner {
        // onlyOwner modifier is from Ownable
        // onlyOwner 修饰符来自 Ownable
        _unpause();
        // _unpause is an internal function from Pausable
        // _unpause 是 Pausable 的内部函数
    }

    // Override ERC20 transfer function to add the whenNotPaused modifier
    // 覆盖 ERC20 transfer 函数以添加 whenNotPaused 修饰符
    function transfer(address to, uint256 amount) public override whenNotPaused returns (bool) {
        // Overrides transfer function from ERC20
        // 覆盖来自 ERC20 的 transfer 函数
        // whenNotPaused modifier is from Pausable
        // whenNotPaused 修饰符来自 Pausable
        return super.transfer(to, amount);
        // super.transfer calls the parent (ERC20) implementation
        // super.transfer 调用父类 (ERC20) 实现
    }

    // Create a mint function that uses SafeMath and respects maxSupply
    // 创建一个使用 SafeMath 并尊重 maxSupply 的 mint 函数
    function mint(address to, uint256 amount) public onlyOwner {
        // onlyOwner modifier is from Ownable
        // onlyOwner 修饰符来自 Ownable
        // This is a custom function, not directly from OpenZeppelin
        // 这是一个自定义函数,不是直接来自 OpenZeppelin 的

        // Using SafeMath's add function to check for overflow
        // 使用 SafeMath 的 add 函数来检查溢出
        uint256 newTotalSupply = totalSupply().add(amount);
        // totalSupply() is from ERC20
        // totalSupply() 来自 ERC20
        // .add() is from SafeMath library
        // .add() 来自 SafeMath 库

        require(newTotalSupply <= maxSupply, "Cannot exceed maximum supply");
        // Custom logic, not from OpenZeppelin

        _mint(to, amount);
        // _mint is an internal function from ERC20
        // _mint 是 ERC20 的内部函数
    }

    // Create a burn function
    // 创建一个 burn 函数
    function burn(uint256 amount) public {
        // Custom function, not directly from OpenZeppelin
        // 自定义函数,并非直接来自 OpenZeppelin

        _burn(msg.sender, amount);
        // _burn is an internal function from ERC20
        // _burn 是 ERC20 的内部函数
    }
}

还有更多智能合约可以在不同的情况下使用

只需打开 OpenZeppelin 的 Github。

花一些时间在 Github 上,你就会明白哪些合约可以用在什么情况下。

感谢你的阅读,我希望这篇文章能帮助你理解这些概念。

与我联系:

领英 | 电子邮件 | Medium | X

  • 原文链接: coinsbench.com/openzeppe...
  • 登链社区 AI 助手,为大家转译优秀英文文章,如有翻译不通的地方,还请包涵~
点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
CoinsBench
CoinsBench
https://coinsbench.com/