Skip to content

Commit

Permalink
fix: changed pools syncing (#1300)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbronco authored Dec 12, 2024
1 parent 61ba34a commit 958fd86
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-mangos-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'backend': patch
---

fix: changed pools syncing
61 changes: 37 additions & 24 deletions modules/actions/pool/v3/sync-pools.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { Chain, PrismaPool, PrismaPoolType } from '@prisma/client';
import { Chain, PrismaPool } from '@prisma/client';
import { prisma } from '../../../../prisma/prisma-client';
import { enrichPoolUpsertsUsd } from '../../../sources/enrichers/pool-upserts-usd';
import { type VaultClient, getVaultClient, getPoolsClient } from '../../../sources/contracts';
import { VaultClient, getVaultClient, getPoolsClient } from '../../../sources/contracts';
import { syncDynamicTypeDataForPools } from './type-data/sync-dynamic-type-data-for-pools';
import { ViemClient } from '../../../sources/viem-client';
import { applyOnchainDataUpdateV3 } from '../../../sources/enrichers/apply-onchain-data';

const syncVaultData = async (
vaultClient: VaultClient,
chain: Chain,
ids: string[],
blockNumber: bigint,
) => {
const syncVaultData = async (vaultClient: VaultClient, chain: Chain, ids: string[], blockNumber: bigint) => {
// Enrich with onchain data for all the pools
const onchainData = await vaultClient.fetchPoolData(ids, blockNumber);

Expand All @@ -37,23 +32,36 @@ const syncVaultData = async (
})
.then((prices) => Object.fromEntries(prices.map((price) => [price.tokenAddress, price.price])));

const poolsWithUSD = dbUpdates.map((upsert) => enrichPoolUpsertsUsd(
{ poolDynamicData: upsert.poolDynamicData, poolToken: upsert.poolToken },
prices,
));
const poolsWithUSD = dbUpdates.map((upsert) =>
enrichPoolUpsertsUsd({ poolDynamicData: upsert.poolDynamicData, poolToken: upsert.poolToken }, prices),
);

// Update pools data to the database
for (const { poolDynamicData } of poolsWithUSD) {
for (const { poolDynamicData, poolToken } of poolsWithUSD) {
try {
await prisma.prismaPoolDynamicData.update({
where: {
poolId_chain: {
poolId: poolDynamicData.id,
chain: chain,
await prisma.$transaction([
prisma.prismaPoolDynamicData.update({
where: {
poolId_chain: {
poolId: poolDynamicData.id,
chain: chain,
},
},
},
data: poolDynamicData,
});
data: poolDynamicData,
}),
...poolToken.map((token) =>
prisma.prismaPoolToken.upsert({
where: {
id_chain: {
id: token.id,
chain,
},
},
create: token,
update: token,
}),
),
]);
} catch (e) {
console.error('Error upserting pool', e);
}
Expand Down Expand Up @@ -83,8 +91,13 @@ export const syncPools = async (
const vaultClient = getVaultClient(client, vaultAddress);
const poolsClient = getPoolsClient(client);

await syncVaultData(vaultClient, chain, pools.map(({id}) => id), blockNumber);
await syncVaultData(
vaultClient,
chain,
pools.map(({ id }) => id),
blockNumber,
);
await syncDynamicTypeDataForPools(poolsClient, pools, blockNumber);

return pools.map(({id}) => id);
};
return pools.map(({ id }) => id);
};
8 changes: 0 additions & 8 deletions modules/controllers/pool-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,8 @@ export function PoolController(tracer?: any) {
})
.then((pools) => pools.map((pool) => pool.id.toLowerCase()));

console.log(
`[syncChangedPoolsV3] Changed ${changedPoolsIds.length} pools between ${fromBlock} and ${toBlock} `,
changedPoolsIds,
);

const poolsToSync = pools.filter((pool) => changedPoolsIds.includes(pool.id.toLowerCase())); // only sync pools that are in the database
if (poolsToSync.length === 0) {
console.log(`[syncChangedPoolsV3] no pools in DB`);
return [];
}
const poolsToSyncIds = poolsToSync.map(({ id }) => id);
Expand All @@ -287,8 +281,6 @@ export function PoolController(tracer?: any) {
// Leaving safety margin for reorgs
await upsertLastSyncedBlock(chain, PrismaLastBlockSyncedCategory.POOLS_V3, toBlock - 10);

console.log(`[syncChangedPoolsV3] updated pools: `, poolsToSyncIds);

return poolsToSyncIds;
},
async updateLiquidity24hAgoV3(chain: Chain) {
Expand Down
2 changes: 0 additions & 2 deletions tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ async function run(job: string = process.argv[2], chainId: string = process.argv
return allSnapshotsSynced;
} else if (job === 'sync-cow-amm-swaps') {
return CowAmmController().syncSwaps(chain);
} else if (job === 'update-com-amm-volume-and-fees') {
return CowAmmController().updateVolumeAndFees(chain);
} else if (job === 'sync-cow-amm-join-exits') {
return CowAmmController().syncJoinExits(chain);
} else if (job === 'update-surplus-aprs') {
Expand Down

0 comments on commit 958fd86

Please sign in to comment.