From dffb00929fd6a5ad67087f112002ea8e0e25fdad Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Wed, 9 Jun 2021 11:40:50 +0200 Subject: [PATCH] fiat: add Currency field to the Price struct Add Currency field to the price struct so that Price can be used to represent any currency. --- fiat/coincap_api.go | 5 +++++ fiat/coincap_api_test.go | 2 ++ fiat/coindesk_api.go | 5 +++++ fiat/coindesk_api_test.go | 2 ++ fiat/fiat.go | 5 ++++- 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/fiat/coincap_api.go b/fiat/coincap_api.go index b4423d9..78cad35 100644 --- a/fiat/coincap_api.go +++ b/fiat/coincap_api.go @@ -15,6 +15,10 @@ import ( const ( // coinCapHistoryAPI is the endpoint we hit for historical price data. coinCapHistoryAPI = "https://api.coincap.io/v2/assets/bitcoin/history" + + // coinCapDefaultCurrency is the currency that the price data returned + // by the Coin Cap API is quoted in. + coinCapDefaultCurrency = "USD" ) // ErrQueryTooLong is returned when we cannot get a granularity level for a @@ -196,6 +200,7 @@ func parseCoinCapData(data []byte) ([]*Price, error) { usdRecords[i] = &Price{ Timestamp: time.Unix(0, ns.Nanoseconds()), Price: decPrice, + Currency: coinCapDefaultCurrency, } } diff --git a/fiat/coincap_api_test.go b/fiat/coincap_api_test.go index 0e79693..8cd97d4 100644 --- a/fiat/coincap_api_test.go +++ b/fiat/coincap_api_test.go @@ -193,10 +193,12 @@ func TestParseCoinCapData(t *testing.T) { { Price: price1, Timestamp: time1, + Currency: coinCapDefaultCurrency, }, { Price: price2, Timestamp: time2, + Currency: coinCapDefaultCurrency, }, } diff --git a/fiat/coindesk_api.go b/fiat/coindesk_api.go index aeeff6b..894c3f3 100644 --- a/fiat/coindesk_api.go +++ b/fiat/coindesk_api.go @@ -17,6 +17,10 @@ const ( // coinDeskTimeFormat is the date format used by coindesk. coinDeskTimeFormat = "2006-01-02" + + // coinDeskDefaultCurrency is the default currency that the price data + // returned by the Coin Desk API is quoted in. + coinDeskDefaultCurrency = "USD" ) // coinDeskAPI implements the fiatBackend interface. @@ -65,6 +69,7 @@ func parseCoinDeskData(data []byte) ([]*Price, error) { usdRecords = append(usdRecords, &Price{ Timestamp: timestamp, Price: decimal.NewFromFloat(price), + Currency: coinDeskDefaultCurrency, }) } diff --git a/fiat/coindesk_api_test.go b/fiat/coindesk_api_test.go index 0c3a612..a648210 100644 --- a/fiat/coindesk_api_test.go +++ b/fiat/coindesk_api_test.go @@ -59,11 +59,13 @@ func TestParseCoinDeskData(t *testing.T) { { Price: price, Timestamp: timestamp, + Currency: coinDeskDefaultCurrency, }, } require.True(t, expectedPrices[0].Price.Equal(prices[0].Price)) require.True(t, expectedPrices[0].Timestamp.Equal(prices[0].Timestamp)) + require.Equal(t, expectedPrices[0].Currency, prices[0].Currency) }) } } diff --git a/fiat/fiat.go b/fiat/fiat.go index 798c9cc..2fb0edc 100644 --- a/fiat/fiat.go +++ b/fiat/fiat.go @@ -23,7 +23,7 @@ var ( errRetriesFailed = errors.New("could not get data within max retries") ) -// Price represents the Bitcoin price in USD at a certain time. +// Price represents the Bitcoin price in the given currency at a certain time. type Price struct { // Timestamp is the time at which the BTC price is quoted. Timestamp time.Time @@ -31,6 +31,9 @@ type Price struct { // Price is the fiat price for the given currency for 1 BTC at the // given timestamp. Price decimal.Decimal + + // Currency is the code of the currency that the Price is quoted in. + Currency string } // retryQuery calls an api until it succeeds, or we hit our maximum retries.