Truffle
無法使用松露連接到多邊形孟買測試網路
我正在嘗試將 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 } };
設置資訊
- WSL2 Ubuntu 18.04
- 節點版本:
v14.15.4
- npm 版本:
6.14.10
- Solidity 編譯器:
0.8.5
- 松露版:
v5.3.11 (core: 5.3.11)
- Web3.js:
v1.3.6
將 RPC 端點更新為
https://rpc-mumbai.maticvigil.com
fromhttps://rpc-mumbai.matic.today
in後,truffle-config.js
它開始正常工作。Polygon 團隊似乎更新了 RPC 端點,但沒有在文件中更新它。更新
最好在 Matic-vigil 上創建一個帳戶,然後為多邊形測試生成 RPC 端點,最後將此端點與 Metamask 和其他服務(如 truffle 和 hardhat)一起使用。
更新-2
- 在 上創建一個帳戶
https://rpc.maticvigil.com/
。- 創建一個應用程序,假設
truffle test
它將生成四個端點。- 兩個用於主網(https 和 WebSocket),兩個用於測試網。使用
testnet https
看起來對https://rpc-mumbai.maticvigil.com/v1/ID
每個ID
使用者來說都是唯一的端點。- 現在將此端點添加到
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 }, },
- 如果您正在使用,請
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 端點是最受信任和最可靠的。