Understanding about OpenZeppelin

Introduction

OpenZeppelin is an important and open-source organization in the blockchain, which provides useful features and services for developing, deploying, and managing smart contracts securely. It also provides an easier way for developers to make a decentralized application (dApps) on the blockchain platform Ethereum. It was founded in 2017.

OpenZeppelin

Key features of OpenZeppelin

Let's discuss some features of OpenZeppelin in the given below.

  1. Smart Contract Library: OpenZeppelin gives an inclusive library of smart contracts which used to address common patterns and functionalities of token standards (ERC20, ERC721), access control, upgradeability, etc.
  2. Security: It is an important feature of OpenZeppelin. In this feature, contracts are tested, audited, and reviewed by security experts. This reduces the risk of vulnerabilities and exploits in the deployed smart contracts.
  3. Upgradability: OpenZeppelin provides tools for creating perfect smart contracts, allowing developers to fix bugs, add features, or improve their contracts without disrupting the existing state or requiring a complete redeployment.
  4. Developer Tools: OpenZeppelin includes various tools and utilities to enhance the developer experience. These tools help with contract deployment, testing, and interacting with smart contracts on the blockchain.
  5. Community and Support: OpenZeppelin has a strong community of developers and users who contribute to the project, share knowledge, and provide support. This collaborative environment promotes continuous improvement and innovation.
  6. Documentation: The framework comes with extensive documentation, tutorials, and examples to help developers understand how to use the components and build secure (dApps) efficiently.

Let's see the main website of "OpenZeppelin" by clicking the link given below.

 Website

Visit to OpenZeppelin website: www.openzeppelin.com

OpenZeppelin Contracts wizard

The OpenZeppelin Contracts Wizard is an interactive tool designed to help developers create smart contracts easily. It allows users to build contracts by selecting from various components provided by the OpenZeppelin Contracts library.

Let's see the figure of Contracts Wizard on the main website of OpenZeppelin below.

 Contracts Wizard

Website Link: https://www.openzeppelin.com

Openzeppelin Contracts Wizard Library

There are mainly three types of contracts wizard libraries that are used by developers to build a decentralized application(dApps).

Library 1. ERC20

ERC20 is a type of technical method used for smart contracts on the Ethereum blockchain. It defines a common list of rules that an Ethereum token must implement, allowing developers to create compatible tokens that can be traded and managed easily.

Features of "ERC20" provided by OpenZeppelin Contracts Wizard.

  • Mintable: It is featured in a token contract that uses the function “mint new tokens” to create a new token. This function is controlled by private accounts or governance processes.
  • Burnable: It is a feature of OpenZeppelin contracts wizard in which the token holders can burn or destroy their token to voluntarily decrease their token supply on the basis of their economic term.
  • Pausable: It is also a feature of OpenZeppelin contracts wizard in which we can pause the many actions in contracts like transfers, minting, and burning of tokens. This is useful in emergency response to security threats and bugs.
  • Permit: It is a feature of OpenZeppelin contracts wizard in which the permit function refers to a specific feature introduced by Ethereum Improvement Proposal (EIP) 2612. This feature allows for a gas-efficient way to approve token allowances using a signed message, rather than requiring an on-chain transaction for approval.
  • Flash Minting: It is a feature of OpenZeppelin contracts wizard in which the creation and destruction of fungible tokens within a single transaction. This concept leverages the ERC20 standard capabilities, making it possible to execute complex financial strategies such as arbitrage, collateral swaps, and more without requiring upfront capital.
  • Transparent: It is part of a smart contract, which defines the Transparent Proxy Pattern is a widely-used method for making smart contracts upgradeable. It involves deploying a proxy contract that delegates calls to an implementation contract.
  • Ownable: It is part of Ethereum smart contracts, the concept of "Ownable" refers to a design pattern that establishes ownership and provides a basic access control mechanism.
  • Timestamp: In the context of smart contracts, a timestamp is a specific point in time. Timestamps in smart contracts are often used to handle time-sensitive operations, such as voting periods, lock-up periods, or scheduling future actions.

Code Example

// SPDX-License-Identifier: MIT
// Compatible with OpenZeppelin Contracts ^5.0.0
pragma solidity ^0.8.20;

import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20BurnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20PausableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20PermitUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20VotesUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20FlashMintUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

