From 9d34c191d86fdb943ffda159eb3e2be96f0ed718 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Wed, 18 Dec 2024 18:52:38 +0100 Subject: [PATCH] common: add test for mark ravs in transactions as redeemed Signed-off-by: Gustavo Inacio --- .../src/allocations/__tests__/tap.test.ts | 49 +++++++++++++++ .../src/allocations/tap-collector.ts | 61 +++++++++++-------- 2 files changed, 85 insertions(+), 25 deletions(-) diff --git a/packages/indexer-common/src/allocations/__tests__/tap.test.ts b/packages/indexer-common/src/allocations/__tests__/tap.test.ts index f35569f0e..64707cdbc 100644 --- a/packages/indexer-common/src/allocations/__tests__/tap.test.ts +++ b/packages/indexer-common/src/allocations/__tests__/tap.test.ts @@ -281,6 +281,55 @@ describe('TAP', () => { ]) }) + test('should mark ravs as redeemed via `markRavsInTransactionsAsRedeemed`', async () => { + const nowSecs = Math.floor(Date.now() / 1000) + const transactions = { + transactions: [ + { + id: 'test', + allocationID: ALLOCATION_ID_2.toString().toLowerCase().replace('0x', ''), + timestamp: nowSecs, + sender: { + id: SENDER_ADDRESS_3.toString().toLowerCase().replace('0x', ''), + }, + }, + ], + _meta: { + block: { + timestamp: nowSecs, + hash: 'test', + }, + }, + } + + const rav2 = { + allocationId: ALLOCATION_ID_2, + last: true, + final: false, + timestampNs: 1709067401177959664n, + valueAggregate: 20000000000000n, + signature: SIGNATURE, + senderAddress: SENDER_ADDRESS_3, + redeemedAt: null, + } + await queryFeeModels.receiptAggregateVouchers.create(rav2) + let ravs = await tapCollector['pendingRAVs']() + await tapCollector['markRavsInTransactionsAsRedeemed'](transactions, ravs) + const redeemedRavs = await queryFeeModels.receiptAggregateVouchers.findAll({ + where: { + last: true, + final: false, + redeemedAt: { + [Op.ne]: null, + }, + }, + }) + // Expect redeemed rav to be returned here + expect(redeemedRavs).toEqual([ + expect.objectContaining({ ...rav2, redeemedAt: nowSecs }), + ]) + }) + test('should mark ravs as final via `markRavsAsFinal`', async () => { // we have a redeemed non-final rav in our database const nowSecs = Math.floor(Date.now() / 1000) diff --git a/packages/indexer-common/src/allocations/tap-collector.ts b/packages/indexer-common/src/allocations/tap-collector.ts index d3b70ba67..486b11a81 100644 --- a/packages/indexer-common/src/allocations/tap-collector.ts +++ b/packages/indexer-common/src/allocations/tap-collector.ts @@ -338,31 +338,8 @@ export class TapCollector { // look for all transactions for that includes senderaddress[] and allocations[] const tapSubgraphResponse = await this.findTransactionsForRavs(ravsLastNotFinal) - // get a list of transacations for ravs marked as not redeemed in our database - const redeemedRavsNotOnOurDatabase = tapSubgraphResponse.transactions.filter((tx) => { - // check if exists in the list sent - !!ravsLastNotFinal.find( - (rav) => - // rav has the same sender address as tx - toAddress(rav.senderAddress) === toAddress(tx.sender.id) && - // rav has the same allocation id as tx - toAddress(rav.allocationId) === toAddress(tx.allocationID) && - // rav was not redeemed - !rav.redeemedAt, - ) - }) - - // 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, - ) - } - } + // check for redeemed ravs in tx list but not marked as redeemed in our database + this.markRavsInTransactionsAsRedeemed(tapSubgraphResponse, ravsLastNotFinal) // Filter unfinalized RAVS fetched from DB, keeping RAVs that have not yet been redeemed on-chain const nonRedeemedRavs = ravsLastNotFinal @@ -397,6 +374,40 @@ export class TapCollector { }) } + public async markRavsInTransactionsAsRedeemed( + tapSubgraphResponse: TapSubgraphResponse, + ravsLastNotFinal: ReceiptAggregateVoucher[], + ) { + // get a list of transactions for ravs marked as not redeemed in our database + const redeemedRavsNotOnOurDatabase = tapSubgraphResponse.transactions + // get only the transactions that exists, this prevents errors marking as redeemed + // transactions for different senders with the same allocation id + .filter((tx) => { + // check if exists in the ravsLastNotFinal list + !!ravsLastNotFinal.find( + (rav) => + // rav has the same sender address as tx + toAddress(rav.senderAddress) === toAddress(tx.sender.id) && + // rav has the same allocation id as tx + toAddress(rav.allocationId) === toAddress(tx.allocationID) && + // rav was marked as not redeemed in the db + !rav.redeemedAt, + ) + }) + + // 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, + ) + } + } + } + public async findTransactionsForRavs( ravs: ReceiptAggregateVoucher[], ): Promise {