How do I complie contracts that are running on >0.6.0 with SafeMath(requires complier >0.8.0)?

Hi, I am trying to add SafeMath to a contract by importing it using
import "@openzeppelin/contracts/utils/math/SafeMath.sol";

The catch is, I am trying to deploy it using
pragma solidity ^0.6.0; on remix with compiler version set at 0.6.0.

As soon as I hit compile

@openzeppelin/contracts/utils/math/SafeMath.sol:4:1: ParserError: Source 
file requires different compiler version (current compiler is 
0.6.0+commit.26b70077.Emscripten.clang - note that nightly 
builds are considered to be strictly less than the released version
pragma solidity ^0.8.0;
^---------------------^

I tried the same by changing the compiler version to 0.8.0 for remix but now I get the same
error as above for 0.8.0 this time.

So,
Q1) How do I go about using SafeMath for files that are working on versions < 0.8.x
Q2) What's the point of using SafeMath at ^0.8.x when the language now implements SafeMath without having the need to import it explicitly(from 0.8.x onwards).

Thank You :slight_smile:

Q1) you could:

  1. try compiling with hardhat which supports using multiple compiler versions at once (see here: https://hardhat.org/guides/compile-contracts#multiple-solidity-versions). However I suspect that might not work in your case, since you are importing the SafeMath file into your contract, which causes a clash of defined versions.
  2. try importing SafeMath from an earlier release (that uses 6.0.0) straight from github:
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.0.0/contracts/math/SafeMath.sol";

Q2) Here is the discussion from the Openzeppelin team on the topic: https://github.com/OpenZeppelin/openzeppelin-contracts/issues/2465 . TLDR: you can leave the SafeMath library out if its a new project and you dont need backwards compatibility or customizable reverts.

1 Like

Thank you ma'am/sir, I was unaware that we can work with earlier versions. Fixed it :slight_smile:

1 Like