Deploy a Token Contract

Learn how to develop and deploy a token contract on GIWA.

What is ERC20?

ERC20 is the token standard on Ethereum. An ERC20 smart contract manages fungible tokens — meaning every token is identical, with no special privileges or behaviors. In other words, one token owned by someone else has exactly the same value as one token you own. Because of this, ERC20 tokens are commonly used as a medium of exchange, voting, staking, and more.

The ERC20 standard defines 6 required functions and 3 optional functions that a smart contract must implement.

The required functions are as follows:

  • totalSupply: A method that defines the total supply of the token.

  • balanceOf: A method that returns the number of tokens held by a wallet address.

  • transfer: A method that transfers a specific amount of tokens from the caller to another user.

  • transferFrom: Another type of transfer method used for token transfers between users. Using this method, the sender does not need to call it directly. However, the recipient can only receive tokens within the allowance range.

  • approve: A method to pre-authorize a certain amount of tokens for a specific user to spend.

  • allowance: A method to check how many tokens one user has authorized another user to transfer.

In addition to the required functions listed above, there are optional functions that improve a token's usability.

  • name: A method that returns the token's name.

  • symbol: A method that returns the token's symbol.

  • decimals: A method that returns the number of decimal places the token uses. It's used to define the token's smallest unit. For example, if an ERC20 token's decimals value is 6, it means the token can be divided down to six decimal places.

To make a token an ERC20 token, you must implement the ERC20 interface, which requires implementing these six methods.


Developing an ERC20 contract

Across Ethereum and other blockchains, many ERC20-compatible tokens have already been deployed. Some implementations prioritize low gas costs, while others focus on enhanced security.

For robust and secure implementations, many developers rely on libraries like OpenZeppelin or Solady. These libraries provide well-audited, community-verified smart contracts, including ERC20 implementations, making them a trusted choice for token development.

In this example, we’ll use the OpenZeppelin library to build an ERC20 token contract.


Set up the development environment

  1. Install Foundry.

curl -L https://foundry.paradigm.xyz | bash
foundryup

  1. Create a Solidity Project

forge init sample-token
cd sample-token

  1. Install the OpenZepplin library

forge install OpenZeppelin/openzeppelin-contracts

Write contracts and tests

src/SampleToken.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract SampleToken is ERC20 {
    uint256 public constant INITIAL_BALANCE = 1e24;

    constructor() ERC20("Sample", "SMP") {
        _mint(msg.sender, INITIAL_BALANCE);
    }
}

After writing a contract, you can add test code to verify it behaves as intended.

test/SampleToken.t.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {Test} from "forge-std/Test.sol";
import {SampleToken} from "src/SampleToken.sol";

contract SampleTokenTest is Test {
    SampleToken public token;

    function setUp() public {
        token = new SampleToken();
    }

    function test_initialBalance() public view {
        assertEq(token.balanceOf(address(this)), token.INITIAL_BALANCE());
    }

    function test_nameAndSymbol() public view {
        assertEq(token.name(), "Sample");
        assertEq(token.symbol(), "SMP");
    }
}

You can build and test the contract with the following commands.

forge build
forge test

Deploying the token

You can write a deployment script like the following.

script/Deploy.s.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {Script} from "forge-std/Script.sol";
import {SampleToken} from "src/SampleToken.sol";

contract DeploySampleToken is Script {
    function run() public {
        vm.startBroadcast();
        SampleToken token = new SampleToken();
        vm.stopBroadcast();
    }
}

You can learn how to run a deployment script and deploy a smart contract on the GIWA Sepolia Testnet here.

Last updated