diff --git a/packages/indexer-common/src/allocations/__tests__/tap.test.ts b/packages/indexer-common/src/allocations/__tests__/tap.test.ts index 4dd3ea4c9..97fc17507 100644 --- a/packages/indexer-common/src/allocations/__tests__/tap.test.ts +++ b/packages/indexer-common/src/allocations/__tests__/tap.test.ts @@ -63,8 +63,15 @@ const setup = async () => { receiptCollector = network.receiptCollector } +const ALLOCATION_ID_1 = toAddress('edde47df40c29949a75a6693c77834c00b8ad626') +const ALLOCATION_ID_2 = toAddress('dead47df40c29949a75a6693c77834c00b8ad624') + +const SENDER_ADDRESS_1 = toAddress('ffcf8fdee72ac11b5c542428b35eef5769c409f0') +const SENDER_ADDRESS_2 = toAddress('dead47df40c29949a75a6693c77834c00b8ad624') + +// last rav not redeemed const rav = { - allocationId: toAddress('edde47df40c29949a75a6693c77834c00b8ad626'), + allocationId: ALLOCATION_ID_1, last: true, final: false, timestampNs: 1709067401177959664n, @@ -73,15 +80,52 @@ const rav = { 'ede3f7ca5ace3629009f190bb51271f30c1aeaf565f82c25c447c7c9501f3ff31b628efcaf69138bf12960dd663924a692ee91f401785901848d8d7a639003ad1b', 'hex', ), - senderAddress: toAddress('ffcf8fdee72ac11b5c542428b35eef5769c409f0'), + senderAddress: SENDER_ADDRESS_1, redeemedAt: null, createdAt: new Date(), updatedAt: new Date(), } +const SIGNATURE = Buffer.from( + 'ede3f7ca5ace3629009f190bb51271f30c1aeaf565f82c25c447c7c9501f3ff31b628efcaf69138bf12960dd663924a692ee91f401785901848d8d7a639003ad1b', + 'hex', +) + +const rav_list = [ + rav, + // redeemed rav but non-final + { + allocationId: ALLOCATION_ID_2, + last: true, + final: false, + timestampNs: 1709067401177959664n, + valueAggregate: 20000000000000n, + signature: SIGNATURE, + senderAddress: SENDER_ADDRESS_1, + redeemedAt: new Date(), + createdAt: new Date(), + updatedAt: new Date(), + }, +] + const setupEach = async () => { sequelize = await sequelize.sync({ force: true }) - await queryFeeModels.receiptAggregateVouchers.create(rav) + await queryFeeModels.receiptAggregateVouchers.bulkCreate(rav_list) + + jest + .spyOn(receiptCollector.tapSubgraph!, 'query') + .mockImplementation(async (): Promise> => { + return { + data: { + transactions: [], + _meta: { + block: { + timestamp: Date.now(), + }, + }, + }, + } + }) } const teardownEach = async () => { // Clear out query fee model tables @@ -125,22 +169,47 @@ describe('TAP', () => { timeout, ) + test('should revert the rav request', async () => { + // we have a redeemed non-final rav in our database + // it's not showing on the subgraph on a specific point in time + // the timestamp of the subgraph is greater than the receipt id + // should revert the rav + // const finalRavs = await queryFeeModels.receiptAggregateVouchers.findAll({ + // where: { last: true, final: true }, + // }) + // + // const allocationIds = [ALLOCATION_ID_1.toString()] + // const blockTimestamp = 1000 + // await receiptCollector['revertRavsRedeemed'](allocationIds, blockTimestamp) + }) + + test('should not revert the rav request, allocation_id not in the list ', async () => { + // we have a redeemed non-final rav in our database + // it's showing on the subgraph on a specific point in time + // the timestamp of the subgraph is greater than the receipt id + // should not revert the rav + }) + + test('should not revert the rav request, timestamp not greater than the subgraph timestamp', async () => { + // we have a redeemed non-final rav in our database + // it's not showing on the subgraph on a specific point in time + // the timestamp of the subgraph is lower than the receipt id + // should not revert the rav + }) + test( 'test ignore final rav', async () => { const date = new Date() const redeemDate = date.setHours(date.getHours() - 2) const rav2 = { - allocationId: toAddress('dead47df40c29949a75a6693c77834c00b8ad624'), + allocationId: ALLOCATION_ID_2, last: true, final: true, timestampNs: 1709067401177959664n, valueAggregate: 20000000000000n, - signature: Buffer.from( - 'ede3f7ca5ace3629009f190bb51271f30c1aeaf565f82c25c447c7c9501f3ff31b628efcaf69138bf12960dd663924a692ee91f401785901848d8d7a639003ad1b', - 'hex', - ), - senderAddress: toAddress('deadbeefcafedeadceefcafedeadbeefcafedead'), + signature: SIGNATURE, + senderAddress: SENDER_ADDRESS_2, redeemedAt: new Date(redeemDate), createdAt: new Date(), updatedAt: new Date(), @@ -171,16 +240,13 @@ describe('TAP', () => { const date = new Date() const redeemDate = date.setHours(date.getHours() - 2) const rav2 = { - allocationId: toAddress('dead47df40c29949a75a6693c77834c00b8ad624'), + allocationId: ALLOCATION_ID_2, last: true, final: false, timestampNs: 1709067401177959664n, valueAggregate: 20000000000000n, - signature: Buffer.from( - 'ede3f7ca5ace3629009f190bb51271f30c1aeaf565f82c25c447c7c9501f3ff31b628efcaf69138bf12960dd663924a692ee91f401785901848d8d7a639003ad1b', - 'hex', - ), - senderAddress: toAddress('deadbeefcafedeadceefcafedeadbeefcafedead'), + signature: SIGNATURE, + senderAddress: SENDER_ADDRESS_2, redeemedAt: new Date(redeemDate), createdAt: new Date(), updatedAt: new Date(), @@ -188,6 +254,7 @@ describe('TAP', () => { await queryFeeModels.receiptAggregateVouchers.create(rav2) const ravs = await receiptCollector['pendingRAVs']() // The point is it will only return the rav that is not final + expect(ravs).toEqual([ expect.objectContaining({ allocationId: rav.allocationId, @@ -253,8 +320,18 @@ describe('TAP', () => { return { data: { transactions: [ - { allocationID: '0xdead47df40c29949a75a6693c77834c00b8ad624' }, + { + allocationID: '0xdead47df40c29949a75a6693c77834c00b8ad624', + sender: { + id: '0xdead47df40c29949a75a6693c77834c00b8ad624', + }, + }, ], + _meta: { + block: { + timestamp: Date.now(), + }, + }, }, } }) @@ -262,16 +339,13 @@ describe('TAP', () => { const date = new Date() const redeemDate = date.setHours(date.getHours() - 2) const rav2 = { - allocationId: toAddress('dead47df40c29949a75a6693c77834c00b8ad624'), + allocationId: ALLOCATION_ID_2, last: true, final: false, timestampNs: 1709067401177959664n, valueAggregate: 20000000000000n, - signature: Buffer.from( - 'ede3f7ca5ace3629009f190bb51271f30c1aeaf565f82c25c447c7c9501f3ff31b628efcaf69138bf12960dd663924a692ee91f401785901848d8d7a639003ad1b', - 'hex', - ), - senderAddress: toAddress('deadbeefcafedeadceefcafedeadbeefcafedead'), + signature: SIGNATURE, + senderAddress: SENDER_ADDRESS_2, redeemedAt: new Date(redeemDate), createdAt: new Date(), updatedAt: new Date(),