Book Image

Security Tokens and Stablecoins Quick Start Guide

By : Weimin Sun, Xun (Brian) Wu, Angela Kwok
Book Image

Security Tokens and Stablecoins Quick Start Guide

By: Weimin Sun, Xun (Brian) Wu, Angela Kwok

Overview of this book

The failure of initial coin offerings (ICOs) is no accident, as most ICOs do not link to a real asset and are not regulated. Realizing the shortcomings of ICOs, the blockchain community and potential investors embraced security token offerings (STOs) and stablecoins enthusiastically. In this book, we start with an overview of the blockchain technology along with its basic concepts. We introduce the concept behind STO, and cover the basic requirements for launching a STO and the relevant regulations governing its issuance. We discuss U.S. securities laws development in launching security digital tokens using blockchain technology and show some real use cases. We also explore the process of STO launches and legal considerations. We introduce popular security tokens in the current blockchain space and talk about how to develop a security token DApp, including smart contract development for ERC1404 tokens. Later, you'll learn to build frontend side functionalities to interact with smart contracts. Finally, we discuss stablecoin technical design functionalities for issuing and operating STO tokens by interacting with Ethereum smart contracts. By the end of this book, you will have learned more about STOs and gained a detailed knowledge of building relevant applications—all with the help of practical examples.
Table of Contents (9 chapters)

Ethereum basics

Ethereum was developed on top of the bitcoin blockchain and shares many key features, such as the distributed ledger and PoW, and so on. However, Ethereum introduces new and critical ingredients. In this section, we will cover them, along with other useful facts.

Ethereum cryptocurrency and tokens

Ether is Ethereum's native cryptocurrency. In other words, ether to Ethereum is like BTC to bitcoin. Due to the forking for handling a hacking event in 2016, two competing Ethereum coins were generated, and both are currently traded at cryptocurrency markets. Their symbols are Ethereum (ETH) and Ethereum Classic (ETC). ETH is priced a lot higher than ETC.

Unlike BTC (that has only one denomination but can be divided into fractions), ether has many denominations. An ether is the biggest unit. The smallest unit is a Wei, named after a digital money pioneer, Wei Dai. Wei invented B-money. Other units include a Gwei, microether, and milliether. They are known by other names as well. For example, a milliether is also called Finney, named after another digital money pioneer, Harold Thomas Finney II, who in 2004 implemented the world's first cryptocurrency, reusable proofs of work (RPOW) before bitcoin. The following table lists conversion rates between ether and other units:

In addition to the native cryptocurrency, Ethereum allows users to issue their tokens. The issuance of a customized token needs to follow predefined technical standards. One well-known standard is the ERC-20 token. ERC refers to Ethereum Request for Comment, and 20 is the number assigned to the request. ERC-20 standard defines a list of rules for issuing ERC-20 tokens. By doing so, it allows for interaction and conversion among Ethereum tokens, and also with Ether. There are many other standards, for example, ERC-223, ERC-721, and so on.

Smart contract

In 1994, Nick Szabo first used the term smart contract. Szabo is a computer scientist and the inventor of Bit Gold. In his blog, Nick Szabo describes it as similar to the vending machine, which is the granddaddy of all smart contracts. A vending machine is built with hard-coded rules that define what actions are to be executed when certain conditions are fulfilled. For example:

  • If Susan inserts a dollar bill, then she will receive a bag of pretzels
  • If Tom inserts a five-dollar bill, then Tom will receive a bag of pretzels and change of four dollars

In other words, rules are defined and enforced by a vending machine physically. Similarly, a smart contract contains rules in program code that are triggered and run on the Ethereum platform when certain conditions are met.

Some important facts about smart contracts are summarized as follows:

  • A smart contract is immutable.
  • A smart contract is permanent.
  • A smart contract is timestamped.
  • A smart contract is globally available.
  • A smart contract is a digitized legal document.
  • A smart contract defines the protocol for facilitating, verifying, or enforcing an agreement among trading parties.
  • Smart contracts allow for the execution of transactions without an intermediary. The transactions are auditable and irreversible.
  • Smart contracts are applicable in many cases.
  • For deploying and running a smart contract, you need to sign it digitally.
  • Smart contract code is visible to everyone. This makes smart contracts vulnerable, as a hacker can tap the flaws in the code and initiate vicious attacks.

Ethereum virtual machine

An Ethereum's smart contract can be developed in one of four languages: Solidity (inspired by JavaScript), Serpent (inspired by Python, and no longer used), LLL (inspired by Lisp), and Mutan (inspired by Go, and no longer used). Since all of them are high-level programming languages, smart contracts need to be compiled into a low-level, machine-runnable language. Ethereum uses a VM approach, similar to the concept of Java Virtual Machine (JVM), to meet this need. The Ethereum erosion of VM is called EVM. Smart contract codes are converted to EVM-runnable bytecodes called opcode. The opcode is then deployed to the Ethereum blockchain for execution. Furthermore, currently, a research-oriented language is under development, called Viper—a strongly-typed Python-based language.

