With Foundry

Prerequisites

  • ETH on Ethernity:

  • Rust:

  • Foundry:

1. Set up the project

1.1 Initialize a new Foundry project:

Forge is the command-line interface (CLI) tool for Foundry, allowing developers to execute operations directly from the terminal.

Open up a terminal and run this command:

forge init my-project

1.2. Install the OpenZeppelin contracts library inside your project, which provides a tested and community-audited implementation of ERC20:

forge install OpenZeppelin/openzeppelin-contracts

2. Write the ERC20 token contract

2.1. In /src of your project directory, create a text file named MyERC20.sol and add:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";

contract MyERC20 is ERC20 {
    constructor() ERC20("MyToken", "MTK") {}
}

This is a simple ERC20 token named "MyToken" with the symbol "MTK". You can name it any way you want and also write any other contracts. Just remember that if your file name is called differently, the commands to deploy will be slightly different.

3. Build the contract

3.1. Use Foundry to compile your smart contract running the following command:

forge build

4. Deploy the ERC20 token contract

4.1. To deploy your contract you will need to run the following command and replace <YOUR_PRIVATE_KEY> with your credentials:

Never share or expose your private key publicly. This will grant complete control to whoever has it. Please store it safely.

forge create --rpc-url https://testnet.ethernitychain.io --private-key <YOUR_PRIVATE_KEY> src/MyERC20.sol:MyERC20

You can add the --verify flag and use blockscout to verify your contract while deploying.

The following is the command to deploy and verify the contract. If you already deployed your contract but still want to verify it, don't panic! Next step will walk you through that case.

forge create --rpc-url https://testnet.ethernitychain.io --private-key <YOUR_PRIVATE_KEY> src/MyERC20.sol:MyERC20 --verify --verifier blockscout --verifier-url https://sepolia.explorer.mode.network/api\?

Copy the β€œDeployed to” value and store it somewhere to use later. This is the address of your contract.

5. Verify the contract

5.1. For already deployed contracts you can use the verify-contract command:

forge verify-contract <CONTRACT_ADDRESS> src/MyERC20.sol:MyERC20  --verifier blockscout --verifier-url https://sepolia.explorer.mode.network/api\?

6. Exploring and interacting with your deployed contract

Use ERNScan to view your contract's details. Paste your contract's address you saved from the deployment output into blockscout's search bar.

Last updated