contract MyToken is Initializable, ERC20Upgradeable, ERC20BurnableUpgradeable, ERC20PausableUpgradeable, OwnableUpgradeable, ERC20PermitUpgradeable, ERC20VotesUpgradeable, ERC20FlashMintUpgradeable {
    /// @custom:oz-upgrades-unsafe-allow constructor
    constructor() {
        _disableInitializers();
    }
    function initialize(address initialOwner) initializer public {
        __ERC20_init("MyToken", "MTK");
        __ERC20Burnable_init();
        __ERC20Pausable_init();
        __Ownable_init(initialOwner);
        __ERC20Permit_init("MyToken");
        __ERC20Votes_init();
        __ERC20FlashMint_init();
    }
    function pause() public onlyOwner {
        _pause();
    }
    function unpause() public onlyOwner {
        _unpause();
    }
    function mint(address to, uint256 amount) public onlyOwner {
        _mint(to, amount);
    }
    function clock() public view override returns (uint48) {
        return uint48(block.timestamp);
    }
    // solhint-disable-next-line func-name-mixedcase
    function CLOCK_MODE() public pure override returns (string memory) {
        return "mode=timestamp";
    }
    // The following functions are overrides required by Solidity.
    function _update(address from, address to, uint256 value)
        internal
        override(ERC20Upgradeable, ERC20PausableUpgradeable, ERC20VotesUpgradeable)
    {
        super._update(from, to, value);
    }
    function nonces(address owner)
        public
        view
        override(ERC20PermitUpgradeable, NoncesUpgradeable)
        returns (uint256)
    {
        return super.nonces(owner);
    }
}

Above the code has been created in the open Zeppelin contracts wizard used by its type "ERC20". And enables its several features, votes utilities, access control, and upgradeability like Mintable, Burnable, Pausable, Permit, Flash Minting, Timestamp, Ownable, Transparent. If you open this code then use " REMIX-Ethereum. IDE " to run the code.

Library 2. ERC721

ERC721 is a type of technique that is used for non-fungible Tokens in the Ethereum blockchain. Unlike ERC20 tokens, which are fungible and identical to each other, ERC721 tokens are unique and distinguishable from one another. It represented digital art, collectibles, and real estate.

Features of "ERC721" provided by OpenZeppelin Contracts Wizard.

  • Mintable: The explanation is the same as "ERC20".
  • Auto Increment ID: It is a feature of a token contract in which the Incremental ‘ID’ of new tokens is automatically assigned. This is commonly used for each new entity (such as a token, user, or record) that needs a distinct identifier.
  • Burnable: The explanation is the same as "ERC20".
  • Pausable: The explanation is the same as "ERC20".
  • Enumerable: It is a feature of token contract that refers to the ability to enumerate tokens on-chain. It means that the smart contract includes functionality to list all tokens or all tokens owned by a specific address.
  • URI Storage: URI storage in the context of Ethereum tokens, refers to the way metadata (such as images, descriptions, and other attributes) associated with a token is stored and accessed.
  • Block Numbers: It is a feature of Ethereum smart contracts that refers to the specific number assigned to a block in the blockchain. This can be useful in on-chain governance, where past token balances might determine voting power at the time a proposal was created.
  • Roles: It is part of smart contracts in which all access control is more granularly than the single-owner model provided by the Ownable pattern. Roles allow multiple accounts to have different permissions to perform specific actions within the contract.
  • Managed: It is also part of contracts that refers to a type of access control where a central contract, often called a manager or management contract, defines a policy that allows certain callers to access certain functions of the managed contract.
  • UUPS: It stands for (Universal Upgradeable Proxy Standard) is a design pattern used for creating upgradeable smart contracts on Ethereum. It is a variation of the proxy pattern and is considered more efficient and flexible than the traditional transparent proxy pattern.

Code Example

// SPDX-License-Identifier: MIT
// Compatible with OpenZeppelin Contracts ^5.0.0
pragma solidity ^0.8.20;

import "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721EnumerableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721URIStorageUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721PausableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721BurnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/cryptography/EIP712Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721VotesUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";

contract MyToken is Initializable, ERC721Upgradeable, ERC721EnumerableUpgradeable, ERC721URIStorageUpgradeable, ERC721PausableUpgradeable, AccessControlUpgradeable, ERC721BurnableUpgradeable, EIP712Upgradeable, ERC721VotesUpgradeable, UUPSUpgradeable {
    bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
    uint256 private _nextTokenId;
    bytes32 public constant UPGRADER_ROLE = keccak256("UPGRADER_ROLE");
    /// @custom:oz-upgrades-unsafe-allow constructor
    constructor() {
        _disableInitializers();
    }
    function initialize(address defaultAdmin, address pauser, address minter, address upgrader)
        initializer public
    {
        __ERC721_init("MyToken", "MTK");
        __ERC721Enumerable_init();
        __ERC721URIStorage_init();
        __ERC721Pausable_init();
        __AccessControl_init();
        __ERC721Burnable_init();
        __EIP712_init("MyToken", "1");
        __ERC721Votes_init();
        __UUPSUpgradeable_init();
        _grantRole(DEFAULT_ADMIN_ROLE, defaultAdmin);
        _grantRole(PAUSER_ROLE, pauser);
        _grantRole(MINTER_ROLE, minter);
        _grantRole(UPGRADER_ROLE, upgrader);
    }
    function pause() public onlyRole(PAUSER_ROLE) {
        _pause();
    }
    function unpause() public onlyRole(PAUSER_ROLE) {
        _unpause();
    }
    function safeMint(address to, string memory uri) public onlyRole(MINTER_ROLE) {
        uint256 tokenId = _nextTokenId++;
        _safeMint(to, tokenId);
        _setTokenURI(tokenId, uri);
    }
    function _authorizeUpgrade(address newImplementation)
        internal
        onlyRole(UPGRADER_ROLE)
        override
    {}
    // The following functions are overrides required by Solidity.
    function _update(address to, uint256 tokenId, address auth)
        internal
        override(ERC721Upgradeable, ERC721EnumerableUpgradeable, ERC721PausableUpgradeable, ERC721VotesUpgradeable)
        returns (address)
    {
        return super._update(to, tokenId, auth);
    }
    function _increaseBalance(address account, uint128 value)
        internal
        override(ERC721Upgradeable, ERC721EnumerableUpgradeable, ERC721VotesUpgradeable)
    {
        super._increaseBalance(account, value);
    }
    function tokenURI(uint256 tokenId)
        public
        view
        override(ERC721Upgradeable, ERC721URIStorageUpgradeable)
        returns (string memory)
    {
        return super.tokenURI(tokenId);
    }
    function supportsInterface(bytes4 interfaceId)
        public
        view
        override(ERC721Upgradeable, ERC721EnumerableUpgradeable, ERC721URIStorageUpgradeable, AccessControlUpgradeable)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }
}

