All you should know about libraries in solidity

It’s very important to know about the libraries in solidity while writing Dapps. In simple words, a library is the reusable piece of code which is deployed once and shared many times.

But libraries are not just limited to reusability, there are few other areas where ethereum developers are using the library feature. This post aims to touch all those topics starting from basics.

Let’s start with a simple library for mathematical operation. The SafeMath library described below contains basic arithmetic operation which takes 2 unsigned integer as input and returns the arithmetic operation result.

library SafeMath {

    function mul(uint256 num1, uint256 num2) internal pure returns (uint256) {
        uint256 result = num1 * num2;
        return result;
    }


    function div(uint256 num1, uint256 num2) internal pure returns (uint256) {
        uint256 result = num1 / num2;
        return result;
    }


    function sub(uint256 num1, uint256 num2) internal pure returns (uint256) {
        uint256 result = num1 - num2;
        return result;
    }


    function add(uint256 num1, uint256 num2) internal pure returns (uint256) {
        uint256 result = num1 + num2;
        return result;
    }
}

“Ideally, libraries are not meant to change state of contract, it should only be used to perform simple operations based on input and returns result”

Before going in depth, let’s cover few prerequisite for better understanding:


Function types

: Solidity has following function types:

How library works?

In blockchain, transaction can change state of smart contract. There are various kinds of state change that can happen in a contract:

Delegate Call:

Paraphrased from the solidity docs :

“Delegatecall is identical to a message call apart from the fact that the code at the target address is executed in the context of the calling contract and msg.sender and msg.value do not change their values.

This means that a contract can dynamically load code from a different address at runtime. Storage, current address and balance still refer to the calling contract, only the code is taken from the called address.”

This low-level function has been very useful as it’s the backbone for implementing libraries.

Deployment of libraries:

Library deployment is a bit different from regular smart contract deployment. There are two scenarios in the library deployment:

pragma solidity ^0.4.23;

import "./SafeMath.sol";

contract ERC20 {

    using SafeMath for uint256;
    mapping(address => uint256) balances;

    function transfer(address _to, uint256 _value) public returns (bool success) {
        balances[msg.sender] = balances[msg.sender].sub(_value);
        balances[_to] = balances[_to].add(_value);
        return true;
    }
}

library SafeMath {

    function mul(uint256 a, uint256 b) external pure returns (uint256) {
        uint256 c = a * b;
        assert(a == 0 || c / a == b);
        return c;
    }


    function div(uint256 a, uint256 b) external pure returns (uint256) {
        uint256 c = a / b;
        return c;
    }


    function sub(uint256 a, uint256 b) external pure returns (uint256) {
        assert(b <= a);
        return a - b;
    }


    function add(uint256 a, uint256 b) external pure returns (uint256) {
        uint256 c = a + b;
        assert(c >= a);
        return c;
    }
}

There are two interesting observations to make.

How to link library contract during deployment?

solc ERC20.sol — libraries “SafeMath:0x40189fb71f54a3ad0370620dfb095382859eb095” — bin

After successful linking, SafeMath references will be removed from the bytecode and contract is ready for deployment.

‘Using for’ in library:

In solidity using X for Y directive means, library function of X is attached with type Y. For instance, Using SafeMath for uint256 . SafeMath functions like add, sub, mul and div are now bound with type uint256. “One condition which should be taken care is, library functions will receive the object they are called on as their first parameter”

For example: In ERC-20 contract mentioned above. Using for directive is using for SafeMath for type uint256.

using SafeMath for uint256;

uint256 a = 10;
uint256 b= 10;

uint256 = a.add(b);

Here add function is available from SafeMath. SafeMath library functions are bound with uint256. Great!!! 🙌 I hope, this article helped you to understand libraries in solidity. Happy Coding 😇

linkedin.com/in/jainsarvesh

medium.com/@sarvesh.sgsits