Skip to content

Commit 3e7bfca

Browse files
authored
Draft: Provide built-in ganache by default (#16)
* update dependencies * prepare for use with multiple blockchain providers * better error handling --fork option (for ganache internal) update dependencies --reset-config switch --show-config-file switch * update changelog * fix web3js error on wrong fork config... * .chain eth_X now issues eth rpc calls to provider * update changelog * update changelog
1 parent ebbcf5d commit 3e7bfca

File tree

7 files changed

+1758
-847
lines changed

7 files changed

+1758
-847
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
# Change Log
22
All notable changes will be documented in this file.
33

4+
## v0.1.0
5+
6+
⚠️ pot. breaking changes: `solidity-shell` now ships with ganache. use `.chain set-provider` to switch chain providers. the `built-in` ganache provider is used by default.
7+
8+
- new: built in ganache provider
9+
- new: `.chain` subcommand
10+
- `.chain restart` - restarts the service (formerly known as `.restartblockchain`)
11+
- `.chain set-provider [fork-url]` - switch between the internal or an external `ganache-cli` command or url-provider. Optionally specify a ganache fork-url.
12+
- ` .chain set-provider internal https://mainnet.infura.io/v3/yourApiKey `
13+
- `.chain accounts` - show ganache accounts
14+
- `.chain eth_<X> [args...]` - arbitrary eth JSONrpc method calls to blockchain provider.
15+
- e.g. `.chain eth_accounts` returns the blockchain providers response to the `eth_accounts` JSONrpc call.
16+
- new: command line switches:
17+
- `--fork` overrides fork-url option for internal ganache provider `solidity-shell --fork https://mainnet.infura.io/v3/yourApiKey`.
18+
- `--reset-config` resets the config file.
19+
- `--show-config-file` prints the path to the config file.
20+
- fix: better error handling. prevent vicious cycles where broken config trashes the app 🤦‍♂️
21+
- update: dependencies and solc references updated
22+
423
## v0.0.11
524
- new: configurable call and deploy gas
625
- new: `.restartblockchain` command to restart ganache e.g. after config changes

bin/main.js

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,44 @@ function saveFile(name, data){
4646
/** MAIN */
4747
const argv = require('minimist')(process.argv, {'--': true});
4848
var config = loadFile(CONFIG_FILE);
49-
const oldGanacheArgs = config.ganacheArgs;
49+
50+
const oldConf = {
51+
ganacheArgs: config.ganacheArgs,
52+
ganacheOptions: config.ganacheOptions
53+
}
54+
5055
if(argv['--'].length){ // temporarily override ganache args
5156
config.ganacheArgs = argv['--'];
5257
}
58+
if(argv['fork']){
59+
config.ganacheOptions.fork = {url: argv['fork']}
60+
}
61+
if(argv['reset-config']){
62+
config = {};
63+
}
64+
if(argv['show-config-file']){
65+
console.log(path.join(CONFIG_HOME, CONFIG_FILE));
66+
process.exit(0);
67+
}
5368

5469
const shell = new InteractiveSolidityShell(config);
5570

5671
process.on('exit', () => {
5772
shell.blockchain.stopService();
5873
if(argv['--'].length){ //restore old ganache args
59-
shell.settings.ganacheArgs = oldGanacheArgs;
74+
shell.settings.ganacheArgs = oldConf.ganacheArgs;
75+
}
76+
if(argv['fork']){
77+
shell.settings.ganacheOptions.fork = oldConf.ganacheOptions.fork;
6078
}
61-
saveFile(CONFIG_FILE, shell.settings)
6279
saveFile(SESSION, shell.dumpSession())
80+
81+
// exit if dirty exit detected
82+
if(process.exitCode!=0){
83+
console.log("🧨 not saving config due to dirty shutdown.")
84+
return;
85+
}
86+
saveFile(CONFIG_FILE, shell.settings)
6387
});
6488

6589
const vorpal = new Vorpal()
@@ -77,7 +101,7 @@ vorpal
77101
.mode('repl', 'Enters Solidity Shell Mode')
78102
.delimiter(c.bold('» '))
79103
.init(function (args, cb) {
80-
this.log(`🚀 Entering interactive Solidity ${c.bold(shell.settings.installedSolidityVersion)} shell. '${c.bold('.help')}' and '${c.bold('.exit')}' are your friends.`);
104+
this.log(`🚀 Entering interactive Solidity ${c.bold(shell.settings.installedSolidityVersion)} shell (🧁:${c.bold(shell.blockchain.name)}). '${c.bold('.help')}' and '${c.bold('.exit')}' are your friends.`);
81105
return cb();
82106
})
83107
.action(function (input, cb) {
@@ -102,7 +126,14 @@ vorpal
102126
${c.bold('General:')}
103127
.help ... this help :)
104128
.exit ... exit the shell
105-
.restartblockchain ... restart the ganache blockchain service
129+
130+
${c.bold('Blockchain:')}
131+
.chain
132+
restart ... restart the blockchain service
133+
set-provider <fork-url> ... "internal" | <shell-command: e.g. ganache-cli> | <https://localhost:8545>
134+
- fork url e.g. https://mainnet.infura.io/v3/yourApiKey
135+
accounts ... return eth_getAccounts
136+
<X> ... return web3.eth.<X>()
106137
107138
${c.bold('Settings:')}
108139
.config ... show settings
@@ -129,7 +160,41 @@ cheers 🙌
129160

130161
break; //show usage
131162
case '.exit': process.exit(); break; //exit -> no more cb()
132-
case '.restartblockchain': shell.blockchain.restartService(); break; //restart ganache
163+
case '.chain':
164+
if(!commandParts[1]){
165+
break;
166+
}
167+
switch(commandParts[1]){
168+
case 'restart':
169+
shell.blockchain.restartService();
170+
this.log(` ✨ '${shell.blockchain.name}' blockchain provider restarted.`)
171+
break;
172+
case 'set-provider':
173+
shell.settings.blockchainProvider = commandParts[2];
174+
if(commandParts.length > 3){
175+
//fork-url
176+
shell.settings.ganacheOptions.fork = {url: commandParts[3]}
177+
} else {
178+
delete shell.settings.ganacheOptions.fork
179+
}
180+
shell.initBlockchain();
181+
this.log(` ✨ '${shell.blockchain.name}' initialized.`)
182+
break;
183+
case 'accounts':
184+
shell.blockchain.getAccounts().then(acc => {
185+
this.log(`\n 🧝‍ ${acc.join('\n 🧝 ')}\n`)
186+
187+
})
188+
break;
189+
default:
190+
if(commandParts[1].startsWith("eth_")){
191+
shell.blockchain.rpcCall(commandParts[1], commandParts.slice(2)).then(res => this.log(res)).catch(e => this.log(e))
192+
}
193+
194+
break;
195+
}
196+
197+
break; //restart ganache
133198
case '.reset': shell.reset(); break; //reset complete state
134199
case '.undo': shell.revert(); break; //revert last action
135200
case '.config':
@@ -214,7 +279,8 @@ vorpal
214279
.command(".exit")
215280
.alias("exit")
216281
vorpal
217-
.command(".restartblockchain")
282+
.command(".chain")
283+
.autocomplete(["restart","set-provider","accounts", "getAccounts"])
218284
vorpal
219285
.command(".config")
220286
.autocomplete(["set","unset"])

0 commit comments

Comments
 (0)