From 7aea63956ee4d6ac83b84a12fe034dd06958319c Mon Sep 17 00:00:00 2001 From: 0xshiba1 <158560741+0xshiba1@users.noreply.github.com> Date: Mon, 13 May 2024 13:17:09 +0800 Subject: [PATCH] claim reward dedupe bug fix --- .../account-details/AccountDetailsDialog.tsx | 5 +- .../account-details/EarningsTabContent.tsx | 5 +- .../account-details/HistoryTabContent.tsx | 74 ++++++++++++++----- frontend/src/lib/events.ts | 28 ------- 4 files changed, 60 insertions(+), 52 deletions(-) diff --git a/frontend/src/components/dashboard/account-details/AccountDetailsDialog.tsx b/frontend/src/components/dashboard/account-details/AccountDetailsDialog.tsx index c37bf62e..45406cc3 100644 --- a/frontend/src/components/dashboard/account-details/AccountDetailsDialog.tsx +++ b/frontend/src/components/dashboard/account-details/AccountDetailsDialog.tsx @@ -30,7 +30,6 @@ import { WithdrawEvent, eventSortAsc, eventSortDesc, - getDedupedClaimRewardEvents, } from "@/lib/events"; import { formatPoints, formatToken } from "@/lib/format"; import { API_URL } from "@/lib/navigation"; @@ -186,9 +185,7 @@ export default function AccountDetailsDialog() { withdraw: (data.withdraw ?? []).slice().sort(eventSortDesc), repay: (data.repay ?? []).slice().sort(eventSortDesc), liquidate: (data.liquidate ?? []).slice().sort(eventSortDesc), - claimReward: getDedupedClaimRewardEvents( - (data.claimReward ?? []).slice().sort(eventSortDesc), - ), + claimReward: (data.claimReward ?? []).slice().sort(eventSortDesc), }); } catch (err) { console.error(err); diff --git a/frontend/src/components/dashboard/account-details/EarningsTabContent.tsx b/frontend/src/components/dashboard/account-details/EarningsTabContent.tsx index d8f0e46d..5927fd96 100644 --- a/frontend/src/components/dashboard/account-details/EarningsTabContent.tsx +++ b/frontend/src/components/dashboard/account-details/EarningsTabContent.tsx @@ -4,6 +4,7 @@ import { ColumnDef } from "@tanstack/react-table"; import BigNumber from "bignumber.js"; import { ParsedObligation } from "@suilend/sdk/parsers/obligation"; +import { Side } from "@suilend/sdk/types"; import { EventsData, @@ -139,7 +140,9 @@ export default function EarningsTabContent({ ); if (claimedAmount.eq(0)) return; - const side = claimRewardEvent.isDepositReward ? "deposit" : "borrow"; + const side = claimRewardEvent.isDepositReward + ? Side.DEPOSIT + : Side.BORROW; result[side][reserve.coinType] = result[side][reserve.coinType] ?? {}; result[side][reserve.coinType][claimRewardEvent.coinType] = diff --git a/frontend/src/components/dashboard/account-details/HistoryTabContent.tsx b/frontend/src/components/dashboard/account-details/HistoryTabContent.tsx index 5471aa9f..12691b6e 100644 --- a/frontend/src/components/dashboard/account-details/HistoryTabContent.tsx +++ b/frontend/src/components/dashboard/account-details/HistoryTabContent.tsx @@ -3,6 +3,7 @@ import { useMemo } from "react"; import { ColumnDef, Row } from "@tanstack/react-table"; import BigNumber from "bignumber.js"; import { formatDate } from "date-fns"; +import { cloneDeep } from "lodash"; import { ChevronDown, ChevronUp } from "lucide-react"; import { useLocalStorage } from "usehooks-ts"; @@ -493,30 +494,65 @@ export default function HistoryTabContent({ }, []) .sort(eventSortDesc); - // Group liquidate events const finalRows: RowData[] = []; for (let i = 0; i < sortedRows.length; i++) { const row = sortedRows[i]; - if (row.eventType !== EventType.LIQUIDATE) finalRows.push(row); - else { - const lastRow = finalRows[finalRows.length - 1]; - - if ( - !lastRow || - lastRow.eventType !== EventType.LIQUIDATE || - (lastRow.event as LiquidateEvent).repayReserveId !== - (row.event as LiquidateEvent).repayReserveId || - (lastRow.event as LiquidateEvent).withdrawReserveId !== - (row.event as LiquidateEvent).withdrawReserveId - ) { - finalRows.push({ - ...row, - subRows: [row], - }); - } else { - (lastRow.subRows as RowData[]).push(row); + switch (row.eventType) { + // Dedupe CLAIM_REWARD events + case EventType.CLAIM_REWARD: { + const claimRewardEvent = row.event as ClaimRewardEvent; + + const lastRow = finalRows[finalRows.length - 1]; + if (!lastRow || lastRow.eventType !== EventType.CLAIM_REWARD) + finalRows.push(cloneDeep(row)); + else { + const lastClaimRewardEvent = lastRow.event as ClaimRewardEvent; + + if ( + lastClaimRewardEvent.coinType === claimRewardEvent.coinType && + lastClaimRewardEvent.isDepositReward === + claimRewardEvent.isDepositReward && + lastClaimRewardEvent.timestamp === claimRewardEvent.timestamp && + lastClaimRewardEvent.digest === claimRewardEvent.digest + ) { + ( + finalRows[finalRows.length - 1].event as ClaimRewardEvent + ).liquidityAmount = new BigNumber( + lastClaimRewardEvent.liquidityAmount, + ) + .plus(claimRewardEvent.liquidityAmount) + .toString(); + } else finalRows.push(row); + } + + break; + } + + // Group LIQUIDATE events + case EventType.LIQUIDATE: { + const liquidateEvent = row.event as LiquidateEvent; + + const lastRow = finalRows[finalRows.length - 1]; + if (!lastRow || lastRow.eventType !== EventType.LIQUIDATE) + finalRows.push({ ...row, subRows: [row] }); + else { + const lastLiquidateEvent = lastRow.event as LiquidateEvent; + + if ( + lastLiquidateEvent.repayReserveId === + liquidateEvent.repayReserveId && + lastLiquidateEvent.withdrawReserveId === + liquidateEvent.withdrawReserveId + ) + (lastRow.subRows as RowData[]).push(row); + else finalRows.push({ ...row, subRows: [row] }); + } + + break; } + default: + finalRows.push(row); } } diff --git a/frontend/src/lib/events.ts b/frontend/src/lib/events.ts index f14341df..00e680ae 100644 --- a/frontend/src/lib/events.ts +++ b/frontend/src/lib/events.ts @@ -1,5 +1,3 @@ -import BigNumber from "bignumber.js"; - export enum EventType { RESERVE_ASSET_DATA = "reserveAssetData", DEPOSIT = "deposit", @@ -169,29 +167,3 @@ export const eventSortDesc = (a: EventRow, b: EventRow) => { export const eventSortAsc = (a: EventRow, b: EventRow) => -1 * eventSortDesc(a, b); - -export const getDedupedClaimRewardEvents = (events: ClaimRewardEvent[]) => { - const dedupedClaimRewardEvents: ClaimRewardEvent[] = []; - for (const event of events) { - const lastEvent = - dedupedClaimRewardEvents[dedupedClaimRewardEvents.length - 1]; - - if (!lastEvent) dedupedClaimRewardEvents.push(event); - else { - if ( - lastEvent.coinType === event.coinType && - lastEvent.isDepositReward === event.isDepositReward && - lastEvent.timestamp === event.timestamp && - lastEvent.digest === event.digest - ) { - dedupedClaimRewardEvents[ - dedupedClaimRewardEvents.length - 1 - ].liquidityAmount = new BigNumber(lastEvent.liquidityAmount) - .plus(event.liquidityAmount) - .toString(); - } else dedupedClaimRewardEvents.push(event); - } - } - - return dedupedClaimRewardEvents; -};