Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/Price.ts
Original file line number Diff line number Diff line change
@@ -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<number> {
public async current(currency: string = "USD"): Promise<number> {
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
Expand Down
13 changes: 10 additions & 3 deletions test/unit/Price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down