Introduction
Are you a Solidity developer looking to make your smart contract development process smoother and more efficient? Then boy, do we have the perfect solution for you! Libraries in Solidity are like the awesome sidekick you never knew you needed – they provide powerful tools to simplify and turbocharge your development process. This article will be your guide to getting to know your new best friend, libraries in Solidity. We’ll cover the basics, explain their advantages, and show you how to use them like a pro. So, what are you waiting for? Let’s get started!
What are Libraries?
Libraries in Solidity are collections of functions that can be used in a contract or in other libraries. Libraries are useful for encapsulating shared code and for promoting code reuse.
Just like smart contracts, you can use a library’s functions in other contracts. But unlike smart contracts, libraries cannot have any state variables, nor can they inherit other contracts.
The purpose of the library is simple, the libraries sit on the blockchain and contain code that can be used by other contracts.
How to create a library in Solidity
Now that we have an idea what libraries are, let’s see how to create one.
To create a library in Solidity, you can define a contract with the library keyword instead of the contract keyword. Here is an example of a simple library that provides a function to calculate the factorial of a number:
Now once I deploy this library, any contract, even in 2050, that will be deployed on evm now onwards can use this library or call the library functions. Library just know one thing: I have been given the task of calculating the factorial of the number passed as a parameter and I will do my job. Doesn’t matter who is the owner or who is calling.
See, not rocket science. But very very crucial when you want to make sure that your solidity code is well structured. Especially when you are dealing with larger codebases and trust me, managing large codebases in solidity is still “not” a solved problem. Hence, libraries are your best friends, I said.
How to use a library in solidity
To use a library in a contract, you need to import it first. Then you can use the using directive to make its functions available to the contract. Here is an example of a contract that uses the Factorial library created above to calculate the factorial of a number and store the result in a contract state variable:
So the syntax is
Using {Library} for {type}.
Example: Using SafeMath for Uint, using Factorial for uint. And so on.
But wait, factorial is declared with one parameter but if you observe, its called on uint n without any parameter? Strange? Well:
For libraries: the member functions receive the object they are called on as their first parameter (like the self variable in Python). If you pass any params while calling the function, it will be treated as a second parameter in the definition of the function. The operator functions receive operands as parameters.
How to deploy Libraries
Library deployment is a bit different from regular smart contract deployment. The library deployment includes two possible situations:
Embedded Library: If a smart contract uses a library with only internal functions, EVM simply adds the library to the contract. Instead of calling a function with a delegate call, it just uses a JUMP statement, which is a normal method call. In this case, you don’t need to deploy a library on its own.
Linked Library: On the other hand, a library needs to be deployed if it has functions that are available to the public or to people outside the library. The deployment of a library will create a unique address in the blockchain. The calling contract needs to be linked to this address.
Differences between using a library and contract inheritance
In Solidity, you can reuse code in your contracts by using libraries or contract inheritance. But there are some important differences between the two:
Libraries are collections of functions that can be called from contracts or from other libraries. When a contract calls a library function, the code for the library function is run in the context of the contract that called it. This is done with “delegateCall.” This means that the library has no storage and can’t change the state of the calling contract or access its storage directly.
Contract inheritance, on the other hand, means making a new contract that “inherits” the code and storage variables of an existing contract, called the “base” contract. When you make a contract that is based on another contract, the code of the base contract is copied into the new contract, and the new contract has access to all of the storage variables and functions of the base contract.
Popular libraries
Here are some popular libraries available in Solidity:
SafeMath: A library for performing arithmetic operations in a way that minimizes the risk of integer overflow or underflow errors.
Strings: String & slice utility library for Solidity contracts.
MerkleProof: A library for dealing with functions related to Merkle Tree proofs.
Libraries in Solidity are a great way to make your smart contract development process much more efficient. They provide powerful tools to simplify and accelerate your workflow, and this article has given you the basics to get you started. So, what are you waiting for? Give libraries in Solidity a try, and you’ll be well on your way to becoming a Solidity development pro. Follow on my socials for more such informative content, and let’s grow together, stronger!