From f8819229eb95f1b7ae34d6b8528cfbabac56d780 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Thu, 29 Aug 2024 17:58:27 +0200 Subject: [PATCH] fix: in case the graphql returns empty Signed-off-by: Gustavo Inacio --- .../src/allocations/query-fees.ts | 89 ++++++++++--------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/packages/indexer-common/src/allocations/query-fees.ts b/packages/indexer-common/src/allocations/query-fees.ts index 4f0c1b235..07973797d 100644 --- a/packages/indexer-common/src/allocations/query-fees.ts +++ b/packages/indexer-common/src/allocations/query-fees.ts @@ -597,55 +597,56 @@ export class AllocationReceiptCollector implements ReceiptCollector { ravLastNotFinal: ReceiptAggregateVoucher[], ): Promise { const tapSubgraphResponse = await this.findTransactionsForRavs(ravLastNotFinal) - - const redeemedRavsNotOnOurDatabase = tapSubgraphResponse.transactions.filter( - (tx) => - !ravLastNotFinal.find( - (rav) => - toAddress(rav.senderAddress) === toAddress(tx.sender.id) && - toAddress(rav.allocationId) === toAddress(tx.allocationID), - ), - ) - - // for each transaction that is not redeemed on our database - // but was redeemed on the blockchain, update it to redeemed - if (redeemedRavsNotOnOurDatabase.length > 0) { - for (const rav of redeemedRavsNotOnOurDatabase) { - await this.markRavAsRedeemed( - toAddress(rav.allocationID), - toAddress(rav.sender.id), - rav.timestamp, - ) - } - } - - // Filter unfinalized RAVS fetched from DB, keeping RAVs that have not yet been redeemed on-chain - const nonRedeemedRavs = ravLastNotFinal - .filter((rav) => !!rav.redeemedAt) - .filter( - (rav) => - !tapSubgraphResponse.transactions.find( - (tx) => + if (tapSubgraphResponse) { + const redeemedRavsNotOnOurDatabase = tapSubgraphResponse.transactions.filter( + (tx) => + !ravLastNotFinal.find( + (rav) => toAddress(rav.senderAddress) === toAddress(tx.sender.id) && toAddress(rav.allocationId) === toAddress(tx.allocationID), ), ) - // we use the subgraph timestamp to make decisions - // block timestamp minus 1 minute (because of blockchain timestamp uncertainty) - const ONE_MINUTE = 60 - const blockTimestampSecs = tapSubgraphResponse._meta.block.timestamp - ONE_MINUTE + // for each transaction that is not redeemed on our database + // but was redeemed on the blockchain, update it to redeemed + if (redeemedRavsNotOnOurDatabase.length > 0) { + for (const rav of redeemedRavsNotOnOurDatabase) { + await this.markRavAsRedeemed( + toAddress(rav.allocationID), + toAddress(rav.sender.id), + rav.timestamp, + ) + } + } - // Mark RAVs as unredeemed in DB if the TAP subgraph couldn't find the redeem Tx. - // To handle a chain reorg that "unredeemed" the RAVs. - // WE use sql directly due to a bug in sequelize update: - // https://github.com/sequelize/sequelize/issues/7664 (bug been open for 7 years no fix yet or ever) - if (nonRedeemedRavs.length > 0) { - await this.revertRavsRedeemed(nonRedeemedRavs, blockTimestampSecs) - } + // Filter unfinalized RAVS fetched from DB, keeping RAVs that have not yet been redeemed on-chain + const nonRedeemedRavs = ravLastNotFinal + .filter((rav) => !!rav.redeemedAt) + .filter( + (rav) => + !tapSubgraphResponse.transactions.find( + (tx) => + toAddress(rav.senderAddress) === toAddress(tx.sender.id) && + toAddress(rav.allocationId) === toAddress(tx.allocationID), + ), + ) - // For all RAVs that passed finality time, we mark it as final - await this.markRavsAsFinal(blockTimestampSecs) + // we use the subgraph timestamp to make decisions + // block timestamp minus 1 minute (because of blockchain timestamp uncertainty) + const ONE_MINUTE = 60 + const blockTimestampSecs = tapSubgraphResponse._meta.block.timestamp - ONE_MINUTE + + // Mark RAVs as unredeemed in DB if the TAP subgraph couldn't find the redeem Tx. + // To handle a chain reorg that "unredeemed" the RAVs. + // WE use sql directly due to a bug in sequelize update: + // https://github.com/sequelize/sequelize/issues/7664 (bug been open for 7 years no fix yet or ever) + if (nonRedeemedRavs.length > 0) { + await this.revertRavsRedeemed(nonRedeemedRavs, blockTimestampSecs) + } + + // For all RAVs that passed finality time, we mark it as final + await this.markRavsAsFinal(blockTimestampSecs) + } return await this.models.receiptAggregateVouchers.findAll({ where: { redeemedAt: null, final: false, last: true }, @@ -654,7 +655,7 @@ export class AllocationReceiptCollector implements ReceiptCollector { public async findTransactionsForRavs( ravs: ReceiptAggregateVoucher[], - ): Promise { + ): Promise { const response = await this.tapSubgraph!.query( gql` query transactions( @@ -690,7 +691,7 @@ export class AllocationReceiptCollector implements ReceiptCollector { ), }, ) - return response.data! + return response.data } // for every allocation_id of this list that contains the redeemedAt less than the current