From 72d560e407e9491851c68cc19f446d69f7e3530d Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Tue, 17 Nov 2020 14:41:04 -0800 Subject: [PATCH] fix(price): Adding BCHA price --- package.json | 2 +- src/price.js | 35 ++++++++++++++++++++++++++++++++++ test/integration/bchn/price.js | 9 +++++++++ test/integration/price.js | 9 +++++++++ test/unit/price.js | 13 ++++++++++++- 5 files changed, 66 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 5defe20..2ce217d 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "test:integration:local": "RESTURL=http://localhost:3000/v3/ mocha --timeout 30000 test/integration", "test:integration:local:bchn": "RESTURL=http://localhost:3000/v3/ bash test/integration/test-free-tier-bchn.sh", "test:integration:local:testnet": "RESTURL=http://localhost:4000/v3/ mocha --timeout 30000 test/integration/testnet", - "test:temp": "mocha --timeout 30000 -g '#Encryption' test/integration/", + "test:temp": "RESTURL=http://localhost:3000/v3/ mocha --timeout 30000 -g '#price' test/integration/", "test:integration:api": "RESTURL=https://api.fullstack.cash/v3/ mocha --timeout 30000 test/integration", "coverage": "nyc report --reporter=text-lcov | coveralls", "coverage:report": "nyc --reporter=html mocha --timeout 25000 test/unit/", diff --git a/src/price.js b/src/price.js index 2bc3743..b6dd6cb 100644 --- a/src/price.js +++ b/src/price.js @@ -140,6 +140,41 @@ class Price { else throw err } } + + /** + * @api price.getBchaUsd() getBchaUsd() + * @apiName Price getBchaUsd() + * @apiGroup Price + * @apiDescription Return current price of BCHA in USD. + * This endpoint gets the USD price of BCHA from the Coinex API. The price + * comes from bch-api, so it has a better chance of working in Tor. + * + * @apiExample Example usage: + *(async () => { + * try { + * let current = await bchjs.Price.getBchaUsd(); + * console.log(current); + * } catch(err) { + * console.err(err) + * } + *})() + * + * // 18.81 + */ + async getBchaUsd() { + try { + const response = await this.axios.get( + `${this.restURL}price/bchausd`, + _this.axiosOptions + ) + // console.log(`response.data: ${JSON.stringify(response.data, null, 2)}`) + + return response.data.usd + } catch (err) { + if (err.response && err.response.data) throw err.response.data + else throw err + } + } } module.exports = Price diff --git a/test/integration/bchn/price.js b/test/integration/bchn/price.js index 42ed1e6..ed203bd 100644 --- a/test/integration/bchn/price.js +++ b/test/integration/bchn/price.js @@ -21,6 +21,15 @@ describe("#price", () => { }) }) + describe("#getBchaUsd", () => { + it("should get the USD price of BCHA", async () => { + const result = await bchjs.Price.getBchaUsd() + console.log(result) + + assert.isNumber(result) + }) + }) + describe("#rates", () => { it("should get the price of BCH in several currencies", async () => { const result = await bchjs.Price.rates() diff --git a/test/integration/price.js b/test/integration/price.js index 1648405..a0eb20f 100644 --- a/test/integration/price.js +++ b/test/integration/price.js @@ -25,6 +25,15 @@ describe("#price", () => { }) }) + describe("#getBchaUsd", () => { + it("should get the USD price of BCHA", async () => { + const result = await bchjs.Price.getBchaUsd() + console.log(result) + + assert.isNumber(result) + }) + }) + describe("#rates", () => { it("should get the price of BCH in several currencies", async () => { const result = await bchjs.Price.rates() diff --git a/test/unit/price.js b/test/unit/price.js index a5dce78..23f1ee7 100644 --- a/test/unit/price.js +++ b/test/unit/price.js @@ -5,7 +5,7 @@ const sinon = require("sinon") const mockDataLib = require("./fixtures/price-mocks") let mockData -describe("#Price", () => { +describe("#price", () => { let sandbox let bchjs @@ -56,4 +56,15 @@ describe("#Price", () => { assert.property(result, "CAD") }) }) + + describe("#getBchaUsd", () => { + it("should get the USD price of BCHA", async () => { + sandbox.stub(bchjs.Price.axios, "get").resolves({ data: { usd: 18.87 } }) + + const result = await bchjs.Price.getBchaUsd() + // console.log(result) + + assert.isNumber(result) + }) + }) })