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.
This commit is contained in:
Elle Mouton 2021-06-09 11:40:50 +02:00
parent 4a7199f38d
commit dffb00929f
No known key found for this signature in database
GPG key ID: D7D916376026F177
5 changed files with 18 additions and 1 deletions

View file

@ -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,
}
}

View file

@ -193,10 +193,12 @@ func TestParseCoinCapData(t *testing.T) {
{
Price: price1,
Timestamp: time1,
Currency: coinCapDefaultCurrency,
},
{
Price: price2,
Timestamp: time2,
Currency: coinCapDefaultCurrency,
},
}

View file

@ -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,
})
}

View file

@ -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)
})
}
}

View file

@ -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.