Solidity Tutorial: all about Libraries

Jean Cvllr
12 min readAug 1, 2019

Table of Contents

  1. What are Libraries in Solidity?
  2. How to create a library contract?
  3. How to deploy libraries?
  4. How to use a Library in a smart contract?
  5. How to interact with libraries?
  6. Understanding functions in libraries.
  7. Events and Libraries
  8. Mappings in Library = more functionalities !
  9. Using structs inside Libraries
  10. Linking libraries with other libraries
  11. Using modifier inside libraries
  12. What you can’t do with Libraries in Solidity?
  13. Some popular existing libraries

References

1. What are Libraries in Solidity?

Libraries can be seen as implicit base contracts of the contracts that use them (Solidity doc)

A library in Solidity is a different type of smart contract that contains reusable code. Once deployed on the blockchain (only once), it is assigned a specific address and its properties / methods can be reused many times by other contracts in the Ethereum network.

They enable to develop in a more modular way. Sometimes, it is helpful to think of a library as a singleton in the EVM, a piece of code that can be called from any contract without the need to deploy it again.

Benefits

However, libraries in Solidity are not only limited to reusability. Here are some of their advantages :

  • Usability : Functions in a library can be used by many contracts. If you have many contracts that have some common code, then you can deploy that common code as a library.
  • Economical : Deploying common code as library will save gas as gas depends on the size of the contract too. Using a base contract instead of a library to split the common code won’t save gas because in Solidity, inheritance works by copying code.
  • Good add-ons : Solidity libraries can be used to add member functions to data types. For instance, think of libraries…

--

--