Skip to content

Commit

Permalink
common: fix paginated tap queries
Browse files Browse the repository at this point in the history
Signed-off-by: Gustavo Inacio <[email protected]>
  • Loading branch information
gusinacio committed Oct 10, 2024
1 parent d5fab3e commit 04a4dde
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
46 changes: 30 additions & 16 deletions packages/indexer-common/src/allocations/tap-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import gql from 'graphql-tag'
// every 15 minutes
const RAV_CHECK_INTERVAL_MS = 900_000

const PAGE_SIZE = 1000

interface RavMetrics {
ravRedeemsSuccess: Counter<string>
ravRedeemsInvalid: Counter<string>
Expand Down Expand Up @@ -111,7 +113,7 @@ export class TapCollector {
// eslint-disable-next-line @typescript-eslint/no-empty-function -- Private constructor to prevent direct instantiation
private constructor() {}

public static async create({
public static create({
logger,
metrics,
transactionManager,
Expand All @@ -121,7 +123,7 @@ export class TapCollector {
networkSpecification,
tapSubgraph,
networkSubgraph,
}: TapCollectorOptions): Promise<TapCollector> {
}: TapCollectorOptions): TapCollector {
const collector = new TapCollector()
collector.logger = logger.child({ component: 'AllocationReceiptCollector' })
collector.metrics = registerReceiptMetrics(
Expand Down Expand Up @@ -219,22 +221,21 @@ export class TapCollector {
rav.getSignedRAV().rav.allocationId.toLowerCase(),
)

let hash: string | undefined = undefined
let block: { hash: string } | undefined = undefined
let lastId = ''
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const returnedAllocations: any[] = []
const pageSize = 1000

for (;;) {
const result = await this.networkSubgraph.query<AllocationsResponse>(
gql`
query allocations(
$lastId: String!
$pageSize: Int!
$hash: String
$block: Block_height
$allocationIds: [String!]!
) {
meta: _meta(block: { hash: $hash }) {
meta: _meta(block: $block) {
block {
number
hash
Expand All @@ -243,7 +244,9 @@ export class TapCollector {
}
allocations(
first: $pageSize
block: { hash: $hash }
block: $block
orderBy: id,
orderDirection: asc,
where: { id_gt: $lastId, id_in: $allocationIds }
) {
id
Expand All @@ -270,16 +273,17 @@ export class TapCollector {
}
}
`,
{ allocationIds, lastId, pageSize, hash },
{ allocationIds, lastId, pageSize: PAGE_SIZE, block },
)
console.log("called query!")
if (!result.data) {
throw `There was an error while querying Network Subgraph. Errors: ${result.error}`
}

if (result.data.allocations.length < pageSize) {
if (result.data.allocations.length < PAGE_SIZE) {
break
}
hash = result.data.meta.block.hash
block = { hash: result.data.meta.block.hash }
lastId = result.data.allocations.slice(-1)[0].id
returnedAllocations.push(...result.data.allocations)
}
Expand Down Expand Up @@ -388,22 +392,32 @@ export class TapCollector {
let meta: TapMeta | undefined = undefined
let lastId = ''
const transactions: TapTransaction[] = []
const pageSize = 1000

for (;;) {
const hash = meta?.block?.hash
let block: { hash: string } | undefined = undefined
if (meta?.block?.hash) {
block = {
hash: meta?.block?.hash,
}
}

const result: QueryResult<TapSubgraphResponse> =
await this.tapSubgraph.query<TapSubgraphResponse>(
gql`
query transactions(
$lastId: String!
$pageSize: Int!
$hash: String
$block: Block_height
$unfinalizedRavsAllocationIds: [String!]!
$senderAddresses: [String!]!
) {
transactions(
first: $pageSize
block: $block
orderBy: id,
orderDirection: asc,
where: {
id_gt: $lastId
type: "redeem"
allocationID_in: $unfinalizedRavsAllocationIds
sender_: { id_in: $senderAddresses }
Expand All @@ -426,8 +440,8 @@ export class TapCollector {
`,
{
lastId,
pageSize,
hash,
pageSize: PAGE_SIZE,
block,
unfinalizedRavsAllocationIds: ravs.map((value) =>
toAddress(value.allocationId).toLowerCase(),
),
Expand All @@ -441,7 +455,7 @@ export class TapCollector {
throw `There was an error while querying Tap Subgraph. Errors: ${result.error}`
}
meta = result.data._meta
if (result.data.transactions.length < pageSize) {
if (result.data.transactions.length < PAGE_SIZE) {
break
}
lastId = result.data.transactions.slice(-1)[0].id
Expand Down
2 changes: 1 addition & 1 deletion packages/indexer-common/src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export class Network {
// --------------------------------------------------------------------------------
let tapCollector: TapCollector | undefined = undefined
if (tapContracts && tapSubgraph) {
tapCollector = await TapCollector.create({
tapCollector = TapCollector.create({
logger,
metrics,
transactionManager: transactionManager,
Expand Down

0 comments on commit 04a4dde

Please sign in to comment.