Install and Configure Foundry (Forge) (2024)

Install and Configure Foundry (Forge) (1)

The last toolkit is the newest one and the last one I want to show you in this section.

It's Foundry, a completely fresh rewrite in Rust. It's blazing fast and not dependent on JavaScript at all.

Install Foundry

To install Foundry, you run an actual shell script on your computer.

You can, for example, create a new folder for foundry in our nft-project folder:

mkdir foundrycd foundrycurl -L https://foundry.paradigm.xyz | bashsource ~/.bashrc foundryup

Install and Configure Foundry (Forge) (2)

Now you will have foundry installed, which comes with three subcommands:

  • forge: the build, test, debug, deploy smart contracts
  • anvil: the foundry equivalent of Ganache
  • cast: low level access to smart contracts (a bit of a truffle console equivalent)

Initializing a new Foundry Project

To get started with foundry, initialize a new project in the foundry directory:

This will download a starter repository and setup everything for the first use.

It looks a bit different than hardhat and truffle:

  • contracts are in the src folder
  • tests are in the test folder and are solely solidity tests
  • scripts are in the scripts folder and are solidity files
  • dependencies are added as git submodules into the lib folder
  • Project is configured using the foundry.toml file
  • After building (which you'll see in a second), the contract artifacts are in the out folder

It doesn't depend on JavaScript at all. Everything is a git submodule. And its also blazing fast.

Building and Testing using Forge

To build the smart contracts, simply run

forge build

It will compile all files and put the ABI, bytecode and AST as json files into subfolders with the name of the contracts in the out folder

Install and Configure Foundry (Forge) (3)

Let's see how we can add our NFT into the folders...

Adding our NFT to Foundry

Install and Configure Foundry (Forge) (4)

First thing is to delete the existing contract, test and script:

rm src/*.sol test/*.sol script/*.sol

Then simply copy over the Spacebears.sol contract from Hardhat. If you try to run forge build, it will fail, because the package for openzeppelin isn't installed.

Install and Configure Foundry (Forge) (5)

Here's one of the biggest differences to HardHat and Truffle: Foundry is not a JavaScript based framework and everything works with git submodules. Here we need to install github-user/repo-name, which is openzeppelin/openzeppelin-contracts for https://github.com/OpenZeppelin/openzeppelin-contracts!

Install openzeppelin with

forge install openzeppelin/openzeppelin-contracts

Which will fail, because we have changes in our branch, which we need to commit first:

Install and Configure Foundry (Forge) (6)

If you run git status it will tell you what changed:

Install and Configure Foundry (Forge) (7)

We added the Spacebear contract and removed all other contracts from the directories.

git add .git commit -a -m "Added spacebear contract and removed everything else"

This should do the trick. Now, let's try again:

forge install openzeppelin/openzeppelin-contracts

We also need to change the imports, as we're not importing from the npm module @openzeppelin/..., we're importing from openzeppelin-contracts...:

// SPDX-License-Identifier: MITpragma solidity ^0.8.4;import "openzeppelin-contracts/token/ERC721/ERC721.sol";import "openzeppelin-contracts/access/Ownable.sol";import "openzeppelin-contracts/utils/Counters.sol";contract Spacebear is ERC721, Ownable { using Counters for Counters.Counter; Counters.Counter private _tokenIdCounter; constructor() ERC721("Spacebear", "SBR") {} function _baseURI() internal pure override returns (string memory) { return "https://ethereum-blockchain-developer.com/2022-06-nft-truffle-hardhat-foundry/nftdata/"; } function safeMint(address to) public onlyOwner { uint256 tokenId = _tokenIdCounter.current(); _tokenIdCounter.increment(); _safeMint(to, tokenId); } function buyToken() public payable { uint256 tokenId = _tokenIdCounter.current(); require(msg.value == tokenId * 0.1 ether, "Not enough funds sent"); _tokenIdCounter.increment(); _safeMint(msg.sender, tokenId); } // The following functions are overrides required by Solidity. function _burn(uint256 tokenId) internal override(ERC721) { super._burn(tokenId); } function tokenURI(uint256 tokenId) public pure override(ERC721) returns (string memory) { return string(abi.encodePacked(_baseURI(),"_",tokenId+1,".json")); }}

Now the compilation works:

Install and Configure Foundry (Forge) (8)

Let's have a look in the next lecture how we can do unit-testing with Foundry. It's a lot of fun, because foundry is

  • blazing fast
  • allows VM modifications from within solidity
Install and Configure Foundry (Forge) (2024)
Top Articles
600 Top Accounting Research Topics To Consider
Top 10 Cryptocurrencies Prices In India | CoinSwitch
Fat Hog Prices Today
Jennifer Hart Facebook
Wizard Build Season 28
The 10 Best Restaurants In Freiburg Germany
Lighthouse Diner Taylorsville Menu
Tj Nails Victoria Tx
Find All Subdomains
Doublelist Paducah Ky
The Best Classes in WoW War Within - Best Class in 11.0.2 | Dving Guides
Embassy Suites Wisconsin Dells
J Prince Steps Over Takeoff
Learn How to Use X (formerly Twitter) in 15 Minutes or Less
What is IXL and How Does it Work?
123 Movies Babylon
Simple Steamed Purple Sweet Potatoes
Detroit Lions 50 50
‘Accused: Guilty Or Innocent?’: A&E Delivering Up-Close Look At Lives Of Those Accused Of Brutal Crimes
Local Dog Boarding Kennels Near Me
Foodland Weekly Ad Waxahachie Tx
National Office Liquidators Llc
Craigslist Red Wing Mn
Csi Tv Series Wiki
Inter-Tech IM-2 Expander/SAMA IM01 Pro
Joann Ally Employee Portal
Cbssports Rankings
Netwerk van %naam%, analyse van %nb_relaties% relaties
Lexus Credit Card Login
Hesburgh Library Catalog
Is Holly Warlick Married To Susan Patton
Frequently Asked Questions - Hy-Vee PERKS
Word Trip Level 359
What Time Does Walmart Auto Center Open
Atlantic Broadband Email Login Pronto
Ket2 Schedule
Gary Lezak Annual Salary
Gold Dipping Vat Terraria
Letter of Credit: What It Is, Examples, and How One Is Used
Electric Toothbrush Feature Crossword
Saline Inmate Roster
18006548818
Stranahan Theater Dress Code
5103 Liberty Ave, North Bergen, NJ 07047 - MLS 240018284 - Coldwell Banker
UNC Charlotte Admission Requirements
Zits Comic Arcamax
Ubg98.Github.io Unblocked
Aspen.sprout Forum
Skybird_06
Latest Posts
Article information

Author: Twana Towne Ret

Last Updated:

Views: 5454

Rating: 4.3 / 5 (44 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Twana Towne Ret

Birthday: 1994-03-19

Address: Apt. 990 97439 Corwin Motorway, Port Eliseoburgh, NM 99144-2618

Phone: +5958753152963

Job: National Specialist

Hobby: Kayaking, Photography, Skydiving, Embroidery, Leather crafting, Orienteering, Cooking

Introduction: My name is Twana Towne Ret, I am a famous, talented, joyous, perfect, powerful, inquisitive, lovely person who loves writing and wants to share my knowledge and understanding with you.