Skip to content

Commit

Permalink
update logic update total balance when refresh rate
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeBoy committed Jul 16, 2024
1 parent 8c9f789 commit e901f7f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
18 changes: 17 additions & 1 deletion libwallet/assets_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ func (mgr *AssetsManager) DeleteDEXData() error {
}

func (mgr *AssetsManager) WatchBalanceChange(listen func()) {
// Reload wallets unmixed balance and reload UI on new blocks.
// Reload total balance on new tx.
txAndBlockNotificationListener := &sharedW.TxAndBlockNotificationListener{
OnTransactionConfirmed: func(_ int, _ string, _ int32) {
listen()
Expand All @@ -974,17 +974,33 @@ func (mgr *AssetsManager) WatchBalanceChange(listen func()) {
listen()
},
}

// add tx listener
for _, wallet := range mgr.AllWallets() {
if !wallet.IsNotificationListenerExist(assetIdentifier) {
if err := wallet.AddTxAndBlockNotificationListener(txAndBlockNotificationListener, assetIdentifier); err != nil {
log.Errorf("Can't listen tx and block notification for %s wallet", wallet.GetWalletName())
}
}
}

// add rate listener
rateListener := &ext.RateListener{
OnRateUpdated: func() {
listen()
},
}
if !mgr.RateSource.IsRateListenerExist(assetIdentifier) {
mgr.RateSource.AddRateListener(rateListener, assetIdentifier)

Check failure on line 994 in libwallet/assets_manager.go

View workflow job for this annotation

GitHub Actions / Build

Error return value of `mgr.RateSource.AddRateListener` is not checked (errcheck)

Check failure on line 994 in libwallet/assets_manager.go

View workflow job for this annotation

GitHub Actions / Build

Error return value of `mgr.RateSource.AddRateListener` is not checked (errcheck)
}
}

func (mgr *AssetsManager) RemoveAssetChange() {
// Remove all listener on tx notification
for _, wallet := range mgr.AllWallets() {
wallet.RemoveTxAndBlockNotificationListener(assetIdentifier)
}

// Remove listener on rate notification
mgr.RateSource.RemoveRateListener(assetIdentifier)
}
39 changes: 39 additions & 0 deletions libwallet/ext/rate_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package ext

import (
"context"
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -115,6 +116,9 @@ type RateSource interface {
GetTicker(market values.Market, cacheOnly bool) *Ticker
ToggleStatus(disable bool)
ToggleSource(newSource string) error
AddRateListener(listener *RateListener, uniqueIdentifier string) error
RemoveRateListener(uniqueIdentifier string)
IsRateListenerExist(uniqueIdentifier string) bool
}

// RateListener listens for new tickers and rate source change notifications.
Expand All @@ -141,6 +145,9 @@ type CommonRateSource struct {
lastUpdate time.Time

disableConversionExchange func()

notificationListenersMu sync.RWMutex
ratesListeners map[string]*RateListener
}

// Used to initialize a rate source.
Expand All @@ -155,6 +162,7 @@ func NewCommonRateSource(ctx context.Context, source string, disableConversionEx
tickers: make(map[values.Market]*Ticker),
sourceChanged: make(chan *struct{}),
disableConversionExchange: disableConversionExchange,
ratesListeners: make(map[string]*RateListener),
}
s.getTicker = s.sourceGetTickerFunc(source)
s.cond = sync.NewCond(&s.mtx)
Expand Down Expand Up @@ -205,6 +213,36 @@ func (cs *CommonRateSource) isDisabled() bool {
return cs.disabled
}

func (cs *CommonRateSource) AddRateListener(listener *RateListener, uniqueIdentifier string) error {
if _, ok := cs.ratesListeners[uniqueIdentifier]; ok {
return errors.New(utils.ErrListenerAlreadyExist)
}

cs.notificationListenersMu.Lock()
defer cs.notificationListenersMu.Unlock()
cs.ratesListeners[uniqueIdentifier] = listener
return nil
}

func (cs *CommonRateSource) IsRateListenerExist(uniqueIdentifier string) bool {
_, ok := cs.ratesListeners[uniqueIdentifier]
return ok
}

func (cs *CommonRateSource) RemoveRateListener(uniqueIdentifier string) {
cs.notificationListenersMu.Lock()
defer cs.notificationListenersMu.Unlock()
delete(cs.ratesListeners, uniqueIdentifier)
}

func (cs *CommonRateSource) pushlishRateUpdated() {
for _, listener := range cs.ratesListeners {
if listener.OnRateUpdated != nil {
listener.OnRateUpdated()
}
}
}

// ToggleSource changes the rate source to newSource. This method takes some
// time to refresh the rates and should be executed a a goroutine.
func (cs *CommonRateSource) ToggleSource(newSource string) error {
Expand Down Expand Up @@ -275,6 +313,7 @@ func (cs *CommonRateSource) Refresh(force bool) {
}()

defer cs.ratesUpdated(time.Now())
defer cs.pushlishRateUpdated()

tickers := make(map[values.Market]*Ticker)
if !force {
Expand Down
1 change: 1 addition & 0 deletions ui/page/root/home_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func (hp *HomePage) OnNavigatedTo() {
}

hp.AssetsManager.WatchBalanceChange(func() {
fmt.Println("Update Total balance")
go hp.CalculateAssetsUSDBalance()
})
}
Expand Down

0 comments on commit e901f7f

Please sign in to comment.