I've been trying to find a way to reference an already deployed library via address IN Solidity code, and couldn't find one, nor could find any explanation on whether this is even possible.

For example think of this scenario:

  1. Alice deploys LibraryAlice at address X. The library has a function called run()
  2. Bob writes a contract that makes use of LibraryAlice, but instead of re-writing the code Bob simply points to the address X.

Basically Bob wants to do:

LibraryAlice lib = LibraryAlice(<addressX>)

How do I achieve this? It feels like an obvious feature that everybody could use to save gas, for example you could just call libraries like SafeMath by inline-instantiating them directly in the solidity code.

All the examples I see keep reusing the same library code and I am confused.


1 Answer 1


First you should write an interface for LibraryAlice

intarface ILibraryAlice {
    function run() external;

then in Bob code instantiate LibraryAlice

ILibraryAlice lib = ILibraryAlice(<addressX>);
  • Been looking for this.
    – enchance
    Commented Apr 10, 2022 at 13:58

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.