From 0f6f321807ed338971636079d2c718b02c2213bf Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 9 Mar 2020 12:14:18 -0700 Subject: [PATCH 1/2] update price api --- lib/Price.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/Price.ts b/lib/Price.ts index 8dd0530b..a6062f78 100644 --- a/lib/Price.ts +++ b/lib/Price.ts @@ -1,12 +1,23 @@ import axios, { AxiosResponse } from "axios" +export interface MarketData { + name: string + time: number + rate: number +} + export class Price { - public async current(currency: string = "usd"): Promise { + public async current(currency: string = "USD"): Promise { try { + const cur: string = currency.toUpperCase() + const response: AxiosResponse = await axios.get( - `https://index-api.bitcoin.com/api/v0/cash/price/${currency.toLowerCase()}` + `https://markets.api.bitcoin.com/rates/convertor?c=BCH&q=${cur}` ) - return response.data.price + + const marketData: MarketData = response.data[cur] + + return marketData.rate } catch (error) { if (error.response && error.response.data) throw error.response.data else throw error From 85b96d63fb646bec1b8cc1943918368cec6578d4 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 9 Mar 2020 12:37:57 -0700 Subject: [PATCH 2/2] update price unit test --- test/unit/Price.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/test/unit/Price.ts b/test/unit/Price.ts index 093a3e1d..42495acb 100644 --- a/test/unit/Price.ts +++ b/test/unit/Price.ts @@ -25,11 +25,18 @@ describe("#Price", (): void => { describe("#single currency", (): void => { it("should get current price for single currency", async () => { // Mock out data for unit test, to prevent live network call. - const data: any = 39229 - const resolved: any = new Promise(r => r({ data: { price: data } })) + const rate: any = 265.6074 + const time: any = 1583781799 + const name: any = "US Dollar" + const currency: any = "USD" + + const resolved: any = new Promise(r => + r({ data: { [currency]: { name, time, rate } } }) + ) + sandbox.stub(axios, "get").returns(resolved) - const result = await bitbox.Price.current("usd") + const result = await bitbox.Price.current(currency) //console.log(`result: ${JSON.stringify(result, null, 2)}`) assert.isNumber(result)