Smart contract development is one of the key requirements for the expansion of blockchain and web3. You might wonder about the need to learn Forge Standard Library for smart contract development. How will it help you in creating innovative, secure and powerful smart contracts for new use cases? Forge Standard Library offers a collection of contracts that can help you write tests with the Foundry framework.
Foundry is a popular toolkit that offers access to a broad collection of tools to develop and deploy decentralized apps on Ethereum blockchain. It has been designed to serve the needs of developers with diverse levels of skill. The Forge Standard Library in Foundry offers the essential functionalities that you need to start writing tests. Let us learn more about different standard libraries that can help you write tests efficiently in this Forge Standard Library guide.
Build your identity as a certified blockchain expert with 101 Blockchains’ Blockchain Certifications designed to provide enhanced career prospects.
Understanding the Fundamentals of Forge Standard Library
Foundry is one of the most popular frameworks for smart contract development with Solidity programming language. It offers a command-line interface for easier creation, management, and deployment of smart contracts. You can use answers to queries like ‘What is Forge Standard Library?’ to measure the power of Foundry as a smart contract development toolkit. The Forge Standard Library offers a collection of useful contracts that ensure easier and faster scripting of tests with a user-friendly experience.
Foundry is better than other popular smart contract development frameworks such as Remix IDE and Hardhat for offering tools like the Forge Standard Library. The in-built support for smart contract testing empowers developers to check the functionality of their smart contracts before deployment. The Forge Standard Library is the most effective and commonly preferred resource for writing tests with the Foundry framework. Take a look at the following important functionalities you can access with Forge Standard Library.
The Forge Standard Library offers an updated cheatcodes interface with ‘Vm.sol’. You can use the following command to import the cheatcodes interface.
import “forge-std/Vm.sol”;
The Forge Standard Library or Forge Std also offers logging functionality like Hardhat with ‘console.sol’ and ‘console2.sol’. You can import the logging functionality by using the following commands.
import “forge-std/console.sol”;
import “forge-std/console2.sol”;
It is important to remember that ‘console2.sol’ features patches for ‘console.sol’ that help Forge Standard Library in decoding traces for all calls to the console. However, ‘console2.sol’ does not offer compatibility with Hardhat.
You can also access basic utilities for writing code in Solidity programming language with ‘Script.sol’ in the Forge Standard Library. The following command will help you import ‘Script.sol’ from the Forge Standard Library.
import “forge-std/Script.sol”;
Discovering the Test Functionality of Forge Standard Library
The primary function of the Forge Standard Library is to help you write tests for Solidity smart contracts with Foundry. You can use a Forge Standard Library guide only when it offers insights on effective ways to use resources like the ‘Test’ contract. You can rely on the ‘Test’ contract in ‘Test.sol’ to gain access to all the important functionalities required for writing tests. It is important to note that ‘Test.sol’ is the superset of DSTest and includes a cheatcode instance, Hardhat console and standard libraries.
Developers can capitalize on the test functionality of Forge Standard Library by importing ‘Test.sol’ and inheriting from ‘Test’ in the test contract. The following command can help you import ‘Test.sol’.
import “forge-std/Test.sol”;
contract ContractTest is Test { …
Once you have inherited from the ‘Test’ contract in your test contract, you can try many other functions such as,
Accessing HEVM through the ‘vm’ instance.
Logging with the Hardhat ‘console’.
Using any one of the Forge Standard libraries.
Asserting and logging with the Dappsys Test.
Are you aspiring to learn the fundamentals of the Ethereum Virtual Machine and smart contracts’ upgradability? Enroll now in the Advanced Solidity Development Course.
Explore the Important Components of Forge Standard Library
Working with the Forge Standard Library can be difficult without the knowledge of its important components. You must know that the Forge Library includes standard libraries and a cheatcodes instance ‘vm’. Developers can also access all Hardhat console functions for logging and Dappsys Test functions for asserting and logging. The Forge Standard Library also helps you access different utility functions in ‘Scripts.sol’.
The standard libraries in Forge are the most crucial resources for writing tests with better speed and ease. You can use Std Logs, Std Errors, Std Assertions, Std Match, Std Storage and Std Cheats for different functionalities. The console logging and Script Utils libraries are also other useful additions among the standard libraries by Forge. Let us take a look at the significance of the standard libraries you can find in Forge.
Std Logs is an important standard library in Forge. It has the capability to expand with the logging of new events from the DSTest library.
Std Assertions are another important highlight in a Forge Standard Library guide for testing Solidity smart contracts. The ‘Assertions’ library expands over the assertion functions found in the DSTest library. The notable examples of Std Assertions include assertTrue, assertLtDecimal, assertEq and many others.
Std Cheats are the wrappers for Forge cheatcodes to ensure better safety alongside improving the developer experience. The examples of Std cheats include skip, hoax, rewind, deal, bound and deployCode. Every addition in the Std Cheats library offers unique functionalities such as skipping forward the block timestamp by a certain number of seconds with skip. You can use the deployCode cheat for deploying a contract with the help of contract bytecode from artifacts directory.
Familiarize yourself with the complete Ethereum smart contract development lifecycle and gain fluency in the best practices for smart contract coding, testing, and deployment with Smart Contracts Development Course.
Std Errors is also another important library in Forge Std that simplifies the developer experience in the testing process. The library includes wrappers for some of the general internal errors and reverts in Solidity. The components of the Std Errors library include assertionError, divisionError, arithmeticError, encodeStorageError and many others.
You can find unique ways to leverage the Std Errors wrappers to your advantage in the testing process. The ‘assertionError’ helps in detecting internal Solidity error upon failure of an ‘assert’. Similarly, the ‘divisionError’ is visible as an internal Solidity error when you have a failed division. The other components in the Std Errors library can support proactive detection of internal errors during the testing process.
The crucial standard libraries in Forge also include Std Storage. It offers important utilities for storage manipulation. You can access query functions and terminator functions with Std Storage library. You can start using Std Storage by importing the following command in the test contract.
import {stdStorage, StdStorage} from “forge-std/Test.sol”;
Now, you must introduce the following line in the test contract.
using stdStorage for StdStorage;
You can leverage the ‘stdstore’ instance for gaining access to Std Storage.
The query functions of Std Storage in the Forge library help in setting the address of the target contract or passing an argument to the function. You can also use query functions for specifying the 4-byte selector to static call in a function. The terminator functions can help you access the slot number, reading the value from the storage slot or specify the data for the storage slot. It is also important to note that Std Storage comes with certain limitations on access to packed slots.
The simplest addition among the standard libraries in Forge Standard Library is Std Match. You can find three distinct tools in the Std Math library for important mathematical functions. The ‘abs’ function helps in obtaining the absolute value of a number while the ‘delta’ function calculates the difference in absolute value of two numbers. The ‘percentDelta’ function helps you calculate the percentage of difference between two numbers.
Script Utils offers access to different utility functions that you can use in tests and scripts. You can learn Forge Standard Library best practices to discover the importance of the two utility functions in the Script Utils library. The ‘computeCreateAddress’ function helps in computing the address on which a contract will be deployed for a specific deployer address. The ‘deriveRememberKey’ function can help in deriving a private key from a mnemonic alongside storing it in the local wallet of forge.
Console logging is also one of the crucial functions that you would come across in the Std libraries in Forge Standard Library. It is somehow similar to the console functions of Hardhat. You can use it for transactions and calls along with view functions. The console logging functions always work whether the call or transaction fails or becomes successful.
Start learning Blockchain with World’s first Blockchain Career Paths with quality resources tailored by industry experts Now!
Final Thoughts
Smart contract development is as effective as the testing process. You can use the Forge Standard Library to write tests for Solidity smart contracts with Foundry. It offers a collection of libraries, utility functions for scripts and Hardhat console functions for logging. The Forge Standard Library also offers access to all Dappsys Test functions that improve the developer experience.
Developers can use a Forge Standard Library guide to explore the functionalities of each function in the standard libraries with examples. In addition, the awareness of Forge cheatcodes can help developers simplify the development experience with the assurance of improved security. The capabilities of Foundry as a smart contract development toolkit are clearly visible in the diverse functionalities offered by the Forge Standard Library. Learn more about Foundry and the Forge Standard Library now.