Deploy an ERC20 Token with Foundry

Deploy an ERC20 Token with Foundry
Do not index
Do not index
I won’t keep you long.
We’re throwing together a quick Foundry project and deploying an ERC20 token to the locally run Anvil chain. We’ll use the OpenZeppelin ERC20 contract as the base. Our token will inherit from that.
This is for developers.
You need some knowledge of Solidity and blockchains to understand the concepts here.
Oh. And you also need Foundry installed.
Now.
Let’s get into it.

 
Go to your terminal and create a new Foundry project.
forge init
Once the project is created, open it up in your preferred IDE (I’m using VS Code).
We need to clean it up. Remove the files in the following 3 folders:
script , src , and test.
With that out the way, we can start putting together our ERC20 token. Create a new file in the src folder called MyCustomToken.sol .
 
In that file, add the smart contract for our token:
// SPDX-License-Identifier : MIT
pragma solidity ^0.8.18;

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

contract MyCustomToken is ERC20 {
    constructor(uint256 _initialSupply) ERC20("My Custom Token", "MCT") {
        _mint(msg.sender, _initialSupply);
    }
}
Here’s what we’re doing in the above code:
  1. We give a license identifier and a Solidity version
  1. Then, we import the ERC20 contract from OpenZeppelin
  1. We create a contract that inherits the ERC20 contract
  1. Our constructor takes an _initialSupply amount, and we supply the token name and ticker symbol.
  1. In the constructor, we mint an initial supply to the wallet that deploys this contract.
 
Before we move on to deployment, we need to install the OpenZeppelin contracts library correctly.
In your terminal, run this:
forge install OpenZeppelin/openzeppelin-contracts --no-commit
And once that’s done installing, go over to your foundry.toml file and add a remapping so that the file looks like this:
[profile.default]
src = "src"
out = "out"
libs = ["lib"]
remappings = ['@openzeppelin=lib/openzeppelin-contracts']

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
This tells Foundry where to find the OpenZeppelin library of contracts.
If you look in your lib folder, you should see an openzeppelin-contracts folder with the full library of contracts there.
Okay.
So now we have a smart contract for our ERC20 token, and we’re ready to deploy it.
Create a new file in your script folder. Call it DeployMyCustomToken.s.sol .
Add the following code:
// SPDX-License-Identifier : MIT
pragma solidity ^0.8.18;

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

contract DeployOurToken is Script {

    uint256 public constant INITIAL_SUPPLY = 1000000;

    function run() external {
        vm.startBroadcast();
        new MyCustomToken(INITIAL_SUPPLY);
        vm.stopBroadcast();
    }
}
Here’s what we’re doing in the above code:
  1. Once again, we need a license identifier and a Solidity version.
  1. Then we import Script from Forge (which is part of Foundry).
  1. We also obviously need to import the token contract.
  1. Then, we can write a smart contract that inherits Script.
  1. Create a constant value for the initial supply.
  1. In the run function, create a new contract and pass the initial supply in.
 
btw… If you want to know what the broadcasts are, check this out.
 
We are ready.
We have a smart contract and a deploy script.
Now we need a blockchain to deploy it on.
Start a local Anvil node with this command in your terminal:
anvil
(yeah, it’s that simple)
And you should see something like this in your terminal:
notion image
This is a locally running blockchain. The accounts and private keys are test accounts and are obviously not meant to be used for real money.
 
Leave that running and open up a new terminal.
 
Deploy the ERC20 with this terminal command:
forge script script/DeployMyCustomToken.s.sol:DeployMyCustomToken --rpc-url http://localhost:8545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 --broadcast
That’s very long, so let’s break it down.
  • First, we tell Foundry to use the script we made above.
  • Then we tell it where to find the blockchain
  • We give it a private key (one of the ones Anvil gave it to us) as the deployer wallet
  • And we tell it to broadcast this to the blockchain
The result should look something like this:
notion image
 
And that’s it!
 
Congrats!
 
We have created an ERC20 contract, a Foundry deploy script and deployed the contract using that script to a locally run Anvil node.

Keep a finger on the pulse of what's happening with Account Abstraction, Smart Contract Wallets and more.

Stay Ahead of the Curve

Subscribe

Written by

Mordi Goldstein
Mordi Goldstein

Founder @ Arena Studios | Subscription-based dev & design services for onchain startups