Solidity

為什麼成功部署到 Rinkeby 的智能合約無法部署到主網?

  • November 13, 2021

truffle.js:

var HDWalletProvider = require("truffle-hdwallet-provider");
var mnemonic = "";

module.exports = {
 networks: {
   development: {
     host: "localhost",
     port: 8545,
     network_id: "*" // Match any network id
   },
     rinkeby: {
     provider: function() { 
      return new HDWalletProvider(mnemonic, "https://rinkeby.infura.io/v3/");
     },
     network_id: 4,
     gasPrice: "450000000", 
   },
   live: {
     networkCheckTimeout: 100000, 
     provider: function() { 
      return new HDWalletProvider(mnemonic, "https://mainnet.infura.io/v3/");
     },
     network_id: 1,
     gasPrice: "450000000",

   },
 },
 compilers: {
   solc: {
     version: "0.8.4",

   },
 },
};

成功部署到 Rinkeby:

  > transaction hash:    
  > Blocks: 1            Seconds: 17
  > contract address:    
  > block number:        8751194
  > block timestamp:     1623504143
  > account:             
  > balance:             18.7469330662
  > gas used:            1476936 (0x168948)
  > gas price:           0.45 gwei
  > value sent:          0 ETH
  > total cost:          0.0006646212 ETH

  > Saving artifacts
  -------------------------------------
  > Total cost:        0.0006646212 ETH


Summary
=======
> Total deployments:   1
> Final cost:          0.0006646212 ETH

部署到主網時出錯:

Error:  *** Deployment Failed ***

"ContractName" could not deploy due to insufficient funds
  * Account:  
  * Balance:  991972954849359 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 /usr/local/lib/node_modules/truffle/build/webpack:/packages/deployer/src/deployment.js:365:1
   at runMicrotasks (<anonymous>)
   at processTicksAndRejections (internal/process/task_queues.js:93:5)
   at Migration._deploy (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/Migration.js:74:1)
   at Migration._load (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/Migration.js:61:1)
   at Migration.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/Migration.js:212:1)
   at Object.runMigrations (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:150:1)
   at Object.runFrom (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:110:1)
   at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:87:1)
   at runMigrations (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/migrate.js:263:1)
   at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/migrate.js:226:1)
   at Command.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/command.js:136:1)
Truffle v5.2.4 (core: 5.2.4)
Node v14.15.0

我的餘額顯然超過了交易所需的餘額。為什麼會這樣?

您需要重新計算部署主網所需的 ETH 數量,如下所示:

deploymentCostInETH = gasAmount * gasPrice

deploymentCostInETH根據目前的天然氣價格而有所不同。

gasAmount主網將與測試網或您部署該特定合約的任何其他網路相同,即 1476936。gasAmount取決於合約的編譯字節碼。

gasPricedeploymentCostInETH跨網路或隨時間變化的原因,因為它取決於目前的汽油價格,而目前的汽油價格又取決於網路擁塞等因素。

假設今天的平均主網 gas 價格(13 gwei = 13 * 10^9 wei = 13 * 10^-9 ETH),計算結果為:

deploymentCostInETH = 1476936 * 13 * 10^-9 = 0.019200168 ETH

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