Foundry + Conflux for Smart Contract Development
What is Foundry?
Foundry is a blazing fast blockchain development toolkit that was developed by Paradigm to eliminate the need for external programming languages and complexities when dealing with core smart contract development with solidity. Foundry has two cli tools called cast and forge which help with network interactions/tooling and contract interactions/testing respectively.
Since Foundry was built using rust, you need to install rust on your machine before installing foundry using foundryup.
Contract Deployment
Use the Foundry+Conflux repo to have a boilerplate workspace which uses foundry, alternatively, running forge init
in an empty folder will initialize that folder to use foundry cli tools.
Remember to use the
--recursive
flag while cloning so that all the gitmodules will be pulled too and also remember to runforge remappings>remappings.txt
to generate new remappings for the gitmodules
The repository simply contains 2 distinct contracts;
- ERC20 tokens
- A swapper contract which allows anyone to create an order/market for a particular amount of Token A to be swapped for another amount of Token B
To deploy the Swapper.sol
contract for example, you can use the forge create
command
forge create Swapper --contracts /$USER/Foundry+Conflux/src/Swapper.sol --private-key $your_private_key --rpc-url https://evmtestnet.confluxrpc.com --legacy
Note:
The path to the contract has to be the absolute path of where that contract is stored
We are using the
— legacy
flag because Foundry uses the EIP1559 transaction object structure which the eSpace testnet doesn’t support
Contract Tests
To test your contracts in Foundry, you need to write your contract tests in solidity and save it with a .t.sol
extension.
contract Foo {
uint256 public x = 1;
function set(uint256 _x) external {
x = _x;
}
function double() external {
x = 2 * x;
}
}
contract FooTest {
Foo foo;
// The state of the contract gets reset before each
// test is run, with the `setUp()` function being called
// each time after deployment.
function setUp() public {
foo = new Foo();
}
// A simple unit test
function testDouble() public {
require(foo.x() == 1);
foo.double();
require(foo.x() == 2);
}
}
👆🏿 Here is a very simple example of a contract and solidity test which can be executed by using the command forge test
.
Also note that foundry performs automatic fuzzing tests for functions that accept
uint/int
as inputs/arguments.
To run the Swapper.t.sol
test that can be found in this template repository, simply run
forge test
Foundry contains a
foundry.toml
file where you can configure some environmental variables which are used when executing eitherforge
orcast
commands.
To learn more about foundry, kindly refer to the Foundry handbook
hosted here