【问题标题】:Source file requires different compiler version : Truffle源文件需要不同的编译器版本:Truffle
【发布时间】:2019-05-27 07:31:39
【问题描述】:

我已经编写了一份简单的可靠智能合约,并尝试使用 truffle 迁移它。

$ truffle migrate
Compiling .\contracts\Election.sol...
Compiling .\contracts\Migrations.sol...

    /D/ethereum/electiondemo/contracts/Migrations.sol:1:1: SyntaxError: Source file requires different compiler version (current compiler is 0.5.0+commit.1d4f565a.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version
    pragma solidity ^0.4.24;
    ^----------------------^

Compilation failed. See above.`enter code here`
Truffle v5.0.0 (core: 5.0.0)
Node v8.11.1

Solidity 版本为 0.5.0。 智能合约代码如下:

pragma solidity ^0.5.0;

contract Election {
    // Read/write candidate
    string public candidate;

    // Constructor
    constructor ( ) public {
        candidate = "Candidate 1";
    }
}

【问题讨论】:

    标签: ethereum solidity truffle


    【解决方案1】:

    找到解决方案: 在 truffle.js 中。你需要指定solidity版本

    module.exports = {
       // See <http://truffleframework.com/docs/advanced/configuration>
       // for more about customizing your Truffle configuration!
       networks: {
           development: {
               host: "127.0.0.1",
               port: 7545,
               network_id: "*" // Match any network id
           }
       },
       compilers: {
           solc: {
               **version: "0.4.24"** // ex:  "0.4.20". (Default: Truffle's installed solc)
           }
       }
    };
    

    在你的智能合约中给出同样的需要

    【讨论】:

      【解决方案2】:

      将以下行添加到truffle-config.js

      {
        compilers: {
          solc: {
            version: "0.4.24" // ex:  "0.4.20". (Default: Truffle's installed solc)
          }
        }
      }
      

      【讨论】:

        【解决方案3】:

        到目前为止,truffle 默认使用“0.5.16”。因此,如果您的代码使用较新的solidity 版本,则会引发错误。 您不需要为 solc 版本输入特定值。

        这是我在合同上使用的

         pragma solidity >=0.7.0 <0.9.0;
        

        在配置文件中

        compilers: {
            solc: {
              // default is 0.5.16
              version: ">=0.7.0 <0.9.0",    // Fetch exact version from solc-bin (default: truffle's version)
            
              }
            }
          },
        

        【讨论】:

          【解决方案4】:

          您的迁移合同 (Migrations.sol) 需要 0.4.24。

          转到您的迁移合同并将您的依赖项更改为 0.5 或将您的主合同依赖项更改为 0.4。*

          【讨论】:

            【解决方案5】:

            到你的 truffle.js / truffle-config.js 添加这个

            module.exports = {
            // See <http://truffleframework.com/docs/advanced/configuration>
            // for more about customizing your Truffle configuration!
            networks: {
              development: {
                host: "127.0.0.1",
                port: 7545,
                network_id: "*" // Match any network id
              }
            },
            compilers: {
              solc: {
                version: "0.4.24" //(Default: Truffle's installed    solc)
              }
             }
            };
            

            然后使用 npx 运行你的包。 npx 是一个原生的 npm 包,所以它随您安装的 nodejs 和 npm 一起提供。 它允许您运行本地节点包二进制文件。这样,您就可以摆脱大量的全局包安装,并使用 package.json 中定义的本地二进制文件。

            npx truffle compile

            npx truffle test(可选)

            npx truffle migrate

            【讨论】:

              猜你喜欢
              • 2019-06-09
              • 1970-01-01
              • 2022-07-22
              • 1970-01-01
              • 2021-08-25
              • 1970-01-01
              • 2011-02-23
              • 2021-08-31
              • 1970-01-01
              相关资源
              最近更新 更多