From 30618551c476c177e808a1cdb64cb2da660e660f Mon Sep 17 00:00:00 2001 From: Justin Do Date: Wed, 10 Jul 2024 18:39:36 +0700 Subject: [PATCH] update logic listen tx and block notificaion, add logic to refresh total value on home page, cleean code --- .../assets/btc/txandblocknotifications.go | 30 +++++++------------ .../assets/dcr/txandblocknotifications.go | 30 +++++++------------ .../assets/ltc/txandblocknotifications.go | 30 +++++++------------ libwallet/assets/ltc/wallet.go | 22 ++++++-------- libwallet/assets_manager.go | 24 +++++++++++++++ ui/page/root/home_page.go | 5 ++++ ui/page/root/overview_page.go | 4 +++ 7 files changed, 72 insertions(+), 73 deletions(-) diff --git a/libwallet/assets/btc/txandblocknotifications.go b/libwallet/assets/btc/txandblocknotifications.go index ee8929f57..9ea87789a 100644 --- a/libwallet/assets/btc/txandblocknotifications.go +++ b/libwallet/assets/btc/txandblocknotifications.go @@ -26,23 +26,7 @@ func (asset *Asset) AddTxAndBlockNotificationListener(txAndBlockNotificationList return errors.New(utils.ErrListenerAlreadyExist) } - asset.txAndBlockNotificationListeners[uniqueIdentifier] = &sharedW.TxAndBlockNotificationListener{ - OnTransaction: func(walletID int, transaction *sharedW.Transaction) { - if txAndBlockNotificationListener.OnTransaction != nil { - go txAndBlockNotificationListener.OnTransaction(walletID, transaction) - } - }, - OnBlockAttached: func(walletID int, blockHeight int32) { - if txAndBlockNotificationListener.OnBlockAttached != nil { - go txAndBlockNotificationListener.OnBlockAttached(walletID, blockHeight) - } - }, - OnTransactionConfirmed: func(walletID int, hash string, blockHeight int32) { - if txAndBlockNotificationListener.OnTransactionConfirmed != nil { - txAndBlockNotificationListener.OnTransactionConfirmed(walletID, hash, blockHeight) - } - }, - } + asset.txAndBlockNotificationListeners[uniqueIdentifier] = txAndBlockNotificationListener return nil } @@ -61,7 +45,9 @@ func (asset *Asset) mempoolTransactionNotification(transaction *sharedW.Transact defer asset.notificationListenersMu.RUnlock() for _, txAndBlockNotificationListener := range asset.txAndBlockNotificationListeners { - txAndBlockNotificationListener.OnTransaction(asset.ID, transaction) + if txAndBlockNotificationListener.OnTransaction != nil { + txAndBlockNotificationListener.OnTransaction(asset.ID, transaction) + } } } @@ -73,7 +59,9 @@ func (asset *Asset) publishTransactionConfirmed(txHash string, blockHeight int32 defer asset.notificationListenersMu.RUnlock() for _, txAndBlockNotificationListener := range asset.txAndBlockNotificationListeners { - txAndBlockNotificationListener.OnTransactionConfirmed(asset.ID, txHash, blockHeight) + if txAndBlockNotificationListener.OnTransactionConfirmed != nil { + txAndBlockNotificationListener.OnTransactionConfirmed(asset.ID, txHash, blockHeight) + } } } @@ -84,6 +72,8 @@ func (asset *Asset) publishBlockAttached(blockHeight int32) { defer asset.notificationListenersMu.RUnlock() for _, txAndBlockNotificationListener := range asset.txAndBlockNotificationListeners { - txAndBlockNotificationListener.OnBlockAttached(asset.ID, blockHeight) + if txAndBlockNotificationListener.OnBlockAttached != nil { + txAndBlockNotificationListener.OnBlockAttached(asset.ID, blockHeight) + } } } diff --git a/libwallet/assets/dcr/txandblocknotifications.go b/libwallet/assets/dcr/txandblocknotifications.go index 3179f343e..0be04f72f 100644 --- a/libwallet/assets/dcr/txandblocknotifications.go +++ b/libwallet/assets/dcr/txandblocknotifications.go @@ -85,23 +85,7 @@ func (asset *Asset) AddTxAndBlockNotificationListener(txAndBlockNotificationList return errors.New(utils.ErrListenerAlreadyExist) } - asset.txAndBlockNotificationListeners[uniqueIdentifier] = &sharedW.TxAndBlockNotificationListener{ - OnTransaction: func(walletID int, transaction *sharedW.Transaction) { - if txAndBlockNotificationListener.OnTransaction != nil { - go txAndBlockNotificationListener.OnTransaction(walletID, transaction) - } - }, - OnBlockAttached: func(walletID int, blockHeight int32) { - if txAndBlockNotificationListener.OnBlockAttached != nil { - go txAndBlockNotificationListener.OnBlockAttached(walletID, blockHeight) - } - }, - OnTransactionConfirmed: func(walletID int, hash string, blockHeight int32) { - if txAndBlockNotificationListener.OnTransactionConfirmed != nil { - txAndBlockNotificationListener.OnTransactionConfirmed(walletID, hash, blockHeight) - } - }, - } + asset.txAndBlockNotificationListeners[uniqueIdentifier] = txAndBlockNotificationListener return nil } @@ -131,7 +115,9 @@ func (asset *Asset) mempoolTransactionNotification(transaction *sharedW.Transact defer asset.notificationListenersMu.RUnlock() for _, txAndBlockNotificationListener := range asset.txAndBlockNotificationListeners { - txAndBlockNotificationListener.OnTransaction(asset.ID, transaction) + if txAndBlockNotificationListener.OnTransaction != nil { + go txAndBlockNotificationListener.OnTransaction(asset.ID, transaction) + } } } @@ -140,7 +126,9 @@ func (asset *Asset) publishTransactionConfirmed(transactionHash string, blockHei defer asset.notificationListenersMu.RUnlock() for _, txAndBlockNotificationListener := range asset.txAndBlockNotificationListeners { - txAndBlockNotificationListener.OnTransactionConfirmed(asset.ID, transactionHash, blockHeight) + if txAndBlockNotificationListener.OnTransactionConfirmed != nil { + go txAndBlockNotificationListener.OnTransactionConfirmed(asset.ID, transactionHash, blockHeight) + } } } @@ -149,6 +137,8 @@ func (asset *Asset) publishBlockAttached(blockHeight int32) { defer asset.notificationListenersMu.RUnlock() for _, txAndBlockNotificationListener := range asset.txAndBlockNotificationListeners { - txAndBlockNotificationListener.OnBlockAttached(asset.ID, blockHeight) + if txAndBlockNotificationListener.OnBlockAttached != nil { + go txAndBlockNotificationListener.OnBlockAttached(asset.ID, blockHeight) + } } } diff --git a/libwallet/assets/ltc/txandblocknotifications.go b/libwallet/assets/ltc/txandblocknotifications.go index 7110a7f00..f42781629 100644 --- a/libwallet/assets/ltc/txandblocknotifications.go +++ b/libwallet/assets/ltc/txandblocknotifications.go @@ -26,23 +26,7 @@ func (asset *Asset) AddTxAndBlockNotificationListener(txAndBlockNotificationList return errors.New(utils.ErrListenerAlreadyExist) } - asset.txAndBlockNotificationListeners[uniqueIdentifier] = &sharedW.TxAndBlockNotificationListener{ - OnTransaction: func(walletID int, transaction *sharedW.Transaction) { - if txAndBlockNotificationListener.OnTransaction != nil { - go txAndBlockNotificationListener.OnTransaction(walletID, transaction) - } - }, - OnBlockAttached: func(walletID int, blockHeight int32) { - if txAndBlockNotificationListener.OnBlockAttached != nil { - go txAndBlockNotificationListener.OnBlockAttached(walletID, blockHeight) - } - }, - OnTransactionConfirmed: func(walletID int, hash string, blockHeight int32) { - if txAndBlockNotificationListener.OnTransactionConfirmed != nil { - txAndBlockNotificationListener.OnTransactionConfirmed(walletID, hash, blockHeight) - } - }, - } + asset.txAndBlockNotificationListeners[uniqueIdentifier] = txAndBlockNotificationListener return nil } @@ -61,7 +45,9 @@ func (asset *Asset) mempoolTransactionNotification(transaction *sharedW.Transact defer asset.notificationListenersMu.RUnlock() for _, txAndBlockNotificationListener := range asset.txAndBlockNotificationListeners { - txAndBlockNotificationListener.OnTransaction(asset.ID, transaction) + if txAndBlockNotificationListener.OnTransaction != nil { + txAndBlockNotificationListener.OnTransaction(asset.ID, transaction) + } } } @@ -73,7 +59,9 @@ func (asset *Asset) publishTransactionConfirmed(txHash string, blockHeight int32 defer asset.notificationListenersMu.RUnlock() for _, txAndBlockNotificationListener := range asset.txAndBlockNotificationListeners { - txAndBlockNotificationListener.OnTransactionConfirmed(asset.ID, txHash, blockHeight) + if txAndBlockNotificationListener.OnTransactionConfirmed != nil { + txAndBlockNotificationListener.OnTransactionConfirmed(asset.ID, txHash, blockHeight) + } } } @@ -84,6 +72,8 @@ func (asset *Asset) publishBlockAttached(blockHeight int32) { defer asset.notificationListenersMu.RUnlock() for _, txAndBlockNotificationListener := range asset.txAndBlockNotificationListeners { - txAndBlockNotificationListener.OnBlockAttached(asset.ID, blockHeight) + if txAndBlockNotificationListener.OnBlockAttached != nil { + txAndBlockNotificationListener.OnBlockAttached(asset.ID, blockHeight) + } } } diff --git a/libwallet/assets/ltc/wallet.go b/libwallet/assets/ltc/wallet.go index 2856a90c2..fabc6bafe 100644 --- a/libwallet/assets/ltc/wallet.go +++ b/libwallet/assets/ltc/wallet.go @@ -19,12 +19,12 @@ import ( "github.com/dcrlabs/ltcwallet/spv/headerfs" _ "github.com/dcrlabs/ltcwallet/walletdb/bdb" // bdb init() registers a driver "github.com/ltcsuite/ltcd/btcec/v2/ecdsa" - "github.com/ltcsuite/ltcd/chaincfg" + ltcchaincfg "github.com/ltcsuite/ltcd/chaincfg" "github.com/ltcsuite/ltcd/chaincfg/chainhash" "github.com/ltcsuite/ltcd/ltcutil" "github.com/ltcsuite/ltcd/ltcutil/gcs" - "github.com/ltcsuite/ltcd/wire" + ltcwire "github.com/ltcsuite/ltcd/wire" ) @@ -60,10 +60,6 @@ type Asset struct { // been introduced. fees feeEstimateCache - // rescanStarting is set while reloading the wallet and dropping - // transactions from the wallet db. - rescanStarting uint32 // atomic - notificationListenersMu sync.RWMutex syncData *SyncData @@ -82,8 +78,8 @@ type neutrinoService interface { BestBlock() (*headerfs.BlockStamp, error) Peers() []*neutrino.ServerPeer GetBlockHeight(hash *chainhash.Hash) (int32, error) - GetBlockHeader(*chainhash.Hash) (*wire.BlockHeader, error) - GetCFilter(blockHash chainhash.Hash, filterType wire.FilterType, options ...neutrino.QueryOption) (*gcs.Filter, error) + GetBlockHeader(*chainhash.Hash) (*ltcwire.BlockHeader, error) + GetCFilter(blockHash chainhash.Hash, filterType ltcwire.FilterType, options ...neutrino.QueryOption) (*gcs.Filter, error) GetBlock(blockHash chainhash.Hash, options ...neutrino.QueryOption) (*ltcutil.Block, error) Stop() error } @@ -426,11 +422,11 @@ func (asset *Asset) SignMessage(passphrase, address, message string) ([]byte, er } var buf bytes.Buffer - err = wire.WriteVarString(&buf, 0, "Litecoin Signed Message:\n") + err = ltcwire.WriteVarString(&buf, 0, "Litecoin Signed Message:\n") if err != nil { return nil, err } - err = wire.WriteVarString(&buf, 0, message) + err = ltcwire.WriteVarString(&buf, 0, message) if err != nil { return nil, err } @@ -460,11 +456,11 @@ func (asset *Asset) VerifyMessage(address, message, signatureBase64 string) (boo // Validate the signature - this just shows that it was valid at all. // we will compare it with the key next. var buf bytes.Buffer - err = wire.WriteVarString(&buf, 0, "Litecoin Signed Message:\n") + err = ltcwire.WriteVarString(&buf, 0, "Litecoin Signed Message:\n") if err != nil { return false, nil } - err = wire.WriteVarString(&buf, 0, message) + err = ltcwire.WriteVarString(&buf, 0, message) if err != nil { return false, nil } @@ -544,7 +540,7 @@ func (asset *Asset) AccountXPubMatches(account uint32, xPub string) (bool, error return acctXPubKey.AccountPubKey.String() == xPub, nil } -func decodeAddress(s string, params *chaincfg.Params) (ltcutil.Address, error) { +func decodeAddress(s string, params *ltcchaincfg.Params) (ltcutil.Address, error) { addr, err := ltcutil.DecodeAddress(s, params) if err != nil { return nil, fmt.Errorf("invalid address %q: decode failed with %#q", s, err) diff --git a/libwallet/assets_manager.go b/libwallet/assets_manager.go index 9350cd2eb..e31497b57 100644 --- a/libwallet/assets_manager.go +++ b/libwallet/assets_manager.go @@ -33,6 +33,7 @@ import ( // TODO: This is the main app's log filename, should probably be defined // elsewhere. const LogFilename = "cryptopower.log" +const assetId = "assets_manager" // Assets is a struct that holds all the assets supported by the wallet. type Assets struct { @@ -960,3 +961,26 @@ func (mgr *AssetsManager) DeleteDEXData() error { // Delete dex client db. return os.Remove(dexDBFile) } + +func (mgr *AssetsManager) ListenAssetChange(listen func()) { + // Reload wallets unmixed balance and reload UI on new blocks. + txAndBlockNotificationListener := &sharedW.TxAndBlockNotificationListener{ + OnTransactionConfirmed: func(walletID int, hash string, blockHeight int32) { + listen() + }, + OnTransaction: func(walletID int, transaction *sharedW.Transaction) { + listen() + }, + } + for _, wallet := range mgr.AllWallets() { + if err := wallet.AddTxAndBlockNotificationListener(txAndBlockNotificationListener, assetId); err != nil { + log.Errorf("Can't listen tx and block notification for %s wallet", wallet.GetWalletName()) + } + } +} + +func (mgr *AssetsManager) RemoveAssetChange() { + for _, wallet := range mgr.AllWallets() { + wallet.RemoveTxAndBlockNotificationListener(assetId) + } +} diff --git a/ui/page/root/home_page.go b/ui/page/root/home_page.go index 109a9e627..e2fb43f01 100644 --- a/ui/page/root/home_page.go +++ b/ui/page/root/home_page.go @@ -197,6 +197,10 @@ func (hp *HomePage) OnNavigatedTo() { if hp.isUpdateAPIAllowed() { go hp.checkForUpdates() } + + hp.AssetsManager.ListenAssetChange(func() { + go hp.CalculateAssetsUSDBalance() + }) } // initDEX initializes a new dex client if dex is not ready. @@ -567,6 +571,7 @@ func (hp *HomePage) OnNavigatedFrom() { activeTab.OnNavigatedFrom() } + hp.AssetsManager.RemoveAssetChange() hp.ctxCancel() } diff --git a/ui/page/root/overview_page.go b/ui/page/root/overview_page.go index 3c7d3291a..f04b2076b 100644 --- a/ui/page/root/overview_page.go +++ b/ui/page/root/overview_page.go @@ -1185,6 +1185,10 @@ func (pg *OverviewPage) listenForMixerNotifications() { pg.reloadBalances() pg.ParentWindow().Reload() }, + OnTransactionConfirmed: func(walletID int, hash string, blockHeight int32) { + }, + OnTransaction: func(walletID int, transaction *sharedW.Transaction) { + }, } wallets := pg.AssetsManager.AllWallets()