当前位置:首页 » 《随便一记》 » 正文

truffle部署指定的合约_sanqima的专栏

13 人参与  2022年02月26日 08:36  分类 : 《随便一记》  评论

点击全文阅读


    在truffle中部署指定的合约,可以根据文件的Index来区别,比如,/migrations目录下,有如下三个文件:
    1_inital_migrations.js
    2_deploy_HWT.js
    3_deploy_SZT.js

    部署第2个合约:2_deploy_HWT.js,使用命令:

migrate -f 2 --to 2

    同理,部署第3个合约:2_deploy_SZT.js,使用命令:

migrate -f 3 --to 3

    类推,部署第n个合约: n_deploy_XXX.js,使用命令:

migrate -f n --to n

1 进入指定的网段

1.1 公共配置

    开启truffle-config.js里的助记词、私钥等配置。

const fs = require('fs');

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

1.2 ropsten网段配置

    在truffle-config.js的networks字段里,开启ropsten网段配置

    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: 20000,  // # of blocks before a deployment times out  (minimum/default: 50)
      skipDryRun: true,     // Skip dry run before migrations? (default: false for public nets )
      port: 8545
    },

1.3 rinkeby网段配置

    在truffle-config.js的networks字段里,开启rinkeby网段配置

    rinkeby: {
      provider: () => new HDWalletProvider(mnemonic, `https://rinkeby.infura.io/v3/` + infuraKey),
      network_id: 4,
    },

1.4 kovan网段配置

    在truffle-config.js的networks字段里,开启kovan网段配置

    kovan: {
      provider: () => new HDWalletProvider(mnemonic, `https://kovan.infura.io/v3/` + infuraKey),
      network_id: 42,
    },

1.5 主网配置

    在truffle-config.js的networks字段里,开启主网配置

    mainnet: {
      provider: () => new HDWalletProvider(mnemonic, `https://mainnet.infura.io/v3/` + infuraKey),
      network_id: 1,
    },

1.6 ganache配置

    在truffle-config.js的networks字段里,添加一个ganache网段,用于本地测试

    ganache: {
      host: "127.0.0.1",     // Localhost (default: none)
      port: 8545,            // Standard Ethereum port (default: none)
      network_id: "*",       // Any network (default: none)
    },

    truffle-config.js的完整配置如下:
    //truffle-config.js

const fs = require('fs');

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

module.exports = {
  networks: {
  
    ganache: {
      host: "127.0.0.1",     // Localhost (default: none)
      port: 8545,            // Standard Ethereum port (default: none)
      network_id: "*",       // Any network (default: none)
    },
    // development: {
    //   host: "127.0.0.1",     // Localhost (default: none)
    //   port: 8545,            // Standard Ethereum port (default: none)
    //   network_id: "*",       // Any network (default: none)
    // },
    // develop: {
    //   host: "127.0.0.1",     // Localhost (default: none)
    //   port: 8545,            // Standard Ethereum port (default: none)
    //   network_id: "*",       // Any network (default: none)
    //   gas: 8500000,           // Gas sent with each transaction (default: ~6700000)
    //   gasPrice: 20000000000,  // 20 gwei (in wei) (default: 100 gwei)
    // },

    // Another network with more advanced options...
    // advanced: {
    // port: 8777,             // Custom port
    // network_id: 1342,       // Custom network
    // gas: 8500000,           // Gas sent with each transaction (default: ~6700000)
    // gasPrice: 20000000000,  // 20 gwei (in wei) (default: 100 gwei)
    // from: <address>,        // Account to send txs from (default: accounts[0])
    // websockets: true        // Enable EventEmitter interface for web3 (default: false)
    // },

    // Useful for deploying to a public network.
    // NB: It's important to wrap the provider as a function.
    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: 20000,  // # of blocks before a deployment times out  (minimum/default: 50)
      skipDryRun: true,     // Skip dry run before migrations? (default: false for public nets )
      port: 8545
    },

    rinkeby: {
      provider: () => new HDWalletProvider(mnemonic, `https://rinkeby.infura.io/v3/` + infuraKey),
      network_id: 4,
    },
    
    kovan: {
      provider: () => new HDWalletProvider(mnemonic, `https://kovan.infura.io/v3/` + infuraKey),
      network_id: 42,
    },
    
    mainnet: {
      provider: () => new HDWalletProvider(mnemonic, `https://mainnet.infura.io/v3/` + infuraKey),
      network_id: 1,
    },
    // Useful for private networks
    // private: {
    // provider: () => new HDWalletProvider(mnemonic, `https://network.io`),
    // network_id: 2111,   // This network is yours, in the cloud.
    // production: true    // Treats this network as if it was a public net. (default: false)
    // }
  },

  // Set default mocha options here, use special reporters etc.
  mocha: {
    timeout: 300000,
	  reporter: 'eth-gas-reporter',
    reporterOptions: { excludeContracts: ['Migrations'] }
  },

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

2 部署指定合约到指定网络

2.1 进入ropsten并部署

    按Ctrl+C、Ctrl+D,退出当前的truffle控制台,重新使用如下命令:

truffle console --network ropsten
//部署第2个合约
migrate -f 2 --to 2

2.2 进入rinkeby并部署

    按Ctrl+C、Ctrl+D,退出当前的truffle控制台,重新使用如下命令:

truffle console --network rinkeby
//部署第2个合约
migrate -f 2 --to 2

2.3 进入kovan并部署

    按Ctrl+C、Ctrl+D,退出当前的truffle控制台,重新使用如下命令:

truffle console --network kovan
//部署第2个合约
migrate -f 2 --to 2

2.4 进入主网并部署

    按Ctrl+C、Ctrl+D,退出当前的truffle控制台,重新使用如下命令:

truffle console --network mainnet
//部署第2个合约
migrate -f 2 --to 2

2.5 进入ganache并部署

    按Ctrl+C、Ctrl+D,退出当前的truffle控制台,重新使用如下命令:

truffle console --network ganache
//部署第2个合约
migrate -f 2 --to 2

点击全文阅读


本文链接:http://zhangshiyu.com/post/35371.html

部署  网段  合约  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1