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:
- MetaMask installed and connected to Westend Asset Hub
- A funded account with some WND tokens (you can get them from the Westend Faucet)
- Basic understanding of Solidity and fungible tokens
Create the ERC-20 Contract¶
To create the ERC-20 contract, you can follow the steps below:
- Navigate to the Polkadot Remix IDE
-
Click in the Create new file button under the contracts folder, and name your contract as
MyToken.sol
-
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 trackingOwnable.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 addressapprove(address spender, uint256 amount)
- grants permission for another address to spend a specific number of tokens on behalf of the token ownertransferFrom(address sender, address recipient, uint256 amount)
- transfers tokens from one address to another, if previously approvedbalanceOf(address account)
- returns the token balance of a specific addressallowance(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:
-
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:
-
Select the Solidity Compiler plugin from the left panel
-
Click the Compile MyToken.sol button
-
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:
-
Select the Deploy & Run Transactions plugin from the left panel
-
Configure the deployment settings
- From the ENVIRONMENT dropdown, select Westend Testnet - MetaMask
- From the ACCOUNT dropdown, select the account you want to use for the deploy
-
Configure the contract parameters
- Enter the address that will own the deployed token contract
- Click the Deploy button to initiate the deployment
-
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:
-
Find your contract under Deployed/Unpinned Contracts, and click it to expand the available methods
-
To mint new tokens:
- Click in the contract to expand its associated methods
- Expand the mint function
- Enter:
- The recipient address
- The amount (remember to add 18 zeros for 1 whole token)
- Click Transact
-
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 addresstransfer(address to, uint256 amount)
- send tokens to another addressapprove(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.
| Created: March 6, 2025