Tokens

contract Tokens is MainStorage, LibConstants, MGovernance, MTokens

Registration of a new token (registerToken) entails defining a new token ID identifying the token within the system, and associating it with an assetData array of bytes and a quantization factor (quantum).

The assetData is a byte array, with a size depending on the token type. For ETH, assetData is 4 bytes long. For ERC20 tokens, it is 36 bytes long.

For each token type, the following constant 4-byte hash is defined, called the selector:

ETH_SELECTOR = bytes4(keccak256(“ETH()”));
ERC20_SELECTOR = bytes4(keccak256(“ERC20Token(address)”));

For each token type, assetData is defined as follows:

For ETH, assetData = ETH_SELECTOR;
For ERC20, assetData = abi.encodePacked(ERC20_SELECTOR, address);

The quantum quantization factor defines the multiplicative transformation from the native token denomination as a 256b unsigned integer to a 63b unsigned integer representation as used by the Stark exchange. Only amounts in the native representation that represent an integer number of quanta are allowed in the system.

The token ID is restricted to be the result of a hash of the assetData and the quantum masked to 250 bits (to be less than the prime used) according to the following formula:

uint256 tokenID = uint256(keccak256(abi.encodePacked(assetData, quantum))) &
0x03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;

Once registered, tokens cannot be removed from the system, as their IDs may be used by off-chain accounts.

New tokens may only be registered by a Token Administrator. A Token Administrator may be instantly appointed or removed by the contract Governor (see MainGovernance). Typically, the Token Administrator’s private key should be kept in a cold wallet.

event LogTokenRegistered(uint256 tokenId, bytes assetData)
event LogTokenAdminAdded(address tokenAdmin)
event LogTokenAdminRemoved(address tokenAdmin)
modifier onlyTokensAdmin()
function registerTokenAdmin(address newAdmin)
external
 onlyGovernance()
function unregisterTokenAdmin(address oldAdmin)
external
 onlyGovernance()
function isTokenAdmin(address testedAdmin)
external
view
returns (bool)
function registerToken(uint256 tokenId, bytes calldata assetData, uint256 quantum)
external
 onlyTokensAdmin()
function getQuantum(uint256 tokenId)
public
view
returns (uint256 quantum)
function getAssetData(uint256 tokenId)
public
view
returns (bytes memory assetData)
function isEther(uint256 tokenId)
internal
view
returns (bool)