In this tutorial, you will learn how to deploy and verify a smart contract on Mode using Foundry. This process is essential for those looking to understand and effectively implement smart contracts in blockchain environments. Let's get started!
Prerequisites:
Have a wallet. If you don't have one, you can create one here.
Have some ETH on Mode testnet. You can follow our guide on how to bridge or use this faucet to claim some ETH on Mode.
Have Foundry installed. You can do so here.
Have a code editor of your preference. I'll be using VSCode (install), but feel free to use any other code editor of your choice.
Environment Setup:
Let's configure our project.
First, create a folder where all our project files will reside. To do this, open your terminal (I'm using Ubuntu) and type ‘mkdir ’. Once the folder is created, navigate into it by typing ‘cd ’. After setting up our project folder, open it in your code editor by typing ‘code .’, which will open VSCode if you're using it.
Create a folder for the project
mkdir <Folder Name>
Navigate to the created folder
cd <Folder Name>
Open the folder in Visual Studio Code
code .
Note: The provided commands are for Ubuntu users. If you are using Windows, you might need to use different commands. Make sure to adapt the commands according to your operating system. ChatGPT is great for that ;)
Your terminal should look something like this:
Now open your code editor, I'm using VSCODE.
Once your project folder is open in VSCode, open the VSCode terminal.
Type forge init
in the terminal to initialize a Foundry project. After initializing Foundry in your project, your terminal should display:
If everything went well, you should see your folder structure similar to:
A brief explanation of each file:
- .github: Typically contains GitHub Actions configurations and workflows.
- lib: Contains libraries and dependencies your project needs.
- script: Stores scripts automating tasks like deployments or tests.
- src: Main folder for your project's source code, including smart contracts.
- test: Folder for testing your code to ensure it functions as expected.
- .gitignore: Specifies files and folders Git should ignore.
- .gitmodules: Used for Git submodules if your project includes them.
- foundry.toml: Foundry-specific configuration file defining tool options.
- README.md: Contains project information, setup instructions, and usage details.
Writing the Smart Contract:
Configure your smart contract. I'll use a customized ERC20, but you can create your own using the OpenZeppelin Wizard.
Smart Contract Compilation:
Blockchain compilation process
Compilation transforms code written in languages like Solidity into bytecode, binary instructions interpreted by the Ethereum Virtual Machine (EVM). This prepares the contract for execution on the blockchain, where it's stored and can interact with other contracts and users via specific transactions.
After creating and saving your smart contract, open the VSCode terminal and type ‘forge build’ to compile the smart contract.
Deploying the Smart Contract:
Once compiled, we'll proceed with deployment. Before running the command, keep reading to know more about it!
Type:
forge create --rpc-url https://sepolia.mode.network --private-key <YOUR_PRIVATE_KEY> src/ModeDeploy.sol:ModeDeploy --constructor-args <arg1> <arg2> <arg3>
Let's break down the command:
-
forge create
: Foundry command to deploy a smart contract. -
--rpc-url https://sepolia.mode.network
: Specifies the RPC node URL for contract deployment. Here, it's for Mode's Sepolia network. -
--private-key <YOUR_PRIVATE_KEY>
: Provides the private key of the account used for deployment. Replace ‘’ with your actual private key. Keep this information secure and do not share it. -
src/ModeDeploy.sol:ModeDeploy
: This argument specifies the location of the contract file and the contract name within that file. In this case:src/ModeDeploy.sol
is the path to the contract file. -
ModeDeploy
is the name of the contract within that file. -
--constructor-args <arg1> <arg2> <arg3>
: This flag provides the arguments that will be passed to the constructor of the smart contract during deployment. Replace , , and with the actual values you want to pass to the constructor.
If your smart contract doesn't require constructor arguments, omit --constructor-args <arg1> <arg2> <arg3>
.
After deploying the smart contract, you can view it on BlockScout by entering the transaction hash of the deployment.
The transaction hash shows details such as deployed tokens, token name, deployer's address, block number, gas consumed, and more. For further details on transaction hash features, refer here.
Verifying the Smart Contract:
To verify a smart contract, it must first be deployed (as done above). Once deployed, open the VSCode terminal and type
forge verify-contract CONTRACT_ADDRESS src/ModeDeploy.sol:ModeDeploy --verifier blockscout --verifier-url https://sepolia.explorer.mode.network/api\?
Let's break down the command:
-
forge verify-contract
This is the Foundry command used to verify a smart contract on a block explorer. -
CONTRACT_ADDRESS
Here, replace CONTRACT_ADDRESS with the deployed contract address you wish to verify. For example, if the contract address is 0x1234567890abcdef1234567890abcdef12345678, the command would look like this:
forge verify-contract 0x1234567890abcdef1234567890abcdef12345678 src/MyERC20.sol:MyERC20 --verifier blockscout --verifier-url https://sepolia.explorer.mode.network/api\?
-
src/ModeDeploy.sol:ModeDeploy
This argument indicates the location of the contract file and the contract name within that file. In this case:
src/ModeDeploy.sol
is the path to the contract file.
ModeDeploy
is the name of the contract within that file.
--verifier blockscout
This flag specifies Blockscout as the verification service. Foundry supports different verifiers, and here we're specifying the use of Blockscout.--verifier-url https://sepolia.explorer.mode.network/api\?
This flag provides the URL of the Blockscout verifier API we will use. In this case, it's the Sepolia network explorer URL on Mode.
To confirm successful verification, enter the contract address on BlockScout. Go to the contract tab.
This tutorial was conducted on Mode's testnet. Here's a link with information on doing this on the mainnet.
If you encounter any issues or have questions or suggestions, join the Mode Discord and obtain the developer role. Here's how to get the role. Feel free to ask anything; we're here to help.
I hope you found this tutorial on Deploying and Verifying a Smart Contract on Mode's Blockchain using Foundry helpful. If you did, follow me on Twitter for more interesting content and updates. Thanks for reading!
Top comments (0)