Skip to content

Commit

Permalink
claim reward dedupe bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
0xshiba1 committed May 13, 2024
1 parent c5b81fb commit 7aea639
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
WithdrawEvent,
eventSortAsc,
eventSortDesc,
getDedupedClaimRewardEvents,
} from "@/lib/events";
import { formatPoints, formatToken } from "@/lib/format";
import { API_URL } from "@/lib/navigation";
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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);
}
}

Expand Down
28 changes: 0 additions & 28 deletions frontend/src/lib/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import BigNumber from "bignumber.js";

export enum EventType {
RESERVE_ASSET_DATA = "reserveAssetData",
DEPOSIT = "deposit",
Expand Down Expand Up @@ -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;
};

0 comments on commit 7aea639

Please sign in to comment.