? ? 在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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/305002.html
標籤:區塊鏈
上一篇:論文閱讀簡記 —— 區塊鏈
