Skip to content

Commit

Permalink
Merge pull request #1 from jplomas/master
Browse files Browse the repository at this point in the history
feat: balance command added
  • Loading branch information
jplomas authored Jul 22, 2019
2 parents 7850c1f + cc5ed39 commit 8e4eb49
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 25 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"@oclif/plugin-help": "^2",
"@theqrl/validate-qrl-address": "^1.1.0",
"aes256": "^1.0.4",
"axios": "^0.19.0",
"bech32": "^1.1.3",
"bignumber.js": "^9.0.0",
"crypto": "^1.0.1",
"kleur": "^3.0.3",
"ora": "^3.4.0",
Expand Down
69 changes: 69 additions & 0 deletions src/commands/balance.js
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}
4 changes: 3 additions & 1 deletion src/commands/create-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const bech32 = require('bech32')
const ora = require('ora')
const fs = require('fs')
const aes256 = require('aes256')
let QRLLIBLoaded = false

class CreateWallet extends Command {
async run() {
Expand Down Expand Up @@ -59,9 +60,10 @@ class CreateWallet extends Command {
setTimeout(() => {
// Test the QRLLIB object has the str2bin function.
// This is sufficient to tell us QRLLIB has loaded.
if (typeof QRLLIB.str2bin === 'function') {
if (typeof QRLLIB.str2bin === 'function' && QRLLIBLoaded === true) {
callBack()
} else {
QRLLIBLoaded = true
return waitForQRLLIB(callBack)
}
return false
Expand Down
139 changes: 139 additions & 0 deletions test/commands/balance.test.js
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)
})
})
33 changes: 19 additions & 14 deletions test/commands/create-wallet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
const assert = require('assert')
const spawn = require('child_process').spawn

const processFlags = {
detached: true,
stdio: 'inherit',
}

describe('create-wallet', () => {
let args = [
'create-wallet',
]
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 @@ -27,7 +32,7 @@ describe('create-wallet', () => {
]
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 @@ -45,7 +50,7 @@ describe('create-wallet', () => {
]
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 @@ -65,7 +70,7 @@ describe('create-wallet', () => {
]
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 @@ -86,7 +91,7 @@ describe('create-wallet', () => {
]
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 @@ -104,7 +109,7 @@ describe('create-wallet', () => {
]
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 @@ -123,7 +128,7 @@ describe('create-wallet', () => {
]
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 @@ -142,7 +147,7 @@ describe('create-wallet', () => {
]
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 @@ -161,7 +166,7 @@ describe('create-wallet', () => {
]
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 @@ -180,7 +185,7 @@ describe('create-wallet', () => {
]
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 @@ -198,7 +203,7 @@ describe('create-wallet', () => {
]
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 @@ -216,7 +221,7 @@ describe('create-wallet', () => {
]
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 @@ -234,7 +239,7 @@ describe('create-wallet', () => {
]
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 @@ -253,7 +258,7 @@ describe('create-wallet', () => {
]
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
Loading

0 comments on commit 8e4eb49

Please sign in to comment.