Understanding HRC-20 Token Smart Contract

HRC-20 is one of the most important smart contract standards on Hash Ahead. It has become the technical standard for all smart contracts used to implement fungible tokens on the Hash Ahead blockchain.

HRC-20 defines a common set of rules that all fungible Hash Ahead tokens should adhere to. Therefore, this token standard allows all types of developers to accurately predict how new tokens will work in the larger Hash Ahead system. This simplifies the task for developers because they can continue their work, knowing that as long as the tokens follow the rules, they do not need to redo each new project every time a new token is released.

Here, the functions that must be implemented by HRC-20 in the form of an interface are introduced.

pragma solidity ^0.8.18;

interface IHRC20 {

    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function allowance(address owner, address spender) external view returns (uint256);

    function transfer(address recipient, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);


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

The purpose of each function is explained line by line below. After that, we will show a simple implementation of an HRC-20 token.

Getter

Returns the total supply of the token. This function is a getter and does not modify the state of the contract. Please note that Solidity does not have floating-point numbers. Therefore, most tokens use 18 decimal places and return total supply and other results as follows: 1 token = 1000000000000000000. You need to be extra careful when dealing with tokens and not assume that every token has 18 decimal places.

The HRC-20 standard allows an address to enable another address to retrieve tokens from it. This getter returns the remaining number of tokens that spender is allowed to spend on behalf of the owner. This function is a getter and does not modify the state of the contract and should return 0 by default.

Function

Transfer tokens from the msg.sender address to the recipient address with the specified amount. This function emits the Transfer event defined later. It returns true if the transfer is successful.

Set the amount of allowance allowed for spender to transfer from the balance of the function caller (msg.sender). This function emits an Approval event. It returns a boolean indicating whether the allowance was successfully set.

This function moves the tokens with a specified amount from the sender's balance to the recipient's balance using the allowance mechanism. It then deducts the amount from the caller's balance. The function emits a Transfer event.

Event

This event is emitted when a quantity of tokens (value) is transferred from the from address to the to address. Typically, this is triggered by a transfer of tokens, but can also be triggered by the minting or burning of tokens, in which case the from or to address will be the zero address (0x00..0000).

This event is emitted when the owner approves the spender to spend a certain value of tokens.

Basic implementation of ERC-20 token

Here's the simplest code for an ERC-20 token:

Last updated