I'm trying to understand the differences between Linked Libraries
and Embedded Libraries
.
I already read some articles and watched these tutorials:
From what I can tell, the difference is:
Embedded Library
: is added to the bytecode of the smart contract you are deploying.Linked Library
: needs to be deployed first. Then, several smart contracts can be linked to it.
Also, what determines whether a library is Linked
or Embedded
is the visibility of its functions:
Embedded Libraries
: All the Library functions are internalLinked Libraries
: The Library contains at least one public or external function
So I tested this code in Remix:
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.6;
library reverseMath {
function add(uint256 _a, uint256 _b) public pure returns(uint256 fakeSum) {
return _a - _b;
}
function sub(uint256 _a, uint256 _b) external pure returns(uint256 fakeDifference) {
return _a + _b;
}
function mul(uint256 _a, uint256 _b) internal pure returns(uint256 fakeProduct) {
return _a / _b;
}
function _div(uint256 _a, uint256 _b) private pure returns(uint256 _fakeQuotient) {
return _a * _b;
}
function div(uint256 _a, uint256 _b) internal pure returns(uint256 fakeQuotient) {
return _div(_a, _b);
}
}
contract X {
using reverseMath for uint256;
function foo1(uint256 _x, uint256 _y) external pure returns (uint256) {
return _x.add(_y);
}
function foo2(uint256 _x, uint256 _y) external pure returns (uint256) {
return _x.sub(_y);
}
function foo3(uint256 _x, uint256 _y) external pure returns (uint256) {
return _x.mul(_y);
}
function foo4(uint256 _x, uint256 _y) external pure returns (uint256) {
return _x.div(_y);
}
}
And it works.
But, since reverseMath
has non-internal functions, it's a Linked Library
, right?
And Linked Libraries
need to be deployed beforehand, right?
1. So how does contract X
work, when I did not deploy the reverseMath
library beforehand?
2. Also, how do you link Smart Contracts to Deployed Libraries?
I tried this, but it's not working:
pragma solidity 0.8.6;
library Library1 {
}
contract Library1User {
Library1 l1 = Library1(0x59c8965945B08a7D3EbC7ed7de7FDe528c1D6cB9);
}