Solc
Truffle-test 上的衝突 solc 版本
我正在開發一些使用 solc 0.7.0 的智能合約。我能夠成功編譯和部署契約,但是當我嘗試執行 $truffle test 時,我收到以下錯誤:
Using network 'development'. Compiling your contracts... =========================== > Compiling ./test/TestSupperApp.sol truffle/Assert.sol:5:1: ParserError: Source file requires different compiler version (current compiler is 0.7.0+commit.9e61f92b.Emscripten.clang) - note that nightly builds are considered to be strictly less than the released version pragma solidity >= 0.4.15 < 0.7.0; ^--------------------------------^ ,truffle/DeployedAddresses.sol:2:1: ParserError: Source file requires different compiler version (current compiler is 0.7.0+commit.9e61f92b.Emscripten.clang) - note that nightly builds are considered to be strictly less than the released version pragma solidity >= 0.7.0 < 0.7.0; ^-------------------------------^ Error: Truffle is currently using solc 0.7.0, but one or more of your contracts specify "pragma solidity >= 0.4.15 < 0.7.0". Please update your truffle config or pragma statement(s). (See https://truffleframework.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.1.41 (core: 5.1.41) Node v11.14.0
這是測試程式碼的開始:
// SPDX-License-Identifier: MIT pragma solidity >=0.4.22 <0.8.0; import "truffle/Assert.sol"; import "truffle/DeployedAddresses.sol"; import "../contracts/SuperApp.sol"; contract TestSuperApp {
我的 truffle-config 的編譯器配置如下所示:
// Configure your compilers compilers: { solc: { version: "0.7.0", // Fetch exact version from solc-bin (default: truffle's version) // docker: true, // Use "0.5.1" you've installed locally with docker (default: false) // settings: { // See the solidity docs for advice about optimization and evmVersion // optimizer: { // enabled: false, // runs: 200 // }, // evmVersion: "byzantium" // } }, },
DeployedAddresses.sol 如何要求編譯器使用 >=0.7.0 AND <0.7.0?我該如何解決?
(OBS.:顯然我應該更改pragma語句,但是由於DeployedAddresses.sol是在測試時動態創建的,所以似乎無法直接更改它)
你應該參考你應該為松露使用什麼版本的solidity編譯器。這可以在 truffle-config.js 中更新。
compilers: { solc: { version: "^0.7.0" } }
(有關配置 Truffle 以使用特定的 solc 編譯器版本的資訊,請參閱https://trufflesuite.com/docs/truffle/reference/configuration#compiler-configuration 。)
然後,您應該確保所有智能合約和智能合約測試都使用此編譯器功能。
pragma solidity ^0.7.0;
如果您再次提及該錯誤,則表明合約與solidity 編譯器版本不匹配。(Assert.sol / DeployedAddresses.sol) - 只需更新 pragma 語句以引用上述版本並匹配從 truffle-config.js 設置的內容,您應該會很好。