From 88121f93900123d7173895efb633e2db28f93fa9 Mon Sep 17 00:00:00 2001 From: Ashwin Phatak Date: Fri, 6 Aug 2021 19:10:25 +0530 Subject: [PATCH] Test for entities after SwapEvent. (#191) Co-authored-by: prathamesh0 --- packages/uni-info-watcher/src/smoke.test.ts | 129 +++++++++++++++++++- packages/uni-info-watcher/test/queries.ts | 49 ++++++-- packages/uni-watcher/src/smoke.test.ts | 1 + 3 files changed, 164 insertions(+), 15 deletions(-) diff --git a/packages/uni-info-watcher/src/smoke.test.ts b/packages/uni-info-watcher/src/smoke.test.ts index 8478bfd2..13049775 100644 --- a/packages/uni-info-watcher/src/smoke.test.ts +++ b/packages/uni-info-watcher/src/smoke.test.ts @@ -34,7 +34,8 @@ import { queryPoolById, queryMints, queryTicks, - queryBurns + queryBurns, + querySwaps } from '../test/queries'; import { checkUniswapDayData, @@ -48,6 +49,7 @@ const NETWORK_RPC_URL = 'http://localhost:8545'; describe('uni-info-watcher', () => { let factory: Contract; let pool: Contract; + let poolCallee: Contract; let token0: Contract; let token1: Contract; let token0Address: string; @@ -198,7 +200,6 @@ describe('uni-info-watcher', () => { describe('MintEvent', () => { const amount = 10; const approveAmount = BigInt(1000000000000000000000000); - let poolCallee: Contract; // Initial entity values let oldFactory: any; @@ -287,7 +288,7 @@ describe('uni-info-watcher', () => { }); it('should create a Mint entity', async () => { - // Check id, owner, sender. + // Check id, origin, owner, sender. // Get the latest Mint. let data: any; const variables = { @@ -437,7 +438,7 @@ describe('uni-info-watcher', () => { }); it('should create a Burn entity', async () => { - // Check id, owner, sender. + // Check id, origin, owner. // Get the latest Burn. let data: any; const variables = { @@ -502,4 +503,124 @@ describe('uni-info-watcher', () => { checkTokenHourData(endpoint, token1.address); }); }); + + describe('SwapEvent', () => { + const sqrtPrice = '4295128938'; + + // Initial entity values + let eventValue: any; + let oldFactory: any; + let oldToken0: any; + let oldToken1: any; + let oldPool: any; + + before(async () => { + // Get initial entity values. + let data: any; + + data = await request(endpoint, queryFactory); + oldFactory = data.factories[0]; + + data = await request(endpoint, queryToken, { id: token0.address }); + oldToken0 = data.token; + + data = await request(endpoint, queryToken, { id: token1.address }); + oldToken1 = data.token; + + data = await request(endpoint, queryPoolById, { id: pool.address }); + oldPool = data.pool; + }); + + it('should trigger SwapEvent', async () => { + // Pool swap. + poolCallee.swapToLowerSqrtPrice(pool.address, BigInt(sqrtPrice), recipient); + + // Wait for SwapEvent. + const eventType = 'SwapEvent'; + eventValue = await watchEvent(uniClient, eventType); + + // Sleeping for 5 sec for the entities to be processed. + await wait(5000); + }); + + it('should update Token entities', async () => { + // Check txCount. + let data: any; + + data = await request(endpoint, queryToken, { id: token0.address }); + const newToken0 = data.token; + + data = await request(endpoint, queryToken, { id: token1.address }); + const newToken1 = data.token; + + expect(newToken0.txCount).to.be.equal((BigInt(oldToken0.txCount) + BigInt(1)).toString()); + expect(newToken1.txCount).to.be.equal((BigInt(oldToken1.txCount) + BigInt(1)).toString()); + }); + + it('should update Factory entity', async () => { + // Check txCount. + const data = await request(endpoint, queryFactory); + const newFactory = data.factories[0]; + expect(newFactory.txCount).to.be.equal((BigInt(oldFactory.txCount) + BigInt(1)).toString()); + }); + + it('should update Pool entity', async () => { + // Check txCount, liquidity, tick, sqrtPrice. + const expectedLiquidity = eventValue.event.liquidity; + const expectedTick = eventValue.event.tick; + const expectedSqrtPrice = eventValue.event.sqrtPriceX96; + + const data = await request(endpoint, queryPoolById, { id: pool.address }); + const newPool = data.pool; + + expect(newPool.txCount).to.be.equal((BigInt(oldPool.txCount) + BigInt(1)).toString()); + expect(newPool.liquidity).to.be.equal(expectedLiquidity); + expect(newPool.tick).to.be.equal(expectedTick); + expect(newPool.sqrtPrice).to.be.equal(expectedSqrtPrice); + }); + + it('should create a Swap entity', async () => { + // Check id, origin. + // Get the latest Swap. + let data: any; + const variables = { + first: 1, + orderBy: 'timestamp', + orderDirection: 'desc', + pool: pool.address + }; + + data = await request(endpoint, querySwaps, variables); + expect(data.swaps).to.not.be.empty; + + const id: string = data.swaps[0].id; + const txCountID = id.split('#')[1]; + const origin = data.swaps[0].origin; + + data = await request(endpoint, queryPoolById, { id: pool.address }); + const poolTxCount = data.pool.txCount; + const expectedOrigin = recipient; + + expect(txCountID).to.be.equal(poolTxCount); + expect(origin).to.be.equal(expectedOrigin); + }); + + it('should update UniswapDayData entity', async () => { + checkUniswapDayData(endpoint); + }); + + it('should update PoolDayData entity', async () => { + checkPoolDayData(endpoint, pool.address); + }); + + it('should update TokenDayData entities', async () => { + checkTokenDayData(endpoint, token0.address); + checkTokenDayData(endpoint, token1.address); + }); + + it('should update TokenHourData entities', async () => { + checkTokenHourData(endpoint, token0.address); + checkTokenHourData(endpoint, token1.address); + }); + }); }); diff --git a/packages/uni-info-watcher/test/queries.ts b/packages/uni-info-watcher/test/queries.ts index a28c4323..7ad27a6a 100644 --- a/packages/uni-info-watcher/test/queries.ts +++ b/packages/uni-info-watcher/test/queries.ts @@ -56,6 +56,19 @@ query queryPoolById($id: ID!) { } }`; +// Getting Tick(s) filtered by pool. +export const queryTicks = gql` +query queryTicksByPool($pool: String) { + ticks(where: { poolAddress: $pool }) { + id + liquidityGross + liquidityNet + price0 + price1 + tickIdx + } +}`; + // Getting Pool(s) filtered by tokens. export const queryPoolsByTokens = gql` query queryPoolsByTokens($tokens: [String!]) { @@ -165,15 +178,29 @@ query queryBurns( } }`; -// Getting Tick(s) filtered by pool. -export const queryTicks = gql` -query queryTicksByPool($pool: String) { - ticks(where: { poolAddress: $pool }) { - id - liquidityGross - liquidityNet - price0 - price1 - tickIdx - } +// Getting burns(s) filtered by pool, tokens and ordered by timestamp. +export const querySwaps = gql` +query querySwaps( + $first: Int, + $orderBy: Swap_orderBy, + $orderDirection: OrderDirection, + $pool: String, + $token0: String, + $token1: String) { + swaps( + first: $first, + orderBy: $orderBy, + orderDirection: $orderDirection, + where: { + pool: $pool, + token0: $token0, + token1: $token1 + }) { + amount0 + amount1 + amountUSD + id + origin + timestamp + } }`; diff --git a/packages/uni-watcher/src/smoke.test.ts b/packages/uni-watcher/src/smoke.test.ts index 079d0ba9..b1dab402 100644 --- a/packages/uni-watcher/src/smoke.test.ts +++ b/packages/uni-watcher/src/smoke.test.ts @@ -258,6 +258,7 @@ describe('uni-watcher', () => { } }); + // Pool swap. const transaction: ContractTransaction = await poolCallee.swapToLowerSqrtPrice(pool.address, BigInt(sqrtPrice), recipient); await transaction.wait(); })().catch((error) => {