How to use SafeMath lib for Solidity version 0.5?

Based on this recommendation by Trail of Bits, of using Solidity version 0.5, over 0.4 and 0.6 for production, I decided to write my contracts in Solidity version 0.5.

Now, when I want to use OZ’s SafeMath library for my contracts, I only find them with pragma solidity ^0.6.0;, and NOT with pragma solidity ^0.5.0;
I tried installing the @openzeppelin/contracts npm package via npm i @openzeppelin/contracts@3.0.1 and npm i @openzeppelin/contracts@3.0.2, apart from the current npm i @openzeppelin/contracts@3.1.0, based on the ‘Versions’ tab from the npm package page here. But all of them give SafeMath.sol file with pragma solidity ^0.6.0; and not pragma solidity ^0.5.0;

So how do I use SafeMath lib with pragma solidity ^0.5.0; now, for my Solidity version 0.5 contracts?

1 Like

Hi @smitrajput,

I would check if the Solidity 0.5 recommendation still applies as it appears this was last updated in March. Solidity version 0.6.0 was released in December 2019 and is now at 0.6.12.


If you want to use a Solidity 0.5 version of OpenZeppelin Contracts then you can install the 2.x release. The latest version is v2.5.1.

npm i @openzeppelin/contracts@2.5.1

SafeMath documentation for OpenZeppelin Contracts v2.x: https://docs.openzeppelin.com/contracts/2.x/utilities#math

Thanks so much @abcoathup. That works.

And yes, it will be really helpful if you could confirm that the recommendation still holds, because there have been many releases of Solidity 0.6.x, even after March.

1 Like

Hi @smitrajput,

I suggest checking that recommendation with Trail of Bits about not using Solidity 0.6 to see if their recommendation has changed.

As an aside, Solidity 0.7 was released today.

:joy: I’m pretty confused rn.

Anyways, yes, I’ll check the recommendation with the team themselves. And thanks for the Solidity 0.7 release update.

1 Like

Hi @smitrajput,

Sorry that you are confused.
I currently recommend using OpenZeppelin Contracts 3.x with Solidity 0.6.

Let me know if you hear anything more about the recommendation to use Solidity 0.5.

Thanks for the recommendation.
And yes, I’ll definitely update you on the Solidity 0.5 recommendation.

1 Like

Hi @smitrajput,

I wanted to share with you the following recommendation:

That helps. Thanks a lot. I conclude using latest Solidity version 0.6 from that.

I’d also like to bring your attention to another question I had asked here, regarding using OZ’s SafeERC20 contracts: this is the link to that question

1 Like

Hi @smitrajput,

I have moved your question to a new topic: Issue calling Transfer function in the USDT contract as it is best to have one topic per question, otherwise they can get a bit lost and make it hard for the community to answer.

That’s actually better. Thanks.

1 Like