【问题标题】:truffle compile contracts specifying incorrect solidity version松露编译合约指定不正确的solidity版本
【发布时间】:2021-08-16 14:37:00
【问题描述】:

对此感到非常困扰,尝试编写 ERC21 收藏品,当我使用 truffle compile 编译我的项目时......我收到一个错误 Truffle is currently using solc 0.5.16, but one or more of your contracts specify "pragma solidity ^0.8.0"

我觉得这很奇怪,因为我已经在我的合同和 package.lock.json 中编辑了版本,尝试删除我的包锁,更改了合同中的版本,然后尝试再次运行 npm install 但无论如何我都会遇到同样的错误我用的是什么版本

我之前在其他项目中遇到过这个错误,但只是通过更正版本来修复它......但这一次不起作用:(

我已经尝试了 pragma solidity >=0.4.21

希望有人能解释我做错了什么?

合约示例

pragma solidity >=0.4.21 <0.6.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract color is ERC721 {
    constructor() ERC721("color", "COLOR") {
    }
}

truffle.config

require('babel-register');
require('babel-polyfill');

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*" // Match any network id
    },
  },
  contracts_directory: './src/contracts/',
  contracts_build_directory: './src/abis/',
  compilers: {
    solc: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  }
}

package-lock.json

"truffle": {
      "version": "5.0.5",
      "resolved": "https://registry.npmjs.org/truffle/-/truffle-5.0.5.tgz",
      "requires": {
        "app-module-path": "^2.2.0",
        "mocha": "^4.1.0",
        "original-require": "1.0.1",
        "solc": "^0.8.0"
      }
    },

【问题讨论】:

    标签: solidity truffle


    【解决方案1】:

    我遇到了同样的问题 :( 检查您的 package.json 以获取旧版本的 Solidity 库。 我在 truffle-config 中指定了 0.8 版本,这让我对 OpenZepplin 最新合约有了更进一步的了解,但仍然有一些旧合约使用 0.5,但它失败了。 我通过编辑 package.json 来解决它以指向最新的 openzepplin 那么

    npm install --upgrade

    然后

    truffle 编译 --all

    我们能否在 truffle 中列出多个 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
        },
        develop: {
          port: 8545
        },
        nftWallet: {
          provider: () => new PrivateKeyProvider(privateKey, "http://localhost:8545"),
          network_id: "*",
          type: "quorum",
          gasPrice: 0
        }
      },
            //Configure your compilers
       compilers: {
         solc: {
            version: "0.8",    // Fetch exact version from solc-bin (default: truffle's version)
    
               }
       }
    
    };
    

    【讨论】:

    • 感谢@salmanMarvasti,我的松露配置中的编译器代码完全缺少版本属性......在将合同中的版本设置为 0.8 后,样板文件并不总是像我想的那样完整:P。 7 明确发布更新我的松露配置它的工作原理!
    猜你喜欢
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 2021-07-25
    相关资源
    最近更新 更多