support specifying account index

This commit is contained in:
Iuri Matias 2019-05-24 17:46:32 -04:00
parent d1fedb5ec9
commit c3b9d1ed4c
4 changed files with 20 additions and 7 deletions

View File

@ -1,7 +1,8 @@
{ {
"accounts": [ "accounts": [
{ {
"mnemonic": "add mnemonic here" "mnemonic": "add mnemonic here",
"numAddresses": 1
} }
] ]
} }

View File

@ -47,9 +47,9 @@ class Actions {
this.provider = new Provider(); this.provider = new Provider();
this.provider.initAccounts(this.accounts); this.provider.initAccounts(this.accounts);
if (url.indexOf("https") >= 0) { if (url.indexOf("https") >= 0) {
this.provider.startWeb3Provider("rpc", url) this.provider.startWeb3Provider("rpc", url, (options.accountIndex || 0))
} else { } else {
this.provider.startWeb3Provider("ws", url) this.provider.startWeb3Provider("ws", url, (options.accountIndex || 0))
} }
} else { } else {
this.web3 = new Web3(); this.web3 = new Web3();
@ -64,7 +64,17 @@ class Actions {
let accounts = await this.web3.eth.getAccounts(); let accounts = await this.web3.eth.getAccounts();
console.dir("== accounts"); console.dir("== accounts");
console.dir(accounts); console.dir(accounts);
this.web3.eth.defaultAccount = accounts[0] this.web3.eth.defaultAccount = accounts[(options.accountIndex || 0)]
if (this.web3.eth.defaultAccount) {
console.dir("using account: " + this.web3.eth.defaultAccount);
} else {
console.log("===================================");
console.log("===================================");
console.error("no account found at index " + (options.accountIndex || 0));
console.log("===================================");
console.log("===================================");
process.exit();
}
let contracts = new Contracts(this.chain, this.web3); let contracts = new Contracts(this.chain, this.web3);
contracts.loadContracts(); contracts.loadContracts();

View File

@ -7,6 +7,7 @@ program
.option('-u, --url [url]', "host to connect to (default: ws://localhost:8556)") .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('-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('-c, --chain [chain]', "environment to run, can be mainnet, ropsten, development (default: development)")
.option('-i, --index [index]', "account index to use (default: 0)")
.parse(process.argv); .parse(process.argv);
let accounts = []; let accounts = [];
@ -17,7 +18,8 @@ if (program.accounts) {
const actions = new Actions(program.chain || "development", accounts || []); const actions = new Actions(program.chain || "development", accounts || []);
actions.connect({ actions.connect({
url: (program.url || "ws://localhost:8556") url: (program.url || "ws://localhost:8556"),
accountIndex: (program.index || 0)
}, async () => { }, async () => {
cmd(actions) cmd(actions)
}); });

View File

@ -35,7 +35,7 @@ class Provider {
} }
} }
startWeb3Provider(type, web3Endpoint) { startWeb3Provider(type, web3Endpoint, accountIndex) {
const self = this; const self = this;
if (type === 'rpc') { if (type === 'rpc') {
@ -69,7 +69,7 @@ class Provider {
self.addresses = [...new Set(self.addresses)]; // Remove duplicates self.addresses = [...new Set(self.addresses)]; // Remove duplicates
if (self.accounts.length) { if (self.accounts.length) {
self.web3.eth.defaultAccount = self.addresses[0]; self.web3.eth.defaultAccount = self.addresses[accountIndex || 0];
} }
const realSend = self.provider.send.bind(self.provider); const realSend = self.provider.send.bind(self.provider);