2

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 internal
  • Linked 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);
}

2 Answers 2

1

In answer to 2., this article explains how to link to a library (that is already deployed) in Remix IDE.

In summary, you do not add the deployed library address within your contract code. Instead, you edit the json file (that is produced when your contract is compiled) to point to the deployed library address.

0
  1. So how does contract X work, when I did not deploy the reverseMath library beforehand?

Not possible, and you do not deploy that library manually but Remix does it for you automatically

2.how do you link Smart Contracts to Deployed Libraries?

This is not done in the solidity code itself, instead it must be linked outside in your javascript files, for example:

const contractFactory = await this.env.ethers.getContractFactory("Example", {
  libraries: {
    ExampleLib: "0x...",
  },
});

see https://hardhat.org/plugins/nomiclabs-hardhat-ethers.html for more details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.