CSC101-Libraries

By akohad Dec7,2022

[ad_1]

Libraries are similar to contract that contain reusable codes to reduce coding time , they are mainly intended for reuse.

Libraries

Same as other programming languages , solidity has library.The concept of libraries in Solidity is not different from other programming languages. The primary idea is the same library is a set of reusable utilities that you can call in your main program to help you to keeping your code clean, reusable, and easy to understand.A Library contains functions which other contracts can call.

Following are the key characteristics of a Solidity Library:

  • Functions of the library can be called directly when they do not modify the state variables
  • Library can not be destroyed as it is assumed to be stateless.
  • A Library cannot have state variables.
  • A Library cannot inherit any element.
  • A Library cannot be inherited.

Note: Deploying a common code by creating a library reduces the gas cost.

Creating Library

A library contract is defined by using the library keyword instead of a general contract. Libraries do not have any storage thus it cannot hold state variables, fallback or payable functions also cannot be created inside the library as it cannot store ethers(in EVM-Compatible network ether means network native coin),but it can implement some data types like struct and enums which are user-defined, and constant variables that are stored in a stack of Ethereum, not in storage.

first of all we need to create solidity file. in this example we call it mylib.sol. in terminal type:

touch mylib.sol

if you are using CSC IDE you can create file in your workspace:

Syntax:

library <libraryName> {
// block of code
}

Example:

pragma solidity ^0.8.0;library Adder{
function add(uint a, uint b) public pure returns(uint) {
return a + b;
}
}

Pure Modifier

The memory modifier tells the compiler that our variable is going to access the blockchain’s memory, pure is a modifier which essentially means that this function does not read, or write to any of the state variables.

Return Type

The last keyword returns describes the type of value this function returns, in our case we are returning the sum of all integers of an array so we return a variable of type uint.

Yay! we did it successfully.!

now it’s time to import and using our library

Importing Library

importing a library is very simple. it just need to use import keyword to import a library in solidity. importing a library can be done as following:

import "./MyLib.sol";

Accessing The Library in The Same File

Accessing a library in a contract, which exists in the same file requires so import statement of any sort.

To access the library, we have created a simple smart contract which contains a function that calls our library’s add function. Because the library and the contract exist in the same file, there’s no need for any import statement.

Using library functions in other smart contract

now we want to access a library function from other contract.

import "./example.sol";

contract MyContract{

function give_sum(uint[] memory data)external view returns(uint)
{
uint sum;
sum = adder.add(data);
return sum;
}}

New to trading? Try crypto trading bots or copy trading

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

[ad_2]

Source link

By akohad

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *