Skip to content

Commit

Permalink
feat: force expire address's balance/netCurve after tx completed, sty…
Browse files Browse the repository at this point in the history
…le tuning.
  • Loading branch information
richardo2016x committed Apr 29, 2024
1 parent 94c4066 commit 6a85ca6
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 32 deletions.
24 changes: 17 additions & 7 deletions src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
};
Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion src/constant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/constant/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Original file line number Diff line number Diff line change
Expand Up @@ -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<Props> = ({ isCache, balance }) => {
export const BalanceLabel: React.FC<Props> = ({ balance }) => {
const splitBalance = splitNumberByStep((balance || 0).toFixed(2));
const { hiddenBalance } = useRabbySelector((state) => state.preference);
const dispatch = useRabbyDispatch();
Expand All @@ -19,8 +19,8 @@ export const BalanceLabel: React.FC<Props> = ({ isCache, balance }) => {
return (
<div
className={clsx(
'cursor-pointer transition-opacity',
isCache && 'opacity-80'
'cursor-pointer transition-opacity'
// isCache && 'opacity-80'
)}
title={splitBalance}
onClick={handleClick}
Expand Down
9 changes: 5 additions & 4 deletions src/ui/views/Dashboard/components/BalanceView/BalanceView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ const BalanceView = ({ currentAccount }) => {
) : (
!!currentBalance && (
<BalanceLabel
isCache={balanceFromCache}
// isCache={balanceFromCache}
balance={currentBalance}
/>
)
Expand Down Expand Up @@ -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'
)}
/>
<div
Expand Down
30 changes: 15 additions & 15 deletions src/ui/views/Dashboard/components/BalanceView/useHomeBalanceView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ export function useHomeBalanceView(currentAddress?: string | undefined) {
? null
: addressBalanceMap[currentAddress ?? ''];

// useEffect(() => {
// 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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 6a85ca6

Please sign in to comment.