2

Say I have the following SimpleMath solidity library defined as follows:

library SimpleMath {

  function add(uint a, uint b) internal pure returns(uint)
  {
    return a + b;
  }
}

How do I import this library using brownie in my python script and call the add method? I would like to run unit tests of library functions.

I don't seem to have an issue importing the library, however I can't access its functions even when changing the access modifier to public I get the following brownie error:

from brownie import SimpleMath

def test_add():
  retVal = SimpleMath.add(10, 20)
  assert retVal == 30

AttributeError: 'ContractContainer' object has no attribute 'add'

Edit: So apparently you have to deploy a library the same way you would deploy a contract and then access it's methods, however I can't access the library's internal methods only external/public:

simpleMath = SimpleMath.deploy({'from': account})
simpleMath = simpleMath.add() # since add is internal cannot access

AttributeError: Contract 'SimpleMath' object has no attribute 'add'

How then can I test a libraries functions that are internal in brownie?

3
  • Import it where? Is it in another file? Jan 19, 2022 at 2:56
  • Hi Patrick, yes the same way I would import a contract called SimpleStorage in my python script and call it's methods.
    – MShakeG
    Jan 19, 2022 at 5:15
  • If you are using hardhat, you can try hardhat-exposed. Although I haven't tried it yet Jul 6, 2022 at 9:20

1 Answer 1

2

Since you can't access internal or private functions externally for both contracts and libraries I wrapped the method/s of the library SimpleMath in the method/s of a contract called TestSimpleMath, which has it's methods set to external, thus exposing the library method/s.

import { SimpleMath } from '.SimpleMath.sol';

contract TestSimpleMath {
  function add(uint a, uint b) external pure returns(uint) {
    return SimpleMath.add(a, b);
  }
}

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.