Skip to content

Commit

Permalink
apr: refactor seed script (#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
ribeirojose authored Jan 3, 2024
1 parent d997419 commit 55ae2ee
Show file tree
Hide file tree
Showing 80 changed files with 55,147 additions and 17,387 deletions.
20 changes: 10 additions & 10 deletions apps/balancer-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@
"@radix-ui/react-toast": "^1.1.5",
"@radix-ui/react-tooltip": "1.0.7",
"@rainbow-me/rainbowkit": "1.3.1",
"@sentry/nextjs": "^7.88.0",
"@sentry/nextjs": "^7.91.0",
"@sentry/profiling-node": "^1.3.2",
"@wagmi/cli": "1.5.2",
"@wagmi/core": "^1.4.12",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"clsx": "^2.1.0",
"copy-to-clipboard": "^3.3.3",
"date-fns": "^2.30.0",
"dotenv": "^16.3.1",
"drizzle-orm": "^0.29.1",
"drizzle-orm": "^0.29.3",
"downshift": "^8.2.3",
"fathom-client": "^3.6.0",
"graphql-request": "6.1.0",
Expand All @@ -76,26 +76,26 @@
"devDependencies": {
"@bleu-fi/eslint-config": "workspace:^",
"@bleu-fi/utils": "workspace:^",
"@testing-library/jest-dom": "6.1.5",
"@testing-library/jest-dom": "6.1.6",
"@testing-library/react": "^14.1.2",
"@testing-library/user-event": "14.5.1",
"@testing-library/user-event": "14.5.2",
"@types/jest": "^29.5.11",
"@types/lodash": "^4.14.202",
"@types/lodash.merge": "^4.6.9",
"@types/node": "^18.18.5",
"@types/plotly.js": "^2.12.31",
"@types/react": "^18.2.45",
"@types/node": "^20.10.6",
"@types/plotly.js": "^2.12.32",
"@types/react": "^18.2.46",
"@types/react-dom": "^18.2.18",
"@types/react-plotly.js": "^2.6.3",
"autoprefixer": "^10.4.16",
"drizzle-kit": "^0.20.7",
"drizzle-kit": "^0.20.9",
"esbuild-register": "^3.5.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "29.7.0",
"npm-run-all": "^4.1.5",
"postcss": "^8.4.32",
"tailwind-scrollbar": "^3.0.5",
"tailwindcss": "^3.3.7",
"tailwindcss": "^3.4.0",
"ts-jest": "^29.1.1",
"typescript": "5.3.3"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export async function fetchDataForPoolIdDateRange(
),
})
.from(rewardsTokenApr)
.where(
sql.raw(
`rewards_token_apr.period_end - rewards_token_apr.timestamp > interval '1 day'`,
),
)
.groupBy(rewardsTokenApr.poolExternalId, rewardsTokenApr.timestamp)
.as("rewardAprSum");

Expand All @@ -63,9 +68,19 @@ export async function fetchDataForPoolIdDateRange(
value: rewardsTokenApr.value,
tokenAddress: rewardsTokenApr.tokenAddress,
tokenSymbol: tokens.symbol,
periodStart: rewardsTokenApr.periodStart,
periodEnd: rewardsTokenApr.periodEnd,
networkSlug: tokens.networkSlug,
})
.from(rewardsTokenApr)
.leftJoin(tokens, eq(tokens.address, rewardsTokenApr.tokenAddress))
.innerJoin(pools, eq(pools.externalId, rewardsTokenApr.poolExternalId))
.innerJoin(
tokens,
and(
eq(tokens.address, rewardsTokenApr.tokenAddress),
eq(pools.networkSlug, tokens.networkSlug),
),
)
.as("rewardAprToken");

