Foundry Guide
Foundry is a tool that allows you to easily write, test and deploy smart contracts on any EVM-compatible blockchain.
EVM Version Compatibility
Auto EVM is compatible with most EVM versions but doesn’t support some features introduced in newer versions like “Paris” or “Shanghai”. When using development tools, you may need to specify an EVM version explicitly. Supported versions: “Istanbul”, “London”.
Getting started
Note: Foundryup does not currently support
PowerShellorCmd, so if you’re on Windows, you will need to install and use Git BASH or WSL as your terminal.
-
Use the
foundryuptoolchain installer and follow the on-screen instructions to installfoundryupand make thefoundryupcommand available in your CLI. Runningfoundryupby itself will install the latest precompiled binaries:forge,cast,anvil, andchisel. Seefoundryup --helpfor more options.curl -L https://foundry.paradigm.xyz | bash -
Once installed, create a project. Let’s name it
hello_autonomys. To initialize the project, run:forge init hello_autonomyscdinto thehello_autonomysdirectory to see the project’s structure.
-
All the necessary repo structure was created automatically, so we can start writing and testing our smart contracts immediately. There are separate directories for storing smart contracts (
src) and testing smart contracts (test). Let’s open theCounter.solsmart contract and add three functions:setNumber(), which sets the uint256 number to the provided value,increment(), which increases the value by 1, anddecrement(), which decreases the value by 1.// SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.1; contract Counter { uint256 public number; function setNumber(uint256 newNumber) public { number = newNumber; } function increment() public { number++; } function decrement() public { number--; } } -
Let’s make sure that all the functions are working properly by adding some tests to the
Counter.t.soltest file, and checking if they pass. In our tests, we first set the initial value ofnumberto 2, before checking if theincrement()function increases the value by 1 and ifdecrement()decreases the value by 1.// SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13; import "forge-std/Test.sol"; import "../src/Counter.sol"; contract CounterTest is Test { Counter public counter; function setUp() public { counter = new Counter(); counter.setNumber(2); } function testIncrement() public { counter.increment(); assertEq(counter.number(), 3); } function testSetNumber(uint256 x) public { counter.setNumber(x); assertEq(counter.number(), x); } function testDecrement() public { counter.decrement(); assertEq(counter.number(), 1); } } -
Let’s build the project by running:
forge buildTest the smart contract is working by running:
forge test
All tests are passing, meaning the smart contract is working as expected.
-
There are two final things we need to do before deploying our smart contract:
- Connect a wallet that has a sufficient balance of tAI3 to cover the gas fees.
- Set an environment variable we will use later.
To make our lives easier, let’s create a new
Makefileas well as a.envfile at the root of our project..envfiles are typically used to store environment variables for your application. They are particularly useful for managing settings that change between deployment environments (e.g., development, testing, staging, and production), and for storing sensitive information. Environment variables can include database connection details, API keys, external resource URIs, or other configuration variables that might change depending on the environment in which the application is running. In our case, we would use it to point to our Auto-EVM RPC URL:RPC_URL=https://auto-evm.taurus.autonomys.xyz/wsAnd then set a private key for the EVM-compatible wallet:
PRIVATE_KEY=”your_private_key_value”Note:
.envfiles should not be committed to your source control (like Git), especially when they contain sensitive data, like your private key. To prevent this, add.envto your.gitignorefile. This helps to keep sensitive keys secure and avoids the risk of exposing them in the application’s code or version control history.In the
Makefile, let’s create shortcuts to the main features of the application:# include .env file and export its env vars -include .env # Builds build: @forge clean && forge build --optimize --optimizer-runs 1000000 # Deployment deploy: @forge create Counter --private-key ${PRIVATE_KEY} --rpc-url ${RPC_URL} --evm-version londonWe’re importing the values for a
PRIVATE_KEYandRPC_URLfrom the.envfile. This allows us to runmake buildfor building the project, andmake deployfor deploying the project, pointing to the provided RPC, and using the providedPRIVATE_KEY. Let’s runmake buildto ensure it’s working properly.
-
To deploy your contract using the specified
RPCandPRIVATE_KEY, run:make deployNote: Do not tip when submitting transactions in an attempt to accelerate them as this could result in dual charges for gas fees. When deploying smart contracts to our Auto EVM domain, you may encounter an error related to gas estimation, typically presenting as:
"No manual gas limit set"or"Gas estimation failed". For more information and solutions, visit the Auto EVM Introduction.
Congratulations, you’ve successfully deployed your smart contract on the Auto EVM!