/
deployer_menu.js
186 lines (146 loc) · 5.48 KB
/
deployer_menu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
require('../utils');
const command = require('inquirer');
const until = require('catchify')
const path = require('path')
const questions = require('../questions');
command.registerPrompt('datetime', require('inquirer-datepicker-prompt'));
const Display = require('../display.js');
const Config = require('../config/index.js')
const Deployer = require('../deployer.js')
const Deployment = require('../deployment.js')
const Account = require('../Account.js')
const Interface = require('./menu.js')
const Options = require('../options/index.js')
const display = new Display();
const config = new Config();
const contractParser = require('../contract_parser')
class DeployerMenu {
constructor({router, settings}) {
this.router = router
this.settings = settings || settings.getLocal()
}
async open() {
await this.showDeployerMenu()
}
async showDeployerMenu() {
display.message.deployerMenu()
let menu = questions.deployerConfigurationMenu({additionalFields: ['Back','Close']})
const options = await command.prompt(menu);
if (options.choice === 'Show deployment settings') {
await this.showDeploymentSettings()
}
else if (options.choice === 'Update deployment settings') {
await this.showDeploySettingsMenu()
}
else if (options.choice === 'Deploy') {
await this.showDeployContractsMenu()
await display.waitUntilKeyPress()
}
else if (options.choice === 'Back') {
await this.router.openMainMenu({})
}
else if (options.choice === 'Close') {
process.exit(1)
}
}
async showDeploySettingsMenu() {
display.message.deployerSettingsMenu()
let menu = questions.deployerSettingsMenu({additionalFields: ['Back']})
const options = await command.prompt(menu);
if (options.choice === 'Show deployment settings') {
await this.showDeploymentSettings()
await this.showDeploySettingsMenu()
}
if (options.choice === 'Default Network/Wallet') {
await this.showDefaultNetworkAndWalletForm()
await this.showDeploySettingsMenu()
}
else if (options.choice === 'Configure Infura Token') {
await this.showInfuraTokenForm()
await this.showDeploySettingsMenu()
}
else if (options.choice === 'Configure Gas Options') {
await this.showGasOptionsForm()
await this.showDeploySettingsMenu()
}
else if (options.choice === 'Back') {
await this.showDeployerMenu()
}
}
async showDeploymentSettings() {
let settings = config.user.parseDeploymentSettings()
display.deploymentSettings(settings)
await display.waitUntilKeyPress()
await this.showDeployerMenu()
}
async showDefaultNetworkAndWalletForm() {
var [err, { network }] = await until(command.prompt(questions.defaultNetworkForm))
if (err) throw err;
let wallets = config.user.getWalletsForNetwork(network)
if (wallets.length == 0) {
process.stdout.write('No wallet configured on this network')
}
else {
let form = questions.defaultWalletForm(wallets)
var [err, { defaultAddress }] = await until(command.prompt(form))
if (err) throw err;
config.user.updateDefaultNetwork(network)
config.user.updateDefaultWallet(defaultAddress)
display.message.walletAndNetworkUpdated()
}
await display.waitUntilKeyPress()
await this.showDeployerMenu()
}
async showInfuraTokenForm() {
let { token } = await command.prompt(questions.infuraTokenForm)
config.user.updateInfuraToken(token)
display.message.infuraTokenUpdated()
await display.waitUntilKeyPress()
await this.showDeploySettingsMenu()
}
async showGasOptionsForm() {
let { gasLimit, gasPrice } = await command.prompt(questions.gasOptionsForm)
config.user.updateDefaultGasLimit(gasLimit)
config.user.updateDefaultGasPrice(gasPrice)
display.message.gasOptionsUpdated()
await display.waitUntilKeyPress()
await this.showDeploySettingsMenu()
}
async showDeployContractsMenu() {
let contracts = await Options.getLocalContracts()
if (!contracts) console.log('No contracts found')
const choice = await this.showContractMenu({ additionalFields: ['Back']})
console.log(choice)
if (choice == 'Back') {
await this.showDeployerMenu()
}
else {
var [err, result] = await until(this.showDeployContractForm(choice))
if (err) throw err;
}
}
async showDeployContractForm(contract) {
let fileName = contract.camelize().capitalize() + '.sol'
let filePath = path.join(config.contracts.flattened, fileName)
let constructorArguments = contractParser.getConstructorParameters({filePath: filePath})
var [error, {password}] = await until(command.prompt(questions.walletPassword))
if (error) console.log(error)
if (constructorArguments.length > 0) {
let form = questions.constructorArgumentsForm(constructorArguments)
var [error, args] = await until(command.prompt(form))
if (error) console.log(error)
}
let deployment = new Deployment({contracts: [ contract ], password, args})
var [error, txHash] = await until(deployment.start())
if (error) console.log(error)
await display.deploymentSuccess(txHash)
}
async showContractMenu({ additionalFields = [] }) {
let optionsList = this.settings.getIncludedContracts().uncamelize();
optionsList = optionsList.concat(additionalFields)
const menu = questions.contractOptionsList(optionsList);
const { choice } = await command.prompt(menu);
return choice;
}
}
module.exports = DeployerMenu