Truffle-Migration

truffle 使用不是我的助記詞的奇怪帳戶遷移契約

  • March 28, 2021

我設置了我的 infura 端點和助記符以將合約部署到 Rinkeby 測試網。像下面這樣。

const infuraKey = process.env.INFURA_KEY
const test_mnemonic = process.env.TEST_MNEMONIC

networks: {
 rinkeby: {
   provider: () => new HDWalletProvider(test_mnemonic, `https://rinkeby.infura.io/v3/${infuraKey}`),
   network_id: 4,
   gas: 6900000,
   gasPrice: 10000000000,
   confirmations: 2,
   timeoutBlocks: 200,
   skipDryRun: true
 }
}

我的賬戶

$$ 0 $$是0xeA28Ab53fFF23859c64DB5BAaA64E466713Afae7,但是當我嘗試使用此命令進行部署時truffle migrate --network rinkeby,它會失敗並出現如下錯誤。

Error: Error: Error:  *** Deployment Failed ***

"Migrations" could not deploy due to insufficient funds

* Account:  0x9416c8F57eddFeb491F2d728EEb13781662Ac606
* Balance:  0 wei
* Message:  insufficient funds for gas * price + value
* Try:
 + Using an adequately funded account
 + If you are using a local Geth node, verify that your node is synced.

at Object.run (/Users/deleo/.nvm/versions/node/v12.13.0/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:96:1)
at processTicksAndRejections (internal/process/task_queues.js:93:5)

如果我的助記符包含,我已經搜尋過0x9416c8F57eddFeb491F2d728EEb13781662Ac606,但它沒有。我不知道為什麼松露總是用錯誤的帳戶遷移。

更新

我發現在 truffle 控制台 ( truffle console --network rinkeby),我輸入web3.eth.getAccounts()並顯示第一個帳戶是0x9416c8F57eddFeb491F2d728EEb13781662Ac606. 為什麼 truffle migrate 在我設置助記詞後使用這個預設帳戶?這個預設帳戶是從哪裡設置的?如何設置我的配置以使用我的助記符?

我想通了!truffle-hdwallet-provider模組正在使用它的帳戶

$$ 0 $$從m/44'/1'/0'/0. 但我試圖使用m/44'/60'/0'/0來自 BIP39 的助記符創建工具的帳戶。這就是原因。

請為您的配置嘗試這種方式:

const HDWalletProvider = require("truffle-hdwallet-provider");
require('dotenv').config()

const providerFactory = network =>
 new HDWalletProvider(
   process.env.MNEMONICS || "", // Mnemonics of the deployer
   `https://${network}.infura.io/v3/${process.env.INFURA_KEY}`, // Provider URL => web3.HttpProvider
   0,
   20
 );

module.exports = {
 compilers: {
   solc: {
     version: "^0.5.5",
     optimizer: {
       enabled: true,
       runs: 200
     },
   }
 },
 networks: {
   rinkeby: {
     provider: providerFactory("rinkeby"),
     network_id: 4,
     gas: 6900000,
     gasPrice: 10000000000 // 10 Gwei
   }
 }
};

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