use findUSDMarketPrice to get btc price

This commit is contained in:
c9s 2022-05-04 17:56:03 +08:00
parent 413c5c0479
commit 5662c5c680
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -27,6 +27,15 @@ func (b Balance) Total() fixedpoint.Value {
return b.Available.Add(b.Locked) return b.Available.Add(b.Locked)
} }
func (b Balance) Net() fixedpoint.Value {
total := b.Total()
netAsset := b.NetAsset
if netAsset.IsZero() {
netAsset = total.Sub(b.Borrowed)
}
return netAsset
}
func (b Balance) String() (o string) { func (b Balance) String() (o string) {
o = fmt.Sprintf("%s: %s", b.Currency, b.Available.String()) o = fmt.Sprintf("%s: %s", b.Currency, b.Available.String())
@ -88,17 +97,16 @@ func (m BalanceMap) Copy() (d BalanceMap) {
// Assets converts balances into assets with the given prices // Assets converts balances into assets with the given prices
func (m BalanceMap) Assets(prices map[string]fixedpoint.Value, priceTime time.Time) AssetMap { func (m BalanceMap) Assets(prices map[string]fixedpoint.Value, priceTime time.Time) AssetMap {
assets := make(AssetMap) assets := make(AssetMap)
btcusdt, hasBtcPrice := prices["BTCUSDT"]
_, btcInUSD, hasBtcPrice := findUSDMarketPrice("BTC", prices)
for currency, b := range m { for currency, b := range m {
if b.Locked.IsZero() && b.Available.IsZero() && b.Borrowed.IsZero() { if b.Locked.IsZero() && b.Available.IsZero() && b.Borrowed.IsZero() {
continue continue
} }
total := b.Available.Add(b.Locked) total := b.Total()
netAsset := b.NetAsset netAsset := b.Net()
if netAsset.IsZero() {
netAsset = total.Sub(b.Borrowed)
}
asset := Asset{ asset := Asset{
Currency: currency, Currency: currency,
@ -114,7 +122,7 @@ func (m BalanceMap) Assets(prices map[string]fixedpoint.Value, priceTime time.Ti
asset.InUSD = netAsset asset.InUSD = netAsset
asset.PriceInUSD = fixedpoint.One asset.PriceInUSD = fixedpoint.One
if hasBtcPrice && !asset.InUSD.IsZero() { if hasBtcPrice && !asset.InUSD.IsZero() {
asset.InBTC = asset.InUSD.Div(btcusdt) asset.InBTC = asset.InUSD.Div(btcInUSD)
} }
} else { // for crypto } else { // for crypto
if market, usdPrice, ok := findUSDMarketPrice(currency, prices); ok { if market, usdPrice, ok := findUSDMarketPrice(currency, prices); ok {
@ -132,7 +140,7 @@ func (m BalanceMap) Assets(prices map[string]fixedpoint.Value, priceTime time.Ti
} }
if hasBtcPrice && !asset.InUSD.IsZero() { if hasBtcPrice && !asset.InUSD.IsZero() {
asset.InBTC = asset.InUSD.Div(btcusdt) asset.InBTC = asset.InUSD.Div(btcInUSD)
} }
} }
} }