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.

1

4 Answers 4

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();


}
2
  • 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? May 11, 2017 at 16:19
  • 3
    I have the same question any development?
    – Sig Touri
    May 13, 2017 at 12:14
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.

1
  • the Lib library constant obviously embbed in contract A.. So the Lib is an embbed Library?
    – frank
    May 24, 2022 at 9:18
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();
    }
}
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.

1
  • 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, 2019 at 14:25

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.

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