From 75adb8f3c3cca825c1fe059d8c804caf0f3da88c Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 4 May 2022 17:27:51 +0800 Subject: [PATCH] fix usd prices caculation --- pkg/types/balance.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/pkg/types/balance.go b/pkg/types/balance.go index 0fa66b349..da2a94923 100644 --- a/pkg/types/balance.go +++ b/pkg/types/balance.go @@ -107,9 +107,14 @@ func (m BalanceMap) Assets(prices map[string]fixedpoint.Value, priceTime time.Ti NetAsset: netAsset, } - usdMarkets := []string{currency + "USDT", currency + "USDC", currency + "USD", "USDT" + currency} - for _, market := range usdMarkets { - if usdPrice, ok := prices[market]; ok { + if strings.HasPrefix(currency,"USD") { // for usd + asset.InUSD = netAsset + asset.PriceInUSD = fixedpoint.One + if hasBtcPrice && !asset.InUSD.IsZero() { + asset.InBTC = asset.InUSD.Div(btcusdt) + } + } else { // for crypto + if market, usdPrice, ok := findUSDMarketPrice(currency, prices) ; ok { // this includes USDT, USD, USDC and so on if strings.HasPrefix(market, "USD") { if !asset.Total.IsZero() { @@ -127,8 +132,9 @@ func (m BalanceMap) Assets(prices map[string]fixedpoint.Value, priceTime time.Ti asset.InBTC = asset.InUSD.Div(btcusdt) } } - assets[currency] = asset } + + assets[currency] = asset } return assets @@ -148,3 +154,13 @@ func (m BalanceMap) Print() { } } + +func findUSDMarketPrice(currency string, prices map[string]fixedpoint.Value) (string, fixedpoint.Value, bool) { + usdMarkets := []string{currency + "USDT", currency + "USDC", currency + "USD", "USDT" + currency} + for _, market := range usdMarkets { + if usdPrice, ok := prices[market]; ok { + return market, usdPrice, ok + } + } + return "", fixedpoint.Zero, false +}