ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

truffle部署指定的合约

2021-10-02 09:06:42  阅读:203  来源: 互联网

标签:network default 指定 js -- truffle 合约 id


    在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

标签:network,default,指定,js,--,truffle,合约,id
来源: https://blog.csdn.net/sanqima/article/details/120583106

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有