Neosify - Buy, Stake & Earn Crypto
Neosify - Buy, Stake & Earn Crypto
Neosify - Buy, Stake & Earn Crypto
Image by WorldSpectrum from Pixabay

What Are Smart Contracts Used For?

By alexroan | Blockchain Developer | 8 May 2020


How Smart Contracts are used to create tokens

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.

pragma solidity ^0.6.0;

contract MyToken {

    mapping (address => uint256) private _balances;

    uint256 private _totalSupply;

    string private _name;
    string private _symbol;

    constructor (string memory name, string memory symbol, uint256 totalSupply) public {
        _name = name;
        _symbol = symbol;
        _totalSupply = totalSupply;

        _balances[msg.sender] = _totalSupply;
    }

    function balanceOf(address account) public view returns (uint256) {
        return _balances[account];
    }

    function transfer(address recipient, uint256 amount) public returns (bool) {
        require(msg.sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");
        require(amount >= _balances[msg.sender], "ERC20: not enough tokens");
        
        _balances[msg.sender] = _balances[msg.sender] - amount;
        _balances[recipient] = _balances[recipient] + amount;
        return true;
    }
}
  • 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.

pragma solidity ^0.6.0;
contract MyWallet {

    address payable private owner;

    constructor() public {
        owner = msg.sender;
    }

    receive() external payable {
    }

    function sendEther(address payable _to, uint _amount) public {
        require(msg.sender == owner, "Must own this wallet to send Ether from it");
        require(address(this).balance >= _amount, "Cannot send more than this wallet holds");
        _to.transfer(_amount);
    }
}

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

pragma solidity ^0.6.0;

import "./contracts/MyToken.sol";

contract MyWallet {

    address payable private owner;
    address private tokenAddress

    constructor(address token) public {
        owner = msg.sender;
        tokenAddress = token;
    }

    receive() external payable {
    }

    function sendEther(address payable _to, uint _amount) public {
        require(msg.sender == owner, "Must own this wallet to send Ether from it");
        require(address(this).balance >= _amount, "Cannot send more than this wallet holds");
        _to.transfer(_amount);
    }
    
    function sendToken(address _to, uint _amount) public {
        MyToken(tokenAddress).transfer(_to, _amount);
    }
}
  • 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.


Learn More

If you’re interested in Blockchain Development, I write tutorials, walkthroughs, hints, and tips on how to get started and build a portfolio. Check out this evolving list of Blockchain Development Resources.

If you enjoyed this post and want to learn more about Blockchain Development or the Blockchain Space in general, I highly recommend signing up to the Blockgeeks platform. They have courses on a wide range of topics in the industry, from Coding to Marketing to Trading. It has proven to be an invaluable tool for my development in the Blockchain space.

How do you rate this article?

48


alexroan
alexroan

Blockchain Developer


Blockchain Developer
Blockchain Developer

Tutorials, walkthrough, hints and tips on Blockchain Development for all levels of expertise.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.