Skip to content

Instantly share code, notes, and snippets.

@dicarlo2
Created February 28, 2018 22:26
Show Gist options
  • Save dicarlo2/4b6f3234c95443a570786828a38508d2 to your computer and use it in GitHub Desktop.
Save dicarlo2/4b6f3234c95443a570786828a38508d2 to your computer and use it in GitHub Desktop.
WIP bootstrap command
/* @flow */
import {
type GetCLIResourceOptions,
type InteractiveCLIArgs,
} from '@neo-one/server-plugin';
import {
Client,
LocalKeyStore,
LocalUserAccountProvider,
LocalMemoryStore,
NEOONEProvider,
} from '@neo-one/client';
import { take } from 'rxjs/operators';
import type { Network } from './NetworkResourceType';
import type NetworkPlugin from './NetworkPlugin';
import constants from './constants';
const getNetwork = async ({
cli,
options,
}: GetCLIResourceOptions): Promise<string> => {
const { network: networkName } = options;
if (networkName != null && typeof networkName === 'string') {
return networkName;
}
const { network } = await cli.getSession(constants.PLUGIN);
if (network != null && typeof network === 'string') {
return network;
}
throw new Error(
'Bootstrap requires a network. Activate a network by ' +
'running `activate network <name>` or specify the network via ' +
'`--network <name>`',
);
};
export default (plugin: NetworkPlugin) => ({ cli }: InteractiveCLIArgs) =>
cli.vorpal
.command(
'bootstrap',
'Bootstraps a Network with test data.',
)
.option('-n, --network <name>', 'Network to bootstrap')
.action(async args => {
const name = await getNetwork({
cli,
args,
options: args.options,
});
const resource = await plugin.networkResourceType
.getResource$({
name,
client: cli.client,
options: {},
})
.pipe(take(1))
.toPromise();
if (resource == null) {
throw new Error(`Network ${name} does not exist.`);
}
const network = ((resource: $FlowFixMe): Network);
const client = new Client({
memory: new LocalUserAccountProvider({
keystore: new LocalKeyStore({
store: new LocalMemoryStore(),
}),
provider: new NEOONEProvider({
options: [{ network: network.name, rpcURL: network.nodes[0].rpcAddress }],
}),
}),
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment