0

How to import and compile contracts of different versions using solidity in remix?

When I import a contract I get this error:

ParserError: Source file requires different compiler version (current compiler is 0.8.3+commit.8d00100c.Emscripten.clang) - note that nightly builds are considered to be strictly less than the released version --> https://github.com/ConsenSysMesh/openzeppelin-solidity/blob/master/contracts/math/SafeMath.sol:1:1: | 1 | pragma solidity ^0.4.23; |

I am importing a contract from open zepplin. The contract is MathSafe and it uses pragma solidity 0.4.23;. My local contract uses 0.8.3.

This is how my code looks like on remix:

// SPDX-License-Identifier: MIT

pragma solidity >=0.4.23 <0.9.0;

import "https://github.com/ConsenSysMesh/openzeppelin-solidity/blob/master/contracts/math/SafeMath.sol";

contract StakingToken is ERC20, Ownable{
    using SafeMath for uint256;
    
    constructor(){}
    
    function addNumber(uint256 number) public view returns(uint256) {
        return number.add(5);
    }
}
2
  • Can you explain more on this error? I can't see any code.
    – Anupam
    Apr 12, 2021 at 12:13
  • Hey, I have added more code to explain the error better.
    – YulePale
    Apr 12, 2021 at 12:25

2 Answers 2

2

First you have to decide which solidity version you want to choose for your project. Its not necessary that you choose the latest version(my opinion). You have to consider the solidity version of external packages that you wish to use in your project.

For example, if you are using Openzeppelin, you can find different versions here: @openzeppelin/contracts.

Here is the updated code compatible with Solidity:0.8.3 with Openzeppelin:

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.23 <0.9.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";

contract StakingToken is ERC20, Ownable{
    using SafeMath for uint256;
    
    constructor()ERC20("MyCoin", "XMC"){}
    
    function addNumber(uint256 number) public pure returns(uint256) {
        return number.add(5);
    }
}
1

Just specify the "minimum/lower version" looking at all your imported and main contracts. In your case that should be 0.4.23 so just set that into your Remix compiler form field (top first field on the left compilernavbar)

enter image description here

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.