Foundry on CrossFi

RonexRonex
3 min read

Why Foundry?

Developing on CrossFi requires a robust and efficient toolset, and Foundry is one of the best frameworks for smart contract development. Foundry offers a fast, developer-friendly environment for writing, testing, and deploying smart contracts on Ethereum-compatible networks (EVM) like CrossFi.

In this guide, we’ll walk through the step-by-step process of setting up Foundry for CrossFi development. Whether you’re a seasoned blockchain developer or just getting started, this setup will prepare you to build and deploy smart contracts seamlessly.

What to expect

✅ Install and configure Foundry on your system.
✅ Set up CrossFi’s RPC for smart contract deployment.
✅ Verify your installation and prepare for development.

✅Deploy a simple Counter Smart contract

Github Repository

Setting up foundry

Incase you have configure foundry on your system you can skip this part

Step 1: Install foundry

Foundry provide a simple installation script that works across major installation systems

For Linux & MacOs

Open terminal and run

curl -L https://foundry.paradigm.xyz | bash

This will install Foundryup. Simply follow the on-screen instructions, and the foundryup command will become available in your CLI.

Step2: Verify Installation

Once you have run foundryup ,Run the following command to check if it has been installed:

forge --version

Which supposed to output something like “forge 0.2.0 (440ec52 2024-04-11T00:18:58.806922049Z)” depending on your version.

Step3: Initialize a Foundry Project

Open a new terminal (any directory of your choice)

forge init crossfi
cd crossfi

which will output as below

Step4: Configure Foundry for crossFi

Update your project's foundry.toml file to include CrossFi's RPC URL:

[rpc_endpoints]
crossfiRPC = "https://rpc.testnet.ms"

Now your foundry.toml should look like the figure below

To confirm The Rpc connection,run

forge test --fork-url  https://rpc.testnet.ms

Create a new .env file on the root directory To add your private key, Which will be used to deploy contracts to CrossFi as shown below:

To obtain your private_key you can use one from External Own wallets i.e metamask,Keprl Wallets.

For the testing purpose You can use cast command which comes with foundry to generate new private key:

cast wallet new

copy the private key and use it inside the .env file

Step 5: Writing Smart Contract

Foundry comes pre-installed with an existing counter smart contract which we will use.

The Smart Contract is located under the src/ directory:

Now this is the contract that we will deploy to crossFi chain.

Step6 : Writing Deploying Script

Open the Script folder and create a file Deploy.s.sol and add the below code snippet:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13 ;

import {Script,console} from "forge-std/Script.sol";
import {Counter} from "../src/Counter.sol";

contract DeployScript is Script{

    function setUp()public{}

    function run()external returns(Counter){
        uint256 my_privateKey = vm.envUint("PRIVATE_KEY");
        vm.startBroadcast(my_privateKey);
        //deploy the counter
        Counter counter = new Counter();
        console.log("Deployed to",address(counter));
        vm.stopBroadcast();
        return counter;

    }
}
  • The {Script} is provided by foundry which used to run scripts for Smart contracts

  • {Counter} Represents the Smart contract from the src/ folder

  • {vm.envUint} Loading .env variables in our case the private key which will be used to deploy the Smart contract;

Step7: Configuring MakeFile for Executing the Script

Create a file Makefile under the root folder and add the below snapshot:

deploy:
     forge script script/Deploy.s.sol:DeployScript --rpc-url crossfiRPC --broadcast --verify -vvvv

Step8: Deploying the Contract

The resulted output should be as follows:

make deploy

Wrapping Up & What's Next

With Foundry set up for CrossFi, you're ready to start developing smart contracts.

Next Series: The next article will be a continuation, where we'll dive into writing, compiling, and optimizing your first smart contract on CrossFi

Thank you for your time, and stay tuned!

12
Subscribe to my newsletter

Read articles from Ronex directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ronex
Ronex

Blockchain/Backend developer ->->->I speak with code ->-> ->->->Gopher->->->