2017-03-29 17:50:05 +00:00
|
|
|
let shelljs = require('shelljs');
|
2017-02-18 13:01:03 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class Simulator {
|
|
|
|
constructor(options) {
|
|
|
|
this.blockchainConfig = options.blockchainConfig;
|
2018-04-27 13:16:29 +00:00
|
|
|
this.logger = options.logger;
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2017-02-18 13:01:03 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
run(options) {
|
|
|
|
let cmds = [];
|
2017-02-18 13:01:03 +00:00
|
|
|
|
2018-04-27 13:16:29 +00:00
|
|
|
const testrpc = shelljs.which('testrpc');
|
|
|
|
const ganache = shelljs.which('ganache-cli');
|
|
|
|
if (!testrpc && !ganache) {
|
2018-05-08 21:49:46 +00:00
|
|
|
this.logger.warn(__('%s is not installed on your machine', 'Ganache CLI (TestRPC)'));
|
|
|
|
this.logger.info(__('You can install it by running: %s', 'npm -g install ganache-cli'));
|
2018-04-27 13:16:29 +00:00
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
|
2018-05-29 19:19:25 +00:00
|
|
|
cmds.push("-p " + (options.port || this.blockchainConfig.rpcPort || 8545));
|
|
|
|
cmds.push("-h " + (options.host || this.blockchainConfig.rpcHost || 'localhost'));
|
2017-12-27 22:48:33 +00:00
|
|
|
cmds.push("-a " + (options.numAccounts || 10));
|
|
|
|
cmds.push("-e " + (options.defaultBalance || 100));
|
|
|
|
cmds.push("-l " + (options.gasLimit || 8000000));
|
2017-02-18 13:01:03 +00:00
|
|
|
|
2018-01-15 21:55:28 +00:00
|
|
|
// adding mnemonic only if it is defined in the blockchainConfig or options
|
|
|
|
let simulatorMnemonic = this.blockchainConfig.simulatorMnemonic || options.simulatorMnemonic;
|
|
|
|
if (simulatorMnemonic) {
|
|
|
|
cmds.push("--mnemonic \"" + (simulatorMnemonic) +"\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
// adding blocktime only if it is defined in the blockchainConfig or options
|
|
|
|
let simulatorBlocktime = this.blockchainConfig.simulatorBlocktime || options.simulatorBlocktime;
|
|
|
|
if (simulatorBlocktime) {
|
|
|
|
cmds.push("-b \"" + (simulatorBlocktime) +"\"");
|
|
|
|
}
|
|
|
|
|
2018-04-27 13:16:29 +00:00
|
|
|
const program = ganache ? 'ganache-cli' : 'testrpc';
|
|
|
|
shelljs.exec(`${program} ${cmds.join(' ')}`, {async : true});
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-18 13:01:03 +00:00
|
|
|
|
|
|
|
module.exports = Simulator;
|