Skip to content

Deploy ERC-20 to Asset Hub

Introduction

ERC-20 tokens are fungible tokens commonly used for creating cryptocurrencies, governance tokens, and staking mechanisms. Asset Hub enables easy token deployment with EVM-compatible smart contracts via PolkaVM.

This tutorial covers deploying an ERC-20 contract on the Westend TestNet using Polkadot Remix IDE, a web-based development tool. OpenZeppelin's ERC-20 contracts are used for security and compliance.

Prerequisites

Before starting, make sure you have:

Create the ERC-20 Contract

To create the ERC-20 contract, you can follow the steps below:

  1. Navigate to the Polkadot Remix IDE
  2. Click in the Create new file button under the contracts folder, and name your contract as MyToken.sol

  3. Now, paste the following ERC-20 contract code into the editor

    MyToken.sol
    // SPDX-License-Identifier: MIT
    // Compatible with OpenZeppelin Contracts ^5.0.0
    pragma solidity ^0.8.22;
    
    import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
    import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
    
    contract MyToken is ERC20, Ownable {
        constructor(address initialOwner)
            ERC20("MyToken", "MTK")
            Ownable(initialOwner)
        {}
    
        function mint(address to, uint256 amount) public onlyOwner {
            _mint(to, amount);
        }
    }
    

    The key components of the code above are:

    • Contract imports

      • ERC20.sol - the base contract for fungible tokens, implementing core functionality like transfers, approvals, and balance tracking
      • Ownable.sol - provides basic authorization control, ensuring only the contract owner can mint new tokens
    • Constructor parameters

      • initialOwner - sets the address that will have administrative rights over the contract
      • "MyToken" - the full name of your token
      • "MTK" - the symbol representing your token in wallets and exchanges
    • Key functions

      • mint(address to, uint256 amount) - allows the contract owner to create new tokens for any address. The amount should include 18 decimals (e.g., 1 token = 1000000000000000000)
      • Inherited Standard ERC-20 functions:
        • transfer(address recipient, uint256 amount) - sends a specified amount of tokens to another address
        • approve(address spender, uint256 amount) - grants permission for another address to spend a specific number of tokens on behalf of the token owner
        • transferFrom(address sender, address recipient, uint256 amount) - transfers tokens from one address to another, if previously approved
        • balanceOf(address account) - returns the token balance of a specific address
        • allowance(address owner, address spender) - checks how many tokens an address is allowed to spend on behalf of another address

    Tip

    Use the OpenZeppelin Contracts Wizard to quickly generate customized smart contracts. Simply configure your contract, copy the generated code, and paste it into Polkadot Remix IDE for deployment. Below is an example of an ERC-20 token contract created with it:

    Screenshot of the OpenZeppelin Contracts Wizard showing an ERC-20 contract configuration.

Compile the Contract

The compilation transforms your Solidity source code into bytecode that can be deployed on the blockchain. During this process, the compiler checks your contract for syntax errors, ensures type safety, and generates the machine-readable instructions needed for blockchain execution. To compile your contract, follow the instructions below:

  1. Select the Solidity Compiler plugin from the left panel

  2. Click the Compile MyToken.sol button

  3. If the compilation succeeded, you'll see a green checkmark indicating success in the Solidity Compiler icon

Deploy the Contract

Deployment is the process of publishing your compiled smart contract to the blockchain, making it permanently available for interaction. During deployment, you'll create a new instance of your contract on the blockchain, which involves:

  1. Select the Deploy & Run Transactions plugin from the left panel

  2. Configure the deployment settings

    1. From the ENVIRONMENT dropdown, select Westend Testnet - MetaMask
    2. From the ACCOUNT dropdown, select the account you want to use for the deploy

  3. Configure the contract parameters

    1. Enter the address that will own the deployed token contract
    2. Click the Deploy button to initiate the deployment

  4. MetaMask will pop up - review the transaction details. Click Confirm to deploy your contract

    If the deployment process succeeded, you will see the transaction details in the terminal, including the contract address and deployment transaction hash:

Interact with Your ERC-20 Contract

Once deployed, you can interact with your contract through Remix:

  1. Find your contract under Deployed/Unpinned Contracts, and click it to expand the available methods

  2. To mint new tokens:

    1. Click in the contract to expand its associated methods
    2. Expand the mint function
    3. Enter:
      • The recipient address
      • The amount (remember to add 18 zeros for 1 whole token)
    4. Click Transact

  3. Confirm the transaction in MetaMask

    If the transaction succeeds, you will see the following output in the terminal:

Other common functions you can use:

  • balanceOf(address) - check token balance of any address
  • transfer(address to, uint256 amount) - send tokens to another address
  • approve(address spender, uint256 amount) - allow another address to spend your tokens

Feel free to explore and interact with the contract's other functions using the same approach - selecting the method, providing any required parameters, and confirming the transaction through MetaMask when needed.

Last update: March 6, 2025
| Created: March 6, 2025