Foundry
Testing and deploying smart contracts using Foundry
- Use
foundryup
toolchain installer
curl -L https://foundry.paradigm.xyz | bash
This will install foundryup
, then simply follow the instructions on-screen, which will 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.
-
Once installed, create a project. Let’s name it
hello_subspace
.To initialize the project, run
forge init hello_subspace
cd into
hello_subspace
directory and let’s have a look at the project’s structure. -
All the necessary repo structure was created automatically, so we can start writing and testing our smart contracts right away. As you can see, there are separate directories for storing smart contracts (src) and testing smart contracts (test). Let’s have a look at the
Counter.sol
smart contract and add a few more functions to the standard behavior. Our smart contract will have three functions:setNumber()
that 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.13;
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 functions are working properly by adding a couple of tests to the
Counter.t.sol
test file and check if they pass.// 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);
}
} -
In our tests, we first set the initial value of number to two, then check if function
increment()
increases the value by 1 and ifdecrement()
decreases the value by 1. Let’s build a project by running:forge build
and ensure that tests are working as expected by running
forge test
Nice, all tests are passing, meaning the smart contract is working as expected.
-
Next, there are two things we need to set, in order to deploy our smart contract:
- We need to connect a wallet that has sufficient balance of tAI3 to cover the gas fees.
- We need to set an environment variable we will use later.
In order to make our lives easier, let’s create a new
Makefile
as well as.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 Core-EVM RPC url by setting
RPC_URL=https://nova-0.gemini-3h.subspace.network/ws
And then set a private key for the EVM-compatible wallet
PRIVATE_KEY=”your_private_key_value”
tipIt's important to note that .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}We're importing the values for a
PRIVATE_KEY
andRPC_URL
from the.env
file.This allows us to run
make build
for building the project andmake deploy
for deploying the project pointing to the provided RPC and using the provided private_key.Let’s run
make build
to make sure it’s working properly.
-
In order to deploy your contract using the specified
RPC
andPRIVATE_KEY
just runmake deploy
Versuchen Sie nicht, eine Transaktion zu beschleunigen (geben Sie zusätzlich zu den Gebühren kein Trinkgeld ein). To read more about this, please refer to this section.
In einigen Fällen kann es bei der Bereitstellung des Skripts zu den Problemen „Kein manueller Gebührenwert festgelegt“ oder „Schätzung der Gebühren fehlgeschlagen“ kommen. Please refer to this section for the solution.
-
Herzlichen Glückwunsch, Sie haben Ihren Smart Contract erfolgreich auf Autonomys EVM bereitgestellt!