Skip to content

Commit

Permalink
move CalculateTotalAssetsBalance and CalculateAssetsUSDBalance to Ass…
Browse files Browse the repository at this point in the history
…etsManager
  • Loading branch information
itswisdomagain committed Nov 23, 2023
1 parent df24e21 commit 6912d03
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 63 deletions.
55 changes: 55 additions & 0 deletions libwallet/assets_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,3 +824,58 @@ func (mgr *AssetsManager) LTCHDPrefix() string {
return ""
}
}

func (mgr *AssetsManager) CalculateTotalAssetsBalance() (map[utils.AssetType]sharedW.AssetAmount, error) {
assetsTotalBalance := make(map[utils.AssetType]sharedW.AssetAmount)

wallets := mgr.AllWallets()
for _, wal := range wallets {
if wal.IsWatchingOnlyWallet() {
continue
}

accountsResult, err := wal.GetAccountsRaw()
if err != nil {
return nil, err
}

assetType := wal.GetAssetType()
for _, account := range accountsResult.Accounts {
assetTotal, ok := assetsTotalBalance[assetType]
if ok {
assetTotal = wal.ToAmount(assetTotal.ToInt() + account.Balance.Total.ToInt())
} else {
assetTotal = account.Balance.Total
}
assetsTotalBalance[assetType] = assetTotal
}
}

return assetsTotalBalance, nil
}

func (mgr *AssetsManager) CalculateAssetsUSDBalance(balances map[utils.AssetType]sharedW.AssetAmount) (map[utils.AssetType]float64, error) {
usdBalance := func(bal sharedW.AssetAmount, market string) (float64, error) {
rate := mgr.RateSource.GetTicker(market)
if rate == nil || rate.LastTradePrice <= 0 {
return 0, fmt.Errorf("No rate information available")
}

return bal.MulF64(rate.LastTradePrice).ToCoin(), nil
}

assetsTotalUSDBalance := make(map[utils.AssetType]float64)
for assetType, balance := range balances {
marketValue, exist := values.AssetExchangeMarketValue[assetType]
if !exist {
return nil, fmt.Errorf("Unsupported asset type: %s", assetType)
}
usdBal, err := usdBalance(balance, marketValue)
if err != nil {
return nil, err
}
assetsTotalUSDBalance[assetType] = usdBal
}

return assetsTotalUSDBalance, nil
}
55 changes: 0 additions & 55 deletions ui/page/components/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,61 +800,6 @@ func CalculateTotalWalletsBalance(wallet sharedW.Asset) (*CummulativeWalletsBala
return cumm, nil
}

func CalculateTotalAssetsBalance(l *load.Load) (map[libutils.AssetType]sharedW.AssetAmount, error) {
assetsTotalBalance := make(map[libutils.AssetType]sharedW.AssetAmount)

wallets := l.AssetsManager.AllWallets()
for _, wal := range wallets {
if wal.IsWatchingOnlyWallet() {
continue
}

accountsResult, err := wal.GetAccountsRaw()
if err != nil {
return nil, err
}

assetType := wal.GetAssetType()
for _, account := range accountsResult.Accounts {
assetTotal, ok := assetsTotalBalance[assetType]
if ok {
assetTotal = wal.ToAmount(assetTotal.ToInt() + account.Balance.Total.ToInt())
} else {
assetTotal = account.Balance.Total
}
assetsTotalBalance[assetType] = assetTotal
}
}

return assetsTotalBalance, nil
}

func CalculateAssetsUSDBalance(l *load.Load, assetsTotalBalance map[libutils.AssetType]sharedW.AssetAmount) (map[libutils.AssetType]float64, error) {
usdBalance := func(bal sharedW.AssetAmount, market string) (float64, error) {
rate := l.AssetsManager.RateSource.GetTicker(market)
if rate == nil || rate.LastTradePrice <= 0 {
return 0, fmt.Errorf("No rate information available")
}

return bal.MulF64(rate.LastTradePrice).ToCoin(), nil
}

assetsTotalUSDBalance := make(map[libutils.AssetType]float64)
for assetType, balance := range assetsTotalBalance {
marketValue, exist := values.AssetExchangeMarketValue[assetType]
if !exist {
return nil, fmt.Errorf("Unsupported asset type: %s", assetType)
}
usdBal, err := usdBalance(balance, marketValue)
if err != nil {
return nil, err
}
assetsTotalUSDBalance[assetType] = usdBal
}

return assetsTotalUSDBalance, nil
}

