truffle部署指定的合约到指定网络
Posted 软件工程小施同学
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了truffle部署指定的合约到指定网络相关的知识,希望对你有一定的参考价值。
摘要: 在truffle中部署指定的合约,可以根据文件的Index来区别,比如,/migrations目录下,有如下三个文件: 1_inital_migrations.js
在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
以上是关于truffle部署指定的合约到指定网络的主要内容,如果未能解决你的问题,请参考以下文章
Truffle Migrations(迁移)- 多个合约一起部署 如何链接库
错误:Truffle 当前使用的是 solc 0.5.16,但您的一个或多个合约指定“pragma solidity ^0.8.0”