Skip to content

Commit

Permalink
Merge branch 'adapt-for-multi-rewardpool'
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Jul 3, 2024
2 parents 0cd5c13 + 6ae3e8d commit e210b85
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 38 deletions.
4 changes: 4 additions & 0 deletions src/config/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as dotenv from 'dotenv';
import { getLogLevelEnv, getNumberEnv } from '../utils/env';

dotenv.config();

export const API_ENV: string = process.env.API_ENV || 'production';
export const API_PORT: number = getNumberEnv('PORT', 4000);
export const API_CORS_ORIGIN: RegExp = new RegExp(
Expand All @@ -8,3 +11,4 @@ export const API_CORS_ORIGIN: RegExp = new RegExp(
);
export const API_RATE_LIMIT = getNumberEnv('API_RATE_LIMIT', 60); // per minute
export const LOG_LEVEL = getLogLevelEnv('LOG_LEVEL', 'info');
export const SUBGRAPH_TAG = process.env.SUBGRAPH_TAG || 'latest';
10 changes: 5 additions & 5 deletions src/queries/InvestorTimeline.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fragment InvestorTimelineClmPosition on CLM {
managerToken {
...InvestorTimelineToken
}
rewardPoolToken {
rewardPoolTokens {
...InvestorTimelineToken
}
underlyingToken0 {
Expand All @@ -19,7 +19,7 @@ fragment InvestorTimelineClmPosition on CLM {
...InvestorTimelineToken
}
managerTotalSupply
rewardPoolTotalSupply
rewardPoolsTotalSupply
token0ToNativePrice
token1ToNativePrice
nativeToUSDPrice
Expand All @@ -36,8 +36,8 @@ fragment InvestorTimelineClmPositionInteraction on ClmPositionInteraction {
totalBalance
managerBalance
managerBalanceDelta
rewardPoolBalance
rewardPoolBalanceDelta
rewardPoolBalances
rewardPoolBalancesDelta
underlyingBalance0
underlyingBalance0Delta
underlyingBalance1
Expand All @@ -53,7 +53,7 @@ query InvestorTimeline($investor_address: String!, $first: Int = 1000, $skip: In
id

managerBalance
rewardPoolBalance
rewardPoolBalances
totalBalance
clm {
...InvestorTimelineClmPosition
Expand Down
4 changes: 2 additions & 2 deletions src/queries/VaultInvestors.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ query VaultInvestors($clmAddress: String!, $first: Int! = 1000, $skip: Int! = 0)
underlyingToken1 {
...Token
}
rewardPoolToken {
rewardPoolTokens {
...Token
}
managerTotalSupply
Expand All @@ -41,7 +41,7 @@ query VaultInvestors($clmAddress: String!, $first: Int! = 1000, $skip: Int! = 0)
userAddress: id
}
managerBalance
rewardPoolBalance
rewardPoolBalances
totalBalance
}
}
3 changes: 1 addition & 2 deletions src/queries/codegen/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { CodegenConfig } from '@graphql-codegen/cli';

const SUBGRAPH_TAG = process.env.SUBGRAPH_TAG || 'latest';
import { SUBGRAPH_TAG } from '../../config/env';

const config: CodegenConfig = {
schema: `https://api.goldsky.com/api/public/project_clu2walwem1qm01w40v3yhw1f/subgraphs/beefy-clm-arbitrum/${SUBGRAPH_TAG}/gn`,
Expand Down
7 changes: 4 additions & 3 deletions src/routes/v1/investor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ async function getTimeline(investor_address: Address): Promise<TimelineClmIntera
const timeline = await getClmTimeline(investor_address);

return timeline.map((interaction): TimelineClmInteractionOutput => {
const { rewardPoolToken, rewardPool } = interaction;
const hasRewardPool = !!rewardPoolToken && !!rewardPool;
const { rewardPoolTokens, rewardPool } = interaction;
const hasRewardPool = rewardPoolTokens.length > 0;

// ensure we don't include partial reward pool data
const rewardPoolFields: ClmInteractionRewardPool | undefined = hasRewardPool
? {
reward_pool_address: rewardPoolToken.address,
reward_pool_address: rewardPoolTokens.map(t => t.address).join(','), // multiple reward pools
reward_pool_balance: rewardPool.balance.toString(),
reward_pool_diff: rewardPool.delta.toString(),
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { type Static, Type } from '@sinclair/typebox';
import { GraphQLClient } from 'graphql-request';
import { isArray } from 'lodash';
import { type ChainId, allChainIds, chainIdSchema } from '../config/chains';
import { SUBGRAPH_TAG } from '../config/env';
import { type Sdk, getSdk } from '../queries/codegen/sdk';
import { withTimeout } from './async';
import { GraphQueryError } from './error';
import { createCachedFactoryByChainId } from './factory';
import { getLoggerFor } from './log';

const SUBGRAPH_TAG = process.env.SUBGRAPH_TAG || 'latest';
const logger = getLoggerFor('sdk');

// adds the context to the response on all sdk queries
Expand Down
54 changes: 29 additions & 25 deletions src/utils/timeline.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Static } from '@sinclair/typebox';
import type Decimal from 'decimal.js';
import Decimal from 'decimal.js';
import { groupBy, keyBy } from 'lodash';
import type { ChainId } from '../config/chains';
import {
Expand Down Expand Up @@ -37,11 +37,11 @@ type TimelineClmInteraction = {
chain: ChainId;
transactionHash: string;
managerToken: Token;
rewardPoolToken: Token | undefined;
rewardPoolTokens: Token[];
token0ToUsd: Decimal;
token1ToUsd: Decimal;
manager: BalanceDelta;
rewardPool: BalanceDelta | undefined;
rewardPool: BalanceDelta;
total: BalanceDelta;
underlying0: BalanceDelta;
underlying1: BalanceDelta;
Expand All @@ -60,7 +60,7 @@ const mergeBalanceDelta = <T extends BalanceDelta>(prev: T, next: T): T => {
const mergeClmPositionInteractions = (
chain: ChainId,
managerToken: Token,
rewardPoolToken: Token | undefined,
rewardPoolTokens: Token[],
token0: Token,
token1: Token,
interactions: InvestorTimelineClmPositionInteractionFragment[]
Expand All @@ -76,15 +76,21 @@ const mergeClmPositionInteractions = (
balance: interpretAsDecimal(interaction.managerBalance, managerToken.decimals),
delta: interpretAsDecimal(interaction.managerBalanceDelta, managerToken.decimals),
};
const rewardPool: BalanceDelta | undefined = rewardPoolToken
? {
balance: interpretAsDecimal(interaction.rewardPoolBalance, managerToken.decimals),
delta: interpretAsDecimal(interaction.rewardPoolBalanceDelta, managerToken.decimals),
}
: undefined;
const rewardPools: BalanceDelta[] = rewardPoolTokens.map((rewardPoolToken, i) => ({
balance: interpretAsDecimal(
interaction.rewardPoolBalances[i] || '0',
rewardPoolToken.decimals
),
delta: interpretAsDecimal(
interaction.rewardPoolBalancesDelta[i] || '0',
rewardPoolToken.decimals
),
}));
const total: BalanceDelta = {
balance: interpretAsDecimal(interaction.totalBalance, managerToken.decimals),
delta: rewardPool ? manager.delta.add(rewardPool.delta) : manager.delta,
delta: manager.delta.add(
rewardPools.reduce((acc, rp) => acc.add(rp.delta), new Decimal(0))
),
};
const underlying0: BalanceDelta = {
balance: interpretAsDecimal(interaction.underlyingBalance0, token0.decimals),
Expand All @@ -103,23 +109,18 @@ const mergeClmPositionInteractions = (
const existingTx = acc[txHash];
if (existingTx) {
const mergedManaged = mergeBalanceDelta(existingTx.manager, manager);
const mergedRewardPool =
existingTx.rewardPool && rewardPool
? mergeBalanceDelta(existingTx.rewardPool, rewardPool)
: rewardPool;
const mergedRewardPool = rewardPools.reduce(
(acc, rp) => mergeBalanceDelta(acc, rp),
existingTx.rewardPool
);
const mergedUnderlying0 = mergeBalanceDelta(existingTx.underlying0, underlying0);
const mergedUnderlying1 = mergeBalanceDelta(existingTx.underlying1, underlying1);

acc[txHash] = {
...existingTx,
manager: mergedManaged,
rewardPool: mergedRewardPool,
total: {
balance: total.balance,
delta: mergedRewardPool
? mergedManaged.delta.add(mergedRewardPool.delta)
: mergedManaged.delta,
},
total: total,
underlying0: mergedUnderlying0,
underlying1: mergedUnderlying1,
usd: {
Expand All @@ -136,11 +137,14 @@ const mergeClmPositionInteractions = (
chain,
transactionHash: txHash,
managerToken,
rewardPoolToken,
rewardPoolTokens,
token0ToUsd,
token1ToUsd,
manager,
rewardPool,
rewardPool: rewardPools.reduce((acc, rp) => mergeBalanceDelta(acc, rp), {
balance: new Decimal(0),
delta: new Decimal(0),
}),
total,
underlying0,
underlying1,
Expand Down Expand Up @@ -173,7 +177,7 @@ const clmPositionToInteractions = (
interactions: InvestorTimelineClmPositionInteractionFragment[]
): TimelineClmInteraction[] => {
const managerToken = toToken(position.managerToken);
const rewardPoolToken = toToken(position.rewardPoolToken);
const rewardPoolTokens = position.rewardPoolTokens.map(toToken).filter(Boolean) as Token[];
const token0 = toToken(position.underlyingToken0);
const token1 = toToken(position.underlyingToken1);
if (!managerToken || !token0 || !token1) {
Expand All @@ -184,7 +188,7 @@ const clmPositionToInteractions = (
return mergeClmPositionInteractions(
chainId,
managerToken,
rewardPoolToken,
rewardPoolTokens,
token0,
token1,
interactions
Expand Down

0 comments on commit e210b85

Please sign in to comment.