Skip to content

Commit

Permalink
fix merging
Browse files Browse the repository at this point in the history
  • Loading branch information
ReflectiveChimp committed Jul 3, 2024
1 parent 8789926 commit ea5d1c4
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions src/utils/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ const mergeBalanceDelta = <T extends BalanceDelta>(prev: T, next: T): T => {
};
};

const sumBalanceDelta = <T extends BalanceDelta>(prev: T, next: T): T => {
return {
...next,
balance: prev.balance.add(next.balance),
delta: prev.delta.add(next.delta),
};
};

/** Merge ClmPositionInteractions that share the same tx hash */
const mergeClmPositionInteractions = (
chain: ChainId,
Expand Down Expand Up @@ -87,11 +95,13 @@ const mergeClmPositionInteractions = (
rewardPoolToken.decimals
),
}));
const rewardPoolTotal: BalanceDelta = rewardPools.reduce(sumBalanceDelta, {
balance: new Decimal(0),
delta: new Decimal(0),
});
const total: BalanceDelta = {
balance: interpretAsDecimal(interaction.totalBalance, managerToken.decimals),
delta: manager.delta.add(
rewardPools.reduce((acc, rp) => acc.add(rp.delta), new Decimal(0))
),
delta: manager.delta.add(rewardPoolTotal.delta),
};
const underlying0: BalanceDelta = {
balance: interpretAsDecimal(interaction.underlyingBalance0, token0.decimals),
Expand All @@ -110,21 +120,23 @@ const mergeClmPositionInteractions = (
const existingTx = acc[txHash];
if (existingTx) {
const mergedManaged = mergeBalanceDelta(existingTx.manager, manager);
const mergedRewardPool = rewardPools.reduce(
(acc, rp) => mergeBalanceDelta(acc, rp),
existingTx.rewardPoolTotal
const mergedRewardPools = rewardPools.map((rp, i) =>
existingTx.rewardPools[i] ? mergeBalanceDelta(existingTx.rewardPools[i], rp) : rp
);
const mergedRewardPoolTotal = mergeBalanceDelta(
existingTx.rewardPoolTotal,
rewardPoolTotal
);
const mergedUnderlying0 = mergeBalanceDelta(existingTx.underlying0, underlying0);
const mergedUnderlying1 = mergeBalanceDelta(existingTx.underlying1, underlying1);
const mergedTotal = mergeBalanceDelta(existingTx.total, total);

acc[txHash] = {
...existingTx,
manager: mergedManaged,
rewardPools: rewardPools.map((rp, i) =>
existingTx.rewardPools[i] ? mergeBalanceDelta(existingTx.rewardPools[i], rp) : rp
),
rewardPoolTotal: mergedRewardPool,
total: total,
rewardPools: mergedRewardPools,
rewardPoolTotal: mergedRewardPoolTotal,
total: mergedTotal,
underlying0: mergedUnderlying0,
underlying1: mergedUnderlying1,
usd: {
Expand All @@ -146,10 +158,7 @@ const mergeClmPositionInteractions = (
token1ToUsd,
manager,
rewardPools,
rewardPoolTotal: rewardPools.reduce((acc, rp) => mergeBalanceDelta(acc, rp), {
balance: new Decimal(0),
delta: new Decimal(0),
}),
rewardPoolTotal,
total,
underlying0,
underlying1,
Expand Down

0 comments on commit ea5d1c4

Please sign in to comment.