Truffle

無法使用松露連接到多邊形孟買測試網路

  • April 10, 2022

我正在嘗試將 Polygon 測試網路與 truffle 一起使用,我按照文件和 youtube影片為 truffle 配置 Polygon 網路。但是當我嘗試執行時truffle deploy --network matic,它失敗並出現以下錯誤。

錯誤日誌

Error: PollingBlockTracker - encountered an error while attempting to update latest block:
Error: Unknown Error: {"statusCode":404,"error":"Not Found"}
   at Request._callback (/home/alpha/GoWorkspace/src/github.com/metadata/solidity_e2e/node_modules/@trufflesuite/web3-provider-engine/subproviders/rpc.js:57:23)
   at Request.self.callback (/home/alpha/GoWorkspace/src/github.com/metadata/solidity_e2e/node_modules/request/request.js:185:22)
   at Request.emit (events.js:315:20)
   at Request.<anonymous> (/home/alpha/GoWorkspace/src/github.com/metadata/solidity_e2e/node_modules/request/request.js:1154:10)
   at Request.emit (events.js:315:20)
   at IncomingMessage.<anonymous> (/home/alpha/GoWorkspace/src/github.com/metadata/solidity_e2e/node_modules/request/request.js:1076:12)
   at Object.onceWrapper (events.js:421:28)
   at IncomingMessage.emit (events.js:327:22)
   at endReadableNT (internal/streams/readable.js:1327:12)
   at processTicksAndRejections (internal/process/task_queues.js:80:21)
   at PollingBlockTracker._performSync (/home/alpha/GoWorkspace/src/github.com/metadata/solidity_e2e/node_modules/eth-block-tracker/src/polling.js:51:24)
   at processTicksAndRejections (internal/process/task_queues.js:93:5)
UnhandledRejections detected
Promise {
 <rejected> {
   code: -32603,
   message: 'Unknown Error: {"statusCode":404,"error":"Not Found"}',
   data: { originalError: {} },
   stack: 'Error: Unknown Error: {"statusCode":404,"error":"Not Found"}\n' +
     '    at Request._callback (/home/alpha/GoWorkspace/src/github.com/metadata/solidity_e2e/node_modules/@trufflesuite/web3-provider-engine/subproviders/rpc.js:57:23)\n' +
     '    at Request.self.callback (/home/alpha/GoWorkspace/src/github.com/metadata/solidity_e2e/node_modules/request/request.js:185:22)\n' +
     '    at Request.emit (events.js:315:20)\n' +
     '    at Request.<anonymous> (/home/alpha/GoWorkspace/src/github.com/metadata/solidity_e2e/node_modules/request/request.js:1154:10)\n' +
     '    at Request.emit (events.js:315:20)\n' +
     '    at IncomingMessage.<anonymous> (/home/alpha/GoWorkspace/src/github.com/metadata/solidity_e2e/node_modules/request/request.js:1076:12)\n' +
     '    at Object.onceWrapper (events.js:421:28)\n' +
     '    at IncomingMessage.emit (events.js:327:22)\n' +
     '    at endReadableNT (internal/streams/readable.js:1327:12)\n' +
     '    at processTicksAndRejections (internal/process/task_queues.js:80:21)'
 }
}

松露-config.js

const HDWalletProvider = require('@truffle/hdwallet-provider');
const fs = require('fs');
const mnemonic = fs.readFileSync(".secret").toString().trim();

module.exports = {
 networks: {
   development: {
     host: "127.0.0.1",     // Localhost (default: none)
     port: 8545,            // Standard Ethereum port (default: none)
     network_id: "*",       // Any network (default: none)
   },
   matic: {
     provider: () => new HDWalletProvider(mnemonic, `https://rpc-mumbai.matic.today`),
     //host: 'https://rpc-mumbai.matic.today',
     network_id: 80001,
     confirmations: 2,
     timeoutBlocks: 200,
     skipDryRun: true
   },
 },

 // Set default mocha options here, use special reporters etc.
 mocha: {
   // timeout: 100000
 },

 // Configure your compilers
 compilers: {
   solc: {
     version: "0.8.5",    // 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"
     // }
   }
 },

 // Truffle DB is currently disabled by default; to enable it, change enabled: false to enabled: true
 //
 // Note: if you migrated your contracts prior to enabling this field in your Truffle project and want
 // those previously migrated contracts available in the .db directory, you will need to run the following:
 // $ truffle migrate --reset --compile-all

 db: {
   enabled: false
 }
};

設置資訊

  1. WSL2 Ubuntu 18.04
  2. 節點版本:v14.15.4
  3. npm 版本:6.14.10
  4. Solidity 編譯器:0.8.5
  5. 松露版: v5.3.11 (core: 5.3.11)
  6. Web3.js:v1.3.6

將 RPC 端點更新為https://rpc-mumbai.maticvigil.comfrom https://rpc-mumbai.matic.todayin後,truffle-config.js它開始正常工作。Polygon 團隊似乎更新了 RPC 端點,但沒有在文件中更新它。

更新

最好在 Matic-vigil 上創建一個帳戶,然後為多邊形測試生成 RPC 端點,最後將此端點與 Metamask 和其他服務(如 truffle 和 hardhat)一起使用。

更新-2

  1. 在 上創建一個帳戶https://rpc.maticvigil.com/
  2. 創建一個應用程序,假設truffle test它將生成四個端點。
  3. 兩個用於主網(https 和 WebSocket),兩個用於測試網。使用testnet https看起來對https://rpc-mumbai.maticvigil.com/v1/ID每個ID使用者來說都是唯一的端點。
  4. 現在將此端點添加到truffle-config.js. 下面是片段。
const HDWalletProvider = require('@truffle/hdwallet-provider');
const fs = require('fs');
const mnemonic = fs.readFileSync(".secret").toString().trim();

module.exports = {
networks: {
 development: {
   host: "127.0.0.1",     // Localhost (default: none)
   port: 8545,            // Standard Ethereum port (default: none)
   network_id: "*",       // Any network (default: none)
 },
 matic: {
   provider: () => new HDWalletProvider(mnemonic, `https://rpc-mumbai.maticvigil.com/v1/ID`),
   network_id: 80001,
   confirmations: 2,
   timeoutBlocks: 200,
   skipDryRun: true
 },
},
  1. 如果您正在使用,請hardhat按照以下程式碼段操作。
defaultNetwork: "polygon_test",
 networks: {
   hardhat: {
   },
   polygon_test: {
     url: `https://rpc-mumbai.maticvigil.com/v1/ID`,
     chainId: 80001,

更新 3

Openzeppelin-upgradable如果使用安全帽部署可升級(外掛)智能合約,請勿使用 Matic vigil 端點。相反,去Infura端點。在這里分享我的個人經歷。

更新 4

在嘗試了所有的 RPC 端點之後,我會推薦Alchemy RPC 端點。Alchemy 端點是最受信任和最可靠的。

引用自:https://ethereum.stackexchange.com/questions/102302