Deploy an EVM contract
Overview
The x/evm
Warden module allows executing Ethereum Virtual Machine (EVM) contracts charged by Evmos and written in Solidity.
This guide explains how to create and deploy a Solidity smart contract on a Warden local chain or on a testnet. You'll deploy a simple counter contract using Foundry's toolset.
Existing Solidity contracts are easy to deploy on Warden, so you can seamlessly port applications from any EVM-compatible chain to Warden and reach new users. You can call Warden precompiles to interact with Warden modules, accessing all core features of Warden Protocol. For advanced usage of EVM contracts with AI Agents, refer to Build an onchain AI Agent.
Prerequisites
Before you start, complete the following prerequisites:
-
Install Foundry by running the following command:
curl -L https://foundry.paradigm.xyz | bash \
foundryup -
Set up a Warden account on a local chain or a testnet. Note down your private key.
-
If you're deploying on a local chain, make sure it's running. You can start your chain by running
wardend start
in a separate terminal window.
1. Create an EVM project
Initialize a new Foundry project and navigate to its directory:
forge init warden-smart-contract --no-commit
cd warden-smart-contract
2. Create a smart contract
After you initialize a Foundry project, the script will automatically create a sample contract named Counter.sol
in the src
directory:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
contract Counter {
uint256 public number;
// A function for setting a new number
function setNumber(uint256 newNumber) public {
number = newNumber;
}
// A function for incrementing the number
function increment() public {
number++;
}
}
This is a counter contract with two functions: for changing the number
variable and for incrementing it.
In the following steps, we're going to deploy this contract without modification.
3. Compile and deploy the contract
-
Set your private key as an environment variable. (You obtained it when setting up your account.)
export PRIVATE_KEY=my-private-key
warningIn production, never store private keys directly in environment variables. Consider using encrypted keystores or secure key management solutions like
env
. -
Set the RPC URL as an environment variable. Specify the standard localhost address or Chiado's EVM endpoint:
- Local node
- Chiado
export RPC_URL=http://127.0.0.1:8545
export RPC_URL=https://evm.chiado.wardenprotocol.org
tipIf you're running a local chain and deploying the contract on different machines, you need to specify your chain's host address. To get it, just execute
wardend status
on the machine hosting the chain. -
To compile and deploy the contract, run this command:
forge create --rpc-url $RPC_URL --private-key $PRIVATE_KEY src/Counter.sol:Counter
You'll see an output similar to the following:
Deployer: 0x6Ea8aC1673402989e7B653aE4e83b54173719C30
Deployed to: 0x2AAbb1a9b8EdE05f183FfD90A324ce02A349F6e5
Transaction hash: 0x38c67c5bd92589ec6e31c2204a577e4c8d365099daad1382ff2596893b405249 -
Note down the value returned as
Deployed to
—that's your contract address. Set it as an environment variable:export CONTRACT_ADDRESS=my-contract-address