Smart contract development is one of the key requirements for the expansion of blockchain and Web3. You may be wondering why it is necessary to learn the Forge standard library for smart contract development. How will it help you create innovative, secure, and powerful smart contracts for new use cases? The Forge standard library offers a collection of contracts that can help you write tests with the Foundry framework.
Foundry is a popular toolkit that provides access to a vast collection of tools for developing and deploying decentralized applications on the Ethereum blockchain. It was designed to meet the needs of developers with varying skill levels. Foundry’s Forge Standard Library provides the essential features you need to get started writing tests. Let’s learn more about the 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 better career prospects.
Understanding the Fundamentals of the Forge Standard Library
Foundry is one of the most popular frameworks for developing smart contracts with the Solidity programming language. It offers a command-line interface to make it easy to create, manage, and deploy smart contracts. You can use the answers to questions like “What is Forge Standard Library?” to gauge the power of Foundry as a smart contract development toolkit. Forge Standard Library offers a collection of useful contracts that ensure easier and faster test scripting with a user-friendly experience.
Foundry is better than other popular smart contract development frameworks like Remix IDE and Hardhat in offering tools like the Forge Standard Library. The built-in support for smart contract testing allows developers to verify the functionality of their smart contracts before deployment. The Forge Standard Library is the most efficient and commonly preferred resource for writing tests with the Foundry framework. Take a look at the following important features that you can access with the Forge Standard Library.
The Forge standard library provides an updated cheat code interface with “Vm.sol”. You can use the following command to import the cheat code interface.
import “forge-std/Vm.sol”;
Forge Standard library or Forge Std also provides logging functionality like Hardhat with “console.sol” and “console2.sol”. You can import the logging functionality using the following commands.
import “forge-std/console.sol”; import “forge-std/console2.sol”;
It is important to remember that console2.sol contains patches for console.sol that help the Forge standard library decode the backtraces of all console calls. However, console2.sol does not provide compatibility with Hardhat.
You can also access the basic utilities for writing code in the 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 Forge Standard Library Testing Feature
The primary function of the Forge Standard Library is to help you write tests for Solidity smart contracts with Foundry. You can only use a guide from the Forge Standard Library when it offers information on efficient ways to use resources such as the “Test” contract. You can rely on the “Test” contract in “Test.sol” to access all the important features required to write tests. It is important to note that “Test.sol” is the superset of DSTest and includes a cheatcode instance, a Hardhat console, and standard libraries.
Developers can take advantage of the testing features of the 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 inherit the “Test” contract in your test contract, you can try many other features such as,
Access HEVM via the “vm” instance.
Logging with the Hardhat “console”.
Using one of the Forge Standard libraries.
Assertion and logging with Dappsys test.
Want to learn the fundamentals of Ethereum Virtual Machine and smart contract upgrades? Enroll in the Advanced Solidity Development course now.
Learn about the important components of the Forge Standard Library
Working with the Forge standard library can be difficult without knowing its important components. You should know that the Forge library includes standard libraries and a cheatcodes instance “vm”. Developers can also access all the Hardhat console functions for logging and Dappsys test functions for assertion and logging. The Forge standard library also helps you to access different utility functions in “Scripts.sol”.
Forge standard libraries are the most crucial resources to write tests faster and easier. You can use Std Logs, Std Errors, Std Assertions, Std Match, Std Storage, and Std Cheats for different functionalities. Console Logging Libraries and Script Utils are also other useful additions among Forge standard libraries. Let’s take a look at the importance of standard libraries that you can find in Forge.
Std Logs is an important standard library in Forge. It has the ability to extend with new event logging from the DSTest library.
Standard assertions are another important point in the Forge Standard Library guide for testing Solidity smart contracts. The “Assertions” library expands on the assertion functions found in the DSTest library. Notable examples of standard assertions include assertTrue, assertLtDecimal, assertEq, and many others.
Std Cheats are wrappers for Forge cheats to provide better security while improving the developer experience. Examples of Std Cheats include skip, hoax, rewind, deal, bound, and deployCode. Each addition in the Std Cheats library provides unique features such as skipping the block timestamp by a certain number of seconds with skip. You can use the deployCode cheat to deploy a contract using the contract bytecode from the artifacts directory.
Get familiar with the complete Ethereum smart contract development lifecycle and master best practices for coding, testing, and deploying smart contracts with the Smart Contract Development course.
Std Errors is also another important library from Forge Std that simplifies the developer experience in the testing process. The library includes wrappers for some of the general internal errors and rollbacks in Solidity. The components of the Std Errors library include assertionError, divisionError, arithmeticError, encodeStorageError, and many more.
You can find unique ways to leverage Std Errors wrappers in the testing process. The assertionError is used to detect a Solidity internal error when an assertion fails. Similarly, the divisionError is visible as a Solidity internal error when you have a division that fails. The other components of the Std Errors library can support proactive detection of internal errors during the testing process.
Forge’s crucial standard libraries also include Std Storage. It provides important utilities for storage manipulation. You can access query functions and termination functions with the Std Storage library. You can start using Std Storage by importing the following command into the test contract.
import {stdStorage, StdStorage} from "forge-std/Test.sol";
Now you need to introduce the following line in the test contract.
using stdStorage for StdStorage;
You can leverage the “stdstore” instance to access Std Storage.
The Std Storage query functions in the Forge library help to set the target contract address or pass an argument to the function. You can also use query functions to specify the 4-byte selector to call statically in a function. Terminator functions can help you access the slot number, read the value of the storage slot, or specify the data of the storage slot. It is also important to note that Std Storage comes with some limitations on accessing compressed slots.
The simplest addition among the standard libraries in Forge Standard Library is Std Match. You can find three separate tools in the Std Math library for important mathematical functions. The “abs” function is used to get the absolute value of a number while the “delta” function calculates the absolute value difference of two numbers. The “percentDelta” function helps you calculate the percentage difference between two numbers.
Script Utils provides access to various utility functions that you can use in tests and scripts. You can explore the Forge Standard Library Best Practices to learn the importance of the two utility functions in the Script Utils library. The “computeCreateAddress” function is used to calculate the address a contract will be deployed to for a specific deployment address. The “deriveRememberKey” function can help derive a private key from a mnemonic while storing it in the local Forge wallet.
Console logging is also one of the crucial functions that you will encounter in the Std libraries of the Forge standard library. It is somewhat similar to the console functions of Hardhat. You can use it for transactions and calls as well as for display functions. The console logging functions always work whether the call or transaction fails or succeeds.
Start learning Blockchain with the world’s first Blockchain career paths with quality resources tailored by industry experts now!
Final Thoughts
Developing smart contracts is only as efficient 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 scripting, and Hardhat console functions for logging. The Forge Standard Library also provides access to all the Dappsys testing functions that enhance the developer experience.
Developers can use a Forge Standard Library guide to explore the functionality of each function in the standard libraries with examples. Additionally, knowledge of Forge cheat codes can help developers simplify the development experience with the assurance of improved security. Foundry’s capabilities as a smart contract development toolkit are clearly visible in the various features offered by the Forge Standard Library. Learn more about Foundry and the Forge Standard Library now.