From ea5d1c4261d6b37f732b0de26217d0862ab8016c Mon Sep 17 00:00:00 2001 From: ReflectiveChimp <55021052+ReflectiveChimp@users.noreply.github.com> Date: Wed, 3 Jul 2024 16:41:57 +0100 Subject: [PATCH] fix merging --- src/utils/timeline.ts | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/utils/timeline.ts b/src/utils/timeline.ts index 7413b55..f05bbdf 100644 --- a/src/utils/timeline.ts +++ b/src/utils/timeline.ts @@ -57,6 +57,14 @@ const mergeBalanceDelta = (prev: T, next: T): T => { }; }; +const sumBalanceDelta = (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, @@ -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), @@ -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: { @@ -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,