Skip to content

Commit 19602c6

Browse files
committed
common: add test for mark ravs in transactions as redeemed
Signed-off-by: Gustavo Inacio <[email protected]>
1 parent abb3a17 commit 19602c6

File tree

2 files changed

+85
-25
lines changed

2 files changed

+85
-25
lines changed

packages/indexer-common/src/allocations/__tests__/tap.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,55 @@ describe('TAP', () => {
281281
])
282282
})
283283

284+
test('should mark ravs as redeemed via `markRavsInTransactionsAsRedeemed`', async () => {
285+
const nowSecs = Math.floor(Date.now() / 1000)
286+
const transactions = {
287+
transactions: [
288+
{
289+
id: 'test',
290+
allocationID: ALLOCATION_ID_2.toString().toLowerCase().replace('0x', ''),
291+
timestamp: nowSecs,
292+
sender: {
293+
id: SENDER_ADDRESS_3.toString().toLowerCase().replace('0x', ''),
294+
},
295+
},
296+
],
297+
_meta: {
298+
block: {
299+
timestamp: nowSecs,
300+
hash: 'test',
301+
},
302+
},
303+
}
304+
305+
const rav2 = {
306+
allocationId: ALLOCATION_ID_2,
307+
last: true,
308+
final: false,
309+
timestampNs: 1709067401177959664n,
310+
valueAggregate: 20000000000000n,
311+
signature: SIGNATURE,
312+
senderAddress: SENDER_ADDRESS_3,
313+
redeemedAt: null,
314+
}
315+
await queryFeeModels.receiptAggregateVouchers.create(rav2)
316+
let ravs = await tapCollector['pendingRAVs']()
317+
await tapCollector['markRavsInTransactionsAsRedeemed'](transactions, ravs)
318+
const redeemedRavs = await queryFeeModels.receiptAggregateVouchers.findAll({
319+
where: {
320+
last: true,
321+
final: false,
322+
redeemedAt: {
323+
[Op.ne]: null,
324+
},
325+
},
326+
})
327+
// Expect redeemed rav to be returned here
328+
expect(redeemedRavs).toEqual([
329+
expect.objectContaining({ ...rav2, redeemedAt: nowSecs }),
330+
])
331+
})
332+
284333
test('should mark ravs as final via `markRavsAsFinal`', async () => {
285334
// we have a redeemed non-final rav in our database
286335
const nowSecs = Math.floor(Date.now() / 1000)

packages/indexer-common/src/allocations/tap-collector.ts

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -338,31 +338,8 @@ export class TapCollector {
338338
// look for all transactions for that includes senderaddress[] and allocations[]
339339
const tapSubgraphResponse = await this.findTransactionsForRavs(ravsLastNotFinal)
340340

341-
// get a list of transacations for ravs marked as not redeemed in our database
342-
const redeemedRavsNotOnOurDatabase = tapSubgraphResponse.transactions.filter((tx) => {
343-
// check if exists in the list sent
344-
!!ravsLastNotFinal.find(
345-
(rav) =>
346-
// rav has the same sender address as tx
347-
toAddress(rav.senderAddress) === toAddress(tx.sender.id) &&
348-
// rav has the same allocation id as tx
349-
toAddress(rav.allocationId) === toAddress(tx.allocationID) &&
350-
// rav was not redeemed
351-
!rav.redeemedAt,
352-
)
353-
})
354-
355-
// for each transaction that is not redeemed on our database
356-
// but was redeemed on the blockchain, update it to redeemed
357-
if (redeemedRavsNotOnOurDatabase.length > 0) {
358-
for (const rav of redeemedRavsNotOnOurDatabase) {
359-
await this.markRavAsRedeemed(
360-
toAddress(rav.allocationID),
361-
toAddress(rav.sender.id),
362-
rav.timestamp,
363-
)
364-
}
365-
}
341+
// check for redeemed ravs in tx list but not marked as redeemed in our database
342+
this.markRavsInTransactionsAsRedeemed(tapSubgraphResponse, ravsLastNotFinal)
366343

367344
// Filter unfinalized RAVS fetched from DB, keeping RAVs that have not yet been redeemed on-chain
368345
const nonRedeemedRavs = ravsLastNotFinal
@@ -397,6 +374,40 @@ export class TapCollector {
397374
})
398375
}
399376

377+
public async markRavsInTransactionsAsRedeemed(
378+
tapSubgraphResponse: TapSubgraphResponse,
379+
ravsLastNotFinal: ReceiptAggregateVoucher[],
380+
) {
381+
// get a list of transactions for ravs marked as not redeemed in our database
382+
const redeemedRavsNotOnOurDatabase = tapSubgraphResponse.transactions
383+
// get only the transactions that exists, this prevents errors marking as redeemed
384+
// transactions for different senders with the same allocation id
385+
.filter((tx) => {
386+
// check if exists in the ravsLastNotFinal list
387+
!!ravsLastNotFinal.find(
388+
(rav) =>
389+
// rav has the same sender address as tx
390+
toAddress(rav.senderAddress) === toAddress(tx.sender.id) &&
391+
// rav has the same allocation id as tx
392+
toAddress(rav.allocationId) === toAddress(tx.allocationID) &&
393+
// rav was marked as not redeemed in the db
394+
!rav.redeemedAt,
395+
)
396+
})
397+
398+
// for each transaction that is not redeemed on our database
399+
// but was redeemed on the blockchain, update it to redeemed
400+
if (redeemedRavsNotOnOurDatabase.length > 0) {
401+
for (const rav of redeemedRavsNotOnOurDatabase) {
402+
await this.markRavAsRedeemed(
403+
toAddress(rav.allocationID),
404+
toAddress(rav.sender.id),
405+
rav.timestamp,
406+
)
407+
}
408+
}
409+
}
410+
400411
public async findTransactionsForRavs(
401412
ravs: ReceiptAggregateVoucher[],
402413
): Promise<TapSubgraphResponse> {

0 commit comments

Comments
 (0)