Libraries and Using for in Solidity
solidity is an object-oriented, high-level language for implementing smart contracts. Smart contracts are programs which govern the behavior of accounts within the Ethereum state. In this article, we will look at two of Solidity features which are Libraries
and Using for
, by covering the following topics:
- Libraries
- Using for
- Solidity libraries and Solidity using for example on Remix.
Solidity Libraries
Solidity Libraries are similar to contracts, But their purpose is that they are deployed only once at a specific address with its code being reused by various contracts. If library functions are called, their code is executed in the context of the calling contract. As a library is an isolated piece of source code, it can only access state variables of calling contract if they are explicitly supplied
Libraries can’t have state variables; they don’t support inheritance and they can’t receive Ether. However, they can contain structs and enums.
Solidity library can be used by anyone once it has been deployed to the blockchain, assuming you know its address and have the source code( with only prototypes or complete implementation). The solidity compiler needs the source code so that it can make sure that the methods you are trying to access actually exist in the library
Solidity library has the following format
library libraryName
{// struct and or enum declaration// function definition with body
}
libraryName
can be changed to your own favorite name. The library body can contain struct and or enum declaration, and also, functions which will perform the various operation in the library.
Solidity Libraries Use cases/ Advantages
Solidity libraries have the following advantages/ use cases
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.
Solidity libraries are referred to as a base contract of the contract that uses it because its functions with the internal visibility, and all its structs and enums are copied to the contract that uses it. Also, if a library contains only internal functions and/or structs/enums, then the library doesn’t need to be deployed, as everything that’s there in the library is copied to the contract that uses it.
Solidity libraries example code
Copy the code below in a file named libraryF.sol
- We start by mentioning the compiler version for which the code is written for using the
pragma Solidity
directive - We define our library using the
library
keyword and we give it the namecount
- Our library has a struct and two functions.
Notice how the function
subUint
receives a struct as argument. In Solidity v0.4.24, this is not possible in contracts, but possible in Solidity libraries
SafeMath library available in open zeppelin smart contracts collection is a popular solidity library that is used to protect from overflow.
Solidity using for
Solidity libraries can be used alongside Solidity using for
. The using A for B;
directive means we attach library functions( from the library A to any type B). When this happens, these functions will receive the object they are called on as their first parameter, much like the variable self
in Python.
The effect of
using A for *;
is that the functions from the library A are attached to all types.
Either way, every single function, including those in which the first parameter’s type would not match the object’s type, is attached. As the function is called, the type is checked, in addition to performing function overload resolution.
For example: Using the libraryF.sol
above. We can use the directive using for libraryF for type uint as below;
import {count} from './libraryF.sol';using count for uint;uint a = 10;
uint b= 10;uint c = a.subUint(b);
Note: We could still do uint c = a - b;
It will return the same result which is 0
. However our library has some added properties to protect from overflow for example; assert(a >= b);
which checks to make sure the first operand a
is greater than or equal to the second operand b
so that the subtraction operation doesn’t result to a negative value.
Solidity libraries and Solidity Using for example on Remix.
We learned about Solidity libraries
and Solidity using for
. Now its time to put all together. We will be using Remix which is a browser-based compiler and IDE that enables users to build Ethereum contracts with Solidity language and to debug transactions.
Set up Remix
Open Remix on your browser and do the following settings;
Choose a compiler( In this case version:0.4.24)
Create two files called libraryF.sol
and article_tuts.sol
.To create a file, you should click the plus icon at your left-top as shown in the diagram below. The files will be located and easily accessible from the left panel under browser
Code your Solidity functions
Copy the lines of code that was provided above under Solidity libraries example code in to the file libraryF.sol
Copy the line of code below in to the file article_tuts.sol
There are some few things we should know about the above code
- We have imported our library count from the file
libraryF.sol
- We used
using for
to link our library count to astruct
and theuint
type. - The first function
add
will call our library functionaddUint
which will addb
toa
. Notice here that we use the formata.addUint(b);
meaninga
is passed automatically as first argument to the library function. - The second function
subHold
does same as the first function. Since this library function accepts a struct object as first argument, this is why we usedhh[0].subUint(a);
wherehh[0]
is the struct object that is automatically passed as first argument.
Deploy and Run
Choose the environment JavaScrip VM
Deploy the contract Math
Perform the various operations( add and subHold)
Thank you
Good job!! 🙌
Thanks for reading and I hope this article helped you to understand using for
and libraries
in solidity.
Happy Coding 😇