Deploy a smart contract on Monad using Foundry
Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
Requirements
Before you begin, you need to install the following tools:
- Rust
- Cargo
1. Installing foundryup
Foundryup is the official installer for the Foundry toolchain.
curl -L https://foundry.paradigm.xyz | bash
This will install Foundryup. Simply follow the on-screen instructions, and the foundryup
command will become available in your CLI.
2. Installing forge
, cast
, anvil
and chisel
binaries
foundryup
If you’re using Windows, you’ll need to install and use Git BASH or WSL as your terminal, since Foundryup currently doesn’t support Powershell or Command Prompt (Cmd).
3. Create a new foundry project
You can use foundry-monad
template to create a new project.
Foundry-Monad is a Foundry template with Monad configuration. So developers don't have to do the initial configuration in Foundry for Monad network.
The below command uses foundry-monad
to create a new foundry project:
forge init --template monad-developers/foundry-monad [project_name]
Alternatively, you can create a default foundry project using the below command:
forge init [project_name]
4. Modify Foundry configuration
Currently, the RPC and block explorer are not public; this page will be updated as soon as they are.
We appreciate your patience.
Skip this step if you are using foundry-monad
template
Update foundry.toml
file to add Monad configuration.
[profile.default]
src = "src"
out = "out"
libs = ["lib"]
# Monad Configuration
# TODO: Add RPC URL and Chain ID
eth-rpc-url="MONAD_RPC_URL"
chain_id = "MONAD_CHAIN_ID"
# TODO: Add Explorer URL and Chain ID
[etherscan]
monadDevnet = { key = "DUMMY_VALUE", url = "EXPLORER_URL", chain = MONAD_CHAIN_ID }
5. Write a smart contract
You can write your smart contracts under the src
folder. There is already a Counter
contract in the project located at src/Counter.sol
.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
contract Counter {
uint256 public number;
function setNumber(uint256 newNumber) public {
number = newNumber;
}
function increment() public {
number++;
}
}
6. Compile the smart contract
forge compile
Compilation process output can be found in the newly created out
directory, which includes Contract ABI and bytecode.
7. Deploy the smart contract
For deploying contract we recommend not to use a private key associated with real funds. Create a new wallet or have a development-only wallet.
Get testnet funds
Currently, the faucet is not public; this page will be updated as soon as it is.
We appreciate your patience.
Deploying smart contracts requires testnet funds. Claim testnet funds via a faucet.
Deploy smart contract
- Using Keystore (Recommended)
- Directly using Private Key
Keystore is a much safer way to using private key with Foundry projects, because keystore encrypts the private key and can later be referenced in any commands that require a private key.
Create a new wallet keystore using the below command
cast wallet import <keystore_name> --interactive
Enter your wallet’s private key when prompted & provide a password to encrypt it.
Run the below command to deploy your smart contracts
forge create src/Counter.sol:Counter --account <keystore_name>
Use the below command to deploy a smart contract by directly pasting the private key in the terminal.
forge create --private-key <your_private_key> src/Counter.sol:Counter
On successful deployment of smart contract, the output should be similar to the following:
Deployer: 0xB1aB62fdFC104512F594fCa0EF6ddd93FcEAF67b
Deployed to: 0x67329e4dc233512f06c16cF362EC3D44Cdc800e0
Transaction hash: 0xa0a40c299170c9077d321a93ec20c71e91b8aff54dd9fa33f08d6b61f8953ee0
Next Steps
Currently, the block explorer is not public; this page will be updated as soon as it is.
We appreciate your patience.
Check out how to verify the deployed smart contract on Monad Explorer.