ThunderChain Asset Smart Contract Standards

TRC1 Token Asset Standards

Introduction

TRC1 is an agreed standard of smart contract token, compatible with Ethereum ERC20 standard. The developer develops token contract in TRC1 agreed standard to realize the standard method and status in the agreement, including the important functions such as transfer and approve.

Abstract

Smart Contract token developed based on TRC1 can be used in interaction by other contract apps to achieve token distribution and transaction.

Standards

Methods:

  1. name

Optional, returns name of the token For example: "MyToekn"

function name() view returns (string name)
  1. symbol

Optional, returns the symbol of the token For example: "MT"

function symbol() view returns (string symbol)
  1. decimals

Optional, returns the number of decimals the token uses

function decimals() view returns (uint8 decimals)
  1. totalSupply

token number function totalSupply() view returns (uint256 totalSupply)

  1. balanceOf

Get token balance of _owner account

function balanceOf(address _owner) view returns (uint256 balance)
  1. transfer

The caller transfers token in the number of _value to _to address account, and the trigger of Transfer Event is compulsory. If the account balance of the caller is insufficient, exception should be thrown.

function transfer(address _to, uint256 _value) returns (bool success)
  1. transferFrom

transferFrom should be used in apprve method below. In approve method, the caller is authorized to use the balance of _from account in certain amount. The caller can use transferFrom method to transfer token in the number of _value from _from account to _to account. The usages include the operation of authorizing the smart contract account to use token of _from account to offset the service fee of transfer, etc.
The call needs to tigger Transfer Event. If token number authorized by _from account is insufficient, exception should be thrown.

function transferFrom(address _from, address _to, uint256 _value) returns (bool success)
  1. approve

To authorize _spender account to use caller's token of _value part. Repeated call will cover _value set previously.

function approve(address _spender, uint256 _value) returns (bool success)
  1. allowance

To inquire token number of _owner account that _spender account is allowed to use.

function allowance(address _owner, address _spender) view returns (uint256 remaining)

Events:

  1. Transfer

If there is transfer token, this event should be triggered. Though transaction amount is 0, it should be still triggered. If the smart contract creates and issues more token, this event should be triggered, and _from account address should be set as 0x0.

event Transfer(address indexed _from, address indexed _to, uint256 _value)
  1. Approval

This event should be triggered when the call of approve method succeeds.

event Approval(address indexed _owner, address indexed _spender, uint256 _value)

Realization

Some realized smart contracts in ERC20 standard are shown as follows. The inheritance of these can achieve token issuance in realized standard smart contract.

TRC2 Unique Asset Standards

Introduction

This file described an irreplaceable asset standard interface.

Abstract

To achieve standard irreplaceable asset in smart contract, and provide basic functions of asset tracing and transfer. We consider the conditions of individual asset ownership and transfer, and operation entrustment to third party. Irreplaceable unique asset can be the ownership of digital content or collections, such as Crypto Kitties and original music album, etc. The assets should be distinguishable. For example, every Crypto Kitty and every original music album should be unique. The ownership of each asset should be traced independently.

Applicable Scenarios

We can create URI for the asset or asset credential to save it at certain reliable position, and create unique asset Token in the smart contract with the URI. Based on the Token, we can achieve:

  • Asset ownership judgment, such as copyright registration
  • Asset transaction, such as digital content sales platform, and artwork auction platform

Standards

Event Description
Transfer asset ownership transfer event
Approval asset approval address revision event
ApprovalForAll operator enabled or disabled event
Function Description
name     smart contract name
symbol   smart contract name abbreviation
tokenURI     inquire given asset URI
balanceOf    inquire owner asset quantity
ownerOf  search asset owner
safeTransferFrom safe asset ownership transfer
transferFrom     asset ownership transfer
approve  set or revise asset approval address
setApprovalForAll enable or disable third-party operator to manage all the assets of msg.sender
getApproved  get approved address of certain asset
isApprovedForAll inquire whether the address is authorized operator of another address
interface TRC2 {
// Triggered when asset ownership changes
// Triggered when asset is created (`from` == 0) and destructed (`to` == 0)
// Not triggered when asset is created and distributed for smart contract creation
// After asset transfer, asset approved address should be reset as blank
    event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);

