欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用Truffle时遇到的问题和解决方法

程序员文章站 2022-03-31 09:56:56
...

问题1

  • 错误信息

Error: CompileError: /C/Users/media/ethereum_work/course3/contracts/Ballot.sol:1:1: ParserError: Source file requires different compiler version (current compiler is 0.5.12+commit.7709ece9.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version
pragma solidity ^0.4.17;
----------------------

Error: Truffle is currently using solc 0.5.12, but one or more of your contracts specify “pragma solidity ^0.4.17”.
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.
at Object.compile (C:\Users\media\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\workflow-compile\legacy\index.js:80:1)
Truffle v5.1.5 (core: 5.1.5)
Node v12.14.0

  • 原因

Truffle和智能合约sol文件的版本不一致导致:sol的版本过低。sol文件的版本在其第一行指定。

  • 解决方法:
  1. 修改sol文件第一行,pragma solidity ^0.4.17;,提高版本。
  2. 或者,修改sol文件的上一级目录下的文件truffle-config.js,改为
// Configure your compilers
  compilers: {
    solc: {
      version: "0.4.17",    // Fetch exact version from solc-bin (default: truffle's version)
    }
  }

问题2

  • 错误信息

Error: *** Deployment Failed ***

“Ballot” exceeded the block limit (with a gas value you set).

  • Block limit: 0x5fdfb1
  • Gas sent: 6721975
  • Try:
    • Sending less gas.
    • Setting a higher network block limit if you are on a
      private network or test client (like ganache).

C:\Users\media\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\deployer\src\deployment.js:364:1
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at Migration._deploy (C:\Users\media\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\migration.js:70:1)
at Migration._load (C:\Users\media\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\migration.js:57:1)

  • 原因
    使用命令truffle migrate --reset部署智能合约的时候设置了过多的gas,大于区块链的限制值。

  • 解决方法
    修改truffle_config.js文件的下面部分的gas字段的值,根据自己的错误信息适当减小gas值。

networks: {

    development: {
     host: "127.0.0.1",     // Localhost (default: none)
     port: 8545,            // Standard Ethereum port (default: none)
     network_id: "*",       // Any network (default: none)
     gas: 4700000
    },
  },

相关标签: 区块链