Smoke test for entities after BurnEvent (#189)

* Test for entities after BurnEvent.

* Fix event value types in uni-info-watcher.

Co-authored-by: prathamesh0 <prathamesh.musale0@gmail.com>
This commit is contained in:
Ashwin Phatak 2021-08-06 17:21:30 +05:30 committed by GitHub
parent 95f830178d
commit 63620f0a0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 239 additions and 56 deletions

View File

@ -12,78 +12,78 @@ export interface PoolCreatedEvent {
__typename: 'PoolCreatedEvent';
token0: string;
token1: string;
fee: bigint;
tickSpacing: bigint;
fee: string;
tickSpacing: string;
pool: string;
}
export interface InitializeEvent {
__typename: 'InitializeEvent';
sqrtPriceX96: bigint;
tick: bigint;
sqrtPriceX96: string;
tick: string;
}
export interface MintEvent {
__typename: 'MintEvent';
sender: string;
owner: string;
tickLower: bigint;
tickUpper: bigint;
amount: bigint;
amount0: bigint;
amount1: bigint;
tickLower: string;
tickUpper: string;
amount: string;
amount0: string;
amount1: string;
}
export interface BurnEvent {
__typename: 'BurnEvent';
owner: string;
tickLower: bigint;
tickUpper: bigint;
amount: bigint;
amount0: bigint;
amount1: bigint;
tickLower: string;
tickUpper: string;
amount: string;
amount0: string;
amount1: string;
}
export interface SwapEvent {
__typename: 'SwapEvent';
sender: string;
recipient: string;
amount0: bigint;
amount1: bigint;
sqrtPriceX96: bigint;
liquidity: bigint;
tick: bigint;
amount0: string;
amount1: string;
sqrtPriceX96: string;
liquidity: string;
tick: string;
}
export interface IncreaseLiquidityEvent {
__typename: 'IncreaseLiquidityEvent';
tokenId: bigint;
liquidity: bigint;
amount0: bigint;
amount1: bigint;
tokenId: string;
liquidity: string;
amount0: string;
amount1: string;
}
export interface DecreaseLiquidityEvent {
__typename: 'DecreaseLiquidityEvent';
tokenId: bigint;
liquidity: bigint;
amount0: bigint;
amount1: bigint;
tokenId: string;
liquidity: string;
amount0: string;
amount1: string;
}
export interface CollectEvent {
__typename: 'CollectEvent';
tokenId: bigint;
tokenId: string;
recipient: string;
amount0: bigint;
amount1: bigint;
amount0: string;
amount1: string;
}
export interface TransferEvent {
__typename: 'TransferEvent';
from: string;
to: string;
tokenId: bigint;
tokenId: string;
}
export interface Block {

View File

@ -472,8 +472,8 @@ export class Indexer {
const token0 = pool.token0;
const token1 = pool.token1;
const amount0 = convertTokenToDecimal(mintEvent.amount0, BigInt(token0.decimals));
const amount1 = convertTokenToDecimal(mintEvent.amount1, BigInt(token1.decimals));
const amount0 = convertTokenToDecimal(BigInt(mintEvent.amount0), BigInt(token0.decimals));
const amount1 = convertTokenToDecimal(BigInt(mintEvent.amount1), BigInt(token1.decimals));
const amountUSD = amount0
.times(token0.derivedETH.times(bundle.ethPriceUSD))
@ -533,12 +533,12 @@ export class Indexer {
mint.owner = mintEvent.owner;
mint.sender = mintEvent.sender;
mint.origin = tx.from;
mint.amount = mintEvent.amount;
mint.amount = BigInt(mintEvent.amount);
mint.amount0 = amount0;
mint.amount1 = amount1;
mint.amountUSD = amountUSD;
mint.tickLower = mintEvent.tickLower;
mint.tickUpper = mintEvent.tickUpper;
mint.tickLower = BigInt(mintEvent.tickLower);
mint.tickUpper = BigInt(mintEvent.tickUpper);
// Tick entities.
const lowerTickIdx = mintEvent.tickLower;
@ -617,8 +617,8 @@ export class Indexer {
const token0 = pool.token0;
const token1 = pool.token1;
const amount0 = convertTokenToDecimal(burnEvent.amount0, BigInt(token0.decimals));
const amount1 = convertTokenToDecimal(burnEvent.amount1, BigInt(token1.decimals));
const amount0 = convertTokenToDecimal(BigInt(burnEvent.amount0), BigInt(token0.decimals));
const amount1 = convertTokenToDecimal(BigInt(burnEvent.amount1), BigInt(token1.decimals));
const amountUSD = amount0
.times(token0.derivedETH.times(bundle.ethPriceUSD))
@ -645,12 +645,13 @@ export class Indexer {
// Pools liquidity tracks the currently active liquidity given pools current tick.
// We only want to update it on burn if the position being burnt includes the current tick.
if (
pool.tick !== null &&
burnEvent.tickLower <= pool.tick &&
burnEvent.tickUpper > pool.tick
) {
pool.liquidity = pool.liquidity - burnEvent.amount;
if (pool.tick !== null) {
if (
BigInt(burnEvent.tickLower) <= BigInt(pool.tick) &&
BigInt(burnEvent.tickUpper) > BigInt(pool.tick)
) {
pool.liquidity = BigInt(pool.liquidity) - BigInt(burnEvent.amount);
}
}
pool.totalValueLockedToken0 = pool.totalValueLockedToken0.minus(amount0);
@ -678,12 +679,12 @@ export class Indexer {
burn.token1 = pool.token1;
burn.owner = burnEvent.owner;
burn.origin = tx.from;
burn.amount = burnEvent.amount;
burn.amount = BigInt(burnEvent.amount);
burn.amount0 = amount0;
burn.amount1 = amount1;
burn.amountUSD = amountUSD;
burn.tickLower = burnEvent.tickLower;
burn.tickUpper = burnEvent.tickUpper;
burn.tickLower = BigInt(burnEvent.tickLower);
burn.tickUpper = BigInt(burnEvent.tickUpper);
// Tick entities.
const lowerTickId = poolAddress + '#' + (burnEvent.tickLower).toString();
@ -757,8 +758,8 @@ export class Indexer {
assert(token0 && token1, 'Pool tokens not found.');
// Amounts - 0/1 are token deltas. Can be positive or negative.
const amount0 = convertTokenToDecimal(swapEvent.amount0, BigInt(token0.decimals));
const amount1 = convertTokenToDecimal(swapEvent.amount1, BigInt(token1.decimals));
const amount0 = convertTokenToDecimal(BigInt(swapEvent.amount0), BigInt(token0.decimals));
const amount1 = convertTokenToDecimal(BigInt(swapEvent.amount1), BigInt(token1.decimals));
// Need absolute amounts for volume.
let amount0Abs = amount0;
@ -807,9 +808,9 @@ export class Indexer {
pool.txCount = BigInt(pool.txCount) + BigInt(1);
// Update the pool with the new active liquidity, price, and tick.
pool.liquidity = swapEvent.liquidity;
pool.liquidity = BigInt(swapEvent.liquidity);
pool.tick = BigInt(swapEvent.tick);
pool.sqrtPrice = swapEvent.sqrtPriceX96;
pool.sqrtPrice = BigInt(swapEvent.sqrtPriceX96);
pool.totalValueLockedToken0 = pool.totalValueLockedToken0.plus(amount0);
pool.totalValueLockedToken1 = pool.totalValueLockedToken1.plus(amount1);
@ -871,7 +872,7 @@ export class Indexer {
swap.amount1 = amount1;
swap.amountUSD = amountTotalUSDTracked;
swap.tick = BigInt(swapEvent.tick);
swap.sqrtPriceX96 = swapEvent.sqrtPriceX96;
swap.sqrtPriceX96 = BigInt(swapEvent.sqrtPriceX96);
// Skipping update pool fee growth as they are not queried.

View File

@ -33,7 +33,8 @@ import {
queryPoolsByTokens,
queryPoolById,
queryMints,
queryTicks
queryTicks,
queryBurns
} from '../test/queries';
import {
checkUniswapDayData,
@ -52,6 +53,8 @@ describe('uni-info-watcher', () => {
let token0Address: string;
let token1Address: string;
let tickLower: number;
let tickUpper: number;
let signer: Signer;
let recipient: string;
let config: Config;
@ -126,7 +129,7 @@ describe('uni-info-watcher', () => {
const eventType = 'PoolCreatedEvent';
await watchEvent(uniClient, eventType);
// Sleeping for 5 sec for the entities to be processed.
// Sleeping for 10 sec for the entities to be processed.
await wait(10000);
});
@ -196,8 +199,6 @@ describe('uni-info-watcher', () => {
const amount = 10;
const approveAmount = BigInt(1000000000000000000000000);
let poolCallee: Contract;
let tickLower: number;
let tickUpper: number;
// Initial entity values
let oldFactory: any;
@ -348,4 +349,157 @@ describe('uni-info-watcher', () => {
checkTokenHourData(endpoint, token1.address);
});
});
describe('BurnEvent', () => {
const amount = 10;
// Initial entity values
let oldFactory: any;
let oldToken0: any;
let oldToken1: any;
let oldPool: any;
let oldLowerTick: any;
let oldUpperTick: 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;
data = await request(endpoint, queryTicks, { pool: pool.address });
expect(data.ticks).to.not.be.empty;
oldLowerTick = _.filter(data.ticks, { tickIdx: tickLower.toString() })[0];
oldUpperTick = _.filter(data.ticks, { tickIdx: tickUpper.toString() })[0];
});
it('should trigger BurnEvent', async () => {
// Pool burn.
pool.burn(BigInt(tickLower), BigInt(tickUpper), BigInt(amount));
// Wait for BurnEvent.
const eventType = 'BurnEvent';
await watchEvent(uniClient, eventType);
// Sleeping for 15 sec for the entities to be processed.
await wait(15000);
});
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.
let expectedLiquidity = BigInt(oldPool.liquidity);
if (oldPool.tick !== null) {
if (
BigInt(tickLower) <= BigInt(oldPool.tick) &&
BigInt(tickUpper) > BigInt(oldPool.tick)
) {
expectedLiquidity = BigInt(oldPool.liquidity) - BigInt(amount);
}
}
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(BigInt(newPool.liquidity)).to.be.equal(expectedLiquidity);
});
it('should create a Burn entity', async () => {
// Check id, owner, sender.
// Get the latest Burn.
let data: any;
const variables = {
first: 1,
orderBy: 'timestamp',
orderDirection: 'desc',
pool: pool.address
};
data = await request(endpoint, queryBurns, variables);
expect(data.burns).to.not.be.empty;
const id: string = data.burns[0].id;
const txCountID = id.split('#')[1];
const origin = data.burns[0].origin;
const owner = data.burns[0].owner;
data = await request(endpoint, queryPoolById, { id: pool.address });
const poolTxCount = data.pool.txCount;
const expectedOrigin = recipient;
const expectedOwner = recipient;
expect(txCountID).to.be.equal(poolTxCount);
expect(origin).to.be.equal(expectedOrigin);
expect(owner).to.be.equal(expectedOwner);
});
it('should update Tick entities', async () => {
// Check liquidityGross, liquidityNet.
const data = await request(endpoint, queryTicks, { pool: pool.address });
expect(data.ticks).to.not.be.empty;
const newLowerTick: any = _.filter(data.ticks, { tickIdx: tickLower.toString() })[0];
const newUpperTick: any = _.filter(data.ticks, { tickIdx: tickUpper.toString() })[0];
const expectedLLG = BigInt(oldLowerTick.liquidityGross) - BigInt(amount);
const expectedLN = BigInt(oldLowerTick.liquidityNet) - BigInt(amount);
const expectedULG = BigInt(oldUpperTick.liquidityGross) - BigInt(amount);
const expectedUN = BigInt(oldUpperTick.liquidityNet) + BigInt(amount);
expect(newLowerTick.liquidityGross).to.be.equal(expectedLLG.toString());
expect(newLowerTick.liquidityNet).to.be.equal(expectedLN.toString());
expect(newUpperTick.liquidityGross).to.be.equal(expectedULG.toString());
expect(newUpperTick.liquidityNet).to.be.equal(expectedUN.toString());
});
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);
});
});
});

View File

@ -137,6 +137,34 @@ query queryMints(
}
}`;
// Getting burns(s) filtered by pool, tokens and ordered by timestamp.
export const queryBurns = gql`
query queryBurns(
$first: Int,
$orderBy: Burn_orderBy,
$orderDirection: OrderDirection,
$pool: String,
$token0: String,
$token1: String) {
burns(
first: $first,
orderBy: $orderBy,
orderDirection: $orderDirection,
where: {
pool: $pool,
token0: $token0,
token1: $token1
}) {
amount0
amount1
amountUSD
id
origin
owner
timestamp
}
}`;
// Getting Tick(s) filtered by pool.
export const queryTicks = gql`
query queryTicksByPool($pool: String) {