R5 Network
WebsiteR5 LabsGitHub
  • Getting Started
    • Hello & Welcome!
  • About R5
    • Overview
    • R5 Components
    • Consensus Mechanism
    • zkNet (Privacy)
  • R5 Coin
  • R5 Tokenomics
  • Tutorials & Guides
    • Connect & Use R5
      • R5 Desktop Wallet
      • MetaMask
      • Rabby Wallet
      • Coinbase Wallet
    • zkNet Web Wallet
    • R5 Desktop Wallet
      • Interface Overview
      • Send a Transaction
      • Receive a Transaction
      • Backup Your Wallet
      • Retrieve Your Private Key
    • How To: Deploy a Node
    • How To: Mine R5
    • How To: GPU Mine R5
    • How To: Build R5 From Source
    • How To: Connect Local Nodes
  • For Developers
    • R5 SDK
      • R5 Relayer
      • R5 Console
      • JS Console
      • CLI Wallet
      • SCdev
      • SSL Proxy
    • Hardware Requirements
    • R5 Testnet
    • R5 Devnet
    • Local Networks
    • JSON-RPC API
      • admin
      • debug
      • ethash
      • miner
      • net
      • r5 (eth)
      • rpc
      • txpool
      • web3
    • Indexer API
    • zkNet API
    • Node Configuration
    • Ethash-R5
    • Smart Contracts
    • Wrapped R5 (Native)
    • Tokens & NFTs
  • Bug Bounty Program
  • Resources
    • Website
    • R5 Labs
    • R5 Labs GitHub
Powered by GitBook
On this page
  • ERC20 Tokens
  • ERC721 Tokens (NFTs)
  1. For Developers

Tokens & NFTs

R5 is fully compatible with standard token protocols, including ERC20 and ERC721, along with other token standards available on Ethereum. This document provides technical guidance on developing and deploying tokens and non-fungible tokens (NFTs) on the R5 network. The following examples illustrate minimal implementations of both ERC20 and ERC721 contracts.


ERC20 Tokens

The ERC20 token standard defines a standard interface for fungible tokens. Contracts adhering to ERC20 allow for the transfer of tokens between addresses and enable applications to manage token balances consistently.

Below is an example of a basic ERC20 token contract written in Solidity (version 0.8.x):

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

contract ERC20Token {
    string public name = "R5Token";
    string public symbol = "R5T";
    uint8 public decimals = 18;
    uint256 public totalSupply;

    mapping(address => uint256) public balanceOf;
    mapping(address => mapping(address => uint256)) public allowance;

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);

    constructor(uint256 _initialSupply) {
        totalSupply = _initialSupply * 10 ** uint256(decimals);
        balanceOf[msg.sender] = totalSupply;
        emit Transfer(address(0), msg.sender, totalSupply);
    }

    function transfer(address _to, uint256 _value) public returns (bool success) {
        require(_to != address(0), "Invalid recipient");
        require(balanceOf[msg.sender] >= _value, "Insufficient balance");
        balanceOf[msg.sender] -= _value;
        balanceOf[_to] += _value;
        emit Transfer(msg.sender, _to, _value);
        return true;
    }

    function approve(address _spender, uint256 _value) public returns (bool success) {
        allowance[msg.sender][_spender] = _value;
        emit Approval(msg.sender, _spender, _value);
        return true;
    }

    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
        require(_to != address(0), "Invalid recipient");
        require(balanceOf[_from] >= _value, "Insufficient balance");
        require(allowance[_from][msg.sender] >= _value, "Allowance exceeded");
        balanceOf[_from] -= _value;
        balanceOf[_to] += _value;
        allowance[_from][msg.sender] -= _value;
        emit Transfer(_from, _to, _value);
        return true;
    }
}

Technical Notes

  • Deployment: Deploy the contract by specifying the initial supply. The total supply is allocated to the deployer’s address.

  • Standard Interface: The functions transfer, approve and transferFrom adhere strictly to the ERC20 specification.

  • Events: Emitted events allow external applications to track token transfers and approvals.


ERC721 Tokens (NFTs)

The ERC721 token standard is used for non-fungible tokens, which represent unique assets. Each token has a unique identifier, and the standard provides methods for transferring ownership and managing token metadata.

Below is an example of a minimal ERC721 contract written in Solidity (version 0.8.x):

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

interface IERC721 {
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
    function balanceOf(address owner) external view returns (uint256 balance);
    function ownerOf(uint256 tokenId) external view returns (address owner);
    function transferFrom(address from, address to, uint256 tokenId) external;
}

contract ERC721Token is IERC721 {
    string public name = "R5NFT";
    string public symbol = "R5N";
    mapping(uint256 => address) private _owners;
    mapping(address => uint256) private _balances;

    // Mint a new NFT to an address.
    function mint(address to, uint256 tokenId) public {
        require(to != address(0), "Invalid recipient");
        require(_owners[tokenId] == address(0), "Token already minted");
        _balances[to] += 1;
        _owners[tokenId] = to;
        emit Transfer(address(0), to, tokenId);
    }

    // Return the balance of an address.
    function balanceOf(address owner) public view override returns (uint256) {
        require(owner != address(0), "Invalid address");
        return _balances[owner];
    }

    // Return the owner of a given token.
    function ownerOf(uint256 tokenId) public view override returns (address) {
        address owner = _owners[tokenId];
        require(owner != address(0), "Token not minted");
        return owner;
    }

    // Transfer a token from one address to another.
    function transferFrom(address from, address to, uint256 tokenId) public override {
        require(ownerOf(tokenId) == from, "Not the token owner");
        require(to != address(0), "Invalid recipient");
        _balances[from] -= 1;
        _balances[to] += 1;
        _owners[tokenId] = to;
        emit Transfer(from, to, tokenId);
    }
}

Technical Notes

  • Minting: The mint function creates a new token and assigns it to an address. Ensure that token IDs are unique.

  • Standard Compliance: Functions such as balanceOf, ownerOf, and transferFrom are implemented according to the ERC721 standard.

  • Events: The Transfer event is emitted upon minting and transferring tokens, allowing external tracking of NFT ownership.

PreviousWrapped R5 (Native)NextBug Bounty Program

Last updated 2 months ago