How to Develop and Deploy Smart Contracts with Foundry & Openzeppelin

Search for a command to run...

No comments yet. Be the first to comment.
x402 is a new payment standard that enables micro payments on the web by leveraging blockchain technology. It leverages the existing 402 Payment Required status code to require payment before serving a response and uses crypto-native payments for spe...

The so called AI slop all over the internet is getting less sloppy. NBC News We're living through an extraordinary moment in history. The rapid evolution of generative AI has unleashed an unprecedented creative boom. These tools have democratized co...

In these last few years, layer two blockchains (L2) have become central to Ethereum's scaling efforts. L2s are fast, cheap, and inherit Ethereum's strong security guarantees. Ethereum serves as the settlement layer, and L2s serve as the execution lay...

A blockchain is a peer-to-peer (P2P) network of nodes that maintains a shared ledger. Every node communicates with other nodes in the network using specialized P2P protocols to verify blocks, transactions, store state, etc. When you’re building an ap...

Tokens represent money-like assets, utility (such as governance or access), rewards and points, game items, tickets, and more. Most tokens on Solana follow the SPL token standard or Token-2022 with extensions, which define how tokens are created, tra...

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust. It consists of three components:

console.log, and cheat codes give you more power and flexibility.The Foundry Book explains the installation of Foundry very well. Check out the instructions here:
https://book.getfoundry.sh/getting-started/installation
Once we installed Foundry, we can create a new project using the following command:
forge init foundry-demo // forge-demo is the name of the project
After creating the project, we check if everything is working correctly with this command:
cd foundry-demo && forge build
Forge manages dependencies using Git Submodules by default, which means that it works with any GitHub repository that contains smart contracts.
To use OpenZeppelin, we need to install it as a dependency in our project with this command:
forge install OpenZeppelin/openzeppelin-contracts
// forge install is command which is used for installing dependencies
// <https://github.com/OpenZeppelin/openzeppelin-contracts>
// use {{username}}/{{repo_name}} from the github url
After installing OpenZeppelin, try importing something from it in the contract (your contract is in the src directory); an error will pop up if you are using VSCode.
To fix this error, Run this command:
forge remappings > remappings.txt
What this command does is creates a remappings.txt file inside the root directory of the project.
At this moment, the content in the file might look like this:
ds-test/=lib/forge-std/lib/ds-test/src/
forge-std/=lib/forge-std/src/
openzeppelin-contracts/=lib/openzeppelin-contracts/contracts/
For more details, visit this website: https://book.getfoundry.sh/config/vscode
Rename the file src/Counter.sol to src/FDemo.sol; the code for our ERC721 smart contract is as below:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "openzeppelin-contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "openzeppelin-contracts/utils/Counters.sol";
contract FDemo is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenId;
constructor() ERC721("FDemo", "FD") {}
function mint(string memory tokenUri) external returns (uint256) {
uint256 newTokenId = _tokenId.current();
_mint(msg.sender, newTokenId);
_setTokenURI(newTokenId, tokenUri);
_tokenId.increment();
return newTokenId;
}
}
Let's start by renaming the test file to match the name of our contract Counter.t.sol to FDemo.t.sol.
Forge uses the following keywords in tests:
setUp: An optional function invoked before each test case runs.function setUp() public {
testNumber = 42;
}
test: Functions prefixed with test are run as a test case.function testNumberIs42() public {
assertEq(testNumber, 42);
}
testFail: The inverse of the test prefix—if the function does not revert, the test fails.function testNumberIs42() public {
assertEq(testNumber, 42);
}
We only have one method, mint, so we will write a test case for this method. This is going to be a pretty simple one.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import "../src/FDemo.sol";
contract FoundryDemoTest is Test {
FDemo instance;
function setUp() public {
instance = new FDemo();
}
function testMint() public {
string memory dummyTokenUri = "ipfs://metadata_url";
uint256 tokenId = instance.mint(dummyTokenUri);
assertEq(dummyTokenUri, instance.tokenURI(tokenId));
}
}
Now, to run this test, we can use the command forge test
We can use traces if we want to explore more details/events/flow about the test cases. To enable them while running test cases use -vvv or -vvvv
The attached screenshot shows the result of test cases with and without traces.

More details on the Traces: https://book.getfoundry.sh/forge/traces
To generate gas reports, use `--gas-report`` with the test command.
forge test --gas-report
More details on the gas report here: https://book.getfoundry.sh/forge/gas-reports
Forge can deploy smart contracts to a given network with the forge create command.
Some options we can use with forge create while deploying the contract.
--rpc-url: RPC URL of the network on which we want to deploy our contract. In our case, we will be using the RPC URL of Polygon's Mumbai testnet.--constructor-args: Pass arguments to the constructor.--private-key: Private key of deployers wallet.We can optionally pass --verify and --etherscan-api-key if we want to verify our contract.
$ forge create --rpc-url <your_rpc_url> \\
--constructor-args "ForgeUSD" "FUSD" 18 1000000000000000000000 \\
--private-key <your_private_key> src/MyToken.sol:MyToken \\
--etherscan-api-key <your_etherscan_api_key> \\
--verify
Let's deploy, now!
forge create --rpc-url <https://rpc.ankr.com/polygon_mumbai>
--private-key <your_private_key> src/FDemo.sol:FDemo
--etherscan-api-key <your_etherscan_api_key>
--verify
Complete code: GitHub
💡 Follow me on Twitter for more awesome stuff like this @pateldeep_eth Linkedin