How to Deploy a smart Contract using Foundry

Posted By : Rahul

Apr 30, 2024

Foundry is a Rust-based smart contract development toolset for Ethereum. The creation and implementation of smart contracts are made easier with Foundry. By managing dependencies, executing tests, and assisting with deployment, it simplifies the process. Additionally, Foundry offers you a variety of tools for creating smart contracts, including:

 

Forge: Allows you to test, build, and implement smart contracts.

 

Cast: Cast is Foundry's CLI utility for making RPC calls to Ethereum. Send transactions, call smart contracts, and get any kind of chain data.

 

Anvil: Foundry ships with a local testnet node. It can be used to communicate over RPC or to test your contracts from frontends.

 

Chisel: Included with Foundry is a powerful Solidity REPL called Chisel. It can be used to rapidly evaluate how Solidity snippets behave on a forked or local network.

 

Also, Explore | Top 5 Smart Contract Development Companies

 

How to Deploy a Smart Contract using Foundry

 

Initialize the project

Use forge init to launch a new Foundry project:
 

init foundry_project forging

 

A new project folder named foundry_project will result from this. The following items will be in the folder:
 


src : Your smart contracts' default directory is called src.

 

tests: the tests' default directory

 

foundry.toml : Foundry project configuration file, foundry.toml

 

lib: includes the required libraries.

 

script: files with Solidity Scripting

 

You may also like | Code Analysis Tools for Solidity Smart Contracts

 

The Counter.sol sample smart contract is provided and can be found inside the src folder.

 

// 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++;
   }
}

 

Use the forge build to compile the smart contract: forge build

 

The following output will appear in your terminal if your contract compiles successfully:

 

rahul@rahul:~/foundry/hello_foundry$ forge build
[â °] Compiling...
[â ˜] Compiling 24 files with 0.8.23
[â ’] Solc 0.8.23 finished in 6.28sCompiler run successful!
[â ¢] Solc 0.8.23 finished in 6.28s

 

The tests directory is created by Foundry and serves as the default location for all of the smart contract testing. The test file names have a.t.sol extension.

 

You can find a specifically named test file called Counter.t.sol if you open the test folder.
 

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


import {Test, console2} from "forge-std/Test.sol";
import {Counter} from "../src/Counter.sol";


contract CounterTest is Test {
   Counter public counter;


   function setUp() public {
       counter = new Counter();
       counter.setNumber(0);
   }


   function test_Increment() public {
       counter.increment();
       assertEq(counter.number(), 1);
   }


   function testFuzz_SetNumber(uint256 x) public {
       counter.setNumber(x);
       assertEq(counter.number(), x);
   }
}

 

To test the Couter.sol smart contract utilising forge test, execute the following file: forge test

 

If all the test are passed then,

 

rahul@rahul:~/foundry/hello_foundry$ forge test
[â ”] Compiling...No files changed, compilation skipped
[â ’] Compiling...

Running 2 tests for test/Counter.t.sol:CounterTest
[PASS] testFuzz_SetNumber(uint256) (runs: 256, μ: 27320, ~: 28409)
[PASS] test_Increment() (gas: 28379)
Test result: ok. 2 passed; 0 failed; 0 skipped; finished in 64.81ms

 

Also, Explore | AI-Driven Smart Contracts: Merging Intelligence with Automation


 

Deploying the smart contract

 

Use the forge create command to deploy the smart contract to a network:


Deploy command : forge create <your_rpc_endpoint> as the --rpc-url. <wallet_private_key>"”private-key counter.sol: Counter in src . By following this tutorial, you can obtain the RPC for the network from Infura: 

 

Link :  https://www.infura.io/blog/post/getting-started-with-infura-28e41844cc89
should the smart contract be successfully deployed, your terminal will display the following output:

 

rahul@rahul:~/foundry/hello_foundry$  forge create src/Counter.sol:Counter --rpc-url https://sepolia.infura.io/v3/<your infura key> -
-private-key <wallet private key> 
[â ”] Compiling...No files changed, compilation skipped
[â ’] Compiling...
Deployer: 0x2Bc5b75F445cdAa418d32C5FD61A11E53c540Ba2
Deployed to: 0xb88758D730bDd6b07958427321169626A479afBc
Transaction hash: 0x4a6ebeb2d942a3c60648a44d8078ef00cb440f73a22acf2a06ee63f95604ef2f

 

If you are looking to bring your business idea into reality using the potential of blokchain and smart contracts, connect with our skilled smart contract developers to get started. 

 

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

September 8, 2024 at 02:22 am

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Telegram Button
Youtube Button
Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text