avatarAlex Roan

Summary

Smart Contracts are used to create tokens on the Ethereum blockchain, with tokens being defined and tracked within these contracts.

Abstract

Smart Contracts, a key feature of the Ethereum blockchain, can hold, send, and receive ETH. One of their major use cases is the creation of tokens, which are defined within these contracts. Tokens are tracked by their respective Smart Contracts, not the underlying Blockchain itself. This distinction is crucial when querying token ownership. The article provides sample code for a Token Smart Contract, which includes functions for transferring tokens and querying token ownership. Additionally, the article discusses the ERC20 standard, a coding standard that tokens must adhere to for compatibility with ERC20-compatible wallets.

Bullet points

  • Smart Contracts can hold, send, and receive ETH.
  • Tokens are a major use case for Smart Contracts.
  • Tokens are defined and tracked within Smart Contracts, not the underlying Blockchain.
  • Sample code for a Token Smart Contract is provided, including functions for transferring tokens and querying token ownership.
  • The ERC20 standard is a coding standard that tokens must adhere to for compatibility with ERC20-compatible wallets.
  • The article provides an analogy to help understand the relationship between the Ethereum blockchain, ETH, and ERC20 tokens.

What Are Smart Contracts Used For?

How Smart Contracts are used to create tokens

Image by WorldSpectrum from Pixabay

Prerequisite: Work through Getting Started With Your First Smart Contract, explaining the basic concepts behind them, and how to write a simple ETH wallet contract.

Now that we know that Smart Contracts can hold, send, and receive ETH, what else can they do?

What Is the Point in Smart Contracts?

The first major use case for Smart Contracts came to prominence in 2017 in the form of tokens. Many of the top 100 coins in play at that time were, in fact, tokens on the Ethereum blockchain. For example, Tron and Binance Coin were both tokens built on Ethereum before they developed their native blockchains.

How Do You Make a Token?

Tokens are defined in Smart Contracts. Once deployed, a Token Smart Contract keeps track of every address that owns tokens, and how many. It also provides functions to let addresses transfer tokens attributed to them to other addresses.

The amount of tokens owned by each person is held in the Smart Contract, not the underlying Blockchain itself, in contrast to ETH. So to find out how many tokens an address has, we need to query the Smart Contract. This is a distinction we need to recognise.

When we’re querying how many ETH an address has, we query the Blockchain.

When we’re querying how many of a Token an address has, we query that Token’s Smart Contract.

Therefore, to know how many ETH and Tokens a wallet has, it needs to know all the addresses of where the Token Smart Contracts are deployed on the blockchain.

Sample Token Code

Let’s assume we want to deploy a token and give it a name, a symbol, determine how many decimal places it has, and the total supply. We want holders to be able to transfer their tokens to other addresses, so we need a transfer function. We also want to be able to query how many tokens any address has.

This is what that contract would look like.

  • The constructor on line 12 takes the parameters and sets up the coin.
  • During construction, line 17 sends all of the tokens to the deployer of the contract (msg.sender of the constructor).
  • Using balanceOf() on line 20 returns how many tokens the given address holds.
  • The transfer() function first checks that both addresses are valid, then checks that the caller of the function has enough tokens to transfer. It then performs the transfer on lines 29 and 30, by reducing the amount owned by the sender, and increasing that of the receiver.

Wallet code

In the previous article, we wrote a Smart Contract that could hold, receive, and transfer ETH. It could not transfer Tokens, because as we mentioned earlier, the address of the Token Smart Contract must be known to do that.

The following code shows the original MyWallet contract from the previous article.

Let’s add the functionality to be able to transfer our new token.

  • Line 8 shows a new state variable called tokenAddress, which we set in the constructor when we create the wallet.
  • We’ve also imported the specification for the MyToken contract on line 3.
  • Line 24 shows a new function called sendToken() which uses the imported MyToken specification to load the tokenAddress and call the transfer() function.

Our wallet can now hold, receive, and send ETH and our Token!

ERC20 Standard

To ensure all Ethereum wallets can hold as many tokens as possible, there is a coding standard that tokens must adhere to. This is called ERC20, and it outlines the minimum requirements for functions and variables that your token must have to be supported by ERC20 compatible wallets.

Our token doesn’t conform to this right now, but take a look through the ERC20 specification and see if you can expand our code to conform to it.

An Analogy

If the Ethereum blockchain was a human body, ETH is the blood. Every transaction costs ETH (in the form of GAS, which is essentially small fractions of ETH). Using that same analogy, ERC20 Tokens are like clothes. They fundamentally rely on the body and blood being there, but in themselves represent something different.

The mechanisms used to transfer ETH from address to address is different from the mechanisms used to transfer ERC20 tokens because the tokens are not an intrinsic part of the body. They are Smart Contracts running on the blockchain.

Further Reading

If you’re interested in blockchain development, I write tutorials, walkthroughs, hints, and tips on how to get started and build a portfolio.

Programming
Blockchain
Cyptocurrency
Ethereum
Smart Contracts
Recommended from ReadMedium