const poolStatsData = await db
Expand Down Expand Up @@ -155,16 +170,20 @@ export async function fetchDataForPoolIdDateRange(
tokenSymbol: rewardAprToken.tokenSymbol,
})
.from(rewardAprToken)
.leftJoin(
.innerJoin(
poolSnapshots,
and(
eq(poolSnapshots.poolExternalId, rewardAprToken.poolExternalId),
eq(poolSnapshots.timestamp, rewardAprToken.timestamp),
eq(poolSnapshots.networkSlug, rewardAprToken.networkSlug),
),
)
.where(
and(
between(rewardAprToken.timestamp, startDate, endDate),
sql.raw(
`"rewardAprToken".period_end - "rewardAprToken".timestamp > interval '1 day'`,
),
eq(rewardAprToken.poolExternalId, poolId),
),
);
Expand All @@ -177,7 +196,13 @@ export async function fetchDataForPoolIdDateRange(
symbol: tokens.symbol,
})
.from(poolTokens)
.leftJoin(tokens, eq(tokens.address, poolTokens.tokenAddress))
.leftJoin(
tokens,
and(
eq(poolTokens.networkSlug, tokens.networkSlug),
eq(tokens.address, poolTokens.tokenAddress),
),
)
.where(eq(poolTokens.poolExternalId, poolId));

