How to Make Your Own Crypto Tokens

Just when everyone was exhausted about missing the boat buying (and then quickly selling) some latest trend-coin, I am about to show you how can actually make your own tokens -- so instead of riding the boat with everyone else, you can be your own captain. Ahoy!

On a more serious note, blockchain technology has wide applications outside just finance and trading. So with this blog post, I want to focus on the incredible flexibility and potential of the underlying technology. In the first part of the article, we will review smart contracts, followed by a technical deep dive to implement two contracts and go through all the steps required.

DeFi, or "decentralized finance" is a broad category of applications powered by blockchain technology. Unlike cryptocurrencies which are just a store of value, DeFi applications can tackle more complex financial use cases, such as lending or settlement. Built on an immutable blockchain, DeFi applications are highly secure, tamper-proof, and distributed. Most DeFi applications are built on the Etherum platform, specifically using smart contracts.

For example, Uniswap, an automated liquidity protocol for trading, is built on smart contracts. It challenges traditional exchanges with an algorithmic pricing and liquidity mechanism that does not rely on order books. As of 5/8/21 Uniswap daily trading volume has reached $2b (CoinBase traded $5.7b the same day). This vividly shows that smart contracts are a mature and performant platform for advanced applications. Uniswap code is publicly available.

This guide will show how to make your own smart contracts and tokens on the Etherium blockchain. By the end of this tutorial you will:

  • Understand how smart contracts work, and how they relate to tokens.
  • Create your own Etherium token.
  • Get familiar with tools and technologies around Etherium smart contracts.

We will start by creating a simple digital contract, and then move to create an actual token. An Etherium token is a type of contract that adheres to the ERC20 Standard.

Smart contracts are programs that define the behavior of digital assets. The actual smart contract and its state are immutable after it is created. All transactions that change state are stored on the blockchain, making a future based on digital currency and assets extremely attractive to both governments and businesses.

How does a blockchain ensure that a contract continues to function and does not misbehave? Well, it executes it in a sandbox called the Ethereum Virtual Machine. By controlling where and how a contract is executed,  the protocol always maintains integrity. The mathematics of the underlying protocol is fascinating.

A Simple Token

Let's go over a very basic smart contract.

It makes a simple promise: give me some data, and I will give it back to you. This simple functionality demonstrates the power of the blockchain.

If you are familiar with programming, you know that variables hold their value only for the duration that a program is executing. Well in the blockchain world, they maintain their value through the life of a contract, and the network is storing it. In this example, value is a state variable. Variables declared within functions are Local and their value disappears after execution.

This language is called Solidity, it will look familiar to C/JS developers in its syntax. It is better to not draw parallels with traditional programs as you have just seen – smart contracts are designed for a very unique purpose.

Technical Dive: Interacting with the Contract

There are multiple Ethereum networks, with Mainnet being the primary public production blockchain – it's where actual-value transactions occur. When people interact with ETH coins, they're talking about Mainnet. Because of its high utilization, it's not practical for us to use it for our tinkering, and we are going to make our own. Ganache makes it super easy to spin up your own local network with as much ETH as you want. It will function identically to Mainnet and we can easily deploy our code thereafter we are done.

Smart contracts run inside the Etherium Virtual Machine (EVM) and we will be using a framework called Truffle to compile Solidity to the EVM bytecode that's then executed on the Ethereum blockchain.

Another tool we'll be using is the MetaMask wallet. It makes it easy to connect to multiple Ethereum networks. It's what we'll be using to debug and interact with the network in Ganache.

You can download my sample project, or create your own. Here are the basic commands to get you going:

truffle init creates an empty project

truffle unbox metacoin populates a project with boilerplate material

truffle install installs dependencies

truffle compile compiles the project

truffle migrate migrates the coin our network

truffle console console interface to our contracts

Once your project is set up, you will need to point Ganache to the project folder.

Truffle provides a console tool to interact with your deployed contact. You can invoke public methods and debug your contract. From the shell run:

truffle(ganache)> let instance = await SampleContract.deployed()
truffle(ganache)> instance.setValue('test')
truffle(ganache)> instance.getValue()
'test'

The state of the contract reflects in Ganache as well. Notice changing the state does require gas, as the change is now committed to the blockchain.

Implementing an ERC-20 Token

An Etherium token is just a standardized contract. It defines a set of functions that must be implemented to integrate with wallets and marketplaces. As we will now see, a contract for the token is not very complicated.

The protocol only prescribes the basic things necessary to transact and exchange tokens. This is all we need to make this work on the Ethereum blockchain!

Let's make our own, we'll call it called SampleToken with a symbol ST.

The implementation is quite straightforward – account balance is held in a key-value store (_balances), which is a bit like a distributed hash table. For obvious reasons, it is not possible to obtain a list of all keys in a mapping nor a list of its values.

For testing and debugging, we included a public mint() function. It can mint new tokens to any address.

The deployment scripts configure properties such as the name and symbol.

Testing

Now to compile and deploy. To view our token in action, mint some SampleTokens to an account we see in Ganache.

Use the private key from Ganache to Import the account. You will see your account balance in ETH. You have to manually add our new token, and you will then see the balance.

You can now transfer money between the Ganache accounts, with the smart contract we just built performing the transactions.

If you wanted to deploy your new token to a different network, you need to edit truffle-config.js to point to another location. You can deploy your new Token to Etherium testnet or even to Mainnet.

Conclusion

In many ways, traditional tokens are quite a narrow application for smart contracts. The capability afforded by distributed, automated execution and immutability are far beyond a mere store of value, and it's just beginning to show its true potential to disrupt. I hope this tutorial has inspired you and given you the fundamentals to explore this exciting new space.


Note: The OpenZeppelin library implements this ERC-20 standard and provides out-of-the-box functionality. If you are building a token, I suggest you use OpenZeppelin. For this example, however, we're going to implement the token ourselves.

References