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

isolating networkContext.chain dependency #533

Merged
merged 1 commit into from
Nov 20, 2023
Merged
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
34 changes: 19 additions & 15 deletions modules/pool/lib/pool-creator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ export class PoolCreatorService {
return networkContext.services.balancerSubgraphService;
}

private get chain() {
return networkContext.chain
}

public async syncAllPoolsFromSubgraph(blockNumber: number): Promise<string[]> {
const existingPools = await prisma.prismaPool.findMany({ where: { chain: networkContext.chain } });
const existingPools = await prisma.prismaPool.findMany({ where: { chain: this.chain } });
const subgraphPools = await this.balancerSubgraphService.getAllPools({}, false);
const sortedSubgraphPools = this.sortSubgraphPools(subgraphPools);

Expand All @@ -39,11 +43,11 @@ export class PoolCreatorService {
}

public async syncNewPoolsFromSubgraph(blockNumber: number): Promise<string[]> {
const existingPools = await prisma.prismaPool.findMany({ where: { chain: networkContext.chain } });
const existingPools = await prisma.prismaPool.findMany({ where: { chain: this.chain } });
const latest = await prisma.prismaPool.findFirst({
orderBy: { createTime: 'desc' },
select: { createTime: true },
where: { chain: networkContext.chain },
where: { chain: this.chain },
});

const subgraphPools = await this.balancerSubgraphService.getAllPools(
Expand Down Expand Up @@ -93,7 +97,7 @@ export class PoolCreatorService {

if (nestedPool) {
await prisma.prismaPoolToken.update({
where: { id_chain: { id: token.id, chain: networkContext.chain } },
where: { id_chain: { id: token.id, chain: this.chain } },
data: { nestedPoolId: nestedPool.id },
});
}
Expand All @@ -106,12 +110,12 @@ export class PoolCreatorService {
let operations: any[] = [];
const pools = await prisma.prismaPool.findMany({
...prismaPoolWithExpandedNesting,
where: { chain: networkContext.chain },
where: { chain: this.chain },
});

//clear any existing
await prisma.prismaPoolExpandedTokens.updateMany({
where: { chain: networkContext.chain },
where: { chain: this.chain },
data: { nestedPoolId: null },
});

Expand Down Expand Up @@ -141,7 +145,7 @@ export class PoolCreatorService {
tokenAddress_poolId_chain: {
tokenAddress: token.address,
poolId: pool.id,
chain: networkContext.chain,
chain: this.chain,
},
},
data: { nestedPoolId: token.nestedPoolId },
Expand All @@ -159,7 +163,7 @@ export class PoolCreatorService {

const allNestedTypePools = await prisma.prismaPool.findMany({
where: {
chain: networkContext.chain,
chain: this.chain,
type: { in: [PrismaPoolType.LINEAR, PrismaPoolType.PHANTOM_STABLE] },
},
select: { id: true, address: true },
Expand All @@ -173,22 +177,22 @@ export class PoolCreatorService {
symbol: token.symbol,
name: token.name,
decimals: token.decimals,
chain: networkContext.chain,
chain: this.chain,
})),
{
address: pool.address,
symbol: pool.symbol || '',
name: pool.name || '',
decimals: 18,
chain: networkContext.chain,
chain: this.chain,
},
],
});

await prisma.prismaPool.create({
data: {
id: pool.id,
chain: networkContext.chain,
chain: this.chain,
createTime: pool.createTime,
address: pool.address,
symbol: pool.symbol || '',
Expand Down Expand Up @@ -286,7 +290,7 @@ export class PoolCreatorService {
await prisma.prismaPoolTokenDynamicData.createMany({
data: poolTokens.map((token) => ({
id: token.id,
chain: networkContext.chain,
chain: this.chain,
poolTokenId: token.id,
blockNumber,
priceRate: token.priceRate || '1.0',
Expand All @@ -303,7 +307,7 @@ export class PoolCreatorService {
public async createAllTokensRelationshipForPool(poolId: string): Promise<void> {
const pool = await prisma.prismaPool.findUnique({
...prismaPoolWithExpandedNesting,
where: { id_chain: { id: poolId, chain: networkContext.chain } },
where: { id_chain: { id: poolId, chain: this.chain } },
});

if (!pool) {
Expand All @@ -330,7 +334,7 @@ export class PoolCreatorService {
skipDuplicates: true,
data: allTokens.map((token) => ({
poolId,
chain: networkContext.chain,
chain: this.chain,
tokenAddress: token.address,
nestedPoolId: token.nestedPoolId || null,
})),
Expand All @@ -350,7 +354,7 @@ export class PoolCreatorService {
const token = poolTokens[i];

await prisma.prismaPoolToken.update({
where: { id_chain: { id: token.id, chain: networkContext.chain } },
where: { id_chain: { id: token.id, chain: this.chain } },
data: {
index: token.index || subgraphPool.tokensList.findIndex((address) => address === token.address),
},
Expand Down
6 changes: 3 additions & 3 deletions modules/pool/lib/pool-snapshot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ export class PoolSnapshotService {
const timestamp = this.getTimestampForRange(range);

return prisma.prismaPoolSnapshot.findMany({
where: { poolId, timestamp: { gte: timestamp }, chain: chain },
where: { poolId, timestamp: { gte: timestamp }, chain },
orderBy: { timestamp: 'asc' },
});
}

public async getSnapshotForPool(poolId: string, timestamp: number) {
public async getSnapshotForPool(poolId: string, timestamp: number, chain: Chain) {
return prisma.prismaPoolSnapshot.findUnique({
where: { id_chain: { id: `${poolId}-${timestamp}`, chain: this.chain } },
where: { id_chain: { id: `${poolId}-${timestamp}`, chain } },
});
}

Expand Down
34 changes: 19 additions & 15 deletions modules/pool/lib/pool-usd-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export class PoolUsdDataService {
return networkContext.services.balancerSubgraphService;
}

private get chain() {
return networkContext.chain;
}

/**
* Liquidity is dependent on token prices, so the values here are constantly in flux.
* When updating, the easiest is to update all pools at once.
Expand All @@ -26,7 +30,7 @@ export class PoolUsdDataService {
minShares: number = 0.00000000001,
maxShares: number = Number.MAX_SAFE_INTEGER,
) {
const tokenPrices = await this.tokenService.getTokenPrices();
const tokenPrices = await this.tokenService.getTokenPrices(this.chain);
const pools = await prisma.prismaPool.findMany({
include: { dynamicData: true, tokens: { include: { dynamicData: true } } },
where: {
Expand All @@ -40,7 +44,7 @@ export class PoolUsdDataService {
},
],
},
chain: networkContext.chain,
chain: this.chain,
},
});

Expand All @@ -66,15 +70,15 @@ export class PoolUsdDataService {
tokenId: item.id,
poolId: pool.id,
poolName: pool.name,
chain: networkContext.chain,
chain: pool.chain,
},
},
);
continue;
}
updates.push(
prisma.prismaPoolTokenDynamicData.update({
where: { id_chain: { id: item.id, chain: networkContext.chain } },
where: { id_chain: { id: item.id, chain: pool.chain } },
data: { balanceUSD: item.balanceUSD },
}),
);
Expand All @@ -86,7 +90,7 @@ export class PoolUsdDataService {
tags: {
poolId: pool.id,
poolName: pool.name,
chain: networkContext.chain,
chain: pool.chain,
},
},
);
Expand All @@ -95,7 +99,7 @@ export class PoolUsdDataService {

updates.push(
prisma.prismaPoolDynamicData.update({
where: { id_chain: { id: pool.id, chain: networkContext.chain } },
where: { id_chain: { id: pool.id, chain: pool.chain } },
data: { totalLiquidity },
}),
);
Expand Down Expand Up @@ -136,7 +140,7 @@ export class PoolUsdDataService {

updates.push(
prisma.prismaPoolDynamicData.update({
where: { id_chain: { id: pool.id, chain: networkContext.chain } },
where: { id_chain: { id: pool.id, chain: this.chain } },
data: { totalLiquidity24hAgo: totalLiquidity, totalShares24hAgo: pool.totalShares },
}),
);
Expand All @@ -153,7 +157,7 @@ export class PoolUsdDataService {
const yesterday = moment().subtract(1, 'day').unix();
const twoDaysAgo = moment().subtract(2, 'day').unix();
const pools = await prisma.prismaPool.findMany({
where: poolIds ? { id: { in: poolIds }, chain: networkContext.chain } : { chain: networkContext.chain },
where: poolIds ? { id: { in: poolIds }, chain: this.chain } : { chain: this.chain },
include: {
swaps: { where: { timestamp: { gte: twoDaysAgo } } },
dynamicData: true,
Expand Down Expand Up @@ -182,7 +186,7 @@ export class PoolUsdDataService {
) {
operations.push(
prisma.prismaPoolDynamicData.update({
where: { id_chain: { id: pool.id, chain: networkContext.chain } },
where: { id_chain: { id: pool.id, chain: pool.chain } },
data: { volume24h, fees24h, volume48h, fees48h },
}),
);
Expand All @@ -198,7 +202,7 @@ export class PoolUsdDataService {
*/
public async updateYieldCaptureForAllPools() {
const pools = await prisma.prismaPool.findMany({
where: { chain: networkContext.chain },
where: { chain: this.chain },
include: {
dynamicData: true,
aprItems: true,
Expand Down Expand Up @@ -252,7 +256,7 @@ export class PoolUsdDataService {

operations.push(
prisma.prismaPoolDynamicData.update({
where: { id_chain: { id: pool.id, chain: networkContext.chain } },
where: { id_chain: { id: pool.id, chain: pool.chain } },
data: { yieldCapture24h, yieldCapture48h },
}),
);
Expand All @@ -272,15 +276,15 @@ export class PoolUsdDataService {
const stakedUsers = await prisma.prismaUserStakedBalance.groupBy({
by: ['poolId'],
_count: { userAddress: true },
where: { chain: networkContext.chain, balanceNum: { gt: 0 } },
where: { chain: this.chain, balanceNum: { gt: 0 } },
});

for (const pool of subgraphPools) {
const staked = stakedUsers.find((stakedUser) => stakedUser.poolId === pool.id);

updates.push(
prisma.prismaPoolDynamicData.update({
where: { id_chain: { id: pool.id, chain: networkContext.chain } },
where: { id_chain: { id: pool.id, chain: this.chain } },
data: {
lifetimeVolume: parseFloat(pool.totalSwapVolume),
lifetimeSwapFees: parseFloat(pool.totalSwapFee),
Expand All @@ -291,7 +295,7 @@ export class PoolUsdDataService {
);

const snapshots = await prisma.prismaPoolSnapshot.findMany({
where: { poolId: pool.id, chain: networkContext.chain },
where: { poolId: pool.id, chain: this.chain },
});

if (snapshots.length > 0) {
Expand All @@ -306,7 +310,7 @@ export class PoolUsdDataService {

updates.push(
prisma.prismaPoolDynamicData.update({
where: { id_chain: { id: pool.id, chain: networkContext.chain } },
where: { id_chain: { id: pool.id, chain: this.chain } },
data: {
sharePriceAth: sharePriceAth.sharePrice,
sharePriceAthTimestamp: sharePriceAth.timestamp,
Expand Down
11 changes: 6 additions & 5 deletions modules/user/lib/user-snapshot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class UserSnapshotService {
where: {
id_chain: {
id: latestStoredUserPoolSnapshot.poolId,
chain: networkContext.chain,
chain: latestStoredUserPoolSnapshot.chain,
},
},
include: {
Expand All @@ -246,8 +246,9 @@ export class UserSnapshotService {
if (totalBalance > 0) {
//enrich with poolsnapshot data and save
const poolSnapshot = await this.poolSnapshotService.getSnapshotForPool(
latestStoredUserPoolSnapshot.poolId,
pool.id,
userSubgraphSnapshot.timestamp,
pool.chain,
);

/*
Expand All @@ -267,7 +268,7 @@ export class UserSnapshotService {
operations.push(
prisma.prismaUserPoolBalanceSnapshot.upsert({
where: {
id_chain: { id: userPoolBalanceSnapshotData.id, chain: networkContext.chain },
id_chain: { id: userPoolBalanceSnapshotData.id, chain: userPoolBalanceSnapshotData.chain },
},
create: userPoolBalanceSnapshotData,
update: userPoolBalanceSnapshotData,
Expand All @@ -280,7 +281,7 @@ export class UserSnapshotService {
id: `${pool.id}-${userSubgraphSnapshot.user.id.toLowerCase()}-${
userSubgraphSnapshot.timestamp
}`,
chain: networkContext.chain,
chain: pool.chain,
timestamp: userSubgraphSnapshot.timestamp,
userAddress: userSubgraphSnapshot.user.id.toLowerCase(),
poolId: pool.id,
Expand All @@ -297,7 +298,7 @@ export class UserSnapshotService {
operations.push(
prisma.prismaUserPoolBalanceSnapshot.upsert({
where: {
id_chain: { id: userPoolBalanceSnapshotData.id, chain: networkContext.chain },
id_chain: { id: userPoolBalanceSnapshotData.id, chain: userPoolBalanceSnapshotData.chain },
},
create: userPoolBalanceSnapshotData,
update: userPoolBalanceSnapshotData,
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
},
"exclude": [
"node_modules",
"debug"
"debug",
"**/*.spec.ts",
"**/*.test.ts"
]
}