Token Template

Summary

Tokens can be designed in a customised manner using a template smart contract that can be extended or instantiated.

Context

Blockchain provides a trustworthy platform to realise tokenisation. In a blockchain application, a token represents a programmable digital unit of value (i.e., assets) recorded on the blockchain for a specific purpose, like buying a product or consuming a service. The same application may use different tokens or asset classes, e.g., a food stamp issued for a child is usually different from an adult.

Problem

A token is usually defined as a data structure embedded in a smart contract to represent an asset. How can developers design and develop different types of tokens with various features?

Forces

  • Flexibility – Every participant within a blockchain network is able to access all the historical transactions on the blockchain, which is required to enable them to validate previous transactions. The transactions on a public blockchain are also accessible to everyone with access to the Internet, simply using tools like a blockchain explorer such as Etherscan.
  • Interoperability and liquidity – Tokens supported by different applications might need to be exchanged frequently.
  • Vulnerabilities – Handling tokens with a high value is risky, as lost tokens cannot be replaced/recovered on the blockchain. Further, vulnerabilities or bugs might be introduced when developing a blockchain-based application. As blockchain is an immutable data store, updating already deployed smart contracts is impossible. This makes it difficult to fix bugs by releasing new versions of a smart contract.
  • Productivity – Blockchain is an emerging technology with limited tooling and documentation. Thus, developers can have a steep learning curve, which affects developer productivity.
  • Cost – On a public blockchain, transaction fees need to be paid to deploy and execute smart contracts. The execution fee is proportional to the computational and storage complexity of the smart contract.

Solution

Blockchain-based payment applications might need to issue different types of tokens with customisation. However, as most tokens have a common set of properties and functions, a token can be defined as a template smart contract. The template can then be either extended or instantiated by another smart contract to issue a new token at run time. Multiple tokens may also be issued based on the same template by setting different properties. There can be two types of tokens: cash tokens and voucher tokens. Cash tokens are fungible tokens that are fully exchangeable with each other and can be divided into smaller denominations for multiple transactions. In contrast, voucher tokens are non-fungible tokens that are unique and must be spent in a single transaction regardless of the price of a product or service. Both cash tokens and voucher tokens can be developed as token templates.

Sequence diagram of a token template

Sequence diagram of a token template

 

The above figure shows the sequence of activities required to create and utilise a token template. In the token template smart contract, the user can define the token’s attributes and methods to manage the token. The common attributes usually consist of essential information such as token name, symbol, number of decimal places, and type. Other attributes such as the minting process, initial distribution of tokens, and the burning process could be specified depending on the token’s intended use. The specified methods enforce rules that govern how the tokens are minted, owned, and transferred. Optional attributes and methods in the template allow customisation. If that is insufficient, the template smart contract can be extended by another smart contract to support customised attributes and methods.

Once developed, the template would change rarely. Thus, it can be thoroughly tested and certified to be free of defects before deployment. Once developed, deploy the template to the blockchain as a factory smart contract. The template contract can be made a factory contract by adding a public method to spawn new child contracts with given properties. Whenever a new token type is to be created, issue a transaction with relevant properties to the spawn function in the factory contract. In turn, the factory contract instantiates the template with the given properties to spawn the token contract. Subsequently, transactions could be issued to the token contract to transact using the newly created tokens. Alternatively, if the new token contract is developed by extending the template, the template can be kept off-chain. The new token contract can then be directly deployed to the blockchain, which internally carries template contract code (i.e., parent contract).

Benefits

  • Productivity and security – A token template can support developing a particular type of token by creating an instance of the token template. A well-defined and tested template simplifies blockchain application development, reduces time, and enhances security and reliability.
  • Flexibility – A blockchain application might support different types of tokens with different rules. The use of token templates in the design can improve modularity.
  • Interoperability and liquidity – As the token template can be designed based on a token standard, it offers high interoperability and liquidity through token transfer and swap.

Drawbacks

  • Cost – If a public blockchain is used, an extra cost is required to deploy the token template contract on the blockchain and call its token contract instance creation function. Further, the generalisation of the template increases the cost of computational and storage complexity of the smart contract, increasing the transaction fees needed to deploy and manipulate it.
  • Productivity – It takes time to design, develop, and test a token template carefully. However, this is a one time process.

Related patterns

  • The token registry pattern can be used to maintain the ownership information of tokens generated using the token contract.
  • Terms and conditions that a generated token needs to satisfy can be specified by linking the token to policies specified using the policy contract pattern.
  • Once redeemed, a spent token can be destroyed or marked as unusable using the burned token pattern.
  • The factory contract pattern could be used to spawn contracts based on the token template.

Known uses

  • ERC-20 and ERC-721 have emerged as the de-facto standards for implementing fungible and non-fungible tokens in Ethereum and sister blockchains, respectively, by instantiating reference implementations like OpenZeppeline.
  • Lorikeet is a model-driven engineering tool that provides off-chain templates for the developers to customise fungible and non-fungible asset data contracts based on ERC-20 and ERC-721 interface definitions.
  • Token Mint is a tool to create fixed supply and mintable ERC-20 and ERC-777 tokens and crowd sale contracts without coding.
  • Token Launcher creates mintable ERC-20 tokens by setting the desired specification.