Skip to content

Commit

Permalink
fix: in case the graphql returns empty
Browse files Browse the repository at this point in the history
Signed-off-by: Gustavo Inacio <[email protected]>
  • Loading branch information
gusinacio committed Aug 29, 2024
1 parent d80a7d6 commit f881922
Showing 1 changed file with 45 additions and 44 deletions.
89 changes: 45 additions & 44 deletions packages/indexer-common/src/allocations/query-fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,55 +597,56 @@ export class AllocationReceiptCollector implements ReceiptCollector {
ravLastNotFinal: ReceiptAggregateVoucher[],
): Promise<ReceiptAggregateVoucher[]> {
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 },
Expand All @@ -654,7 +655,7 @@ export class AllocationReceiptCollector implements ReceiptCollector {

public async findTransactionsForRavs(
ravs: ReceiptAggregateVoucher[],
): Promise<TapSubgraphResponse> {
): Promise<TapSubgraphResponse | undefined> {
const response = await this.tapSubgraph!.query<TapSubgraphResponse>(
gql`
query transactions(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f881922

Please sign in to comment.