2

Suppose I have a contract that uses a library, which has already been deployed to the blockchain at a certain address. Is there a way to reference that deployed library in the contract similar to referencing deployed contracts via interfaces?

Say I have the following code in solidity:

pragma solidity ^0.7.6;
import "./Library.sol";
contract A {
    using Library for uint256;
    constructor (){
        uint256 a = 1;
        uint256 b = a.increment();
    }
}

and a library in the same directory with the filename "Library.sol" that contains the following lines of code:

pragma solidity ^0.7.6;
library Library {
    function increment(uint256 x) {
        return x + 1;
    }
}

Is there a way to reference the library of type Library that was deployed at a particular address?

2 Answers 2

2

I think you could use web3 to link the contract to be deployed to the library that is already deployed, as the following StackExchange answer suggests.

code:

const ConvertLib = artifacts.require("ConvertLib");
const MetaCoin = artifacts.require("MetaCoin");

module.exports = function(deployer) {
  ConvertLib.address = "0xabce987676...";
  deployer.link(ConvertLib, MetaCoin);
  deployer.deploy(MetaCoin);
};
3

When you compile a contract that calls external library functions, the compiler embeds the address of the library (or a placeholder for such an address) in its bytecode. It works this way no matter if you compile the contract and the library together. Linking to an already deployed library is actually the usual situation that any tool has to handle.

If you compile them together, the tool you are using probably hides it from you and streamlines the process by deploying the library automatically and then recompiling (or at least relinking) the contract. The tool likely provides an option to supply your own address of an already deployed library. I don't know which tool you are using but the compiler has a command-line option called --libraries that lets you supply library addresses. See Library Linking.

3
  • awesome! Thanks @cameel! I was always wondering where in the solidity code I can add the address of the library so that the generated bytecode replaces the part with the dollar signs with the actual address.
    – Marlo
    Sep 2, 2021 at 16:30
  • But isn't it strange that there is no solidity code that links the library to the interface in the same way that you'd link an interface or another contract?
    – Marlo
    Sep 2, 2021 at 16:32
  • 1
    Not sure if I understand the question. Are you asking why it's not possible to just take a library address and cast it to a library type, e.g. L(_address) just like you can do I(_address) or C(_address)? It's just a design decision in the language. It's technically possible but libraries were meant as a way to split your contract into smaller components. They have no state so there should be no need to have more than one instance.
    – cameel
    Sep 3, 2021 at 17:52

Your Answer

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

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