I'm trying to link my contract to an external library by defining
- an interface contract that defines the library functions and
- 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
contract SafeMath
tolibrary SafeMath
? If I am correct you can't use abstract contract as library, you will have to implement functions directly inlibrary
. Have a look at example.