3

I have a contract fooContract which is using a fooLib library, calling fooLib.foo(LibStruct storage param)

The transaction cost to create fooContract seems to depend on the code in the fooLib.foo() function despite the params are unchanged.

Ie. using solidity browser fooContract create costs 3,185,059. if I comment out the code in fooLib.foo(), the fooContract create cost goes down to 2,816,000 .

As far as I understood libs are a way to move out functionality for contracts too large for block gas limit.

Am I missing a point here?

  • This link could help you ethereum.stackexchange.com/a/2760/6237 – vincentLg Jun 7 '17 at 5:58
  • Thanks for that, it's indeed useful. Although I'm not talking about the cost of the transaction to call fooLib.foo() but about the contract creation cost of fooContract. – szerte Jun 7 '17 at 8:52
  • Gas cost at creation depends on what happens in the constructor and the size of the compiled code, so what you describe seems normal. – vincentLg Jun 7 '17 at 9:09
0

I suspect not using a library the way it's intended to achieve gas costs in your contract. I assume what you're doing is something like:

library fooLib {
    // foo code
}

contract fooContract is fooLib {
    // foo code
}

In which case when you create fooContract you have to pay the compilation costs for fooLib. What you want to do is deploy fooLib on it's own, specify how to interact with the library via an interface, and specify the library address to link to with the solc compiler during compilation of fooContract. By doing this you would avoid having to pay the compilation cost of fooLib when deploying fooContract and instead just pay the transaction fees when utilizing the library.

The following stackexchange post has a solid detailed description of this process:

A very good aragon article on this:

0

The internal functions in libraries are pulled into the calling contract at compile time and a regular JUMP call is used instead of a DELEGATECALL. From Solidity docs: http://solidity.readthedocs.io/en/v0.4.21/contracts.html#libraries

  • which is more expensive, jump or delegate call? will they both scale linearly? for example if I have 5 jumps the delegate call cost = 5 * delegate_call_jump_cost? – valem Nov 23 '18 at 4:43

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.