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,approveandtransferFromadhere 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):
Technical Notes
Minting: The
mintfunction creates a new token and assigns it to an address. Ensure that token IDs are unique.Standard Compliance: Functions such as
balanceOf,ownerOf, andtransferFromare implemented according to the ERC721 standard.Events: The
Transferevent is emitted upon minting and transferring tokens, allowing external tracking of NFT ownership.
Last updated