Skip to main content

ERC​

  • ERC is an acronym that stands for Ethereum Request for Comments.
  • ERCs are application-level standards for Ethereum and can include token standards, name registries, library formats etc.
  • Common ERC standards define a required set of functions for a token type, allowing applications and smart contracts to interact with them in a predictable way.
  • Most common ERC standard used is ERC-20, a type of standard that defines the creation, use and exchange of Ethereum-based tokens.

ERC-20: Token Standard​

ERC-20
Motivation

A standard interface allows any tokens on Ethereum to be re-used by other applications: from wallets to decentralized exchanges.

IERC20.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)pragma solidity ^0.8.0;/** * @dev Interface of the ERC20 standard as defined in the EIP. */interface IERC20 {    /**     * @dev Emitted when `value` tokens are moved from one account (`from`) to     * another (`to`).     *     * Note that `value` may be zero.     */    event Transfer(address indexed from, address indexed to, uint256 value);    /**     * @dev Emitted when the allowance of a `spender` for an `owner` is set by     * a call to {approve}. `value` is the new allowance.     */    event Approval(address indexed owner, address indexed spender, uint256 value);    /**     * @dev Returns the amount of tokens in existence.     */    function totalSupply() external view returns (uint256);    /**     * @dev Returns the amount of tokens owned by `account`.     */    function balanceOf(address account) external view returns (uint256);    /**     * @dev Moves `amount` tokens from the caller's account to `to`.     *     * Returns a boolean value indicating whether the operation succeeded.     *     * Emits a {Transfer} event.     */    function transfer(address to, uint256 amount) external returns (bool);    /**     * @dev Returns the remaining number of tokens that `spender` will be     * allowed to spend on behalf of `owner` through {transferFrom}. This is     * zero by default.     *     * This value changes when {approve} or {transferFrom} are called.     */    function allowance(address owner, address spender) external view returns (uint256);    /**     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.     *     * Returns a boolean value indicating whether the operation succeeded.     *     * IMPORTANT: Beware that changing an allowance with this method brings the risk     * that someone may use both the old and the new allowance by unfortunate     * transaction ordering. One possible solution to mitigate this race     * condition is to first reduce the spender's allowance to 0 and set the     * desired value afterwards:     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729     *     * Emits an {Approval} event.     */    function approve(address spender, uint256 amount) external returns (bool);    /**     * @dev Moves `amount` tokens from `from` to `to` using the     * allowance mechanism. `amount` is then deducted from the caller's     * allowance.     *     * Returns a boolean value indicating whether the operation succeeded.     *     * Emits a {Transfer} event.     */    function transferFrom(        address from,        address to,        uint256 amount    ) external returns (bool);}

Make your own Coin​



ERC-721​

ERC-721
  • EIP: ERC-721
  • Title: Non-fungible Token Standard
  • Author: Dieter Shirley dete@axiomzen.co
  • Type: Standard
  • Created: 2017-09-20
motivation

A standard interface for non-fungible tokens (NFT), also known as deeds. A standard interface allows wallet/broker/auction applications to work with any NFT on Ethereum. We provide for simple ERC-721 smart contracts as well as contracts that track an arbitrarily large number of NFTs. Additional applications are discussed below.

IERC721.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.7.0) (token/ERC721/IERC721.sol)pragma solidity ^0.8.0;import "../../utils/introspection/IERC165.sol";/** * @dev Required interface of an ERC721 compliant contract. */interface IERC721 is IERC165 {    /**     * @dev Emitted when `tokenId` token is transferred from `from` to `to`.     */    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);    /**     * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.     */    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);    /**     * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.     */    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);    /**     * @dev Returns the number of tokens in ``owner``'s account.     */    function balanceOf(address owner) external view returns (uint256 balance);    /**     * @dev Returns the owner of the `tokenId` token.     *     * Requirements:     *     * - `tokenId` must exist.     */    function ownerOf(uint256 tokenId) external view returns (address owner);    /**     * @dev Safely transfers `tokenId` token from `from` to `to`.     *     * Requirements:     *     * - `from` cannot be the zero address.     * - `to` cannot be the zero address.     * - `tokenId` token must exist and be owned by `from`.     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.     *     * Emits a {Transfer} event.     */    function safeTransferFrom(        address from,        address to,        uint256 tokenId,        bytes calldata data    ) external;    /**     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients     * are aware of the ERC721 protocol to prevent tokens from being forever locked.     *     * Requirements:     *     * - `from` cannot be the zero address.     * - `to` cannot be the zero address.     * - `tokenId` token must exist and be owned by `from`.     * - If the caller is not `from`, it must have been allowed to move this token by either {approve} or {setApprovalForAll}.     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.     *     * Emits a {Transfer} event.     */    function safeTransferFrom(        address from,        address to,        uint256 tokenId    ) external;    /**     * @dev Transfers `tokenId` token from `from` to `to`.     *     * WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.     *     * Requirements:     *     * - `from` cannot be the zero address.     * - `to` cannot be the zero address.     * - `tokenId` token must be owned by `from`.     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.     *     * Emits a {Transfer} event.     */    function transferFrom(        address from,        address to,        uint256 tokenId    ) external;    /**     * @dev Gives permission to `to` to transfer `tokenId` token to another account.     * The approval is cleared when the token is transferred.     *     * Only a single account can be approved at a time, so approving the zero address clears previous approvals.     *     * Requirements:     *     * - The caller must own the token or be an approved operator.     * - `tokenId` must exist.     *     * Emits an {Approval} event.     */    function approve(address to, uint256 tokenId) external;    /**     * @dev Approve or remove `operator` as an operator for the caller.     * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.     *     * Requirements:     *     * - The `operator` cannot be the caller.     *     * Emits an {ApprovalForAll} event.     */    function setApprovalForAll(address operator, bool _approved) external;    /**     * @dev Returns the account approved for `tokenId` token.     *     * Requirements:     *     * - `tokenId` must exist.     */    function getApproved(uint256 tokenId) external view returns (address operator);    /**     * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.     *     * See {setApprovalForAll}     */    function isApprovedForAll(address owner, address operator) external view returns (bool);}

References​