FiatRates: store rates as float64 instead of json.Number
parent
14c64410f7
commit
bef572c494
|
@ -303,7 +303,7 @@ type BalanceHistory struct {
|
|||
Txs uint32 `json:"txs"`
|
||||
ReceivedSat *Amount `json:"received"`
|
||||
SentSat *Amount `json:"sent"`
|
||||
FiatRate string `json:"fiatRate,omitempty"`
|
||||
FiatRate float64 `json:"fiatRate,omitempty"`
|
||||
Txid string `json:"txid,omitempty"`
|
||||
}
|
||||
|
||||
|
|
|
@ -925,7 +925,7 @@ func (w *Worker) setFiatRateToBalanceHistories(histories BalanceHistories, fiat
|
|||
continue
|
||||
}
|
||||
if rate, found := ticker.Rates[fiat]; found {
|
||||
bh.FiatRate = string(rate)
|
||||
bh.FiatRate = rate
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
@ -1153,7 +1153,7 @@ func (w *Worker) getFiatRatesResult(currency string, ticker *db.CurrencyRatesTic
|
|||
Rates: ticker.Rates,
|
||||
}, nil
|
||||
}
|
||||
rates := make(map[string]json.Number, 1)
|
||||
rates := make(map[string]float64, 1)
|
||||
timestamp := ticker.Timestamp.UTC().Unix()
|
||||
if rate, found := ticker.Rates[currency]; !found {
|
||||
return nil, NewAPIError(fmt.Sprintf("Currency %q is not available for timestamp %d.", currency, timestamp), true)
|
||||
|
|
|
@ -37,14 +37,14 @@ const FiatRatesTimeFormat = "20060102150405" // YYYYMMDDhhmmss
|
|||
// CurrencyRatesTicker contains coin ticker data fetched from API
|
||||
type CurrencyRatesTicker struct {
|
||||
Timestamp *time.Time // return as unix timestamp in API
|
||||
Rates map[string]json.Number
|
||||
Rates map[string]float64
|
||||
}
|
||||
|
||||
// ResultTickerAsString contains formatted CurrencyRatesTicker data
|
||||
type ResultTickerAsString struct {
|
||||
Timestamp int64 `json:"ts,omitempty"`
|
||||
Rates map[string]json.Number `json:"rates,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Timestamp int64 `json:"ts,omitempty"`
|
||||
Rates map[string]float64 `json:"rates,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// ResultTickersAsString contains a formatted CurrencyRatesTicker list
|
||||
|
|
|
@ -9,7 +9,6 @@ import (
|
|||
"blockbook/tests/dbtestdata"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"math/big"
|
||||
"os"
|
||||
|
@ -1131,20 +1130,26 @@ func TestRocksTickers(t *testing.T) {
|
|||
ts1, _ := time.Parse(FiatRatesTimeFormat, "20190628000000")
|
||||
ticker1 := &CurrencyRatesTicker{
|
||||
Timestamp: &ts1,
|
||||
Rates: map[string]json.Number{
|
||||
"usd": "20000",
|
||||
Rates: map[string]float64{
|
||||
"usd": 20000,
|
||||
},
|
||||
}
|
||||
|
||||
ts2, _ := time.Parse(FiatRatesTimeFormat, "20190629000000")
|
||||
ticker2 := &CurrencyRatesTicker{
|
||||
Timestamp: &ts2,
|
||||
Rates: map[string]json.Number{
|
||||
"usd": "30000",
|
||||
Rates: map[string]float64{
|
||||
"usd": 30000,
|
||||
},
|
||||
}
|
||||
d.FiatRatesStoreTicker(ticker1)
|
||||
err := d.FiatRatesStoreTicker(ticker1)
|
||||
if err != nil {
|
||||
t.Errorf("Error storing ticker! %v", err)
|
||||
}
|
||||
d.FiatRatesStoreTicker(ticker2)
|
||||
if err != nil {
|
||||
t.Errorf("Error storing ticker! %v", err)
|
||||
}
|
||||
|
||||
ticker, err := d.FiatRatesFindTicker(&key) // should find the closest key (ticker1)
|
||||
if err != nil {
|
||||
|
|
|
@ -100,7 +100,7 @@ func (cg *Coingecko) getTicker(timestamp *time.Time) (*db.CurrencyRatesTicker, e
|
|||
|
||||
type FiatRatesResponse struct {
|
||||
MarketData struct {
|
||||
Prices map[string]json.Number `json:"current_price"`
|
||||
Prices map[string]float64 `json:"current_price"`
|
||||
} `json:"market_data"`
|
||||
}
|
||||
|
||||
|
|
|
@ -143,7 +143,7 @@ func (rd *RatesDownloader) findEarliestMarketData() (*time.Time, error) {
|
|||
// syncLatest downloads the latest FiatRates data every rd.PeriodSeconds
|
||||
func (rd *RatesDownloader) syncLatest() error {
|
||||
timer := time.NewTimer(rd.periodSeconds)
|
||||
var lastTickerRates map[string]json.Number = nil
|
||||
var lastTickerRates map[string]float64 = nil
|
||||
sameTickerCounter := 0
|
||||
for {
|
||||
ticker, err := rd.downloader.getTicker(nil)
|
||||
|
|
|
@ -154,7 +154,7 @@ func newPostRequest(u string, body string) *http.Request {
|
|||
return r
|
||||
}
|
||||
|
||||
func insertFiatRate(date string, rates map[string]json.Number, d *db.RocksDB) error {
|
||||
func insertFiatRate(date string, rates map[string]float64, d *db.RocksDB) error {
|
||||
convertedDate, err := db.FiatRatesConvertDate(date)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -168,39 +168,39 @@ func insertFiatRate(date string, rates map[string]json.Number, d *db.RocksDB) er
|
|||
|
||||
// InitTestFiatRates initializes test data for /api/v2/tickers endpoint
|
||||
func InitTestFiatRates(d *db.RocksDB) error {
|
||||
if err := insertFiatRate("20180320020000", map[string]json.Number{
|
||||
"usd": "2000.0",
|
||||
"eur": "1300.0",
|
||||
if err := insertFiatRate("20180320020000", map[string]float64{
|
||||
"usd": 2000.0,
|
||||
"eur": 1300.0,
|
||||
}, d); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := insertFiatRate("20180320030000", map[string]json.Number{
|
||||
"usd": "2001.0",
|
||||
"eur": "1301.0",
|
||||
if err := insertFiatRate("20180320030000", map[string]float64{
|
||||
"usd": 2001.0,
|
||||
"eur": 1301.0,
|
||||
}, d); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := insertFiatRate("20180320040000", map[string]json.Number{
|
||||
"usd": "2002.0",
|
||||
"eur": "1302.0",
|
||||
if err := insertFiatRate("20180320040000", map[string]float64{
|
||||
"usd": 2002.0,
|
||||
"eur": 1302.0,
|
||||
}, d); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := insertFiatRate("20180321055521", map[string]json.Number{
|
||||
"usd": "2003.0",
|
||||
"eur": "1303.0",
|
||||
if err := insertFiatRate("20180321055521", map[string]float64{
|
||||
"usd": 2003.0,
|
||||
"eur": 1303.0,
|
||||
}, d); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := insertFiatRate("20191121140000", map[string]json.Number{
|
||||
"usd": "7814.5",
|
||||
"eur": "7100.0",
|
||||
if err := insertFiatRate("20191121140000", map[string]float64{
|
||||
"usd": 7814.5,
|
||||
"eur": 7100.0,
|
||||
}, d); err != nil {
|
||||
return err
|
||||
}
|
||||
return insertFiatRate("20191121143015", map[string]json.Number{
|
||||
"usd": "7914.5",
|
||||
"eur": "7134.1",
|
||||
return insertFiatRate("20191121143015", map[string]float64{
|
||||
"usd": 7914.5,
|
||||
"eur": 7134.1,
|
||||
}, d)
|
||||
}
|
||||
|
||||
|
@ -569,7 +569,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
|
|||
status: http.StatusOK,
|
||||
contentType: "application/json; charset=utf-8",
|
||||
body: []string{
|
||||
`{"ts":1574344800,"rates":{"eur":7100.0}}`,
|
||||
`{"ts":1574344800,"rates":{"eur":7100}}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -578,7 +578,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
|
|||
status: http.StatusOK,
|
||||
contentType: "application/json; charset=utf-8",
|
||||
body: []string{
|
||||
`{"ts":1521511200,"rates":{"usd":2000.0}}`,
|
||||
`{"ts":1521511200,"rates":{"usd":2000}}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -587,7 +587,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
|
|||
status: http.StatusOK,
|
||||
contentType: "application/json; charset=utf-8",
|
||||
body: []string{
|
||||
`{"ts":1521611721,"rates":{"usd":2003.0}}`,
|
||||
`{"ts":1521611721,"rates":{"usd":2003}}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -785,7 +785,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
|
|||
status: http.StatusOK,
|
||||
contentType: "application/json; charset=utf-8",
|
||||
body: []string{
|
||||
`[{"time":1521514800,"txs":1,"received":"9876","sent":"0","fiatRate":"1301.0"},{"time":1521594000,"txs":1,"received":"9000","sent":"9876","fiatRate":"1303.0"}]`,
|
||||
`[{"time":1521514800,"txs":1,"received":"9876","sent":"0","fiatRate":1301},{"time":1521594000,"txs":1,"received":"9000","sent":"9876","fiatRate":1303}]`,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -821,7 +821,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
|
|||
status: http.StatusOK,
|
||||
contentType: "application/json; charset=utf-8",
|
||||
body: []string{
|
||||
`[{"time":1521514800,"txs":1,"received":"1","sent":"0","fiatRate":"2001.0"}]`,
|
||||
`[{"time":1521514800,"txs":1,"received":"1","sent":"0","fiatRate":2001}]`,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -1286,7 +1286,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
|
|||
"timestamps": []string{"1521507600"},
|
||||
},
|
||||
},
|
||||
want: `{"id":"25","data":{"tickers":[{"ts":1521511200,"rates":{"eur":1300.0}}]}}`,
|
||||
want: `{"id":"25","data":{"tickers":[{"ts":1521511200,"rates":{"eur":1300}}]}}`,
|
||||
},
|
||||
{
|
||||
name: "websocket getFiatRatesForTimestamps multiple timestamps usd",
|
||||
|
@ -1308,7 +1308,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
|
|||
"timestamps": []string{"1570346615", "1574346615"},
|
||||
},
|
||||
},
|
||||
want: `{"id":"27","data":{"tickers":[{"ts":1574344800,"rates":{"eur":7100.0}},{"ts":1574346615,"rates":{"eur":7134.1}}]}}`,
|
||||
want: `{"id":"27","data":{"tickers":[{"ts":1574344800,"rates":{"eur":7100}},{"ts":1574346615,"rates":{"eur":7134.1}}]}}`,
|
||||
},
|
||||
{
|
||||
name: "websocket getFiatRatesForTimestamps multiple timestamps with an error",
|
||||
|
@ -1373,7 +1373,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
|
|||
"fiat": "usd",
|
||||
},
|
||||
},
|
||||
want: `{"id":"33","data":[{"time":1521514800,"txs":1,"received":"1","sent":"0","fiatRate":"2001.0"}]}`,
|
||||
want: `{"id":"33","data":[{"time":1521514800,"txs":1,"received":"1","sent":"0","fiatRate":2001}]}`,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -791,7 +791,7 @@ func (s *WebsocketServer) OnNewTxAddr(tx *bchain.Tx, addrDesc bchain.AddressDesc
|
|||
}
|
||||
}
|
||||
|
||||
func (s *WebsocketServer) broadcastTicker(currency string, rates map[string]json.Number) {
|
||||
func (s *WebsocketServer) broadcastTicker(currency string, rates map[string]float64) {
|
||||
s.fiatRatesSubscriptionsLock.Lock()
|
||||
defer s.fiatRatesSubscriptionsLock.Unlock()
|
||||
as, ok := s.fiatRatesSubscriptions[currency]
|
||||
|
@ -820,7 +820,7 @@ func (s *WebsocketServer) broadcastTicker(currency string, rates map[string]json
|
|||
// OnNewFiatRatesTicker is a callback that broadcasts info about fiat rates affecting subscribed currency
|
||||
func (s *WebsocketServer) OnNewFiatRatesTicker(ticker *db.CurrencyRatesTicker) {
|
||||
for currency, rate := range ticker.Rates {
|
||||
s.broadcastTicker(currency, map[string]json.Number{currency: rate})
|
||||
s.broadcastTicker(currency, map[string]float64{currency: rate})
|
||||
}
|
||||
s.broadcastTicker("!ALL!", ticker.Rates)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue