Loading...
pragma solidity ^0.8.3;
import"https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol";
contract MyContract{
//1st Method of calling solidity library function
using SafeMath for uint256;
function addition(uint256 a, uint256 b) public payable returns (bool,uint256)
{ return a.tryAdd(b);}
//2nd Method of calling solidity library function
function multiply(uint256 a, uint256 b) public payable returns (bool,uint256)
{ return SafeMath.tryMul(a,b);}
}
Here, in this Contract example we have imported openzeppelin SafeMath Library.
In solidity contracts the libraries contain internal functions that means they can be called only within the contract inside a function.
Here also we will see 2 methods of calling library functions
In 1st method we can see that we are passing only one argument inside "tryAdd" library function & the next argument we put at the begining followed by "." .Here solidity by default recognises the library function as we have written "using SafeMath for uint256;".
In 2nd method we can see that we are passing two arguments inside unlike the 1st one since we have written library name "SafeMath" followed by "." . Here no need to write "using SafeMath for uint256;"
For many more exciting tutorials keep following my username @BlockTalks_Raj
!!!!!HAPPY LEARNING!!!!!