Back to homepage

# Libraries in Solidity: how to create, use them, and most popular

Published

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.

## How to create a library

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:

``````library Factorial {
function factorial(uint n) public pure returns (uint) {
uint result = 1;
for (uint i = 1; i <= n; i++) {
result *= i;
}
return result;
}
}
``````

## How to use a library

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:

``````import "./Factorial.sol";

contract FactorialCalculator {
using Factorial for *;

uint public factorial;

constructor(uint n) public {
factorial = Factorial.factorial(n);
}
}
``````

## Differences between using a library or contract inheritance

In Solidity, you can use libraries or contract inheritance to reuse code in your contracts. However, there are some key differences between the two approaches:

• Libraries are collections of functions that can be called from a contract or from other libraries. When you call a library function from a contract, the code for the library function is executed in the context of the calling contract. This means that the library has no storage, and it cannot modify the state of the calling contract or access its storage directly.

• Contract inheritance on the other hand, involves creating a new contract that "inherits" the code and storage variables of an existing contract, known as the "base" contract. When you create a contract that inherits from 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.

Here are some key points to consider when deciding whether to use libraries or contract inheritance in your Solidity code:

• Use libraries when you want to provide utility functions that can be called from multiple contracts, but do not need to modify the state of the calling contract or access its storage.

• Use contract inheritance when you want to create a new contract that extends the functionality of an existing contract and has access to its storage variables and functions.

• Be aware that using contract inheritance can increase the size of your contract code, as the code of the base contract is copied into the derived contract. This can have implications for gas costs and contract deployment.

• Consider using a combination of both libraries and contract inheritance to create a modular and scalable contract design. For example, you could use a library to provide utility functions that are shared by multiple contracts, and use contract inheritance to create specific implementations of those functions that can access contract-specific storage.

Here are some popular libraries available in Solidity:

TAGS

If you enjoyed this article consider sharing it on social media or buying me a coffee ✌️