// Triggered when asset approved address is revised
// 0 address means no approval address
// When Transfer event is triggered, asset approved address should be reset as blank
    event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);

// Triggered when operator is enabled or disabled
// The operator can manage all the assets of the asset owner
    event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);

// Inquire asset quantity of the asset owner
// The asset distribution to 0 address is invalid. This function for inquiring 0 address will receive throw exception
// address of @param _owner inquiry of balance
// @return `_owner` asset quantity, which may be 0
    function balanceOf(address _owner) external view returns (uint256);

// Search asset owner
// The asset distribution to 0 address is invalid. The inquiry will receive throw exception
// @param _tokenId asset identification
// @return asset owner
    function ownerOf(uint256 _tokenId) external view returns (address);

// Transfer asset ownership from one address to another address
// Unless `msg.sender` is asset owner, authorized operator or authorized address of the asset, exception will be thrown
// If `_from` is not the asset owner, exception will be thrown
// If `_to` is 0 address, exception will be thrown
// If `_tokenId` is not valid asset, exception will be thrown
// After the transfer, use function to check whether `_to` is a smart contract (code size > 0)
// If so, call `onERC721Received` of `_to` and exception will be thrown. If returned value is not
    // `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`
// @param _from current asset owner
// @param _to new owner
// @param _tokenId asset for transfer
// @param data additional data with no designated format. Sent when function `_to` is called
    function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable;

// Transfer asset ownership from one address to another address
// data parameter is blank and others are the same with the previous function
// @param _from current asset owner
// @param _to new owner
// @param _tokenId asset for transfer
    function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable;

// Asset ownership transfer
// The caller needs to confirm that `_to` can receive the asset. Or else, they can be lost permanently
// Unless `msg.sender` is asset owner, authorized operator or authorized address of the asset, exception will be thrown
// If `_from` is not the asset owner, exception will be thrown
// If `_to` is 0 address, exception will be thrown
// If `_tokenId` is not valid asset, exception will be thrown
// @param _from current asset owner
// @param _to new owner
// @param _tokenId asset for transfer
    function transferFrom(address _from, address _to, uint256 _tokenId) external payable;

// Set or revise asset approval address
// zero address means no approval address
// Unless `msg.sender` is asset owner, or authorized operator of the owner, exception will be thrown
// @param _approved new approved address of the asset
// @param _tokenId asset identification
    function approve(address _approved, uint256 _tokenId) external payable;

// Enable or disable third-party operator to manage all the assets of `msg.sender`
// Send ApprovalForAll event. The smart contract must allow the setting of multiple operators for each owner
// @param _operator address added to authorized operator set
// @param _approved fill in true when it is enabled, and false when it is disabled
    function setApprovalForAll(address _operator, bool _approved) external;

// get approved address of certain asset
// Exception is thrown when `_tokenId` is invalid
// @param _tokenId asset identification
// @return approved address of the asset. If there is no setting, return 0 address
    function getApproved(uint256 _tokenId) external view returns (address);

// Inquire whether the address is the authorized operator of another address
// @param _owner asset owner address
// @param _operator operator address
// Return true if @return `_operator` is the operator of `_owner`. Or else, return false
    function isApprovedForAll(address _owner, address _operator) external view returns (bool);

// Smart Contract name
    function name() external view returns (string _name);

// Smart Contract name abbreviation
    function symbol() external view returns (string _symbol);

// Inquire given asset URI
// Exception is thrown if `_tokenId` is invalid
    function tokenURI(uint256 _tokenId) external view returns (string);
}

Asset Identification