// SecondsToDays takes time in seconds and returns its string equivalent in the format ddhhmm.
func SecondsToDays(totalTimeLeft int64) string {
q, r := divMod(totalTimeLeft, 24*60*60)
Expand Down
7 changes: 5 additions & 2 deletions ui/page/components/wallet_account_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,14 @@ func newSelectorModal(l *load.Load, assetType ...utils.AssetType) *selectorModal
}
}

if len(wallets) == 0 {
if len(assetType) == 0 {
wallets = sm.AssetsManager.AllWallets()
}

sm.selectedWallet = wallets[0] // Set the default wallet to wallet loaded by cryptopower.
if len(wallets) > 0 {
sm.selectedWallet = wallets[0] // Set the default wallet to wallet loaded by cryptopower.
}

sm.accountSelector = false

sm.Modal.ShowScrollbar(true)
Expand Down
4 changes: 2 additions & 2 deletions ui/page/root/home_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,13 +771,13 @@ func (hp *HomePage) unlockWalletForSyncing(wal sharedW.Asset, unlock load.NeedUn

func (hp *HomePage) CalculateAssetsUSDBalance() {
if hp.AssetsManager.ExchangeRateFetchingEnabled() {
assetsBalance, err := components.CalculateTotalAssetsBalance(hp.Load)
assetsBalance, err := hp.AssetsManager.CalculateTotalAssetsBalance()
if err != nil {
log.Error(err)
return
}

assetsTotalUSDBalance, err := components.CalculateAssetsUSDBalance(hp.Load, assetsBalance)
assetsTotalUSDBalance, err := hp.AssetsManager.CalculateAssetsUSDBalance(assetsBalance)
if err != nil {
log.Error(err)
return
Expand Down
4 changes: 2 additions & 2 deletions ui/page/root/overview_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ func (pg *OverviewPage) recentProposal(gtx C) D {

func (pg *OverviewPage) updateAssetsUSDBalance() {
if pg.AssetsManager.ExchangeRateFetchingEnabled() {
assetsTotalUSDBalance, err := components.CalculateAssetsUSDBalance(pg.Load, pg.assetsTotalBalance)
assetsTotalUSDBalance, err := pg.AssetsManager.CalculateAssetsUSDBalance(pg.assetsTotalBalance)
if err != nil {
log.Error(err)
return
Expand Down Expand Up @@ -1091,7 +1091,7 @@ func (pg *OverviewPage) updateAssetsUSDBalance() {
}

func (pg *OverviewPage) updateAssetsSliders() {
assetsBalance, err := components.CalculateTotalAssetsBalance(pg.Load)
assetsBalance, err := pg.AssetsManager.CalculateTotalAssetsBalance()
if err != nil {
log.Error(err)
return
Expand Down
4 changes: 2 additions & 2 deletions ui/page/root/wallet_selector_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ func (pg *WalletSelectorPage) OnNavigatedTo() {

go func() {
// calculate total assets balance
assetsBalance, err := components.CalculateTotalAssetsBalance(pg.Load)
assetsBalance, err := pg.AssetsManager.CalculateTotalAssetsBalance()
if err != nil {
log.Error(err)
}
pg.assetsBalance = assetsBalance

// calculate total assets balance in USD
assetsTotalUSDBalance, err := components.CalculateAssetsUSDBalance(pg.Load, assetsBalance)
assetsTotalUSDBalance, err := pg.AssetsManager.CalculateAssetsUSDBalance(assetsBalance)
if err != nil {
log.Error(err)
}
Expand Down

0 comments on commit 6912d03

Please sign in to comment.