Solidity – What Version is Used When Specifying pragma solidity >0.7.0 <0.9.0?

bytecodecompilersolidity

I am new to Solidity but have learned you can tell the compiler to use a range of Solidity versions e.g ^0.8.10 || > 0.7.0 < 0.9.0 when deploying smart contracts. My question is how does the compiler pick which one to compile the code with given multiple versions options?

Best Answer

The compilier does not choose, there is a different version of the compilier (solc) for each solidity version. Then, often hardhat, foundry, ect. automatically selects a solc version that works for your contracts.

So, the compiler is whatever solc version is defined in your local environment. For example hardhat, foundry, truffle, remix, ect. That syntax mean the contract should compile and work with any versions between those. Check your hardhat/truffle/foundry config files to find what version of solidity they are using to compile, or also there is a place on the remix UI to select exact version if you are using that.

When one of those tools auto-selects a version, they might use different logic to decide which version. There's no standard way of determining which should be used, so any in range in considered valid. Also, be careful when using version ranges, and take care to make sure the contracts works correctly on all major versions in the range. If you're importing from a library like openzeppelin, I wouldn't worry about it too much so long as the version you are using is in the range.