Auto EVMFoundry Guide

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 PowerShell or Cmd, so if you’re on Windows, you will need to install and use Git BASH or WSL as your terminal.

  1. Use the foundryup toolchain installer and follow the on-screen instructions to install foundryup and make the foundryup command available in your CLI. Running foundryup by itself will install the latest precompiled binaries: forge, cast, anvil, and chisel. See foundryup --help for more options.

    curl -L https://foundry.paradigm.xyz | bash
  2. Once installed, create a project. Let’s name it hello_autonomys. To initialize the project, run:

    forge init hello_autonomys

    cd into the hello_autonomys directory to see the project’s structure.

    Foundry-1

  3. 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 the Counter.sol smart contract and add three functions: setNumber(), which sets the uint256 number to the provided value, increment(), which increases the value by 1, and decrement(), 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--;
        }
    }
  4. Let’s make sure that all the functions are working properly by adding some tests to the Counter.t.sol test file, and checking if they pass. In our tests, we first set the initial value of number to 2, before checking if the increment() function increases the value by 1 and if decrement() 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);
        }
    }
  5. Let’s build the project by running:

    forge build

    Test the smart contract is working by running:

    forge test

    Foundry-2

    All tests are passing, meaning the smart contract is working as expected.

  6. 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 Makefile as well as a .env file at the root of our project. .env files 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/ws

    And then set a private key for the EVM-compatible wallet:

    PRIVATE_KEY=”your_private_key_value”

    Note: .env files should not be committed to your source control (like Git), especially when they contain sensitive data, like your private key. To prevent this, add .env to your .gitignore file. 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 london

    We’re importing the values for a PRIVATE_KEY and RPC_URL from the .env file. This allows us to run make build for building the project, and make deploy for deploying the project, pointing to the provided RPC, and using the provided PRIVATE_KEY. Let’s run make build to ensure it’s working properly.

    Foundry-3

  7. To deploy your contract using the specified RPC and PRIVATE_KEY, run:

    make deploy

    Note: 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!