Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only query reward tokens once per position #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/queries/InvestorTimeline.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ fragment InvestorTimelineClmPositionClm on CLM {
...Token
}
rewardPoolTokensOrder
rewardTokens {
...Token
}
rewardTokensOrder
underlyingToken0 {
...Token
}
Expand Down Expand Up @@ -58,12 +62,6 @@ fragment InvestorTimelineClmPositionInteraction on ClmPositionInteraction {
token0ToNativePrice
token1ToNativePrice
nativeToUSDPrice
clm {
rewardTokensOrder
rewardTokens {
...Token
}
}
type
}

Expand Down
2 changes: 1 addition & 1 deletion src/routes/v1/investor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function clmInteractionToOutput(interaction: TimelineClmInteraction): TimelineCl
reward_pool_claim_details: interaction.rewardBalancesDelta.map((rewardBalanceDelta, i) => ({
claimed_amount: rewardBalanceDelta.toString(),
reward_to_usd: interaction.rewardsToUsd[i].toString(),
reward_address: interaction.clm.rewardTokens[i],
reward_address: interaction.rewardTokens[i].address,
})),
claimed_reward_pool: interaction.claimedRewardPool?.toString(),
};
Expand Down
54 changes: 24 additions & 30 deletions src/utils/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { isDefined } from './array';
import { fromUnixTime } from './date';
import { interpretAsDecimal } from './decimal';
import { sortEntitiesByOrderList } from './entity-order';
import { FriendlyError } from './error';
import { getLoggerFor } from './log';
import type { Address } from './scalar-types';
import { type PaginatedAllSdkResult, executeOnAllSdks, paginate } from './sdk';
Expand Down Expand Up @@ -51,16 +52,14 @@ export type TimelineClmInteraction = {
manager: BalanceDelta;
rewardPools: BalanceDelta[];
rewardPoolTotal: BalanceDelta;
rewardBalancesDelta: Decimal[];
claimedRewardPool: string | null;
rewardsToUsd: Decimal[];
total: BalanceDelta;
underlying0: BalanceDelta;
underlying1: BalanceDelta;
usd: BalanceDelta;
clm: {
rewardTokens: Array<string>;
};
claimedRewardPool: string | null;
rewardTokens: Token[];
rewardBalancesDelta: Decimal[];
rewardsToUsd: Decimal[];
actions: ClmActionsEnum[];
};

Expand Down Expand Up @@ -107,6 +106,7 @@ const mergeClmPositionInteractions = (
chain: ChainId,
managerToken: Token,
rewardPoolTokens: Token[],
rewardTokens: Token[],
token0: Token,
token1: Token,
interactions: InvestorTimelineClmPositionInteractionFragment[]
Expand Down Expand Up @@ -137,18 +137,6 @@ const mergeClmPositionInteractions = (
delta: new Decimal(0),
});

const rewardTokens = interaction.clm.rewardTokensOrder.map(address => {
const token = interaction.clm.rewardTokens.find(
t => t.address.toLowerCase() === address.toLowerCase()
);
if (!token) {
throw new Error(`Missing reward token ${address}`);
}
return {
address: token.address,
decimals: Number.parseInt(token.decimals),
};
});
const rewardBalancesDelta: Decimal[] = interaction.rewardBalancesDelta.map((rewardDelta, i) =>
interpretAsDecimal(rewardDelta, rewardTokens[i].decimals)
);
Expand Down Expand Up @@ -200,8 +188,7 @@ const mergeClmPositionInteractions = (
const mergedRewardsToUsd =
rewardBalancesDelta.length > 0 ? rewardsToUsd : existingTx.rewardsToUsd;
const mergedClaimedRewardPool =
existingTx.claimedRewardPool ??
(interaction.claimedRewardPool ? interaction.claimedRewardPool.id : null);
interaction.claimedRewardPool?.id || existingTx.claimedRewardPool || null;

acc[txHash] = {
...existingTx,
Expand Down Expand Up @@ -235,19 +222,15 @@ const mergeClmPositionInteractions = (
manager,
rewardPools,
rewardPoolTotal,
rewardBalancesDelta,
claimedRewardPool: interaction.claimedRewardPool
? interaction.claimedRewardPool.id
: null,
claimedRewardPool: interaction.claimedRewardPool?.id || null,
rewardTokens,
rewardsToUsd,
rewardBalancesDelta,
total,
underlying0,
underlying1,
usd,
actions: [interaction.type],
clm: {
rewardTokens: interaction.clm.rewardTokensOrder,
},
};
}

Expand All @@ -266,12 +249,22 @@ const clmPositionToInteractions = (
): TimelineClmInteraction[] => {
const { id, clm } = position;
const managerToken = toToken(clm.managerToken);
const orderedRewardPoolTokenAddresses = sortEntitiesByOrderList(
const rewardPoolTokens = sortEntitiesByOrderList(
clm.rewardPoolTokens,
'address',
clm.rewardPoolTokensOrder
);
const rewardPoolTokens = orderedRewardPoolTokenAddresses.map(toToken).filter(Boolean) as Token[];
)
.map(toToken)
.filter(isDefined);
if (rewardPoolTokens.length < clm.rewardPoolTokensOrder.length) {
throw new FriendlyError(`Missing reward pool tokens for position ${id}`);
}
const rewardTokens = sortEntitiesByOrderList(clm.rewardTokens, 'address', clm.rewardTokensOrder)
.map(toToken)
.filter(isDefined);
if (rewardTokens.length < clm.rewardTokensOrder.length) {
throw new FriendlyError(`Missing reward tokens for position ${id}`);
}
const token0 = toToken(clm.underlyingToken0);
const token1 = toToken(clm.underlyingToken1);
if (!managerToken || !token0 || !token1) {
Expand All @@ -283,6 +276,7 @@ const clmPositionToInteractions = (
chainId,
managerToken,
rewardPoolTokens,
rewardTokens,
token0,
token1,
interactions
Expand Down
Loading