From 573f8bb221e06bf07e78e813392e0460bc13aafe Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 4 May 2022 19:26:26 +0800 Subject: [PATCH 1/2] use net asset to calculate inUSD --- pkg/types/asset.go | 4 ++-- pkg/types/balance.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/types/asset.go b/pkg/types/asset.go index 76737744c..8440539f3 100644 --- a/pkg/types/asset.go +++ b/pkg/types/asset.go @@ -14,13 +14,13 @@ type Asset struct { Currency string `json:"currency" db:"currency"` Total fixedpoint.Value `json:"total" db:"total"` - NetAsset fixedpoint.Value `json:"netAsset" db:"net_asset"` + NetAsset fixedpoint.Value `json:"netAsset" db:"net_asset"` // InUSD is net asset in USD InUSD fixedpoint.Value `json:"inUSD" db:"net_asset_in_usd"` // InBTC is net asset in BTC - InBTC fixedpoint.Value `json:"inBTC" db:"net_asset_in_btc"` + InBTC fixedpoint.Value `json:"inBTC" db:"net_asset_in_btc"` Time time.Time `json:"time" db:"time"` Locked fixedpoint.Value `json:"lock" db:"lock" ` diff --git a/pkg/types/balance.go b/pkg/types/balance.go index bf3fb4cf0..e5dcc6c2c 100644 --- a/pkg/types/balance.go +++ b/pkg/types/balance.go @@ -129,13 +129,13 @@ func (m BalanceMap) Assets(prices map[string]fixedpoint.Value, priceTime time.Ti if market, usdPrice, ok := findUSDMarketPrice(currency, prices); ok { // this includes USDT, USD, USDC and so on if strings.HasPrefix(market, "USD") { // for prices like USDT/TWD - if !asset.Total.IsZero() { - asset.InUSD = asset.Total.Div(usdPrice) + if !asset.NetAsset.IsZero() { + asset.InUSD = asset.NetAsset.Div(usdPrice) } asset.PriceInUSD = fixedpoint.One.Div(usdPrice) } else { // for prices like BTC/USDT - if !asset.Total.IsZero() { - asset.InUSD = asset.Total.Mul(usdPrice) + if !asset.NetAsset.IsZero() { + asset.InUSD = asset.NetAsset.Mul(usdPrice) } asset.PriceInUSD = usdPrice } From d5b203a9255770b1c4b5926f342c15978fc5ab88 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 4 May 2022 19:32:29 +0800 Subject: [PATCH 2/2] render borrowed in the attachment --- pkg/types/asset.go | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/pkg/types/asset.go b/pkg/types/asset.go index 8440539f3..043fa89c6 100644 --- a/pkg/types/asset.go +++ b/pkg/types/asset.go @@ -76,7 +76,7 @@ func (m AssetMap) Slice() (assets []Asset) { func (m AssetMap) SlackAttachment() slack.Attachment { var fields []slack.AttachmentField - var totalBTC, totalUSD fixedpoint.Value + var netAssetInBTC, netAssetInUSD fixedpoint.Value var assets = m.Slice() @@ -86,26 +86,38 @@ func (m AssetMap) SlackAttachment() slack.Attachment { }) for _, a := range assets { - totalUSD = totalUSD.Add(a.InUSD) - totalBTC = totalBTC.Add(a.InBTC) + netAssetInUSD = netAssetInUSD.Add(a.InUSD) + netAssetInBTC = netAssetInBTC.Add(a.InBTC) } for _, a := range assets { if !a.InUSD.IsZero() { + text := fmt.Sprintf("%s (≈ %s) (≈ %s) (%s)", + a.NetAsset.String(), + USD.FormatMoney(a.InUSD), + BTC.FormatMoney(a.InBTC), + a.InUSD.Div(netAssetInUSD).FormatPercentage(2), + ) + + if !a.Borrowed.IsZero() { + text += fmt.Sprintf(" Borrowed: %s", a.Borrowed.String()) + } + fields = append(fields, slack.AttachmentField{ Title: a.Currency, - Value: fmt.Sprintf("%s (≈ %s) (≈ %s) (%s)", - a.Total.String(), - USD.FormatMoney(a.InUSD), - BTC.FormatMoney(a.InBTC), - a.InUSD.Div(totalUSD).FormatPercentage(2), - ), + Value: text, Short: false, }) } else { + text := fmt.Sprintf("%s", a.NetAsset.String()) + + if !a.Borrowed.IsZero() { + text += fmt.Sprintf(" Borrowed: %s", a.Borrowed.String()) + } + fields = append(fields, slack.AttachmentField{ Title: a.Currency, - Value: fmt.Sprintf("%s", a.Total.String()), + Value: text, Short: false, }) } @@ -113,8 +125,8 @@ func (m AssetMap) SlackAttachment() slack.Attachment { return slack.Attachment{ Title: fmt.Sprintf("Net Asset Value %s (≈ %s)", - USD.FormatMoney(totalUSD), - BTC.FormatMoney(totalBTC), + USD.FormatMoney(netAssetInUSD), + BTC.FormatMoney(netAssetInBTC), ), Fields: fields, }