Creating a Non-Collateralized Stablecoin – Alisya Kainth
Using pure math to back a stable currency.
Stablecoins are one of the most important components of a cryptocurrency environment. They are essential for stability and accessibility both for fiat currencies and exchange for various cryptocurrencies. This means that people don’t have to risk their investments in the volatile 10% fluctuations of the market with coins such as Bitcoin if they don’t want to.
There are three main types of stablecoins:
- Centralized stablecoins, which are backed by a fiat currency like USD or metals like gold.
- Crypto-collateralized stablecoins, which are backed by cryptocurrencies like Bitcoin or Ethereum.
- Non-collateralized stablecoins, which are backed by basically just math
A common example of a stablecoin is Tether, which is pegged to USD, or in other words, is a centralized stablecoin. Of course the only instability introduced with stablecoins is if the value of the pegged currency drops in value (e.g USD decreasing in value itself).
As I said before we are going to be focusing on non-collateralized stablecoins and finding out how to create our own. Non-collateralized stablecoins rely on smart contracts that use a concept called seniorage shares to maintain a stability of 1.
To achieve this the smart contract has two main responsibilities other than those of basic coin functionality. The first is to burn coins when demand decreases and the value of the coin drops below 1. The second is to issue new coins when demand increases and the value of the coin rises above 1
by maintaining the following rules, the smart contract makes sure that the value of the coin is always 1.
For the purposes of learning, we are going to test and deploy this on the Ethereum blockchain so technically it’s an ERC token, just because it is easy for beginners to understand, but the main part is understanding the smart contract. The first thing we need to do to create our own is to set up the development environment that means we need to download node.js, truffle, ganache, and a good IDE, I suggest solidity. Or, for understanding smart contract purposes, you could simply open a remix IDE by going to ethereum.remix.org.
Next we are going to start implementing basic functionality to our coin
which means we need a couple of standard functions including BalanceOf, Transfer, Approve, etc. These standards are not new, I have defined them in my other article, Creating ERC721 Tokens, which you can read. Or, you can look at the code in my Github for this project to see how they work since they are simple to understand.
Now we can start implementing seniorage shares functionality. For this we need a couple of things, a centralized account that acts as a bank
this address hold all issued coins and is the only account with the right to burn or issue coins — this address is the address of the smart contract.
Next we need to define two functions, you can call them more suitable names in the future but ill call them mint and burn for the sake of this article.
The mint function issues new coins and looks something like this:
The burn function destroys coins and looks something like this:
After this, if you are using truffle tools, you can migrate them to a local blockchain and test the contract to see if it really works which is the most important part. For the test, you can copy this script and execute “truffle test” and what should happen is all three check marks should show.
For a overview of how to migrate a smart contract and deploy it to the Ethereum blockchain (if you want), you can read my other article on creating ERC721 tokens. Stablecoins, however, are not meant to operate on other blockchains since in order to be stable, must be in control of the overarching blockchain (unless backed by other cryptocurrencies).
This test first begins by buying a large number of coins then calls the value of the contract to see if it issued more coins for supply, and the check mark here indicates it did. The next check mark indicates that the contract was able to burn coins when being sold too. The final one checks to make sure a transfer of coins to a different address also does not increase or decrease the value.
After this, you have a non-collateralized stablecoin! You can now buy, trade and sell this coin at will if you ever put it on a real blockchain.
All the code for this project can be found in my Github.
https://github.com/alisyakainth/stablecoin
Published at Tue, 31 Dec 2019 03:40:12 +0000
{flickr|100|campaign}