Every asset is identified by unique ID in uint256 type in the smart contract. The identification cannot be revised in contract life cycle, and key value pair (contract address, uint256 tokenId) can globally uniquely and completely identify specific asset in blockchain. For realization, the smart contract can simply add from 0 for the ID, but should not assume ID value has any specific format. It must deem ID as a black box. It also needs to be noted that the asset may become invalid due to destruction.

Transfer Mechanism

Provide safe transfer function safeTransferFrom and unsafe function transferFrom. The transfer can be initiated by the following objects:

  • Asset owner
  • Asset approved address
  • Authorized operator of current asset owner

Moreover, authorized operator can also set asset approved address, which provides a set of powerful tool for wallet, agent and auction apps, and enables the rapid use of plenty of assets.

TRC2_1 Authorizable Unique Asset Standards

Introduction

The file describes authorizable irreplaceable asset standard interface.

Abstract

To achieve standard irreplaceable asset in smart contract, and provide basic function of asset tracing and transfer. We consider the conditions of individual's asset ownership and transfer, and operation entrustment to third party. Irreplaceable unique asset can be the ownership of digital content or collections, such as Crypto Kitties and original music album, etc. The assets should be distinguishable. For example, every Crypto Kitty and every original music album should be unique. The ownership of each asset should be traced independently.

Applicable Scenarios

We can create URI for the asset or asset credential to save it at certain reliable position, and create unique asset Token in the smart contract with the URI. Based on the Token, we can achieve:

  • Asset ownership judgment, such as copyright registration
  • Asset transaction, such as digital content sales platform, and artwork auction platform

Standards

Event Description
Transfer asset ownership transfer event
Approval asset approval address revision event
ApprovalForAll operator enabled or disabled event
Accessible access authority revision event
Function Description
name     smart contract name
symbol   smart contract name abbreviation
tokenURI     inquire given asset URI
balanceOf    inquire owner asset quantity
ownerOf  search asset owner
safeTransferFrom safe asset ownership transfer
transferFrom     asset ownership transfer
approve  set or revise asset approval address
setApprovalForAll enable or disable third-party operator to manage all the assets of msg.sender
getApproved  get approved address of certain asset
isApprovedForAll inquire whether the address is authorized operator of another address
setAccessible set/revise/cancel access authority
accessible inquire access authority
interface TRC2_1 {
// Triggered when asset ownership changes
// Triggered when asset is created (`from` == 0) and destructed (`to` == 0)
// Not triggered when asset is created and distributed for contract creation
// After asset transfer, asset approved address should be reset as blank
    event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);

// Triggered when asset approved address is revised
// 0 address means no approval address
// When Transfer event is triggered, asset approved address should be reset as blank
    event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);

// Triggered when operator is enabled or disabled
// The operator can manage all the assets of the asset owner
    event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);

// Triggered when setting/revising/cancelling access authority
    event Accessible(uint256 _tokenId, address _to, uint256 _data);

// Inquire asset quantity of the asset owner
// The asset distribution to 0 address is invalid. This function for inquiring 0 address will throw exception
// address of @param _owner inquiry of balance
// @return `_owner` asset quantity, which may be 0
    function balanceOf(address _owner) external view returns (uint256);

// Search asset owner
// The asset distribution to 0 address is invalid. The inquiry will throw exception
// @param _tokenId asset identification
// @return asset owner
    function ownerOf(uint256 _tokenId) external view returns (address);

// Transfer asset ownership from one address to another address
// Unless `msg.sender` is asset owner, authorized operator or authorized address of the asset, exception will be thrown
// If `_from` is not the asset owner, exception will be thrown
// If `_to` is 0 address, exception will be thrown
// If `_tokenId` is not valid asset, exception will be thrown
// After the transfer, use function to check whether `_to` is a smart contract (code size > 0)
// If so, call `onERC721Received` of `_to` and throw exception will be executed. If returned value is not
    // `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`
