NXT support for ERC20 tokens

From Nxt Wiki
(Redirected from Erc 20)
Jump to: navigation, search

1 Introduction

ERC20 is a popular Ethereum standard for token management, explained here https://github.com/ethereum/EIPs/issues/20

The NXT asset exchange already supports most of the ERC20 operations out of the box without the need to issue a smart contract.

This document explains how to implement the ERC20 APIs using the equivalent NXT APIs.

2 Issuing Tokens

Each ERC20 token is represented as an NXT asset.

Issue the asset using the issueAsset API.

Each asset is identified by a unique asset id set to the transaction id of the issueAsset transaction.

3 Implementing the ERC20 APIs

totalSupply - to get the total token supply invoke the getAsset API and supply the asset id. The total supply is provided by the "initialQuantityQNT" attribute, the existing supply, after possible share deletes, is specified by the "quantityQNT" attribute. Quantity values are always provided as integer values, the number of decimal positions to apply to these quantities is specified by the "decimals" attribute.

balanceOf - to get the account balance of another account with a given address, invoke the getAccountAssets API and provide the NXT address as the "account" parameter and the asset id as the "asset" parameter.

For example http://localhost:7876/nxt?requestType=getAccountAssets&account=yourAccountId&asset=6789385243274909976

The "unconfirmedQuantityQNT" attribute in the response represents the current account balance. The "quantityQNT" attribute represents the quantity available for use at the moment after taking into account balance locked by open asset orders.

transfer - to send tokens from your address to another address use the transferAsset API, specify the recipient address, the asset id and the quantity to transfer as well as your account passphrase, the transaction fee and transaction deadline.

The transferAsset transaction is recorded on the NXT blockchain. You can later track token transfers using the getAssetTransfers API

transferFrom - transferring tokens from a sender account to a recipient account in case the transaction issuer is not the sender, is not directly supported by NXT (it's also unclear what is the exact use case for this API) however in case you need to support it, you can implement the following procedure:

Submit a tranferAsset API but instead of specifying the sender secretPharse specify the sender publicKey which you can obtain using the getAccountPublicKey API by providing the sender address. As a result you'll receive the unsigned transaction bytes in hex format. Send the these bytes to the owner of the sender account, you can use a sendMessage transaction or send it off blockchain. The sender account should then submit a broadcastTransaction API to sign the bytes with his passphrase and record the transaction on the blockchain.

approve - various approval methods can be implemented using the NXT phasing attachment, including the ability to approve a transaction only by an account holding a certain asset balance.

allowance - is not directly implemented by NXT, by default the allowance is the the available asset balance.

4 Trigger the ERC20 events

Events such as transfer and approve can be triggered by using the getBlock API, iterate over the transactions included in the block and look for asset transfer transactions for a given asset (transaction type 2 subType 1).

Care should be taken to notify only on phased transactions with the attribute "approved".

5 Summary

The NXT asset exchange supports out of the box most of the functionality provided by the Ethereum ERC20 standard.

In addition the asset exchange provides a full dex functionality and additional options such as the ability to issue a signleton asset for reduced fee or delete asset shares.

NXT also supports a different type of token called "currency", among other features, a currency provides the ability to restrict token transfers so that they are allowed only between the issuer account and other accounts. This function prevents free trade of the token without the issuer permission which is desirable in case of voting rights distribution for example.