diff --git a/src/actions.js b/src/actions.js index 45b56f8..696df1e 100644 --- a/src/actions.js +++ b/src/actions.js @@ -4,6 +4,7 @@ const PledgeUtils = require('./pledge-utils'); const TrxUtils = require('./trx-utils'); const Contracts = require("./contracts.js"); const Provider = require("./provider.js"); +const Web3 = require('web3'); function doAction(actionText, action) { console.dir(actionText) @@ -32,22 +33,30 @@ function doAction(actionText, action) { class Actions { - constructor(chain) { + constructor(chain, accounts) { this.chain = chain || "development"; + this.accounts = accounts || []; } - connect(url, cb) { + connect(options, cb) { + const url = options.url; + const infura = options.infura; + console.dir("connecting to: " + url); - this.provider = new Provider(); - const accounts = [{mnemonic: ""}] - this.provider.initAccounts(accounts); - this.provider.startWeb3Provider("ws", url) - - //web3.setProvider(url); + if (this.accounts.length > 0) { + this.provider = new Provider(); + this.provider.initAccounts(this.accounts); + this.provider.startWeb3Provider("ws", url) + } else { + this.web3 = new Web3(); + this.web3.setProvider(url); + } setTimeout(async () => { - this.web3 = this.provider.web3; + if (this.accounts.length > 0) { + this.web3 = this.provider.web3; + } let accounts = await this.web3.eth.getAccounts(); console.dir("== accounts"); diff --git a/src/index.js b/src/index.js index add2fb8..b3ca264 100644 --- a/src/index.js +++ b/src/index.js @@ -5,12 +5,22 @@ const Actions = require('./actions.js'); program .version('0.1.0') .option('-u, --url [url]', "host to connect to (default: ws://localhost:8556)") + .option('-a, --accounts [accounts]', "accounts file, if not defined uses accounts in the connecting node") .option('-c, --chain [chain]', "environment to run, can be mainnet, ropsten, development (default: development)") + .option('-i, --infura [infuraKey]', "infuraKey (default: undefined)") .parse(process.argv); -const actions = new Actions(program.chain || "development"); +let accounts = []; +if (program.accounts) { + accounts = require(process.cwd() + "/" + program.accounts).accounts; +} -actions.connect(program.url || "ws://localhost:8556", async () => { +const actions = new Actions(program.chain || "development", accounts || []); + +actions.connect({ + url: (program.url || "ws://localhost:8556"), + infura: program.infura +}, async () => { cmd(actions) });