Ethereum gas

An Ethereum transaction can call a smart contract, which can, in turn, call another smart contract and then another, and so on. Thus, an improperly written smart contract may lead to circular calls and result in infinite loops. Stopping a smart contract infinite loop is almost impossible, since thousands of nodes worldwide run the same looping code. To stop the infinite loop, all running nodes need to be shut down within a short time window. Even if one node fails to comply, the infinite loop is still alive. When other nodes are back to the network, the running infinite loop is brought back to these nodes as well. It is a logistical nightmare to coordinate and shut down all nodes worldwide at approximately the same time.

To resolve this issue, the concept of gas was introduced. A vehicle relies on an engine, which depends on gas for energy. If an engine runs out of gas, the vehicle stops. When a transaction is submitted, a requester is required to provide the max gas amount. Each execution step of a smart contract uses a certain amount of gas. An infinite-looping smart contract will eventually lead to the maximum gas amount being used up, and a node will no longer execute the contract. Another advantage of using gas is that it makes hacking prohibitively expensive and, therefore, deters hacking activities.

Gas is a metering unit for measuring consumption just like a kilowatt is the unit for measuring electricity usage. Suppose in a month a family uses 210 KW. Before sending a bill to the family, the utility company first converts 210 KW into US dollars based on a predefined conversion rate. Suppose a unit of KW costs $0.20 USD; the total charge for the month is 0.2 × 210 = $42 USD.

Similarly, gas usage is converted into ether for being charged to a requestor. Ethereum allows a requester to specify the conversion rate when the transaction is submitted. A validator (Ethereum's equivalent to the miner for bitcoin) has the option of giving a preference to transactions with higher rates. If a requester does not specify a rate, EVM uses a default rate, which varies. For example, in 2016, the rate for 1 gas was 0.00001 ETH. In 2018, 1 gas was 0.00000002 ETH.

Account

Unlike bitcoin where the term address is used, Ethereum uses the term account. However, Ethereum accounts can have addresses. That is, for bitcoin, the concepts of account and address are combined into one, while Ethereum separates them. Ethereum supports two types of accounts: externally owned accounts (owned by users who keep private keys of the accounts) and contract accounts.

The key facts about externally controlled accounts are listed as follows:

  • They maintain ether balances
  • They can initiate transactions for either transferring ether coins or triggering smart contracts
  • They are controlled by users via private keys
  • They have no associated smart contract code

Facts about contract accounts are as follows:

  • They keep ether balances
  • They have associated smart contract code
  • Smart contract code execution is triggered by transactions or calls that are received from other contracts

For both types of accounts, they consist of four components:

  • Nonce: For an externally owned account, it refers to the number of transactions sent from the account; for a contract account, it is the number of contracts associated with the account
  • Balance: It is the number of Wei owned by this account
  • StorageRoot: A 256-bit hash of contents of an account
  • CodeHash: The hash of the code of this account in EVM—this is the code that gets executed when the code is called

When ether is transferred from contract accounts to an externally owned account, there is a fee, for example, 21,000 unit of gas. When ether is sent from an externally owned account to a contract account, the fee is usually higher, which depends on the smart contract code and the data being sent along with the transaction.

Ethereum addresses of accounts have the following format:

  • They start with the prefix 0x, a common identifier for hexadecimal, followed by the number string to be constructed by following these steps:
    1. First, take the Keccak-256 hash (big-endian) of the elliptic curve digital signature algorithm (ECDSA) public key
    2. Then, take the last 20 bytes of the hash

Since, in hexadecimal, two digits are stored in one byte, a 20-bytes address is represented with 40 hexadecimal digits. The following is an example of an Ethereum address: 0xe99356bde974bbe08721d77712168fa074279267.

With a browsing tool, you can retrieve Ethereum account balances. For example, you can go to https://www.etherchain.org to obtain the top account balances in ether.

Oracle

Ethereum smart contracts are executed on nodes worldwide. To yield the same outcomes, nodes have to take the same set of inputs. This is called determinism. Ethereum relies on the determinism property validating smart contract outputs. That is, validating nodes have to yield the same results while running the same code. In this sense, the determinism property plays a key role in enabling nodes reaching a consensus.

Maintaining determinism can be a challenging task. On the one side, Ethereum is a general-purpose platform. Its smart contracts require data or inputs from external sources such as the internet. Without access to these sources of information, use cases for smart contracts will be restrictive. On the other side, even with a tiny time difference, validating nodes may retrieve different information from an external source. With different inputs, nodes will end up with different outputs.

Consequently, the determinism property does not hold. For avoiding the issue, smart contracts are not permitted to call an internet URL or pull data from an external source directly. To resolve the paradox, Ethereum relies on Oracle.

A definition of Oracle is as follows:

"A shrine in which a deity reveals hidden knowledge or the divine purpose through such a person."
– Merriam-Webster

In blockchain, oracle refers to the third-party or decentralized data feed services that provide external data. Oracle provides interfaces from the real world to the digital world. Oracle data is not part of blockchain and is available off-chain.

There are different types of oracles. Two of them are software oracles and hardware oracles:

  • Software oracles: This normally refers to easily accessible online information such as stock index prices, FX rates, economic news, weather forecasts, and so on. Software oracles are useful since they provide smart contracts with a wide range of information and up-to-date data.
  • Hardware oracles: This normally refers to scanned information such as UPS delivery scanning, registered mail scanning, supplier goods delivery scanning, and so on. This feed can be useful to activate a smart contract acting on an event's occurrence.

Off-the-chain data

There are multiple scenarios where data cannot be stored on a chain:

  • State variables: Data stored on an Ethereum blockchain is immutable. However, contents of state variables vary as account balances change. A solution is to save them off-the-chain.
  • Oracle: We have just talked about that.
  • Digitized assets: Commonly digitized assets require a large dataset to describe/define them. Given a limited size of blocks, it is not feasible for hosting complete asset information on a chain.
  • Trimmed blocks: For optimization, Ethereum full nodes need to keep a portion of the distributed ledger, that is, to trim a ledger. The trimmed blocks are saved off-the-chain at a centralized location for supporting future inquiries.

PoS

Proof of Stake (PoS) is an algorithm for choosing a validator to build the next block. Per the PoS algorithm, when a validator owns more coins, the validator has a higher chance to be chosen. Compared to PoW, PoS is much more energy efficient and quicker.

A pure PoS will lead to the richest validator being selected frequently, causing a supernode problem, referring to a node validating the majority of the blocks being added to the chain. This obviously will not work. Additional randomness is required to give other validators better chances. Several randomization methods are available:

  • Randomized block selection: Uses a formula to look for the lowest hash value in combination with the size of the stake for selecting a validator.
  • Coin age-based selection: Coins owned long enough, say 30 days, are eligible to compete for the next block. A validator with older and larger sets of coins have a better chance of being granted the role.
  • Delegated PoS: This implementation chooses a limited number of nodes to propose and validate blocks being added to the blockchain.
  • Randomized PoS: Each node is selected randomly using a verifiable random beacon for building the new block.

Ethereum is working on replacing PoW with PoS in future releases.

Performance considerations

Ethereum is inherently slow. The average waiting time for a validator building a block is 17 seconds. It usually requires 12 blocks in depth before a transaction (containing the first block) is confirmed. This is 12 × 17 = 204 seconds or 3.4 minutes of waiting time for a transaction to be confirmed. The 12-blocks-in-depth rule is necessary. When a block is newly added to the blockchain by a validator to its ledger copy, there could be a competing path worked on by other validators. The validator may lose the competition for building the longest blockchain. Per blockchain protocol, the validator has to drop its own block being worked on and add the winning block to its ledger copy. The 12-blocks-in-depth rule assures that a transaction does not end up in a block to be dropped later.

Throughput is a measure of how many units of information a system can process in a given time window. For measuring the performance of a transaction platform, the throughput is expressed in terms of throughput per second (TPS). To calculate Ethereum TPS, we take the approximate number of transactions in a block (using 2,000). Then, we divide it by the waiting time in seconds for a transaction to be confirmed, 204 seconds. So, Ethereum TPS is approximately 9.8, that is, almost 10 transactions per second. By applying the same approach, we can estimate the TPS for bitcoin, which is about 0.5 transaction per second. On the other hand, Visa has a TPS of 2,000 with a peak TPS of 40,000. A high performance database such as VoltDB can handle over a million insertions a second. A stock exchange can match thousands of trades a second. It clearly shows a gap that needs to be closed by the blockchain community.

Ethereum is working on multiple solutions to increase TPS. PoS is worked on as a replacement of the computationally inefficient PoW algorithm. PoS is not fully implemented and upgraded on mainnet due to concerns regarding the emergence of a set of supernodes (which receive an outsized role in building the new blocks). Casper is the Ethereum community's attempt to transiting out of PoW and into PoS. Per Casper protocol, validators set aside a portion of their ether as a stake. When a validator identifies a candidate block, ether is bet on that block by the validator. If the block is indeed added to the chain, the validator is rewarded based on the size of its bet. Validators acting maliciously will be penalized by having their stakes removed.

Led by Vitalik, the Ethereum Foundation is also working on the sharding approach, which is aiming at increasing TPS by 80 times. Sharding splits up the state of the network into multiple shards, where each shard has its transaction history and portion of the network's state.

Another idea to increase TPS is Plasma. Plasma is a technique for conducting off-the-chain transactions while relying on the underlying Ethereum blockchain to provide its security. Therefore, Plasma belongs to the group of off-chain technologies. Truebit is another example of this.