Solidity

使用 Pragma Solidity 0.4.24 時遇到錯誤

  • April 26, 2021

我在智能合約上使用了 pragma solidity 0.4.24,但是在執行 truffle compile 命令時,出現如下錯誤:

===========================
> Compiling ./contracts/ImageRegister.sol
> Compiling ./contracts/Migrations.sol

/Users/adobe/Downloads/an_drive/contracts/ImageRegister.sol:1:1: ParserError: Source file requires different compiler version (current compiler is 0.5.16+commit.9c3226ce.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version
pragma solidity 0.4.24;
^---------------------^
,/Users/adobe/Downloads/an_drive/contracts/Migrations.sol:1:1: ParserError: Source file requires different compiler version (current compiler is 0.5.16+commit.9c3226ce.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version
pragma solidity 0.4.24;
^---------------------^

Error: Truffle is currently using solc 0.5.16, but one or more of your contracts specify "pragma solidity 0.4.24".
Please update your truffle config or pragma statement(s).
(See https://trufflesuite.com/docs/truffle/reference/configuration#compiler-configuration for information on
configuring Truffle to use a specific solc compiler version.)

Compilation failed. See above.
Truffle v5.3.3 (core: 5.3.3)
Node v12.17.0

我很困惑如何解決這個問題

通過https://www.trufflesuite.com/docs/truffle/reference/configuration說,在 truffle.js 中設置編譯版本

module.exports = {
 compilers: {
   solc: {
     version: <string>, // A version or constraint - Ex. "^0.5.0"
                        // Can also be set to "native" to use a native solc
     }
   }
}

引用自:https://ethereum.stackexchange.com/questions/97807