[ad_1]
The Library
Let’s create a file called MyLibrary.sol
//SPDX-License-Identifer: MITpragma solidity ^0.8.22;
library MyLibrary {
function addOne(uint256 _myNumber) internal returns (uint256) {
return _myNumber + 1;
}
}
Noteworthy here is the library
keyword, where for a contract creation, we would use contract
instead. Also, note that we declare all functions with internal
, as the Solidity docs on libraries state, “…A library is embedded into the contract if all library functions are internal. Otherwise, the library must be deployed and then linked before the contract is deployed.”
Read more on the topic here. Now let’s move on to
The Contract
Let’s now use MyLibrary
inside a new file called MyContract.sol
//SPDX-License-Identifier: MITpragma solidity ^0.8.22;
import { MyLibrary } from "./MyLibrary.sol";
contract MyContract {
using MyLibrary for uint256;
uint256 myNumber = 42;
function getMyNumber() public view returns (uint256) {
return myNumber;
}
function getMyNumberPlusOne() public view returns (uint256) {
return myNumber.plusOne();
}
}
Here is the neat part: using MyLibrary for uint256
tells the compiler to “inject” our functions from MyLibrary
for all variables of type uint256
! And because myNumber
is of type uint256
, we now can return myNumber.plusOne()
! Note that myNumber
will be passed in as the first argument to function plusOne()
. So at execution time, think of it as getting called like this: plusOne(myNumber)
But what if our Library function takes in more than one argument? Let’s extend MyLibrary
with another function:
//SPDX-License-Identifer: MITpragma solidity ^0.8.22;
library MyLibrary {
function addOne(uint256 _myNumber) internal pure returns (uint256) {
return _myNumber + 1;
}
function addTwoNumbers(uint256 _firstNumber, uint256 _secondNumber) internal pure returns (uint256) {
return _firstNumber + _secondNumber;
}
}
And in MyContract
we add function addSomethingToMyNumber
:
//SPDX-License-Identifier: MITpragma solidity ^0.8.22;
import { MyLibrary } from "./MyLibrary.sol";
contract MyContract {
using MyLibrary for uint256;
uint256 myNumber = 42;
function getMyNumber() public view returns (uint256) {
return myNumber;
}
function getMyNumberPlusOne() public view returns (uint256) {
return myNumber.plusOne();
}
function addSomethingToMyNumber(uint256 _addThisNumberToMyNumber) public view returns (uint256) {
return myNumber.addTwoNumbers(_addThisNumberToMyNumber);
}
}
We can now call myNumber.addTwoNumbers(_addThisNumberToMyNumber);
Here, myNumber
will still be passed as the first argument to addTwoNumbers()
and _addThisNumberToMyNumber
would be the second argument. So at execution time, imagine it being called as addTwoNumbers(myNumber, _addThisNumberToMyNumber);
I highly recommend you go ahead and deploy MyContract
f.e. onto the Remix VM via Remix IDE and execute getMyNumber
and addSomethingToMyNumber
to verify that MyLibrary
works correctly:
Let me know in the comments if you like this short guide and if you want to see more like this. Also, let me know if you tried it and what your experience was.
Unlimited thanks go out to Patrick Collins for sharing his Blockchain development course for FREE on YouTube, where I took inspiration for this article.
[ad_2]
Source link