From 8619024077d6001d5978f557ba9ae0d86f8daba6 Mon Sep 17 00:00:00 2001 From: Philemon Ukane Date: Wed, 25 Sep 2024 11:44:38 +0100 Subject: [PATCH] use cache for bond fee Signed-off-by: Philemon Ukane --- libwallet/assets/dcr/dex_wallet.go | 2 +- ui/page/dcrdex/dex_onboarding_page.go | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/libwallet/assets/dcr/dex_wallet.go b/libwallet/assets/dcr/dex_wallet.go index d2bd6f76a..7c1a1c733 100644 --- a/libwallet/assets/dcr/dex_wallet.go +++ b/libwallet/assets/dcr/dex_wallet.go @@ -547,7 +547,7 @@ func (dw *DEXWallet) UnlockAccount(ctx context.Context, pass []byte, _ string) e // Part of the Wallet interface. func (dw *DEXWallet) SyncStatus(_ context.Context) (*dexasset.SyncStatus, error) { ss := new(dexasset.SyncStatus) - if dw.syncData != nil { + if dw.syncData != nil && dw.ctx.Err() == nil { // dex might call this method during wallet shutdown. ss.Synced = dw.syncData.isSynced() ss.Blocks = uint64(dw.syncData.syncedTo()) ss.TargetHeight = uint64(dw.syncData.targetHeight()) diff --git a/ui/page/dcrdex/dex_onboarding_page.go b/ui/page/dcrdex/dex_onboarding_page.go index a34b6d245..f3bc03e0c 100644 --- a/ui/page/dcrdex/dex_onboarding_page.go +++ b/ui/page/dcrdex/dex_onboarding_page.go @@ -143,6 +143,8 @@ type DEXOnboarding struct { materialLoader material.LoaderStyle isLoading bool existingDEXServer string + + bondFeeCache map[uint32]uint64 } // NewDEXOnboarding creates a new DEX onboarding pages. Specify @@ -168,6 +170,7 @@ func NewDEXOnboarding(l *load.Load, existingDEXServer string) *DEXOnboarding { nextBtn: th.Button(values.String(values.StrNext)), materialLoader: material.Loader(th.Base), existingDEXServer: existingDEXServer, + bondFeeCache: make(map[uint32]uint64), } pg.onBoardingSteps = map[onboardingStep]dexOnboardingStep{ @@ -246,6 +249,11 @@ func (pg *DEXOnboarding) OnNavigatedFrom() { if pg.bondSourceWalletSelector != nil { pg.bondSourceWalletSelector.SelectedWallet().RemoveTxAndBlockNotificationListener(DEXOnboardingPageID) } + + // Clear cache + if len(pg.bondFeeCache) != 0 { + pg.bondFeeCache = make(map[uint32]uint64) + } } // Layout draws the page UI components into the provided C @@ -833,9 +841,14 @@ func (pg *DEXOnboarding) stepWaitForBondConfirmation(gtx C) D { func (pg *DEXOnboarding) bondAmountInfoDisplay(gtx C) D { asset := pg.bondSourceWalletSelector.SelectedWallet() assetType := asset.GetAssetType() - icon := pg.Theme.AssetIcon(assetType) bondAsset := pg.bondServer.bondAssets[assetType] - bondsFeeBuffer := pg.AssetsManager.DexClient().BondsFeeBuffer(bondAsset.ID) + bondsFeeBuffer, found := pg.bondFeeCache[bondAsset.ID] + if !found { + bondsFeeBuffer = pg.AssetsManager.DexClient().BondsFeeBuffer(bondAsset.ID) + pg.bondFeeCache[bondAsset.ID] = bondsFeeBuffer + } + + icon := pg.Theme.AssetIcon(assetType) return layout.Flex{Axis: layout.Horizontal}.Layout(gtx, layout.Rigid(func(gtx C) D { if icon == nil { @@ -1395,7 +1408,12 @@ func (pg *DEXOnboarding) bondAccountHasEnough() bool { } bondAsset := pg.bondServer.bondAssets[asset.GetAssetType()] - bondsFeeBuffer := pg.AssetsManager.DexClient().BondsFeeBuffer(bondAsset.ID) + bondsFeeBuffer, found := pg.bondFeeCache[bondAsset.ID] + if !found { + bondsFeeBuffer = pg.AssetsManager.DexClient().BondsFeeBuffer(bondAsset.ID) + pg.bondFeeCache[bondAsset.ID] = bondsFeeBuffer + } + bondCost := uint64(pg.newTier)*bondAsset.Amt + bondsFeeBuffer bondAmt := asset.ToAmount(int64(bondCost))