0

Is it possible to use a library within a library?

library L2 {
  //...
}

library L1 {
  using L2 for *;
  //...
}

This compiles and runs just fine but what I really want to know is if L1 is referencing an already-deployed L2 or if its bytecode is redeployed as a part of L1. I ask because I'm able to deploy L1 without deploying L2. Furthermore when I look at the bytecode for L1 theres no __L2____________________________________ for the address of L2.

1

It should be that L1 only use functions from L2 that are "inlined".

For example OpenZeppelin's SafeMath functions are internal and they are "inlined", and you do not have to link to SafeMath.


When I say "inlined" it means the code of the function is copied from the library into the generated by the compiler.

Let's say your contract uses add from SafeMath.

uint a = balance.add(msg.value).

When the compiler inline a function it is like it copies the code from the library in your contract.

function __add__(uint r, uint s) private returns (uint) { /* code from SafeMath */ }

uint a = __add__(balance, msg.value);
  • What do you mean by "inlined"? – Jonah Feb 5 at 3:49
  • If I deploy L2 then deploy L1, will L2's bytecode be on the blockchain twice? – Jonah Feb 5 at 3:54
  • @Jonah See the edited answer I try to explain "inlined" (It is a concept I borrow from C++ see here for more details en.wikipedia.org/wiki/Inline_function). How much the code is deployed depends on the function access, external functions are not inlined. – Ismael Feb 5 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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