From 4b95fa127c7e541581a9a5f047216fa3ebdf8170 Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Date: Tue, 2 Mar 2021 22:02:32 -0400 Subject: [PATCH] feat(price): Added BCH price feed to bch-api from Coinex --- src/routes/v4/price.js | 37 ++++++++++++++++++++ test/v4/integration/price.js | 8 +++++ test/v4/price.js | 66 ++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) diff --git a/src/routes/v4/price.js b/src/routes/v4/price.js index 4e2532c..3fc2977 100644 --- a/src/routes/v4/price.js +++ b/src/routes/v4/price.js @@ -27,6 +27,9 @@ class Price { this.coinexPriceUrl = 'https://api.coinex.com/v1/market/ticker?market=bchausdt' + this.bchCoinexPriceUrl = + 'https://api.coinex.com/v1/market/ticker?market=bchusdt' + this.router = express.Router() this.router.get('/', _this.root) this.router.get('/usd', _this.getUSD) @@ -150,6 +153,40 @@ class Price { return _this.errorHandler(err, res) } } + + /** + * @api {get} /price/bchusd Get the USD price of BCH + * @apiName Get the USD price of BCH + * @apiGroup Price + * @apiDescription Get the USD price of BCH from Coinex. + * + * + * @apiExample Example usage: + * curl -X GET "https://api.fullstack.cash/v4/price/bchusd" -H "accept: application/json" + * + */ + async getBCHUSD (req, res, next) { + try { + // Request options + const opt = { + method: 'get', + baseURL: _this.bchCoinexPriceUrl, + timeout: 15000 + } + + const response = await axios.request(opt) + // console.log(`response.data: ${JSON.stringify(response.data, null, 2)}`) + + const price = Number(response.data.data.ticker.last) + + return res.json({ usd: price }) + } catch (err) { + // Write out error to error log. + wlogger.error('Error in price.js/getBCHUSD().', err) + + return _this.errorHandler(err, res) + } + } } module.exports = Price diff --git a/test/v4/integration/price.js b/test/v4/integration/price.js index 2bbec83..0b96988 100644 --- a/test/v4/integration/price.js +++ b/test/v4/integration/price.js @@ -39,6 +39,14 @@ describe('#price', () => { const result = await price.getBCHAUSD(req, res) console.log(`result: ${util.inspect(result)}`) + assert.isNumber(result.usd) + }) + }) + describe('#getBCHUSD', () => { + it('should get the USD price of BCH', async () => { + const result = await price.getBCHUSD(req, res) + console.log(`result: ${util.inspect(result)}`) + assert.isNumber(result.usd) }) }) diff --git a/test/v4/price.js b/test/v4/price.js index 5b15e6a..b9615bb 100644 --- a/test/v4/price.js +++ b/test/v4/price.js @@ -277,4 +277,70 @@ describe('#PriceRouter', () => { assert.isNumber(result.usd) }) }) + + describe('#getBCHUSD', () => { + // const getNetworkInfo = controlRoute.testableComponents.getNetworkInfo + + it('should throw 500 when network issues', async () => { + uut.bchCoinexPriceUrl = 'http://fakeurl/api/' + + await uut.getBCHUSD(req, res) + // console.log(`result: ${util.inspect(result)}`) + + 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('returns proper error when downstream service stalls', async () => { + // Mock the timeout error. + sandbox.stub(uut.axios, 'request').throws({ code: 'ECONNABORTED' }) + + const result = await uut.getBCHUSD(req, res) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.') + assert.include( + result.error, + 'Could not communicate with full node', + 'Error message expected' + ) + }) + + it('returns proper error when downstream service is down', async () => { + // Mock the timeout error. + sandbox.stub(uut.axios, 'request').throws({ code: 'ECONNREFUSED' }) + + const result = await uut.getBCHUSD(req, res) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.') + assert.include( + result.error, + 'Could not communicate with full node', + 'Error message expected' + ) + }) + + it('should get the USD price of BCH', async () => { + // Mock the RPC call for unit tests. + if (process.env.TEST === 'unit') { + sandbox + .stub(uut.axios, 'request') + .resolves({ data: mockData.mockCoinexFeed }) + } + + const result = await uut.getBCHUSD(req, res) + // console.log(`result: ${util.inspect(result)}`) + + assert.isNumber(result.usd) + }) + }) })