Above the code has been generated in the open Zeppelin contracts wizard used by its type "ERC721". And enables several features, votes utilities, access control, and upgradeability like Mintable, Auto Increment Ids, Burnable, Pausable, Enumerable, URI Storage, Block Numbers, Roles, and UUPS. If you open this code then use " REMIX-Ethereum. IDE " to run the code.

Library 3. ERC1155

The ERC-1155 is a level of standard in smart contracts. ERC1155 is a multi-token standard that allows the creation and management of multiple types of tokens within a single contract. This implementation is designed to be modular, making it easy to extend and customize according to the needs of different projects.

Features of "ERC1155" provided by OpenZeppelin Contracts Wizard.

  • Mintable: The explanation is the same as "ERC20".
  • Burnable: The explanation is the same as "ERC20".
  • Supply Tracking: It is the feature of the smart contract that refers to the ability to monitor and manage the total supply of each individual token ID within a single smart contract. ERC1155 is a multi-token standard on the Ethereum blockchain that supports fungible and non-fungible tokens in one contract. In ERC1155 that allows for multiple types of tokens to be managed under one contract address.
  • Pausable: The explanation is the same as "ERC20".
  • Updatable URI: In this feature of smart contract. An updatable URI is used for its ability to change the Uniform Resource Identifier (URI) which is connected with a specific token ID after the token has been created. The URI points to the JSON file that contains the metadata about the token, its name, description, image, and other attributes.
  • Managed: The explanation is the same as "ERC721".
  • Transparent: The explanation is the same as "ERC20".

Code Example

// SPDX-License-Identifier: MIT
// Compatible with OpenZeppelin Contracts ^5.0.0
pragma solidity ^0.8.20;

import "@openzeppelin/contracts-upgradeable/token/ERC1155/ERC1155Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/manager/AccessManagedUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC1155/extensions/ERC1155PausableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC1155/extensions/ERC1155BurnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC1155/extensions/ERC1155SupplyUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
contract MyToken is Initializable, ERC1155Upgradeable, AccessManagedUpgradeable, ERC1155PausableUpgradeable, ERC1155BurnableUpgradeable, ERC1155SupplyUpgradeable {
    /// @custom:oz-upgrades-unsafe-allow constructor
    constructor() {
        _disableInitializers();
    }
    function initialize(address initialAuthority) initializer public {
        __ERC1155_init("");
        __AccessManaged_init(initialAuthority);
        __ERC1155Pausable_init();
        __ERC1155Burnable_init();
        __ERC1155Supply_init();
    }
    function setURI(string memory newuri) public restricted {
        _setURI(newuri);
    }
    function pause() public restricted {
        _pause();
    }
    function unpause() public restricted {
        _unpause();
    }
    function mint(address account, uint256 id, uint256 amount, bytes memory data) public restricted {
        _mint(account, id, amount, data);
    }
    function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) public restricted {
        _mintBatch(to, ids, amounts, data);
    }
    // The following functions are overrides required by Solidity.
    function _update(address from, address to, uint256[] memory ids, uint256[] memory values) internal override(ERC1155Upgradeable, ERC1155PausableUpgradeable, ERC1155SupplyUpgradeable) {
        super._update(from, to, ids, values);
    }
}

Above the code has been created in the open zeppelin contracts wizard used by its type "ERC1155". And enables its all several features, votes utilities, access control, and upgradeability like Mintable, Burnable, Supply Tracking, Pausable, Updatable URI, Managed, and Transparent. If you open this code then use " REMIX-Ethereum. IDE " to run the code.

Conclusion

OpenZeppelin plays a crucial role in advancing blockchain technology by providing tools and resources that make it easier and safer to develop decentralized applications. Their efforts contribute significantly to the overall security and accessibility of the blockchain ecosystem.


Similar Articles