-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jplomas/master
feat: balance command added
- Loading branch information
Showing
7 changed files
with
273 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* eslint new-cap: 0 */ | ||
const {Command, flags} = require('@oclif/command') | ||
const {red, green} = require('kleur') | ||
// const ora = require('ora') | ||
// might be nice to add ora spinner whilst loading from API | ||
const validateQrlAddress = require('@theqrl/validate-qrl-address') | ||
const axios = require('axios') | ||
const BigNumber = require('bignumber.js') | ||
|
||
const shorPerQuanta = 10 ** 9 | ||
|
||
const GetBalance = async function (address, api) { | ||
return axios.get(api, {params: {address: address}}).then((response => { | ||
return response.data | ||
})).catch(error => { | ||
return {error: 1, errorMessage: error.message} | ||
}) | ||
} | ||
|
||
class Balance extends Command { | ||
async run() { | ||
const {args, flags} = this.parse(Balance) | ||
const address = args.address | ||
if (!validateQrlAddress.hexString(address).result) { | ||
this.log(`${red('⨉')} Unable to get a balance: invalid QRL address`) | ||
this.exit(1) | ||
} | ||
let api = '' | ||
if (flags.api) { | ||
api = flags.api | ||
} else { | ||
api = 'https://brooklyn.theqrl.org/api/GetBalance' | ||
} | ||
const bal = await GetBalance(address, api) | ||
if (bal.error === 1) { | ||
this.log(`${red('⨉')} ${bal.errorMessage}`) | ||
this.exit(1) | ||
} | ||
let balance = new BigNumber(bal.data.balance) | ||
if (flags.shor) { | ||
this.log(`${green('✓')} Balance: ${balance} Shor`) | ||
} | ||
if (flags.quanta || !flags.shor) { | ||
// default to showing balance in Quanta if no flags | ||
this.log(`${green('✓')} Balance: ${balance / shorPerQuanta} Quanta`) | ||
} | ||
} | ||
} | ||
|
||
Balance.description = `Get a wallet balance from the network | ||
... | ||
TODO | ||
` | ||
|
||
Balance.args = [ | ||
{ | ||
name: 'address', | ||
description: 'address to return balance for', | ||
required: true, | ||
}, | ||
] | ||
|
||
Balance.flags = { | ||
shor: flags.boolean({char: 's', default: false, description: 'reports the balance in Shor'}), | ||
quanta: flags.boolean({char: 'q', default: false, description: 'reports the balance in Quanta'}), | ||
api: flags.string({char: 'a', required: false, description: 'api endpoint (for custom QRL network deployments)'}), | ||
} | ||
|
||
module.exports = {Balance} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* global before */ | ||
const assert = require('assert') | ||
const spawn = require('child_process').spawn | ||
|
||
const processFlags = { | ||
detached: true, | ||
stdio: 'inherit', | ||
} | ||
|
||
describe('balance', () => { | ||
let args = [ | ||
'balance', | ||
'Q010500bc576efa69fd6cbc854f2224f149f0b0a4d18fcb30c1feab64781245f4f27a61874227f3', | ||
'-a', | ||
'https://brooklyn.theqrl.org/nottheapi/', | ||
] | ||
let exitCode | ||
before(done => { | ||
let process = spawn('./bin/run', args, processFlags) | ||
process.on('exit', code => { | ||
exitCode = code | ||
done() | ||
}) | ||
}) | ||
it('exit code should be non-0 if API is down', () => { | ||
assert.notStrictEqual(exitCode, 0) | ||
}) | ||
}) | ||
|
||
describe('balance', () => { | ||
let args = [ | ||
'balance', | ||
] | ||
let exitCode | ||
before(done => { | ||
let process = spawn('./bin/run', args, processFlags) | ||
process.on('exit', code => { | ||
exitCode = code | ||
done() | ||
}) | ||
}) | ||
it('exit code should be non-0 if passed without an argument', () => { | ||
assert.notStrictEqual(exitCode, 0) | ||
}) | ||
}) | ||
|
||
describe('balance', () => { | ||
let args = [ | ||
'balance', | ||
'Q010500bc576efa69fd6cbc854f2224f149f0b0a4d18fcb30c1feab64781245f4f27a61874227f3', | ||
] | ||
let exitCode | ||
before(done => { | ||
let process = spawn('./bin/run', args, processFlags) | ||
process.on('exit', code => { | ||
exitCode = code | ||
done() | ||
}) | ||
}) | ||
it('exit code should be 0 if passed with a valid address as argument', () => { | ||
assert.strictEqual(exitCode, 0) | ||
}) | ||
}) | ||
|
||
describe('balance', () => { | ||
let args = [ | ||
'balance', | ||
'Q010500bc576efa69fd6cbc854f2224f149f0b0a4d18fcb30c1feab64781245f4f27a61874227f3', | ||
'-s', | ||
] | ||
let exitCode | ||
before(done => { | ||
let process = spawn('./bin/run', args, processFlags) | ||
process.on('exit', code => { | ||
exitCode = code | ||
done() | ||
}) | ||
}) | ||
it('exit code should be 0 if passed with a valid address and a -s flag', () => { | ||
assert.strictEqual(exitCode, 0) | ||
}) | ||
}) | ||
|
||
describe('balance', () => { | ||
let args = [ | ||
'balance', | ||
'Q010500bc576efa69fd6cbc854f2224f149f0b0a4d18fcb30c1feab64781245f4f27a61874227f3', | ||
'-q', | ||
] | ||
let exitCode | ||
before(done => { | ||
let process = spawn('./bin/run', args, processFlags) | ||
process.on('exit', code => { | ||
exitCode = code | ||
done() | ||
}) | ||
}) | ||
it('exit code should be 0 if passed with a valid address and a -q flag', () => { | ||
assert.strictEqual(exitCode, 0) | ||
}) | ||
}) | ||
|
||
describe('balance', () => { | ||
let args = [ | ||
'balance', | ||
'Q010500bc576efa69fd6cbc854f2224f149f0b0a4d18fcb30c1feab64781245f4f27a61874227f3', | ||
'-s', | ||
'-q', | ||
] | ||
let exitCode | ||
before(done => { | ||
let process = spawn('./bin/run', args, processFlags) | ||
process.on('exit', code => { | ||
exitCode = code | ||
done() | ||
}) | ||
}) | ||
it('exit code should be 0 if passed with a valid address, -s and -q flags', () => { | ||
assert.strictEqual(exitCode, 0) | ||
}) | ||
}) | ||
|
||
describe('balance', () => { | ||
let args = [ | ||
'balance', | ||
'Q010500bc576efa69fd6cbc854f2224f149f0b0a4d18fcb30c1feab64781245f4f27a61874227f4', | ||
] | ||
let exitCode | ||
before(done => { | ||
let process = spawn('./bin/run', args, processFlags) | ||
process.on('exit', code => { | ||
exitCode = code | ||
done() | ||
}) | ||
}) | ||
it('exit code should be non-0 if passed with an invalid address as argument', () => { | ||
assert.notStrictEqual(exitCode, 0) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.