Skip to content

Commit

Permalink
fix: fix hub pool repository inserts (#76)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexandru Matei <alexandrumatei3693@gmail.com>
  • Loading branch information
amateima and alexandrumatei36 authored Oct 17, 2024
1 parent fab3abe commit 23d901b
Showing 1 changed file with 55 additions and 31 deletions.
86 changes: 55 additions & 31 deletions packages/indexer/src/database/HubPoolRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as across from "@across-protocol/sdk";
import { DataSource, entities, utils } from "@repo/indexer-database";

export class HubPoolRepository extends utils.BaseRepository {
private chunkSize = 1000;

constructor(postgres: DataSource, logger: winston.Logger) {
super(postgres, logger, true);
}
Expand All @@ -23,12 +25,18 @@ export class HubPoolRepository extends utils.BaseRepository {
finalised: event.blockNumber <= lastFinalisedBlock,
};
});
await this.postgres
.createQueryBuilder(entities.ProposedRootBundle, "b")
.insert()
.values(formattedEvents)
.orUpdate(["finalised"], ["transactionHash"])
.execute();

const chunkedEvents = across.utils.chunk(formattedEvents, this.chunkSize);
await Promise.all(
chunkedEvents.map((eventsChunk) =>
this.postgres
.createQueryBuilder(entities.ProposedRootBundle, "b")
.insert()
.values(eventsChunk)
.orUpdate(["finalised"], ["transactionHash"])
.execute(),
),
);
}

public async formatAndSaveRootBundleDisputedEvents(
Expand All @@ -42,12 +50,17 @@ export class HubPoolRepository extends utils.BaseRepository {
finalised: event.blockNumber <= lastFinalisedBlock,
};
});
await this.postgres
.createQueryBuilder(entities.RootBundleDisputed, "b")
.insert()
.values(formattedEvents)
.orUpdate(["finalised"], ["transactionHash"])
.execute();
const chunkedEvents = across.utils.chunk(formattedEvents, this.chunkSize);
await Promise.all(
chunkedEvents.map((eventsChunk) =>
this.postgres
.createQueryBuilder(entities.RootBundleDisputed, "b")
.insert()
.values(eventsChunk)
.orUpdate(["finalised"], ["transactionHash"])
.execute(),
),
);
}

public async formatAndSaveRootBundleCanceledEvents(
Expand All @@ -62,12 +75,18 @@ export class HubPoolRepository extends utils.BaseRepository {
finalised: event.blockNumber <= lastFinalisedBlock,
};
});
await this.postgres
.createQueryBuilder(entities.RootBundleCanceled, "b")
.insert()
.values(formattedEvents)
.orUpdate(["finalised"], ["transactionHash"])
.execute();

const chunkedEvents = across.utils.chunk(formattedEvents, this.chunkSize);
await Promise.all(
chunkedEvents.map((eventsChunk) =>
this.postgres
.createQueryBuilder(entities.RootBundleCanceled, "b")
.insert()
.values(eventsChunk)
.orUpdate(["finalised"], ["transactionHash"])
.execute(),
),
);
}

public async formatAndSaveRootBundleExecutedEvents(
Expand All @@ -85,14 +104,13 @@ export class HubPoolRepository extends utils.BaseRepository {
finalised: event.blockNumber <= lastFinalisedBlock,
};
});
// Split the events into chunks of 1000 to avoid exceeding the max query length
const chunks = across.utils.chunk(formattedEvents, 1000);
const chunkedEvents = across.utils.chunk(formattedEvents, this.chunkSize);
await Promise.all(
chunks.map((chunk) =>
chunkedEvents.map((eventsChunk) =>
this.postgres
.createQueryBuilder(entities.RootBundleExecuted, "b")
.insert()
.values(chunk)
.values(eventsChunk)
.orUpdate(
["finalised"],
["chainId", "leafId", "groupIndex", "transactionHash"],
Expand All @@ -117,15 +135,21 @@ export class HubPoolRepository extends utils.BaseRepository {
finalised: event.blockNumber <= lastFinalisedBlock,
};
});
await this.postgres
.createQueryBuilder(entities.SetPoolRebalanceRoute, "b")
.insert()
.values(formattedEvents)
.orUpdate(
["finalised"],
["transactionHash", "transactionIndex", "logIndex"],
)
.execute();

const chunkedEvents = across.utils.chunk(formattedEvents, this.chunkSize);
await Promise.all(
chunkedEvents.map((eventsChunk) =>
this.postgres
.createQueryBuilder(entities.SetPoolRebalanceRoute, "b")
.insert()
.values(eventsChunk)
.orUpdate(
["finalised"],
["transactionHash", "transactionIndex", "logIndex"],
)
.execute(),
),
);
}

/**
Expand Down

0 comments on commit 23d901b

Please sign in to comment.