-
Notifications
You must be signed in to change notification settings - Fork 92
/
index.js
302 lines (241 loc) · 9.64 KB
/
index.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
require('./lib/utils.js');
const questions = require('./lib/questions');
const command = require('inquirer');
const clear = require('clear');
const Table = require('cli-table2');
const config = require('./config.js');
const assembler = require('./lib/assembler/index.js');
command.registerPrompt('datetime', require('inquirer-datepicker-prompt'));
const Options = require('./lib/configuration/configuration.js');
const Compiler = require('./lib/compiler.js');
const Display = require('./lib/display.js');
const configuration = new Configuration();
const compiler = new Compiler(config.artifactsFolder);
const display = new Display(configuration);
// main();
async function main() {
await showMainMenu();
}
// TODO refactor and create a separate display module
async function showMainMenu() {
display.message.mainMenu();
const { choice } = await command.prompt(questions.categories);
if (choice === 'Configure Contracts') {
await showConfigurationMenu();
} else if (choice === 'Display Contract Configuration') {
display.showConfiguration(configuration);
await display.waitUntilKeyPress();
await showMainMenu();
} else if (choice === 'Build Contracts') {
await assembler.build(configuration);
await display.buildSuccess();
} else if (choice === 'Compile Contracts') {
await showCompilerMenu();
} else if (choice === 'Close') {
process.exit();
} else if (choice === 'Help') {
display.help();
}
}
async function showConfigurationMenu() {
display.message.configurationMenu();
const options = await command.prompt(questions.configurationMenu);
if (options.choice === 'Edit Configuration') {
await showContractSelectionMenu();
} else if (options.choice === 'Display Current Configuration') {
display.showConfiguration(configuration);
await display.waitUntilKeyPress();
} else if (options.choice === 'Save Configuration') {
try {
this.settings.save();
display.message.configurationSaved();
await display.waitUntilKeyPress();
}
catch(e) {
if (e instanceof Error) {
display.message.configurationNotSaved();
await display.waitUntilKeyPress();
}
}
} else if (options.choice === 'Load Previous Configuration') {
configuration.load();
display.message.configurationLoaded();
await display.waitUntilKeyPress();
} else if (options.choice === 'Back') {
await showMainMenu();
}
await showConfigurationMenu();
}
// TODO replace the hardcoded list of contracts by parsing the files in the templates folder
async function showContractSelectionMenu() {
display.message.contractSelectionMenu();
const contractFiles = ['Presale', 'Presale Token', 'Token', 'Token Sale', 'Wallet'];
const contracts = questions.contractCheckboxList(contractFiles);
const { choice } = await command.prompt(contracts);
configuration.setIncludedContracts(choice);
await showContractConfigurationMenu();
}
async function showContractConfigurationMenu() {
display.message.contractConfigurationMenu();
const additionalFields = ['Display Contract Configuration', 'Additional Options', 'Go to main menu'];
const choice = await showContractMenu({ additionalFields: ['Display Contract Configuration', 'Go to main menu'] });
if (choice === 'Presale Token') {
await showPresaleTokenMenu();
} else if (choice === 'Presale') {
await showPresaleMenu();
} else if (choice === 'Token') {
await showTokenMenu();
} else if (choice === 'TokenSale') {
await showTokenSaleMenu();
} else if (choice === 'Wallet') {
await requestWalletParameters();
} else if (choice === 'Display Contract Configuration') {
display.showConfiguration(configuration);
await display.waitUntilKeyPress();
await showContractConfigurationMenu();
} else if (choice === 'Advanced Options') {
await requestAdvancedOptions();
} else if (choice === 'Back') {
await showConfigurationMenu();
}
}
async function showPresaleTokenMenu() {
display.message.paramsRequest('Token');
const { choice } = await command.prompt(questions.tokenMenu);
if (choice === 'Base Configuration') {
display.message.paramsRequest('Presale Token');
await configuration.presaleToken.updateBasicOptions();
await showPresaleTokenMenu();
} else if (choice === 'Token Type') {
display.message.paramsRequest('Token Type');
await configuration.presaleToken.updateTokenStandard();
await showPresaleTokenMenu();
} else if (choice === 'Advanced Configuration') {
await configuration.presaleToken.updateAdvancedOptions();
await showPresaleTokenMenu();
} else if (choice === 'Display Current Configuration') {
display.showConfiguration(configuration);
await display.waitUntilKeyPress();
await showPresaleTokenMenu();
} else if (choice === 'Back') {
await showContractConfigurationMenu();
}
}
async function showTokenMenu() {
display.message.paramsRequest('Token');
const { choice } = await command.prompt(questions.tokenMenu);
if (choice === 'Base Configuration') {
display.message.paramsRequest('Token');
await configuration.token.updateBasicOptions();
await showTokenMenu();
} else if (choice === 'Token Type') {
display.message.paramsRequest('Token Type');
await configuration.token.updateTokenStandard();
await showTokenMenu();
} else if (choice === 'Advanced Configuration') {
await configuration.token.updateAdvancedOptions();
await showTokenMenu();
} else if (choice === 'Display Current Configuration') {
display.showConfiguration(configuration);
await display.waitUntilKeyPress();
await showTokenMenu();
} else if (choice === 'Back') {
await showContractConfigurationMenu();
}
}
async function showTokenSaleMenu() {
display.message.paramsRequest('TokenSale');
const { choice } = await command.prompt(questions.tokenSaleMenu);
if (choice === 'Base Configuration') {
display.message.paramsRequest('Presale');
await configuration.tokenSale.updateBasicOptions();
await showTokenSaleMenu();
} else if (choice === 'Cap Configuration') {
display.message.paramsRequest('Cap Configuration');
await configuration.tokenSale.updateCapOptions();
await showTokenSaleMenu();
} else if (choice === 'Timing Configuration') {
display.message.paramsRequest('Timing Configuration');
await configuration.tokenSale.updateTimeLimits();
await showTokenSaleMenu();
} else if (choice === 'Advanced Configuration') {
await configuration.tokenSale.updateAdvancedOptions();
await showTokenSaleMenu();
} else if (choice === 'Display Current Configuration') {
display.showConfiguration(configuration);
await display.waitUntilKeyPress();
await showTokenSaleMenu();
} else if (choice === 'Back') {
await showContractConfigurationMenu();
}
}
async function showPresaleMenu() {
display.message.paramsRequest('TokenSale');
const { choice } = await command.prompt(questions.tokenSaleMenu);
if (choice === 'Base Configuration') {
display.message.paramsRequest('Presale');
await configuration.presale.updateBasicOptions();
await showPresaleMenu();
} else if (choice === 'Cap Configuration') {
display.message.paramsRequest('Cap Configuration');
await configuration.presale.updateCapOptions();
await showPresaleMenu();
} else if (choice === 'Timing Configuration') {
display.message.paramsRequest('Timing Configuration');
await configuration.presale.updateTimeLimits();
await showPresaleMenu();
} else if (choice === 'Advanced Configuration') {
await configuration.presale.updateAdvancedOptions();
await showPresaleMenu();
} else if (choice === 'Display Current Configuration') {
display.showConfiguration(configuration);
await display.waitUntilKeyPress();
await showPresaleMenu();
} else if (choice === 'Back') {
await showContractConfigurationMenu();
}
}
async function requestWalletParameters() {
display.message.paramsRequest('Wallet');
const options = await command.prompt(questions.wallet);
configuration.wallet = new WalletOptions(options);
await showContractConfigurationMenu();
}
async function requestAdvancedOptions() {
display.message.paramsRequest('Advanced Options');
const options = await command.prompt(questions.advancedOptions);
await showContractConfigurationMenu();
}
async function showCompilerMenu() {
const { choice } = await command.prompt(questions.compilerMenu);
if (choice === 'Compile All Contracts') {
const contracts = configuration.getIncludedContracts();
await compiler.compileAll(contracts);
await display.compileSuccess();
console.log('Contracts have been written to your root folder');
} else if (choice === 'Compile Contract') {
const choice = await showContractMenu({ additionalFields: [] });
await compiler.compile(choice);
await display.compileSuccess();
console.log('Contracts have been written to your root folder');
} else if (choice === 'Print Bytecode') {
const choice = await showContractMenu({ additionalFields: [] });
const bytecode = await compiler.getByteCode(choice);
console.log(bytecode);
await display.waitUntilKeyPress();
} else if (choice === 'Print ABI') {
const choice = await showContractMenu({ additionalFields: [] });
const ABI = await compiler.getABI(choice);
console.log(ABI);
await display.waitUntilKeyPress();
}
await showMainMenu();
}
async function showContractMenu({ additionalFields = [] }) {
const optionsList = configuration.getIncludedContracts().uncamelize();
additionalFields.forEach((field) => { optionsList.push(field); });
const menu = questions.contractOptionsList(optionsList);
const { choice } = await command.prompt(menu);
return choice;
}
module.exports = { main }