3

I recently became aware of linked vs embedded libraries. My understanding is that if your library uses public pure functions and/or manages storage, then you need to deploy and link the library to your contract that uses that library. Compared to embedded libraries who's functions are all internal and pure, apparently you don't need to worry about deploying or linking as the library's bytecode is embedded in the bytecode of the main contract.

Is that correct?

How can I embed a library? I made a contract and uses a library that I also created, initially the function in the library that I call from my main contract was marked as public. my migrations looks like this (using truffle):

const mainContract= artifacts.require("mainContract");
const library= artifacts.require("library");

module.exports = function (deployer) {
  deployer.deploy(library);
  deployer.link(library, mainContract);
  deployer.deploy(mainContract);
};

that works. but I wanted to try to embed the library. so I made every function internal pure, however now I'm not able to deploy the main contract.

I always get "mainContract" ran out of gas. Something in the constructor (ex: infinite loop) caused gas estimation to fail.

I tried:

const mainContract= artifacts.require("mainContract");
const library= artifacts.require("library");

module.exports = function (deployer) {
  deployer.deploy(library);
  deployer.deploy(mainContract);
};

and

const mainContract= artifacts.require("mainContract");
const library= artifacts.require("library");

module.exports = function (deployer) {
  deployer.deploy(mainContract);
};


also tried truffle migrate and truffle migrate --reset

but nothing makes a difference.

my contracts use OpenZeppelin libraries as well, which I assume are automatically embedded somehow, how can I do that with libraries that I create?

Thanks!!

0

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.