未找到松露配置 solc 版本杂注

Truffle config solc version pragma not found

我正在尝试编译智能合约,truffle 在使用此编译器配置时找不到 solc。它无法从源文件中自动检测 solc 版本。

module.exports = {
    compilers: {
        solc: {
            version: 'pragma',
            optimizer: {
                enabled: true,
                runs: 200,
            },
        },
    },
};

truffle compile 执行的结果

Error: Could not find a compiler version matching pragma. compilers.solc.version option must be a string specifying:
   - a path to a locally installed solcjs
   - a solc version or range (ex: '0.4.22' or '^0.5.0')
   - a docker image name (ex: 'stable')
   - 'native' to use natively installed solc

松露 solc 文档

solc¶
Solidity compiler settings. Supports optimizer settings for solc, as well as other settings such as debug and metadata settings.

You may specify... 
+ any solc-js version (using semver) listed at solc-bin. Specify the one you want and Truffle will get it for you. 
+ "native" to use a natively compiled solc binary (you'll need to install this yourself, links to help below). 
+ a dockerized solc tag from one of images published here. 
+ a path to a locally available solc 
+ "pragma" to have Truffle autodetect solc versions from your source files. This can be used to compile using multiple versions of solc. 
+ a solc-js parser for faster docker and native compilations

总的来说,我决定调查可能是什么问题,发现如果你的项目中有不同版本的文件,你可以指定 pragma 并编译不同版本的文件。我是从 here

了解到的

据我了解,此功能可从 truffle 5.2.0 及更高版本开始使用。我还找到了一个很棒的存储库,其中包含一个很可能会有所帮助的示例 - v5.2-example-box。如果您将您的项目与这个项目进行比较,您很可能肯定能够找到错误。


初步回答

我建议按照 documentation 中的建议安装特定版本的 solc 以检查是否存在任何其他问题。

对于我的工作,我在 truffle-config.js 中使用以下设置:

module.exports = {
  compilers: {
    solc: {
      version: '0.8.3',
      parser: 'solcjs'
    }
  }
}

您还需要安装特定版本的软件包。这将匹配来自 truffle-config.js 的版本,在本例中,此版本为 0.8.3

要安装该软件包,您需要为 npmyarn 调用这些命令之一,具体取决于您使用的是什么。

npm i solc@0.8.3

yarn add solc@0.8.3

也是我的情况,合约中有这样一个版本:

pragma solidity >=0.8.0;