Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Solidity Tutorials- Libraries in solidity
1. + 9 1 9 5 0 1 7 0 7 7 4 1 T U T O R I A L S D I A R Y
S O L I D I T Y
L I B R A R I E S
2. + 9 1 9 5 0 1 7 0 7 7 4 1 T U T O R I A L S D I A R Y
confidential @Tutorials Diary | www.tutorialsdiary.com
What is Library ?
A library is a different type of contract, that doesn't have any storage and cannot hold ether. This is
helpful when we want to re-use some piece of code.
This does not have payable and fallback function so libraries do not hold ether and do not have any
storage.
Library function deployed only at one specific address and these functions are called in context of
calling contract.
3. + 9 1 9 5 0 1 7 0 7 7 4 1 T U T O R I A L S D I A R Y
confidential @Tutorials Diary | www.tutorialsdiary.com
What is Library ?
pragma solidity ^ 0.4.20;
library math{
function add(uint _fValue, uint _sValue) public returns(uint){
return _fValue + _sValue;
}
}
contract salary{
function salarywithBouns(uint _salary, uint _bonus) public returns(uint){
return math.add(_salary,_bonus);
}
}
4. + 9 1 9 5 0 1 7 0 7 7 4 1 T U T O R I A L S D I A R Y
confidential @Tutorials Diary | www.tutorialsdiary.com
Using for Keyword
We can attach library function to any data type by using "using" and "for" keywords.
using A for B; pragma solidity ^ 0.4.20;
library math{
function add(uint _fValue, uint _sValue) public returns(uint){
return _fValue + _sValue;
}
}
contract salary{
using math for uint;
function salarywithBouns(uint _salary, uint _bonus) public
returns(uint){
return _salary.add(_bonus);
}
}