-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
123 lines (111 loc) · 3.69 KB
/
index.js
File metadata and controls
123 lines (111 loc) · 3.69 KB
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
#!/usr/bin/env node
const chalk = require('chalk');
const clear = require('clear');
const figlet = require('figlet');
const rest = require('restler');
const CLI = require('clui');
const Spinner = CLI.Spinner;
const pkg = require('./package.json');
const files = require('./lib/files');
const etherui = require('./lib/etherui');
const APIVersion = pkg.APIVersion;
const baseUrl = pkg.baseUrl;
clear();
console.log(
chalk.yellow(
figlet.textSync('ETHERUI.NET', { horizontalLayout: 'full' })
)
);
if (!files.fileExists('truffle.js') && !files.fileExists('truffle-config.js')) {
console.log(chalk.red('You need to run this command into truffle project folder with file truffle.js or truffle-config.js'));
process.exit();
}
const getEtheruiToken = async () => {
// Fetch token from config store
const token = etherui.getStoredEtheruiToken();
if (token.authToken && token.userId) {
console.log(chalk.bgCyan('Reuse previous token. To reissue new token you need to delete file by path:'));
console.log(chalk.bgCyan(token.path));
return token;
}
// No token found, use credentials to access Etherui account
const newToken = await etherui.setEtheruiCredentials();
if (!newToken) {
console.log(chalk.red('Unauthorized. Username or password is wrong.'));
process.exit();
}
return newToken;
};
const getSmartContract = async () => {
const smartContractFile = await etherui.getSmartContractFile();
return smartContractFile;
};
const getSmartContractAddress = async () => {
const smartContractAddress = await etherui.getSmartContractAddress();
return smartContractAddress;
};
const createSmartContractNewUI = async (smartContractAddress, SCJSON, token) => {
const asyncWrapper = async () => {
return new Promise(resolve => {
const status = new Spinner('We are creating new UI for your Smart Contract, please wait...');
status.start();
rest.post(`${baseUrl}/api/${APIVersion}/smartContractNewUI`,
{ data:
{
address: smartContractAddress,
contract_name: SCJSON.contract_name,
abi: JSON.stringify(SCJSON.abi),
arguments: null,
},
headers: {
Accept: '*/*',
'User-Agent': 'Etherui-deploy',
'X-Auth-Token': token.authToken,
'X-User-Id': token.userId,
},
timeout: 10000,
}
).on('complete', (result) => {
if (result instanceof Error) {
console.log('Error:', result.message);
} else {
try {
if (result.status === 'success' && result.data) {
resolve(result.data);
} else {
throw new Error(result.message);
}
} catch (err) {
console.log(chalk.red(err.message));
} finally {
status.stop();
}
}
});
});
};
const wrapperResult = await asyncWrapper();
return wrapperResult;
};
const run = async () => {
try {
// Retrieve & Set Authentication Token
const token = await getEtheruiToken();
// Get Smart contract content
const smartContract = await getSmartContract();
// Get Smart contract address
const smartContractAddress = await getSmartContractAddress();
// Transform to JSON
const SCJSON = JSON.parse(smartContract);
// Send all to create new UI
const done = await createSmartContractNewUI(smartContractAddress, SCJSON, token);
if (done) {
console.log(chalk.green(`All done! You can check your new UI for Smart contract by URL: ${baseUrl}/smartcontract/${smartContractAddress}`));
} else {
console.log(chalk.red('Something wrong. Please, try again.'));
}
} catch (err) {
console.log(chalk.red(err.message));
}
};
run();