const returnData = poolStatsData.map((pool) => {
Expand Down
100 changes: 57 additions & 43 deletions apps/balancer-tools/src/app/apr/(utils)/fetchForDateRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
inArray,
sql,
SQLWrapper,
sum,
} from "drizzle-orm";

import { PoolTypeEnum } from "./types";
Expand Down Expand Up @@ -66,11 +67,10 @@ export async function fetchDataForDateRange({
.select({
poolExternalId: yieldTokenApr.poolExternalId,
timestamp: yieldTokenApr.timestamp,
yieldValueSum: sql<number>`sum(${yieldTokenApr.value})`.as(
"yieldValueSum",
),
yieldValueSum: sum(yieldTokenApr.value).as("yieldValueSum"),
})
.from(yieldTokenApr)
.where(between(yieldTokenApr.timestamp, startDate, endDate))
.groupBy(yieldTokenApr.poolExternalId, yieldTokenApr.timestamp)
.as("yieldAprSum");

Expand All @@ -83,41 +83,49 @@ export async function fetchDataForDateRange({
),
})
.from(rewardsTokenApr)
.where(between(rewardsTokenApr.timestamp, startDate, endDate))
.groupBy(rewardsTokenApr.poolExternalId, rewardsTokenApr.timestamp)
.as("rewardAprSum");

const poolAprForDate = db
const avgApr = sql<number>`cast(sum(coalesce(${swapFeeApr.value},0) + coalesce(${vebalApr.value},0) + coalesce(${yieldAprSum.yieldValueSum},0)+ coalesce(${rewardAprSum.rewardValueSum},0)) / count(${poolSnapshots.id}) as decimal)`;
const avgLiquidity = sql<number>`cast(sum(${poolSnapshots.liquidity}) / count(${poolSnapshots.id}) as decimal)`;

const sortFieldMapping: Record<string, SQLWrapper> = {
apr: avgApr,
tvl: avgLiquidity,
};

const orderBy =
order === "desc"
? desc(sortFieldMapping[sort])
: asc(sortFieldMapping[sort]);

const orderedPoolAprForDate = await db
.select({
poolExternalId: poolSnapshots.poolExternalId,
network: pools.networkSlug,
type: pools.poolType,
symbol: pools.symbol,
avgApr:
sql<number>`cast(sum(coalesce(${swapFeeApr.value},0) + coalesce(${vebalApr.value},0) + coalesce(${yieldAprSum.yieldValueSum},0)+ coalesce(${rewardAprSum.rewardValueSum},0)) / count(${poolSnapshots.timestamp}) as decimal)`.as(
"avgApr",
),
avgApr: avgApr.as("avgApr"),
avgFeeApr:
sql<number>`cast(sum(${swapFeeApr.value}) / count(${poolSnapshots.timestamp}) as decimal)`.as(
sql<number>`cast(sum(${swapFeeApr.value}) / count(${poolSnapshots.id}) as decimal)`.as(
"avgFeeApr",
),
avgVebalApr:
sql<number>`cast(sum(${vebalApr.value}) / count(${poolSnapshots.timestamp}) as decimal)`.as(
sql<number>`cast(sum(${vebalApr.value}) / count(${poolSnapshots.id}) as decimal)`.as(
"avgVebalApr",
),
avgVolume:
sql<number>`cast(sum(${poolSnapshots.swapVolume}) / count(${poolSnapshots.timestamp}) as decimal)`.as(
sql<number>`cast(sum(${poolSnapshots.swapVolume}) / count(${poolSnapshots.id}) as decimal)`.as(
"avgVolume",
),
avgLiquidity:
sql<number>`cast(sum(${poolSnapshots.liquidity}) / count(${poolSnapshots.timestamp}) as decimal)`.as(
"avgLiquidity",
),
avgLiquidity: avgLiquidity.as("avgLiquidity"),
avgYieldTokenApr:
sql<number>`cast(sum(coalesce(${yieldAprSum.yieldValueSum},0)) / count(${poolSnapshots.timestamp}) as decimal)`.as(
sql<number>`cast(sum(coalesce(${yieldAprSum.yieldValueSum},0)) / count(${poolSnapshots.id}) as decimal)`.as(
"avgYieldTokenApr",
),
avgRewardTokenApr:
sql<number>`cast(sum(coalesce(${rewardAprSum.rewardValueSum},0)) / count(${poolSnapshots.timestamp}) as decimal)`.as(
sql<number>`cast(sum(coalesce(${rewardAprSum.rewardValueSum},0)) / count(${poolSnapshots.id}) as decimal)`.as(
"avgRewardTokenApr",
),
})
Expand Down Expand Up @@ -150,16 +158,25 @@ export async function fetchDataForDateRange({
eq(rewardAprSum.timestamp, poolSnapshots.timestamp),
),
)
.leftJoin(pools, eq(pools.externalId, poolSnapshots.poolExternalId))
.leftJoin(poolTokens, eq(poolTokens.poolExternalId, pools.externalId))
.leftJoin(tokensTable, eq(tokensTable.address, poolTokens.tokenAddress))
.innerJoin(pools, eq(pools.externalId, poolSnapshots.poolExternalId))
.innerJoin(
poolTokens,
eq(poolTokens.poolExternalId, poolSnapshots.poolExternalId),
)
.innerJoin(tokensTable, eq(tokensTable.address, poolTokens.tokenAddress))
.where(
and(
between(poolSnapshots.timestamp, startDate, endDate),
between(poolSnapshots.liquidity, minTvl, maxTvl),
(networks ? inArray(pools.networkSlug, networks) : true) as SQLWrapper,
(tokens ? inArray(tokensTable.symbol, tokens) : true) as SQLWrapper,
(typesArray ? inArray(pools.poolType, typesArray) : true) as SQLWrapper,
(networks?.length
? inArray(pools.networkSlug, networks)
: true) as SQLWrapper,
(tokens?.length
? inArray(tokensTable.symbol, tokens)
: true) as SQLWrapper,
(typesArray?.length
? inArray(pools.poolType, typesArray)
: true) as SQLWrapper,
),
)
.groupBy(
Expand All @@ -168,24 +185,12 @@ export async function fetchDataForDateRange({
pools.symbol,
pools.poolType,
)
.as("poolAprForDate");

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const sortFieldMapping: Record<string, any> = {
apr: poolAprForDate.avgApr,
tvl: poolAprForDate.avgLiquidity,
};
const sortField = sortFieldMapping[sort];

const orderBy = order === "desc" ? desc(sortField) : asc(sortField);

const orderedPoolAprForDate = await db
.select()
.from(poolAprForDate)
.orderBy(orderBy)
.where(gt(poolAprForDate.avgApr, 0.1))
.having(gt(avgApr, 0))
.limit(Number(limit));

console.log({ tokens, orderedPoolAprForDate });

const poolsTokens = await db
.select({
poolExternalId: poolTokens.poolExternalId,
Expand All @@ -194,12 +199,21 @@ export async function fetchDataForDateRange({
symbol: tokensTable.symbol,
})
.from(poolTokens)
.leftJoin(tokensTable, eq(tokensTable.address, poolTokens.tokenAddress))
.where(
inArray(
poolTokens.poolExternalId,
orderedPoolAprForDate.map((p) => p.poolExternalId as string),
.leftJoin(
tokensTable,
and(
eq(tokensTable.address, poolTokens.tokenAddress),
eq(tokensTable.networkSlug, poolTokens.networkSlug),
),
)
.where(
// @ts-expect-error
orderedPoolAprForDate?.length
? inArray(
poolTokens.poolExternalId,
orderedPoolAprForDate.map((p) => p.poolExternalId as string),
)
: true,
);

const returnData = orderedPoolAprForDate.map((pool) => {
Expand Down
16 changes: 8 additions & 8 deletions apps/cow-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@
"@safe-global/protocol-kit": "^2.0.0",
"@safe-global/safe-core-sdk-types": "^2.3.0",
"@safe-global/safe-gateway-typescript-sdk": "^3.13.3",
"@sentry/nextjs": "^7.88.0",
"@sentry/nextjs": "^7.91.0",
"@sentry/profiling-node": "^1.3.2",
"@wagmi/cli": "1.5.2",
"@wagmi/core": "^1.4.12",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"clsx": "^2.1.0",
"copy-to-clipboard": "^3.3.3",
"date-fns": "^2.30.0",
"downshift": "^8.2.3",
Expand All @@ -76,15 +76,15 @@
"@bleu-fi/utils": "workspace:^",
"@graphql-codegen/cli": "5.0.0",
"@graphql-codegen/typescript-graphql-request": "6.1.0",
"@testing-library/jest-dom": "6.1.5",
"@testing-library/jest-dom": "6.1.6",
"@testing-library/react": "^14.1.2",
"@testing-library/user-event": "14.5.1",
"@testing-library/user-event": "14.5.2",
"@types/jest": "^29.5.11",
"@types/lodash": "^4.14.202",
"@types/lodash.merge": "^4.6.9",
"@types/node": "^18.18.5",
"@types/plotly.js": "^2.12.31",
"@types/react": "^18.2.45",
"@types/node": "^20.10.6",
"@types/plotly.js": "^2.12.32",
"@types/react": "^18.2.46",
"@types/react-dom": "^18.2.18",
"@types/react-plotly.js": "^2.6.3",
"autoprefixer": "^10.4.16",
Expand All @@ -93,7 +93,7 @@
"npm-run-all": "^4.1.5",
"postcss": "^8.4.32",
"tailwind-scrollbar": "^3.0.5",
"tailwindcss": "^3.3.7",
"tailwindcss": "^3.4.0",
"ts-jest": "^29.1.1",
"typescript": "5.3.3"
}
Expand Down
2 changes: 1 addition & 1 deletion apps/pool-metadata/subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"devDependencies": {
"@bleu-fi/utils": "workspace:^",
"@bleu-fi/tsconfig": "workspace:*",
"@graphprotocol/graph-cli": "0.64.0",
"@graphprotocol/graph-cli": "0.64.1",
"@graphprotocol/graph-ts": "0.32.0",
"@types/fs-extra": "^11.0.4",
"@types/js-yaml": "^4.0.9",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"homepage": "https://github.com/bleu-fi/monorepo#readme",
"devDependencies": {
"@bleu-fi/eslint-config": "workspace:*",
"@types/node": "^18.18.5",
"@types/node": "^20.10.6",
"concurrently": "^8.2.2",
"ghooks": "^2.0.4",
"lint-staged": "^15.2.0",
Expand Down
11 changes: 6 additions & 5 deletions packages/balancer-apr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
"devDependencies": {
"@bleu-fi/tsconfig": "workspace:*",
"@bleu-fi/utils": "workspace:^",
"@types/node": "18.18.5",
"drizzle-kit": "^0.20.7",
"@types/node": "20.10.6",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"drizzle-kit": "^0.20.9",
"esbuild-register": "^3.5.0",
"graphql": "16.8.1",
"micromatch": "4.0.5",
Expand All @@ -35,11 +37,10 @@
"swr": ">=2"
},
"dependencies": {
"@vercel/postgres": "^0.5.1",
"drizzle-orm": "^0.29.1",
"drizzle-orm": "^0.29.3",
"p-throttle": "^6.1.0",
"postgres": "^3.4.3",
"tiny-invariant": "^1.3.1",
"viem": "^1.20.2"
"viem": "^1.21.4"
}
}
Loading

0 comments on commit 55ae2ee

Please sign in to comment.