From b3ffa165cbb620034da0b77f7b5f70a88487b0a2 Mon Sep 17 00:00:00 2001 From: Nam Nguyen Date: Thu, 19 Oct 2023 09:54:17 +0700 Subject: [PATCH] chore: clear old txs (#2309) --- .../Campaign/CampaignButtonWithOptions.tsx | 2 +- src/state/transactions/reducer.ts | 24 ++++++++++++------- src/state/transactions/updater.tsx | 3 ++- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/pages/Campaign/CampaignButtonWithOptions.tsx b/src/pages/Campaign/CampaignButtonWithOptions.tsx index 81123fec39..e5c310f737 100644 --- a/src/pages/Campaign/CampaignButtonWithOptions.tsx +++ b/src/pages/Campaign/CampaignButtonWithOptions.tsx @@ -79,7 +79,7 @@ export default function CampaignButtonWithOptions({ selectedCampaignLeaderboard, } = useSelector((state: AppState) => state.campaigns) const transactions = useMemo( - () => (campaign ? transactionsState[parseInt(campaign.rewardChainIds)] ?? {} : {}), + () => (campaign ? transactionsState[Number(campaign.rewardChainIds) as ChainId] ?? {} : {}), [transactionsState, campaign], ) const [claimingCampaignRewardId, setClaimingCampaignRewardId] = useSetClaimingCampaignRewardId() diff --git a/src/state/transactions/reducer.ts b/src/state/transactions/reducer.ts index 668e8cc35b..df7c6e293d 100644 --- a/src/state/transactions/reducer.ts +++ b/src/state/transactions/reducer.ts @@ -1,3 +1,4 @@ +import { ChainId } from '@kyberswap/ks-sdk-core' import { createReducer } from '@reduxjs/toolkit' import { findTx } from 'utils' @@ -12,16 +13,23 @@ import { removeTx, replaceTx, } from './actions' -import { GroupedTxsByHash, TransactionExtraInfo } from './type' +import { GroupedTxsByHash, TransactionDetails, TransactionExtraInfo } from './type' -const now = () => new Date().getTime() - -interface TransactionState { - [chainId: number]: GroupedTxsByHash | undefined +type TransactionState = { + [chainId in ChainId]?: GroupedTxsByHash | undefined } const initialState: TransactionState = {} +const clearOldTransactions = (transactions: GroupedTxsByHash | undefined): GroupedTxsByHash | undefined => { + if (!transactions) return undefined + const chainTxs = Object.values(transactions ?? {}).filter(Boolean) as TransactionDetails[][] + chainTxs.sort((a, b) => a[0].addedTime - b[0].addedTime) + const slicedChainTxs = chainTxs.slice(-10).filter(tx => tx[0].addedTime > Date.now() - 7 * 24 * 60 * 60 * 1000) + const result = slicedChainTxs.reduce((acc, cur) => ({ ...acc, [cur[0].hash]: cur }), {}) as GroupedTxsByHash + return result +} + export default createReducer(initialState, builder => builder .addCase( @@ -44,13 +52,13 @@ export default createReducer(initialState, builder => hash, type, from, - addedTime: now(), + addedTime: Date.now(), chainId, extraInfo, group: getTransactionGroupByType(type), }) chainTxs[txs[0].hash] = txs - transactions[chainId] = chainTxs + transactions[chainId] = clearOldTransactions(chainTxs) }, ) .addCase(clearAllTransactions, (transactions, { payload: { chainId } }) => { @@ -67,7 +75,7 @@ export default createReducer(initialState, builder => const tx = findTx(transactions[chainId], hash) if (!tx) return tx.receipt = receipt - tx.confirmedTime = now() + tx.confirmedTime = Date.now() const newExtraInfo: TransactionExtraInfo = { ...tx.extraInfo, needCheckSubgraph } tx.extraInfo = newExtraInfo }) diff --git a/src/state/transactions/updater.tsx b/src/state/transactions/updater.tsx index 4813dd3cfa..251887e441 100644 --- a/src/state/transactions/updater.tsx +++ b/src/state/transactions/updater.tsx @@ -1,4 +1,5 @@ import { BigNumber } from '@ethersproject/bignumber' +import { ChainId } from '@kyberswap/ks-sdk-core' import { ParsedTransactionWithMeta } from '@solana/web3.js' import { findReplacementTx } from 'find-replacement-tx' import { useEffect, useMemo } from 'react' @@ -46,7 +47,7 @@ export default function Updater(): null { const lastBlockNumber = useBlockNumber() const dispatch = useDispatch() - const transactionsState = useSelector( + const transactionsState = useSelector( state => state.transactions[chainId], )