// @param _from current asset owner
// @param _to new owner
// @param _tokenId asset for transfer
// @param data additional data with no designated format. Sent when function `_to` is called
    function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable;

// Transfer asset ownership from one address to another address
// data parameter is blank and others are the same with the previous function
// @param _from current asset owner
// @param _to new owner
// @param _tokenId asset for transfer
    function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable;

// Asset ownership transfer
// The caller needs to confirm that `_to` can receive the asset. Or else, they can be lost permanently
// Unless `msg.sender` is asset owner, authorized operator or authorized address of the asset, exception will be thrown
// If `_from` is not the asset owner, exception will be thrown
// If `_to` is 0 address, exception will be thrown
// If `_tokenId` is not valid asset, exception will be thrown
// @param _from current asset owner
// @param _to new owner
// @param _tokenId asset for transfer
    function transferFrom(address _from, address _to, uint256 _tokenId) external payable;

// Set or revise asset approval address
// zero address means no approval address
// Unless `msg.sender` is asset owner, or authorized operator of the owner, exception will be thrown
// @param _approved new approved address of the asset
// @param _tokenId asset identification
    function approve(address _approved, uint256 _tokenId) external payable;

// Enable or disable third-party operator to manage all the assets of `msg.sender`
// Send ApprovalForAll event. The smart contract must allow the setting of multiple operators for each owner
// @param _operator address added to authorized operator set
// @param _approved fill in true when it is enabled, and false when it is disabled
    function setApprovalForAll(address _operator, bool _approved) external;

// get approved address of certain asset
// Exception is thrown when `_tokenId` is invalid
// @param _tokenId asset identification
// @return approved address of the asset. If there is no setting, return 0 address
    function getApproved(uint256 _tokenId) external view returns (address);

// Inquire whether the address is the authorized operator of another address
// @param _owner asset owner address
// @param _operator operator address
// Return true if @return `_operator` is the operator of `_owner`. Or else, return false
    function isApprovedForAll(address _owner, address _operator) external view returns (bool);

// Set/revise/cancel access authority
// Unless `msg.sender` is asset owner, authorized operator or authorized address of the asset, exception will be thrown
// @param _tokenId asset for operation
// @param _to user address
// @param _data user-defined data
    function setAccessible(uint256 _tokenId, address _to, uint256 _data) external;

// Inquire access authority
// @param _tokenId asset for inquiry
// @param _to user address
// Return user-defined `_data` valye @return. If there is no record, return 0
    function accessible(uint256 _tokenId, address _to) external view returns (uint256);

// Contract name
    function name() external view returns (string _name);

/// Contract name abbreviation
    function symbol() external view returns (string _symbol);

// Inquire given asset URI
// Exception will be thrown if `_tokenId` is invalid
    function tokenURI(uint256 _tokenId) external view returns (string);
}

Asset Identification

Every asset is identified by unique ID in uint256 type in the smart contract. The identification cannot be revised in contract life cycle, and key value pair (contract address, uint256 tokenId) can globally uniquely and completely identify specific asset in blockchain. For realization, the smart contract can simply add from 0 for the ID, but should not assume ID value has any specific format. It must deem ID as a black box. It also needs to be noted that the asset may become invalid due to destruction.

Transfer Mechanism

Provide safe transfer function safeTransferFrom and unsafe function transferFrom. The transfer can be initiated by the following objects:

  • Asset owner
  • Asset approved address
  • Authorized operator of current asset owner

Moreover, authorized operator can also set asset approved address, which provides a set of powerful tool for wallet, agent and auction apps, and enables the rapid use of plenty of assets.

Access Authorization

Function setAccessible can be used to authorize other user's access to certain asset. The last parameter data is used to store extra user-defined data, such as time stamp storing authorization expiry. When data parameter is set as 0, cancelling authorization operation should be executed. The authorized user should be allowed to execute read-only access to asset, and cannot revise, transfer the asset or reauthorize the asset to third party.