From 6a85ca67f3b4e25a7f96e1a1ef784529c70e940a Mon Sep 17 00:00:00 2001 From: richardo2016x Date: Mon, 29 Apr 2024 13:14:34 +0800 Subject: [PATCH] feat: force expire address's balance/netCurve after tx completed, style tuning. --- src/background/controller/wallet.ts | 24 ++++++++++----- src/constant/index.ts | 2 +- src/constant/timeout.ts | 2 +- .../components/BalanceView/BalanceLabel.tsx | 8 ++--- .../components/BalanceView/BalanceView.tsx | 9 +++--- .../BalanceView/useHomeBalanceView.ts | 30 +++++++++---------- 6 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/background/controller/wallet.ts b/src/background/controller/wallet.ts index 5fdd325c2bf..21f6cffa7b2 100644 --- a/src/background/controller/wallet.ts +++ b/src/background/controller/wallet.ts @@ -1071,6 +1071,13 @@ export class WalletController extends BaseController { return this.getTotalBalanceCached.fn([address], address, force); }; + forceExpireAddressBalance = (address: string, isTestnet = false) => { + if (isTestnet) { + return this.getTestnetTotalBalanceCached.forceExpire(address); + } + return this.getTotalBalanceCached.forceExpire(address); + }; + isAddressBalanceExpired = (address: string, isTestnet = false) => { if (isTestnet) { return this.getTestnetTotalBalanceCached.isExpired(address); @@ -1095,6 +1102,10 @@ export class WalletController extends BaseController { return this.getNetCurveCached.fn([address], address, force); }; + forceExpireNetCurve = (address: string) => { + return this.getNetCurveCached.forceExpire(address); + }; + isNetCurveExpired = (address: string) => { return this.getNetCurveCached.isExpired(address); }; @@ -3786,13 +3797,12 @@ autoLockService.onAutoLock = async () => { }); }; refreshBalanceService.onRefreshBalance = async (ctx) => { - eventBus.emit(EVENTS.broadcastToUI, { - method: EVENTS.REFRESH_HOME_BALANCE, - params: { - accountToRefresh: - ctx?.accountToRefresh || refreshBalanceService.accountToRefresh, - }, - }); + const address = + ctx?.accountToRefresh || refreshBalanceService.accountToRefresh; + if (!address) return; + + wallet.forceExpireAddressBalance(address); + wallet.forceExpireNetCurve(address); }; export default wallet; diff --git a/src/constant/index.ts b/src/constant/index.ts index eb0c0208996..d5b66a9a33f 100644 --- a/src/constant/index.ts +++ b/src/constant/index.ts @@ -490,7 +490,7 @@ export const EVENTS = { }, LOCK_WALLET: 'LOCK_WALLET', RELOAD_TX: 'RELOAD_TX', - REFRESH_HOME_BALANCE: 'REFRESH_HOME_BALANCE', + // FORCE_EXPIRE_ADDRESS_BALANCE: 'FORCE_EXPIRE_ADDRESS_BALANCE', }; export const EVENTS_IN_BG = { diff --git a/src/constant/timeout.ts b/src/constant/timeout.ts index accac60cb48..5c8381e21e2 100644 --- a/src/constant/timeout.ts +++ b/src/constant/timeout.ts @@ -22,5 +22,5 @@ export const BALANCE_LOADING_TIMES = appIsDev : { CHECK_INTERVAL: 2 * 1e3, TIMEOUT: DEFT_BALANCE_LOADING_TIMEOUT_PROD, - DELAY_AFTER_TX_COMPLETED: 30 * 60 * 1e3, + DELAY_AFTER_TX_COMPLETED: 5 * 60 * 1e3, }; diff --git a/src/ui/views/Dashboard/components/BalanceView/BalanceLabel.tsx b/src/ui/views/Dashboard/components/BalanceView/BalanceLabel.tsx index b52bc98e537..8a700b8091b 100644 --- a/src/ui/views/Dashboard/components/BalanceView/BalanceLabel.tsx +++ b/src/ui/views/Dashboard/components/BalanceView/BalanceLabel.tsx @@ -4,10 +4,10 @@ import clsx from 'clsx'; import React from 'react'; interface Props { - isCache: boolean; + // isCache: boolean; balance: number; } -export const BalanceLabel: React.FC = ({ isCache, balance }) => { +export const BalanceLabel: React.FC = ({ balance }) => { const splitBalance = splitNumberByStep((balance || 0).toFixed(2)); const { hiddenBalance } = useRabbySelector((state) => state.preference); const dispatch = useRabbyDispatch(); @@ -19,8 +19,8 @@ export const BalanceLabel: React.FC = ({ isCache, balance }) => { return (
{ ) : ( !!currentBalance && ( ) @@ -378,9 +378,10 @@ const BalanceView = ({ currentAccount }) => { src={ArrowNextSVG} className={clsx( 'absolute w-[20px] h-[20px] top-[8px] right-[10px]', - balanceFromCache - ? !currentHover && 'opacity-0' - : !currentHover && 'opacity-80' + !currentHover && 'opacity-80' + // balanceFromCache + // ? !currentHover && 'opacity-0' + // : !currentHover && 'opacity-80' )} />
{ + // if (!currentAddress) return; + + // const handler = async (ret) => { + // if (!isSameAddress(currentAddress, ret.accountToRefresh)) return; + + // deleteHomeBalanceByAddress(ret.accountToRefresh); + // }; + // eventBus.addEventListener(EVENTS.FORCE_EXPIRE_ADDRESS_BALANCE, handler); + + // return () => { + // eventBus.removeEventListener(EVENTS.FORCE_EXPIRE_ADDRESS_BALANCE, handler); + // }; + // }, [currentAddress, deleteHomeBalanceByAddress]); + return { currentHomeBalanceCache: currentHomeBalanceCache?.balance ? currentHomeBalanceCache @@ -137,21 +152,6 @@ export function useRefreshHomeBalanceView(options: { onRefresh(expiration); }, BALANCE_LOADING_TIMES.CHECK_INTERVAL); - useEffect(() => { - if (!currentAddress) return; - - const handler = async (ret) => { - if (!isSameAddress(currentAddress, ret.accountToRefresh)) return; - - onRefresh({ balanceExpired: true, curveExpired: true, isManual: false }); - }; - eventBus.addEventListener(EVENTS.REFRESH_HOME_BALANCE, handler); - - return () => { - eventBus.removeEventListener(EVENTS.REFRESH_HOME_BALANCE, handler); - }; - }, [currentAddress, onRefresh]); - return { onRefresh, isManualRefreshing,