Skip to content

Commit

Permalink
asset: reconcile the asset fields
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 16, 2024
1 parent 888f6fd commit cbbf669
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions pkg/types/asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func NewMapFromBalanceMap(

if assetPrice, ok := priceSolver.ResolvePrice(cu, fiat, "USDT"); ok {
asset.PriceInUSD = assetPrice
asset.InUSD = netAsset.Mul(assetPrice)
asset.NetAssetInUSD = netAsset.Mul(assetPrice)
if hasBtcPrice {
asset.InBTC = asset.InUSD.Div(btcInUSD)
asset.NetAssetInBTC = asset.NetAssetInUSD.Div(btcInUSD)
}

asset.DebtInUSD = debt.Mul(assetPrice)
Expand All @@ -72,15 +72,15 @@ type Asset struct {

Interest fixedpoint.Value `json:"interest" db:"interest"`

// InUSD is net asset in USD
InUSD fixedpoint.Value `json:"inUSD" db:"net_asset_in_usd"`
// NetAssetInUSD is net asset in USD
NetAssetInUSD fixedpoint.Value `json:"netAssetInUSD" db:"net_asset_in_usd"`

DebtInUSD fixedpoint.Value `json:"debtInUSD" db:"debt_in_usd"`

InterestInUSD fixedpoint.Value `json:"interestInUSD" db:"interest_in_usd"`

// InBTC is net asset in BTC
InBTC fixedpoint.Value `json:"inBTC" db:"net_asset_in_btc"`
// NetAssetInBTC is net asset in BTC
NetAssetInBTC fixedpoint.Value `json:"netAssetInBTC" db:"net_asset_in_btc"`

Time time.Time `json:"time" db:"time"`
Locked fixedpoint.Value `json:"lock" db:"lock" `
Expand All @@ -101,8 +101,8 @@ func (m Map) Merge(other Map) Map {
asset.Locked = asset.Locked.Add(existing.Locked)
asset.Available = asset.Available.Add(existing.Available)
asset.Borrowed = asset.Borrowed.Add(existing.Borrowed)
asset.InUSD = asset.InUSD.Add(existing.InUSD)
asset.InBTC = asset.InBTC.Add(existing.InBTC)
asset.NetAssetInUSD = asset.NetAssetInUSD.Add(existing.NetAssetInUSD)
asset.NetAssetInBTC = asset.NetAssetInBTC.Add(existing.NetAssetInBTC)
asset.DebtInUSD = asset.DebtInUSD.Add(existing.DebtInUSD)
asset.InterestInUSD = asset.InterestInUSD.Add(existing.InterestInUSD)
}
Expand All @@ -127,11 +127,11 @@ func (m Map) Filter(f func(asset *Asset) bool) Map {

func (m Map) InUSD() (total fixedpoint.Value) {
for _, a := range m {
if a.InUSD.IsZero() {
if a.NetAssetInUSD.IsZero() {
continue
}

total = total.Add(a.InUSD)
total = total.Add(a.NetAssetInUSD)
}

return total
Expand All @@ -142,15 +142,15 @@ func (m Map) PlainText() (o string) {

// sort assets
sort.Slice(assets, func(i, j int) bool {
return assets[i].InUSD.Compare(assets[j].InUSD) > 0
return assets[i].NetAssetInUSD.Compare(assets[j].NetAssetInUSD) > 0
})

sumUsd := fixedpoint.Zero
sumBTC := fixedpoint.Zero
for _, a := range assets {
usd := a.InUSD
btc := a.InBTC
if !a.InUSD.IsZero() {
usd := a.NetAssetInUSD
btc := a.NetAssetInBTC
if !a.NetAssetInUSD.IsZero() {
o += fmt.Sprintf(" %s: %s (≈ %s) (≈ %s)",
a.Currency,
a.NetAsset.String(),
Expand Down Expand Up @@ -189,21 +189,21 @@ func (m Map) SlackAttachment() slack.Attachment {

// sort assets
sort.Slice(assets, func(i, j int) bool {
return assets[i].InUSD.Compare(assets[j].InUSD) > 0
return assets[i].NetAssetInUSD.Compare(assets[j].NetAssetInUSD) > 0
})

for _, a := range assets {
netAssetInUSD = netAssetInUSD.Add(a.InUSD)
netAssetInBTC = netAssetInBTC.Add(a.InBTC)
netAssetInUSD = netAssetInUSD.Add(a.NetAssetInUSD)
netAssetInBTC = netAssetInBTC.Add(a.NetAssetInBTC)
}

for _, a := range assets {
if !a.InUSD.IsZero() {
if !a.NetAssetInUSD.IsZero() {
text := fmt.Sprintf("%s (≈ %s) (≈ %s) (%s)",
a.NetAsset.String(),
currency.USD.FormatMoney(a.InUSD),
currency.BTC.FormatMoney(a.InBTC),
a.InUSD.Div(netAssetInUSD).FormatPercentage(2),
currency.USD.FormatMoney(a.NetAssetInUSD),
currency.BTC.FormatMoney(a.NetAssetInBTC),
a.NetAssetInUSD.Div(netAssetInUSD).FormatPercentage(2),
)

if !a.Borrowed.IsZero() {
Expand Down

0 comments on commit cbbf669

Please sign in to comment.