forked from trufflesuite/ganache-cli-archive
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·231 lines (192 loc) · 5.88 KB
/
cli.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
#!/usr/bin/env node
// make sourcemaps work!
require('source-map-support/register')
// `yargs/yargs` required to work with webpack, see here.
// https://github.com/yargs/yargs/issues/781
var yargs = require('yargs');
var Ganache = require("@shyftnetwork/shyft_ganache-core");
var pkg = require("./package.json");
var corepkg = require("@shyftnetwork/shyft_ganache-core/package.json");
var URL = require("url");
var fs = require("fs");
var initArgs = require("./args")
var BN = require("bn.js");
var to = require("@shyftnetwork/shyft_ganache-core/lib/utils/to");
var detailedVersion = "Ganache CLI v" + pkg.version + " (@shyftnetwork/shyft_ganache-core: " + corepkg.version + ")";
var isDocker = "DOCKER" in process.env && process.env.DOCKER.toLowerCase() === "true";
var argv = initArgs(yargs, detailedVersion, isDocker).argv;
function parseAccounts(accounts) {
function splitAccount(account) {
account = account.split(',')
return {
secretKey: account[0],
balance: account[1]
};
}
if (typeof accounts === 'string')
return [ splitAccount(accounts) ];
else if (!Array.isArray(accounts))
return;
var ret = []
for (var i = 0; i < accounts.length; i++) {
ret.push(splitAccount(accounts[i]));
}
return ret;
}
if (argv.d) {
argv.s = "TestRPC is awesome!"; // Seed phrase; don't change to Ganache, maintain original determinism
}
if (typeof argv.unlock == "string") {
argv.unlock = [argv.unlock];
}
var logger = console;
// If quiet argument passed, no output
if (argv.q === true){
logger = {
log: function() {}
};
}
// If the mem argument is passed, only show memory output,
// not transaction history.
if (argv.mem === true) {
logger = {
log: function() {}
};
setInterval(function() {
console.log(process.memoryUsage());
}, 1000);
}
var options = {
port: argv.p,
hostname: argv.h,
debug: argv.debug,
seed: argv.s,
mnemonic: argv.m,
total_accounts: argv.a,
default_balance_ether: argv.e,
blockTime: argv.b,
gasPrice: argv.g,
gasLimit: argv.l,
accounts: parseAccounts(argv.account),
unlocked_accounts: argv.unlock,
fork: argv.f,
network_id: argv.i,
verbose: argv.v,
secure: argv.n,
db_path: argv.db,
account_keys_path: argv.acctKeys,
vmErrorsOnRPCResponse: !argv.noVMErrorsOnRPCResponse,
logger: logger,
allowUnlimitedContractSize: argv.allowUnlimitedContractSize,
time: argv.t,
keepAliveTimeout: argv.keepAliveTimeout
}
var fork_address;
// If we're forking from another client, don't try to use the same port.
if (options.fork) {
var split = options.fork.split("@");
fork_address = split[0];
var block;
if (split.length > 1) {
block = split[1];
}
if (URL.parse(fork_address).port == options.port) {
options.port = (parseInt(options.port) + 1);
}
options.fork = fork_address + (block != null ? "@" + block : "");
}
var server = Ganache.server(options);
console.log(detailedVersion);
server.listen(options.port, options.hostname, function(err, result) {
if (err) {
console.log(err);
return;
}
var state = result ? result : server.provider.manager.state;
console.log("");
console.log("Available Accounts");
console.log("==================");
var accounts = state.accounts;
var addresses = Object.keys(accounts);
addresses.forEach(function(address, index) {
var balance = new BN(accounts[address].account.balance).divRound(new BN("1000000000000000000")).toString();
var line = "(" + index + ") " + address + " (~" + balance + " ETH)";
if (state.isUnlocked(address) == false) {
line += " 🔒";
}
console.log(line);
});
console.log("");
console.log("Private Keys");
console.log("==================");
addresses.forEach(function(address, index) {
console.log("(" + index + ") " + "0x" + accounts[address].secretKey.toString("hex"));
});
if (options.account_keys_path != null) {
console.log("");
console.log("Saving accounts and keys to " + options.account_keys_path);
var obj = {}
obj.addresses = accounts;
obj.private_keys = {};
addresses.forEach(function(address, index) {
obj.private_keys[address] = accounts[address].secretKey.toString("hex");
});
var json = JSON.stringify(obj);
fs.writeFile(options.account_keys_path, json, 'utf8',function(err){
if(err) throw err;
})
}
if (options.accounts == null) {
console.log("");
console.log("HD Wallet");
console.log("==================");
console.log("Mnemonic: " + state.mnemonic);
console.log("Base HD Path: " + state.wallet_hdpath + "{account_index}")
}
if (options.gasPrice) {
console.log("");
console.log("Gas Price");
console.log("==================");
console.log(options.gasPrice);
}
if (options.gasLimit) {
console.log("");
console.log("Gas Limit");
console.log("==================");
console.log(options.gasLimit);
}
if (options.fork) {
console.log("");
console.log("Forked Chain");
console.log("==================");
console.log("Location: " + fork_address);
console.log("Block: " + to.number(state.blockchain.fork_block_number));
console.log("Network ID: " + state.net_version);
console.log("Time: " + (state.blockchain.startTime || new Date()).toString());
}
console.log("");
console.log("Listening on " + options.hostname + ":" + options.port);
});
process.on('uncaughtException', function(e) {
console.log(e.stack);
process.exit(1);
})
// See http://stackoverflow.com/questions/10021373/what-is-the-windows-equivalent-of-process-onsigint-in-node-js
if (process.platform === "win32") {
require("readline").createInterface({
input: process.stdin,
output: process.stdout
})
.on("SIGINT", function () {
process.emit("SIGINT");
});
}
process.on("SIGINT", function () {
// graceful shutdown
server.close(function(err) {
if (err) {
console.log(err.stack || err);
}
process.exit();
});
});