Compare commits

...
Author SHA1 Message Date
ethan 0c737b119e moved endpoint to price library plus unit/integrationt tests 2021-11-01 14:13:16 +11:00
Chris Troutner 04a3348610 Merge pull request #166 from ethanmackie/master
Adding a getCurrencyInfo endpoint to bch-api in both v4 and v5 routes
2021-10-29 09:48:14 -07:00
ethan 4af75f2cee Merge branch 'master' of https://github.com/ethanmackie/bch-api 2021-10-29 10:53:36 +11:00
ethan 45887fb24b Added a getCurrencyInfo endpoint to v4 and v5 routes 2021-10-29 10:44:24 +11:00
ethan 8dbc5c3e40 Added a getCurrencyInfo endpoint to v4 and v5 routes 2021-10-28 18:25:14 +11:00
4 changed files with 93 additions and 2 deletions
+36
View File
@@ -36,6 +36,7 @@ class Price {
this.router.get('/rates', _this.getBCHRate) this.router.get('/rates', _this.getBCHRate)
this.router.get('/bchausd', _this.getBCHAUSD) this.router.get('/bchausd', _this.getBCHAUSD)
this.router.get('/bchusd', _this.getBCHUSD) this.router.get('/bchusd', _this.getBCHUSD)
this.router.get('/getcurrencyinfo', this.getCurrencyInfo)
} }
// DRY error handler. // DRY error handler.
@@ -56,6 +57,41 @@ class Price {
return res.json({ status: 'price' }) return res.json({ status: 'price' })
} }
/**
* @api {get} /price/getcurrencyinfo Get information about the currency.
* @apiName getcurrencyinfo
* @apiGroup Price
* @apiDescription Returns an object containing the currency's ticker, satoshisperunit and decimals.
*
*
* @apiExample Example usage:
* curl -X GET "https://api.fullstack.cash/v5/price/getcurrencyinfo" -H "accept: application/json"
*
*
*/
async getCurrencyInfo (req, res, next) {
try {
const {
BitboxHTTP,
// username,
// password,
requestConfig
} = routeUtils.setEnvVars()
requestConfig.data.id = 'getcurrencyinfo'
requestConfig.data.method = 'getcurrencyinfo'
const response = await BitboxHTTP(requestConfig)
return res.json(response.data.result)
} catch (err) {
// Write out error to error log.
wlogger.error('Error in price.js/getCurrencyInfo().', err)
return _this.errorHandler(err, res)
}
}
/** /**
* @api {get} /price/usd Get the USD price of BCH * @api {get} /price/usd Get the USD price of BCH
* @apiName Get the USD price of BCH * @apiName Get the USD price of BCH
+10 -1
View File
@@ -11,7 +11,7 @@ const assert = require('chai').assert
const util = require('util') const util = require('util')
util.inspect.defaultOptions = { depth: 1 } util.inspect.defaultOptions = { depth: 1 }
const Price = require('../../../src/routes/v4/price') const Price = require('../../../src/routes/v5/price')
const price = new Price() const price = new Price()
const { mockReq, mockRes } = require('../mocks/express-mocks') const { mockReq, mockRes } = require('../mocks/express-mocks')
@@ -50,4 +50,13 @@ describe('#price', () => {
assert.isNumber(result.usd) assert.isNumber(result.usd)
}) })
}) })
describe('#getCurrencyInfo', () => {
it('should get the full node currency settings', async () => {
const result = await price.getCurrencyInfo(req, res)
console.log(`result: ${util.inspect(result)}`)
assert.isNumber(result.satoshisperunit)
assert.isNumber(result.decimals)
})
})
}) })
+7
View File
@@ -235,7 +235,14 @@ const mockCoinexFeed = {
message: 'OK' message: 'OK'
} }
const mockCurrencyInfo = {
ticker: 'BCHA',
satoshisperunit: 100000000,
decimals: 8
}
module.exports = { module.exports = {
mockCoinbaseFeed, mockCoinbaseFeed,
mockCurrencyInfo,
mockCoinexFeed mockCoinexFeed
} }
+40 -1
View File
@@ -12,8 +12,9 @@
const chai = require('chai') const chai = require('chai')
const assert = chai.assert const assert = chai.assert
const sinon = require('sinon') const sinon = require('sinon')
const nock = require('nock') // HTTP mocking
const Price = require('../../src/routes/v5/price') const Price = require('../../src/routes/v5/price')
let uut let uut
// Mocking data. // Mocking data.
@@ -31,6 +32,7 @@ describe('#PriceRouter', () => {
before(() => { before(() => {
// Set default environment variables for unit tests. // Set default environment variables for unit tests.
if (!process.env.TEST) process.env.TEST = 'unit' if (!process.env.TEST) process.env.TEST = 'unit'
process.env.RPC_BASEURL = 'http://fakenode:fakeport'
}) })
// Setup the mocks before each test. // Setup the mocks before each test.
@@ -44,12 +46,19 @@ describe('#PriceRouter', () => {
req.body = {} req.body = {}
req.query = {} req.query = {}
// Activate nock if it's inactive.
if (!nock.isActive()) nock.activate()
sandbox = sinon.createSandbox() sandbox = sinon.createSandbox()
uut = new Price() uut = new Price()
}) })
afterEach(() => { afterEach(() => {
// Clean up HTTP mocks.
nock.cleanAll() // clear interceptor list.
nock.restore()
// Restore Sandbox // Restore Sandbox
sandbox.restore() sandbox.restore()
}) })
@@ -343,4 +352,34 @@ describe('#PriceRouter', () => {
assert.isNumber(result.usd) assert.isNumber(result.usd)
}) })
}) })
describe('#getCurrencyInfo', async () => {
it('should throw 500 when network issues', async () => {
await uut.getCurrencyInfo(req, res)
assert.isAbove(
res.statusCode,
499,
'HTTP status code 500 or greater expected.'
)
// console.log(res)
assert.include(
res.output.error,
'Network error: Could not communicate with full node or other external service'
)
})
it('should get the currency settings of the full node', async () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === 'unit') {
// intercept the RPC_BASEURL parameter
nock(`${process.env.RPC_BASEURL}`)
.post((uri) => uri.includes('/'))
.reply(200, { result: mockData.mockCurrencyInfo })
}
const result = await uut.getCurrencyInfo(req, res)
assert.hasAllKeys(result, ['ticker', 'satoshisperunit', 'decimals'])
})
})
}) })