Solidity
遷移到 Ropsten 網路時出現問題
我正在嘗試將契約遷移到 ROPSTEN 網路。它失敗的原因如下。請指教。
遷移命令
$env:MNEMONIC=' XX XX XX';truffle migrate --reset --network ropsten
錯誤
Error: There was a timeout while attempting to connect to the network at undefined. Check to see that your provider is valid. If you have a slow internet connection, try configuring a longer timeout in your Truffle config. Use the networks[networkName].networkCheckTimeout property to do this. at Timeout._onTimeout (C:\Users\skmsu\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\provider\index.js:57:1) at listOnTimeout (internal/timers.js:557:17) at processTimers (internal/timers.js:500:7)
松露-config.js
require('dotenv').config(); const HDWalletProvider = require('truffle-hdwallet-provider'); console.log('ab - from powershell file process.env.MNEMONIC : '+ process.env.MNEMONIC); const infuraProvider = network => providerWithMnemonic( process.env.MNEMONIC || '', `https://${network}.infura.io/${process.env.INFURA_API_KEY}` ); const providerWithMnemonic = (mnemonic, rpcEndpoint) => new HDWalletProvider(mnemonic, rpcEndpoint); module.exports = { // See <http://truffleframework.com/docs/advanced/configuration> // to customize your Truffle configuration! networks: { development: { host: 'localhost', port: 8545, network_id: '*', // eslint-disable-line camelcase } , mainnet: { provider: infuraProvider('mainnet'), gasPrice: 10000000000, // 10 Gwei gas: 4000000, network_id: '1' }, ropsten: { provider: infuraProvider('ropsten'), gasPrice: 1000000000, // 1 Gwei gas: 4000000, network_id: '3' } }, solc: { optimizer: { enabled: true, runs: 200 } } };
嘗試使用
@truffle/hdwallet-provider
.truffle-hdwallet-provider
已棄用。此外,驗證您的 Ropsten 項目 ENDPOINT。
i.e: https://ropsten.infura.io/v3/j4hg5g45jhfj8hgfhfghgtg
讓我知道這是否適合您,如果沒有,那麼會找到其他方法。
- 我假設你已經成功安裝了@truffle/hdwallet-provider。如果你不這樣做,試試這個
$ npm install @truffle/hdwallet-provider
或查看https://github.com/trufflesuite/truffle/blob/develop/packages/hdwallet-provider/README.md#install
- 把你的 Infura API KEY
.infura
和助記符放到.secret
. 兩者處於同一水平truffle-config.js
。- 改成
truffle-config.js
這樣const HDWalletProvider = require('@truffle/hdwallet-provider'); const fs = require('fs'); const infuraKey = fs.readFileSync(".infura").toString().trim(); 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) }, ropsten: { provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/` + infuraKey), network_id: 3, // Ropsten's id gas: 5500000, // Ropsten has a lower block limit than mainnet confirmations: 2, // # of confs to wait between deployments. (default: 0) timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50) skipDryRun: true // Skip dry run before migrations? (default: false for public nets ) }, }, // Set default mocha options here, use special reporters etc. mocha: { // timeout: 100000 }, // Configure your compilers compilers: { solc: { // version: "0.8.0", // Fetch exact version from solc-bin (default: truffle's version) settings: { // See the solidity docs for advice about optimization and evmVersion optimizer: { enabled: false, runs: 200 }, } } }, db: { enabled: false } };
- 然後你可以這樣執行
$ truffle migrate --network ropsten
- 請記住,不要
.infura
將and送出.secret
到 GitHub 或任何 repo。