Skip to content

Commit

Permalink
update logic listen tx and block notificaion, add logic to refresh to…
Browse files Browse the repository at this point in the history
…tal value on home page, cleean code
  • Loading branch information
JustinBeBoy committed Jul 10, 2024
1 parent 97b87ef commit 3061855
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 73 deletions.
30 changes: 10 additions & 20 deletions libwallet/assets/btc/txandblocknotifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}
}
}

Expand All @@ -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)
}
}
}

Expand All @@ -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)
}
}
}
30 changes: 10 additions & 20 deletions libwallet/assets/dcr/txandblocknotifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
}
}

Expand All @@ -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)
}
}
}

Expand All @@ -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)
}
}
}
30 changes: 10 additions & 20 deletions libwallet/assets/ltc/txandblocknotifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}
}
}

Expand All @@ -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)
}
}
}

Expand All @@ -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)
}
}
}
22 changes: 9 additions & 13 deletions libwallet/assets/ltc/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions libwallet/assets_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Check failure on line 36 in libwallet/assets_manager.go

View workflow job for this annotation

GitHub Actions / Build

var-naming: const assetId should be assetID (revive)

Check warning on line 36 in libwallet/assets_manager.go

View workflow job for this annotation

GitHub Actions / Build

var-naming: const assetId should be assetID (revive)

// Assets is a struct that holds all the assets supported by the wallet.
type Assets struct {
Expand Down Expand Up @@ -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) {

Check failure on line 968 in libwallet/assets_manager.go

View workflow job for this annotation

GitHub Actions / Build

unused-parameter: parameter 'walletID' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 968 in libwallet/assets_manager.go

View workflow job for this annotation

GitHub Actions / Build

unused-parameter: parameter 'walletID' seems to be unused, consider removing or renaming it as _ (revive)
listen()
},
OnTransaction: func(walletID int, transaction *sharedW.Transaction) {

Check failure on line 971 in libwallet/assets_manager.go

View workflow job for this annotation

GitHub Actions / Build

unused-parameter: parameter 'walletID' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 971 in libwallet/assets_manager.go

View workflow job for this annotation

GitHub Actions / Build

unused-parameter: parameter 'walletID' seems to be unused, consider removing or renaming it as _ (revive)
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)
}
}
5 changes: 5 additions & 0 deletions ui/page/root/home_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -567,6 +571,7 @@ func (hp *HomePage) OnNavigatedFrom() {
activeTab.OnNavigatedFrom()
}

hp.AssetsManager.RemoveAssetChange()
hp.ctxCancel()
}

Expand Down
4 changes: 4 additions & 0 deletions ui/page/root/overview_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,10 @@ func (pg *OverviewPage) listenForMixerNotifications() {
pg.reloadBalances()
pg.ParentWindow().Reload()
},
OnTransactionConfirmed: func(walletID int, hash string, blockHeight int32) {

Check failure on line 1188 in ui/page/root/overview_page.go

View workflow job for this annotation

GitHub Actions / Build

unused-parameter: parameter 'walletID' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 1188 in ui/page/root/overview_page.go

View workflow job for this annotation

GitHub Actions / Build

unused-parameter: parameter 'walletID' seems to be unused, consider removing or renaming it as _ (revive)
},
OnTransaction: func(walletID int, transaction *sharedW.Transaction) {
},
}

wallets := pg.AssetsManager.AllWallets()
Expand Down

0 comments on commit 3061855

Please sign in to comment.