Solidity for Developers: Libraries in Solidity
As a programming language, Solidity shares many similarities with more common languages like Java, JavaScript, and Cpp. However, there are many features that are unique to Solidity, and libraries are one of them.
What is a library?
A library is a stripped-down version of a smart contract. Just like smart contracts, we can use library functions in other contracts. But unlike smart contracts, libraries cannot have any state variables, nor can they inherit from other contracts.
The purpose of this library is simple, these libraries live on the blockchain and contain code that can be used by other contracts.
Create a library in Solidity
Throughout this article, we will be using the Remix IDE because it makes deploying, executing, and debugging the entire contract very easy.
create a file
To create a new file, open the Remix navigation tab and click the New File button. For our purposes, we will name the file example.sol.
Create file
Define Pragmas
The first step in writing any Solidity code is to define pragmas. Solidity has changed quite a bit over the past few years, and pragmas define which version of the EVM compiler our Solidity code is valid for.
Define Pragmas
library declaration
Just like a smart contract, we declare a library with the library keyword, followed by an identifier. We're going to name our library adder
library method
Our Library will contain a function that performs the sum of all numbers in the uint array.
define function
Just like in JavaScript, functions in Solidity are declared using the function keyword. The function takes a single argument, which is an array of uints with the memory modifier.
"pure" modifier
The memory modifier tells the compiler that our variable will access the memory of the blockchain, and pure is a modifier that essentially means that this function does not read or write any state variables.
Define the return type
The last keyword returns(uint) describes the type of value returned by the function, in this case we are returning the sum of all integers in the array, so we return a variable of type uint.
final look
Here's a final glimpse of our finished library.
pragma solidity >=0.7.0 <0.9.0;
library adder{
function add(uint[] memory self) public pure returns(uint)
{
uint sum = 0;
for(uint i = 0;i<self.length;i++)
{
sum += self[i];
}
return sum;
}
}
Using libraries in smart contracts
With the library complete, we can now use it in our smart contracts. If the library exists in the same file as the contract, no imports need to be made.
Access libraries in the same file
Accessing libraries in contracts that exist in the same file requires an import statement of any kind.
To access the library, we create a simple smart contract that contains a function that calls the library's add function. Because the library and contract exist in the same file, no import statements are required.
understand the contract
A given contract has only one function, the give_sum function accepts an argument of type integer array and uses the standard library method add to generate the sum.
External is an access specifier that declares that this method function can only be called from outside the current contract.
access from a different file
To access a library from a different contract, we need to use an ' import ' statement. In Solidity, we can import urls and file paths.
pragma solidity >=0.7.0 <0.9.0;
import "./example.sol";
contract new_contract{
//function to access the library
function give_sum(uint[] memory data)external view returns(uint)
{
uint sum;
sum = adder.add(data);
return sum;
}
}
In the import statement, we import the library that exists in the same folder as the current contract, for which we use relative paths.
run smart contract
To run a smart contract, we must deploy the adder library behind the contract.
After deploying these two files, we can see that these two contracts are deployed in deployed contracts.
Now, we can test the smart contract by passing it an array of integers.
It works. This means our library has successfully executed from a different contract.
in conclusion
That basically sums up what libraries are, and how to create your own. As mentioned before, they are just a stripped-down version of smart contracts, and one of the big benefits of using libraries instead of inheriting smart contracts is that they save on gas fees.
Source:https://medium.com/coinmonks/solidity-for-developers-libraries-in-solidity-f8c7e348dc24
about
ChinaDeFi - ChinaDeFi.com is a research-driven DeFi innovation organization, and we are also a blockchain development team. Every day, from nearly 900 pieces of content from more than 500 high-quality information sources around the world, we look for content that is more in-depth and systematic, and synchronizes to the Chinese market at the fastest speed to provide decision-making auxiliary materials.
Layer 2 Daoist Friends - Welcome blockchain technology enthusiasts and research analysts who are interested in Layer 2 to contact Gavin (WeChat: chinadefi) to discuss the landing opportunities brought by Layer 2. Please pay attention to our WeChat public account "Decentralized Financial Community" .