Skip to content

Commit

Permalink
feat: balance command & unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jplomas committed Jul 22, 2019
1 parent 962e9ff commit cc5ed39
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 12 deletions.
48 changes: 41 additions & 7 deletions src/commands/balance.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
/* global QRLLIB */
/* eslint new-cap: 0 */

const {Command, flags} = require('@oclif/command')
const {red} = require('kleur')
// const Crypto = require('crypto')
// const bech32 = require('bech32')
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} = this.parse(Balance)
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`)
}
}
}

Expand All @@ -32,4 +60,10 @@ Balance.args = [
},
]

module.exports = Balance
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}
94 changes: 89 additions & 5 deletions test/commands/balance.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
/* 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)
let process = spawn('./bin/run', args, processFlags)
process.on('exit', code => {
exitCode = code
done()
Expand All @@ -18,14 +44,14 @@ describe('balance', () => {
})
})

describe('balance Q010500bc576efa69fd6cbc854f2224f149f0b0a4d18fcb30c1feab64781245f4f27a61874227f3', () => {
describe('balance', () => {
let args = [
'balance',
'Q010500bc576efa69fd6cbc854f2224f149f0b0a4d18fcb30c1feab64781245f4f27a61874227f3',
]
let exitCode
before(done => {
let process = spawn('./bin/run', args)
let process = spawn('./bin/run', args, processFlags)
process.on('exit', code => {
exitCode = code
done()
Expand All @@ -36,14 +62,72 @@ describe('balance Q010500bc576efa69fd6cbc854f2224f149f0b0a4d18fcb30c1feab6478124
})
})

describe('balance Q010500bc576efa69fd6cbc854f2224f149f0b0a4d18fcb30c1feab64781245f4f27a61874227f4', () => {
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)
let process = spawn('./bin/run', args, processFlags)
process.on('exit', code => {
exitCode = code
done()
Expand Down

0 comments on commit cc5ed39

Please sign in to comment.