This tutorial will guide you through setting up Foundry for Sei EVM development and using OpenZeppelin contracts to build secure, standardized smart contracts. We’ll cover environment setup, contract creation, deployment, and show how to leverage OpenZeppelin’s pre-built components with the powerful Foundry toolkit.
Deploy to Testnet FirstIt is highly recommended that you deploy to testnet (atlantic-2) first and verify everything works as expected before committing to mainnet. Doing so helps you catch bugs early, avoid unnecessary gas costs, and keep your users safe.
Try it before you install
Installing Foundry and scaffolding a project takes a few minutes. If you just want to see a contract deploy to Sei, you can do both of these right now in the browser.
First, confirm the RPC endpoints you’ll put in foundry.toml respond:
Then add Sei testnet to your wallet and deploy a minimal Counter.sol from Remix — no forge, no install. In Remix: compile under Solidity Compiler, then Deploy & Run with Environment set to Injected Provider — MetaMask.
Table of Contents
Prerequisites
Before we begin, ensure you have the following:
- Foundry installed on your system
- A basic understanding of Solidity and smart contract development
- A wallet with SEI tokens for gas
- Node.js (v18.0.0 or later) for ethers.js interactions
Setting Up Your Development Environment
First, let’s install Foundry if you haven’t already. Follow the installation guide or use the quick install command:
Create a new Foundry project:
This will create a standard Foundry project structure with src/, test/, and script/ directories.
Configuring Foundry for Sei EVM
Create a foundry.toml file in your project root to configure Foundry for Sei networks:
Create a .env file to store your private key and other sensitive information:
Add .env to your .gitignore file to prevent committing sensitive information such as your PRIVATE_KEY and potentially lose funds.
Using OpenZeppelin Contracts
OpenZeppelin provides a library of secure, tested smart contract components. Let’s install OpenZeppelin contracts:
Create a remappings.txt file to properly map the imports:
Creating and Deploying Smart Contracts
Let’s create different types of smart contracts. Choose from the tabs below based on what you want to build:
Counter Contract
ERC20 Token
ERC721 NFT
Let’s start with a simple counter contract. Update the default src/Counter.sol file:Create a deployment script in script/DeployCounter.s.sol:script/DeployCounter.s.sol
Create an ERC20 token using OpenZeppelin. Create src/SeiToken.sol:Create a deployment script in script/DeploySeiToken.s.sol:script/DeploySeiToken.s.sol
Create an ERC721 NFT contract using OpenZeppelin. Create src/SeiNFT.sol:Create a deployment script in script/DeploySeiNFT.s.sol:script/DeploySeiNFT.s.sol
Testing Your Smart Contracts
Foundry provides excellent testing capabilities. Let’s create comprehensive tests for our contracts.
Counter Tests
ERC20 Tests
ERC721 Tests
Update test/Counter.t.sol: Create test/SeiToken.t.sol: Create test/SeiNFT.t.sol:
Run your tests with:
For more verbose output:
Deploying to Sei Testnet and Mainnet
Now let’s deploy our contracts to the Sei networks. You can use either Forge scripts or direct deployment commands.
Using Forge Scripts (Recommended)
Deploy to Sei testnet:
Deploy to Sei mainnet:
Using Direct Forge Create Commands
Alternatively, you can deploy directly:
Successful deployment will output:
Interacting with Deployed Contracts
Once deployed, you can interact with your contracts using Foundry’s cast tool or ethers.js.
Using Cast Commands
Using ethers.js
Install ethers.js to interact with your contracts programmatically:
Create a Node.js script to interact with your deployed contracts:
Foundry generates the ABI for contracts in the out folder. You can use this ABI to interact with contracts from other tools like ethers.js or web3.js.