14

Is it possible to access a library constant in a contract that imports the library?

I am using solidity 0.4.11

For instance:

library Lib {

    bytes1 constant flag = 0x01
    ...
}

separate file

import Lib    
contract A {
    bytes1 x = (I have tried Lib.flag, and flag, neither works)
}

Trying to use Lib.flag, I get Member "flag" not found or not visible after argument-dependent lookup in type(library Lib).

Trying to use flag, I get Undeclared identifier.

Jake Gillberg
  • 141
  • 1
  • 5

4 Answers4

5

Since Solidity 0.6.2 it's possible to access it directly as <LibraryName>.<constant>:

// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.6.2;

library Lib { bytes1 constant flag = 0x01; }

contract A { bytes1 public x = Lib.flag; }

I believe the change that introduced this is listed on the 0.6.2 changelog as "Allow accessing external functions via contract and interface names to obtain their selector."

You can test on Remix this code and once you change the contract version to 0.6.1 it won't compile.

I'm adding this answer because it ended up as a top result for my search for "accessing library variables", if it ends up being the same for others it's worth keeping everybody updated.

user8977154
  • 206
  • 2
  • 1
  • the Lib library constant obviously embbed in contract A.. So the Lib is an embbed Library? – frank May 24 '22 at 09:18
5

Try

pragma solidity ^0.4.7;

library lib {

    bytes1 public constant  flag = 0x01;

    function g() constant returns (bytes1){
    return flag;
}

}



contract test {


     bytes1 x = lib.g();


}
Badr Bellaj
  • 18,780
  • 4
  • 58
  • 75
  • 10
    Sure, I understand I could create a getter function, but that doesn't really answer my question. It seems like library constants should be accessible in the contract since internal functions are accessible. Is this not the case? – Jake Gillberg May 11 '17 at 16:19
  • 3
    I have the same question any development? – Sig Touri May 13 '17 at 12:14
2

Solidity v0.5

pragma solidity >=0.5.0 <0.6.0;

library MyLib { bytes1 public constant AWESOME_FLAG = 0x01; }

contract MyContract { function getFlag() public returns (bytes1) { return MyLib.AWESOME_FLAG; } }

The answer is no, you cannot do that. The following error is thrown:

TypeError: Member "AWESOME_FLAG" not found or not visible after argument-dependent lookup in type(library MyLib).

Solidity v0.6.2 and Above

Defining constants in libraries is possible since Solidity v0.6.2. The code above will compile just fine, thanks to this change made to the compiler:

Allow accessing external functions via contract and interface names to obtain their selector.

Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143
  • Any alternative. I'm having problems with contracts reaching the max.size limit. Trying to move contstants to an aux.library just doesn't work, and calling a public function does not decrease the contract size at all. :( – earizon May 14 '19 at 14:25
2

you can store a constant value in a library and then add a function that returns the constant, and then call that function inside your regular contract.

pragma solidity ^0.5.0;

library L{
    address public constant t = 0x2020202020202020202020202020202020202020;

    function tAddress() public pure returns(address){
        return t;
    }
}

contract C{
    function getAddress() public pure returns(address){
        return L.tAddress();
    }
}