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

fix: syncing changed pools on V3 #1267

Merged
merged 1 commit into from
Dec 11, 2024
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
5 changes: 5 additions & 0 deletions .changeset/giant-trains-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'backend': patch
---

fix syncing v3 changed pools
16 changes: 12 additions & 4 deletions modules/controllers/pool-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,19 @@ export function PoolController(tracer?: any) {
*/
async syncChangedPoolsV3(chain: Chain) {
const {
subgraphs: { balancerV3 },
balancer: {
v3: { vaultAddress, routerAddress },
},
} = config[chain];

// Guard against unconfigured chains
if (!vaultAddress) {
if (!vaultAddress || !balancerV3) {
throw new Error(`Chain not configured: ${chain}`);
}

const viemClient = getViemClient(chain);
const subgraphClient = getVaultSubgraphClient(balancerV3);

const lastSyncBlock = await getLastSyncedBlock(chain, PrismaLastBlockSyncedCategory.POOLS_V3);
const fromBlock = lastSyncBlock + 1;
Expand All @@ -259,9 +261,14 @@ export function PoolController(tracer?: any) {
where: { chain, protocolVersion: 3 },
});

const changedPools = await getChangedPoolsV3(vaultAddress, viemClient, BigInt(fromBlock), BigInt(toBlock));
// RPC for some reason isn't working, maybe event signatures are wrong?
// const changedPools = await getChangedPoolsV3(vaultAddress, viemClient, BigInt(fromBlock), BigInt(toBlock));
const changedPoolsIds = await subgraphClient
.getAllInitializedPools({
_change_block: { number_gte: fromBlock },
})
.then((pools) => pools.map((pool) => pool.id.toLowerCase()));

const changedPoolsIds = changedPools.map((id) => id.toLowerCase());
const poolsToSync = pools.filter((pool) => changedPoolsIds.includes(pool.id.toLowerCase())); // only sync pools that are in the database
if (poolsToSync.length === 0) {
return [];
Expand All @@ -274,7 +281,8 @@ export function PoolController(tracer?: any) {
chain,
);

await upsertLastSyncedBlock(chain, PrismaLastBlockSyncedCategory.POOLS_V3, toBlock);
// Leaving safety margin for reorgs
await upsertLastSyncedBlock(chain, PrismaLastBlockSyncedCategory.POOLS_V3, toBlock - 10n);

return poolsToSync.map(({ id }) => id);
},
Expand Down
Loading