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
To check rust is correctly installed run this command
cargo --version
To install foundry run this command in your bash terminal
curl -L https://foundry.paradigm.xyz | bash
This will install following tools
- Forge
- Cast
- Anvil
- 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>
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;
}
}
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;
}
}
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
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
This will run local blockchain node
Now run below command to deploy our smart contract
forge create <your_contract_name> --interactive
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
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
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
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
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.
Top comments (0)