[Ethereum] Solidity link to external library address – use Lib for uint256 breaks

solidity

I'm trying to link my contract to an external library by defining

  1. an interface contract that defines the library functions and
  2. the main contract that refers to the actual library address.

My library must be deployed externally and not be fully defined in the same contract (has presented the below challenge when using use Library for unit256)

// 1. Set up an interface defining the functions that exist within the target library (but not implementing them!!) 
contract SafeMath {
  function mul(uint256 a, uint256 b) internal pure returns (uint256);
  function div(uint256 a, uint256 b) internal pure returns (uint256);
  function sub(uint256 a, uint256 b) internal pure returns (uint256);
  function add(uint256 a, uint256 b) internal pure returns (uint256);
}

// 2. Define the main contract with reference to the interface and external deployed library address 
contract Foo {
  SafeMath ExternalSafeMath = SafeMath(0x013......);

  // THE LINE BELOW BREAKS 
  // It expects a library but instead gets contract. I must find a work around or obtain advice on how to otherwise implement this 
  using ExternalSafeMath for uint256;

  // Contract  continues ...
}

As seen above, I specify an interface contract that defines the functions that this library contains (as per suggestions in this forum's previous answers).

When attempting to issue a using ExternalSafeMath for unit256, the compiler correctly complains that it expects the ExternalSafeMath object to be a library, and NOT a contract. But as it stands, it is currently a contract for interface purposes.

THE QUESTION: How can I achieve a using Library for uint256 on an EXTERNALLY deployed library? And how will my syntax of code change in the code that follows

THE CONTEXT: My requirement is that I cannot implement the entire library in one contract deployment.

This is due to my contract implementation exceeding gas limit during deployment. Hence needing me to modularise contracts to separate addresses but still retain functionality of the current code

Best Answer

There's a typo. (actually two) It is not use ExternalSafeMath for uint256:

It's using ExternalSafeMath for uint256;

  • using, not use
  • you have a colon instead of a semi-colon at the end.

Also, if you are using Open-Zeppelin's SafeMath lib, the correct usage is:

uint256 answer = a.add(b);
Related Topic