From 4b564510e00564e1e812add59afce5713a8f3bbe Mon Sep 17 00:00:00 2001 From: Vladyslav Burzakovskyy Date: Wed, 22 Jan 2020 12:37:33 +0100 Subject: [PATCH] balanceHistory: return all currencies if the "currency" parameter is empty --- api/worker.go | 12 ++++++------ api/xpub.go | 8 +++----- server/public_test.go | 20 ++++++++++---------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/api/worker.go b/api/worker.go index 6477a5ed..7d34822c 100644 --- a/api/worker.go +++ b/api/worker.go @@ -925,7 +925,9 @@ func (w *Worker) setFiatRateToBalanceHistories(histories BalanceHistories, curre } else if ticker == nil { continue } - if len(currencies) > 0 { + if len(currencies) == 0 { + bh.FiatRates = ticker.Rates + } else { rates := make(map[string]float64) for _, currency := range currencies { currency = strings.ToLower(currency) @@ -967,11 +969,9 @@ func (w *Worker) GetBalanceHistory(address string, fromTimestamp, toTimestamp in } } bha := bhs.SortAndAggregate(groupBy) - if len(currencies) > 0 { - err = w.setFiatRateToBalanceHistories(bha, currencies) - if err != nil { - return nil, err - } + err = w.setFiatRateToBalanceHistories(bha, currencies) + if err != nil { + return nil, err } glog.Info("GetBalanceHistory ", address, ", blocks ", fromHeight, "-", toHeight, ", count ", len(bha), " finished in ", time.Since(start)) return bha, nil diff --git a/api/xpub.go b/api/xpub.go index 7f9e8440..6fb7470b 100644 --- a/api/xpub.go +++ b/api/xpub.go @@ -623,11 +623,9 @@ func (w *Worker) GetXpubBalanceHistory(xpub string, fromTimestamp, toTimestamp i } } bha := bhs.SortAndAggregate(groupBy) - if len(currencies) > 0 { - err = w.setFiatRateToBalanceHistories(bha, currencies) - if err != nil { - return nil, err - } + err = w.setFiatRateToBalanceHistories(bha, currencies) + if err != nil { + return nil, err } glog.Info("GetUtxoBalanceHistory ", xpub[:16], ", blocks ", fromHeight, "-", toHeight, ", count ", len(bha), ", finished in ", time.Since(start)) return bha, nil diff --git a/server/public_test.go b/server/public_test.go index 2c91271b..802df350 100644 --- a/server/public_test.go +++ b/server/public_test.go @@ -1,4 +1,4 @@ -// +build unittest +// build unittest package server @@ -767,7 +767,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":"12345","sent":"0"},{"time":1521594000,"txs":1,"received":"0","sent":"12345"}]`, + `[{"time":1521514800,"txs":1,"received":"12345","sent":"0","rates":{"eur":1301,"usd":2001}},{"time":1521594000,"txs":1,"received":"0","sent":"12345","rates":{"eur":1303,"usd":2003}}]`, }, }, { @@ -776,7 +776,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"},{"time":1521594000,"txs":1,"received":"9000","sent":"9876"}]`, + `[{"time":1521514800,"txs":1,"received":"9876","sent":"0","rates":{"eur":1301,"usd":2001}},{"time":1521594000,"txs":1,"received":"9000","sent":"9876","rates":{"eur":1303,"usd":2003}}]`, }, }, { @@ -794,7 +794,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":"12345","sent":"0"}]`, + `[{"time":1521514800,"txs":1,"received":"12345","sent":"0","rates":{"eur":1301,"usd":2001}}]`, }, }, { @@ -803,7 +803,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"},{"time":1521594000,"txs":1,"received":"118641975500","sent":"1"}]`, + `[{"time":1521514800,"txs":1,"received":"1","sent":"0","rates":{"eur":1301,"usd":2001}},{"time":1521594000,"txs":1,"received":"118641975500","sent":"1","rates":{"eur":1303,"usd":2003}}]`, }, }, { @@ -812,7 +812,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"}]`, + `[{"time":1521514800,"txs":1,"received":"1","sent":"0","rates":{"eur":1301,"usd":2001}}]`, }, }, { @@ -830,7 +830,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) { status: http.StatusOK, contentType: "application/json; charset=utf-8", body: []string{ - `[{"time":1521594000,"txs":1,"received":"118641975500","sent":"1"}]`, + `[{"time":1521594000,"txs":1,"received":"118641975500","sent":"1","rates":{"eur":1303,"usd":2003}}]`, }, }, { @@ -1350,7 +1350,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) { "descriptor": "mtGXQvBowMkBpnhLckhxhbwYK44Gs9eEtz", }, }, - want: `{"id":"31","data":[{"time":1521514800,"txs":1,"received":"12345","sent":"0"},{"time":1521594000,"txs":1,"received":"0","sent":"12345"}]}`, + want: `{"id":"31","data":[{"time":1521514800,"txs":1,"received":"12345","sent":"0","rates":{"eur":1301,"usd":2001}},{"time":1521594000,"txs":1,"received":"0","sent":"12345","rates":{"eur":1303,"usd":2003}}]}`, }, { name: "websocket getBalanceHistory xpub", @@ -1360,7 +1360,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) { "descriptor": dbtestdata.Xpub, }, }, - want: `{"id":"32","data":[{"time":1521514800,"txs":1,"received":"1","sent":"0"},{"time":1521594000,"txs":1,"received":"118641975500","sent":"1"}]}`, + want: `{"id":"32","data":[{"time":1521514800,"txs":1,"received":"1","sent":"0","rates":{"eur":1301,"usd":2001}},{"time":1521594000,"txs":1,"received":"118641975500","sent":"1","rates":{"eur":1303,"usd":2003}}]}`, }, { name: "websocket getBalanceHistory xpub from=1521504000&to=1521590400 currencies=[usd]", @@ -1399,7 +1399,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) { "currencies": []string{}, }, }, - want: `{"id":"35","data":[{"time":1521514800,"txs":1,"received":"1","sent":"0"}]}`, + want: `{"id":"35","data":[{"time":1521514800,"txs":1,"received":"1","sent":"0","rates":{"eur":1301,"usd":2001}}]}`, }, }