Skip to content

Commit

Permalink
common: add test for mark ravs in transactions as redeemed
Browse files Browse the repository at this point in the history
Signed-off-by: Gustavo Inacio <[email protected]>
  • Loading branch information
gusinacio committed Dec 18, 2024
1 parent c9f2fd0 commit 9d34c19
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 25 deletions.
49 changes: 49 additions & 0 deletions packages/indexer-common/src/allocations/__tests__/tap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
61 changes: 36 additions & 25 deletions packages/indexer-common/src/allocations/tap-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<TapSubgraphResponse> {
Expand Down

0 comments on commit 9d34c19

Please sign in to comment.