James Bachini

Solidity Libraries | Solidity Tips & Examples

Solidity Library

A solidity library is a reusable contract that contains functions that can be called by other contracts. When you import a Solidity library into your contract, you can use the functions provided by the library.

  1. How To Import Solidity Libraries
  2. Popular Solidity Libraries
  3. Create Your Own Solidity Library
  4. Conclusion

How To Import Solidity Libraries

In your smart contract, you can import the library by using the import keyword followed by the file name of the library. Note that you either need to use a locally hosted library file or a external library like OpenZeppelin.

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {
    constructor() ERC20("MyToken", "MYTKN") {
        _mint(msg.sender, 1000000 ether);
    }
}

The code above imports an ERC20 library and uses this to create MyToken.

If you have errors when compiling with library not found, try installing the OpenZeppelin libraries from the command line

npm install @openzeppelin/contracts

OpenZeppelin – My favourite collection of Solidity templates and utilities.
https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts

Third Web – A collection of libraries for tokens, staking, drops etc.
https://github.com/thirdweb-dev/contracts/tree/main/contracts


Create Your Own Solidity Library

Here is an example of how you can create your own Solidity library. Let’s first create the library in a file called TestLibrary.sol

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

library TestLibrary {
    function subtract(uint a, uint b) internal pure returns (uint) {
        return a - b;
    }
}

Then we can create a TestContract.sol file and import the library. Note that there is an example of this code importing into the same file in the Solidity Snippets Github Repo

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import "./TestLibrary.sol";

contract MyContract {
    function func(uint a, uint b) public pure returns (uint) {
        return TestLibrary.subtract(a, b);
    }
}

In the above example we are importing the library we just created and using the TestLibrary.subtract(a, b) function from the new library. Note that the internal visibility modifier is used in the library function to ensure that it can only be called by contracts that inherit from the library. This is because library functions cannot be called by external accounts or contracts.


Conclusion

Solidity libraries provide an effective way to write modular and expandable code in smart contract development. By creating reusable libraries for specific functions, developers can avoid duplication of code and reduce the risk of errors, resulting in more efficient and secure code.

The use of libraries can also promote collaboration between developers, as multiple contracts can rely on a single library to perform a specific task. By embracing the use of Solidity libraries, developers can build more robust and scalable decentralized applications that meet the needs of their users.



Get The Blockchain Sector Newsletter, binge the YouTube channel and connect with me on Twitter

The Blockchain Sector newsletter goes out a few times a month when there is breaking news or interesting developments to discuss. All the content I produce is free, if you’d like to help please share this content on social media.

Thank you.

James Bachini

Disclaimer: Not a financial advisor, not financial advice. The content I create is to document my journey and for educational and entertainment purposes only. It is not under any circumstances investment advice. I am not an investment or trading professional and am learning myself while still making plenty of mistakes along the way. Any code published is experimental and not production ready to be used for financial transactions. Do your own research and do not play with funds you do not want to lose.