10

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.

3

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


}
  • 7
    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
  • @SigTouri Unfortunately no that's not possible. Check my new answer below. – Paul Berg Mar 6 at 15:46
1

Using Solidity ^0.5.0

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).

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

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

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.