Skip to content

Commit

Permalink
chore: clear old txs (#2309)
Browse files Browse the repository at this point in the history
  • Loading branch information
namgold authored Oct 19, 2023
1 parent 34c1a5a commit b3ffa16
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/pages/Campaign/CampaignButtonWithOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
24 changes: 16 additions & 8 deletions src/state/transactions/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ChainId } from '@kyberswap/ks-sdk-core'
import { createReducer } from '@reduxjs/toolkit'

import { findTx } from 'utils'
Expand All @@ -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(
Expand All @@ -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 } }) => {
Expand All @@ -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
})
Expand Down
3 changes: 2 additions & 1 deletion src/state/transactions/updater.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -46,7 +47,7 @@ export default function Updater(): null {

const lastBlockNumber = useBlockNumber()
const dispatch = useDispatch<AppDispatch>()
const transactionsState = useSelector<AppState, AppState['transactions'][number]>(
const transactionsState = useSelector<AppState, AppState['transactions'][ChainId]>(
state => state.transactions[chainId],
)

Expand Down

0 comments on commit b3ffa16

Please sign in to comment.