DEV Community

Abhishek Yadav
Abhishek Yadav

Posted on

1

Streamline Your Smart Contract Development with Foundry

What is foundry??

Foundry is a comprehensive toolchain specifically designed to empower developers in the realm of smart contract creation. It streamlines the entire development process, offering a robust set of features that manage dependencies, compile code, run tests, facilitate deployments, and even enable interaction with the blockchain through both command-line and Solidity scripts. In essence, Foundry acts as a one-stop shop for smart contract development, eliminating the need to juggle multiple disparate tools.
More on official website

To summarize we can it does everything we require in the blockchain development and comes built in with utility tools.

How to install??
Before going further make sure you have installed GIT BASH because these commands will not work on windows terminal.
Before installing foundry we need to install latest version of the rust.
To install rust run below command in your bash terminal

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Enter fullscreen mode Exit fullscreen mode

To check rust is correctly installed run this command

cargo --version
Enter fullscreen mode Exit fullscreen mode

To install foundry run this command in your bash terminal

curl -L https://foundry.paradigm.xyz | bash
Enter fullscreen mode Exit fullscreen mode

This will install following tools

  1. Forge
  2. Cast
  3. Anvil
  4. Chisel

If you want to learn in depth about these tools check out this official doc

Now let's start our project using foundry.

$ forge init <project_name>
Enter fullscreen mode Exit fullscreen mode

This will create template project in in this directory
cd into the folder.
Now let's understand all the folder one by one.
src --> In this folder we will write all our smart contracts
script --> here we write our deploy scripts
For Now these to enough to write our smart contract and continue working.
Now Delete existing file from the src directory and create a new file and write your smart contract.

For Example: let's create a file with name myContract.sol

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

contract myContract {
    uint256 myNumber;

    function store(uint256 _myNumber) public {
        myNumber = _myNumber;
    }

    function getMyNumber() public view returns (uint256) {
        return myNumber;
    }
}
Enter fullscreen mode Exit fullscreen mode

Now Delete existing file in file script directory and add your file with name DeploymentScript.s.sol
Code:

pragma solidity ^0.8.18;

import { myContract } from "../src/SimpleStore.sol";
import  "../lib/forge-std/src/Script.sol";

contract DeploymentScript is Script {
    function run() external returns (myContract) {

        // all transactions will take place in between the startBroadcast() and stopBroadcast()
        vm.startBroadcast();
        myContract myc = new myContract();
        vm.stopBroadcast();
        return myc;
    }
}
Enter fullscreen mode Exit fullscreen mode

In above code are importing our contract then importing standard lib provided in lib folder

Since we have created our contract let's compile it make sure everything is ok

forge build
Enter fullscreen mode Exit fullscreen mode

Now lets deploy our contract although you can deploy your contract using deploy script but we can also use our bash terminal to deploy contract.

Steps to deploy

First run this command

anvil
Enter fullscreen mode Exit fullscreen mode

This will run local blockchain node

Now run below command to deploy our smart contract

forge create <your_contract_name> --interactive
Enter fullscreen mode Exit fullscreen mode

By default if you will not provide rpc-url it will try to deploy on local node where anvil running.
If you are using Ganache and need to provide an RPC URL, use:

forge create <your_contract_name> --rpc-url <your_rpc-url> --interactive
Enter fullscreen mode Exit fullscreen mode

Now let's use our deploy script to deploy our smart contract
Use this command

forge script script/DeploymentScript.s.sol --rpc-url <rpc-url> --interactive --sender <address> --broadcast -vvvv
Enter fullscreen mode Exit fullscreen mode

Make sure to copy address and private key corresponding to that address from bash terminal where anvil running

Once you run above command you will see some in your anvil bash terminal.

We use cast to interact with deployed smart contract from bash terminal.

To call store function we can use following command.

cast send 0x5FbDB2315678afecb367f032d93F642f64180aa3 "store(uint256)" 123 --rpc-url http://127.0.0.1:8545 --interactive
Enter fullscreen mode Exit fullscreen mode

when prompted give private key of wallet (you can get it from bash terminal of anvil)

Now to call getMyNumber which is view function we can use following command

cast call 0x5FbDB2315678afecb367f032d93F642f64180aa3 "getMyNumber()" --rpc-url http://127.0.0.1:8545
Enter fullscreen mode Exit fullscreen mode

You may have noticed when call we store function we do need private key where as while we call getMyNumber we do not.
Let's understand the reason:
In Solidity, functions can be categorized into two main types: state-changing functions and view/pure functions.

State-changing functions:

These functions can modify the state of the blockchain. Any modification to the contract's state variables or storage will require a transaction, and transactions in Ethereum incur gas fees.
Examples include functions that modify the contract's state, such as writing to storage variables.

View and Pure functions:

These functions are read-only and do not modify the state. They don't require a transaction and can be called without incurring gas fees. View functions are allowed to read the state of the contract but cannot modify it. Pure functions do not read or modify the state; they are entirely self-contained.

The store function modifies the state by updating the myNumber state variable. Therefore, it is a state-changing function and requires a transaction, incurring gas fees.

The getMyNumber function, on the other hand, is declared as view, indicating that it only reads from the state and does not modify it. As a result, it can be called without a transaction and does not incur gas fees.

Thank you for reading.

Billboard image

Use Playwright to test. Use Playwright to monitor.

Join Vercel, CrowdStrike, and thousands of other teams that run end-to-end monitors on Checkly's programmable monitoring platform.

Get started now!

Top comments (0)

Migrate from Solidity to Rust

Migrate from EVM

Migrate your smart contracts from Solidity to Rust. Let us know how we can help.

Make the Move

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay