Test for Transaction entity after Mint, Burn & Swap events. (#193)

Co-authored-by: prathamesh0 <prathamesh.musale0@gmail.com>
This commit is contained in:
Ashwin Phatak 2021-08-09 12:08:37 +05:30 committed by GitHub
parent a9d9b39c37
commit 16b041c5cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 249 additions and 55 deletions

View File

@ -41,7 +41,8 @@ import {
checkUniswapDayData, checkUniswapDayData,
checkPoolDayData, checkPoolDayData,
checkTokenDayData, checkTokenDayData,
checkTokenHourData checkTokenHourData,
checkTransaction
} from '../test/utils'; } from '../test/utils';
const NETWORK_RPC_URL = 'http://localhost:8545'; const NETWORK_RPC_URL = 'http://localhost:8545';
@ -103,6 +104,7 @@ describe('uni-info-watcher', () => {
describe('PoolCreatedEvent', () => { describe('PoolCreatedEvent', () => {
// NOTE Skipping checking entity updates that cannot be gotten/derived using queries. // NOTE Skipping checking entity updates that cannot be gotten/derived using queries.
// Checked entities: Token, Pool.
const fee = 500; const fee = 500;
@ -145,6 +147,8 @@ describe('uni-info-watcher', () => {
}); });
it('should create a Pool entity', async () => { it('should create a Pool entity', async () => {
// Checked values: feeTier
const variables = { const variables = {
tokens: [token0Address, token1Address] tokens: [token0Address, token1Address]
}; };
@ -168,6 +172,9 @@ describe('uni-info-watcher', () => {
}); });
describe('InitializeEvent', () => { describe('InitializeEvent', () => {
// Checked entities: Pool, PoolDayData.
// Unchecked entities: Bundle, Token.
const sqrtPrice = '4295128939'; const sqrtPrice = '4295128939';
const tick = TICK_MIN; const tick = TICK_MIN;
@ -186,6 +193,10 @@ describe('uni-info-watcher', () => {
// Sleeping for 5 sec for the entities to be processed. // Sleeping for 5 sec for the entities to be processed.
await wait(5000); await wait(5000);
});
it('should update Pool entity', async () => {
// Checked values: sqrtPrice, tick.
const data = await request(endpoint, queryPoolById, { id: pool.address }); const data = await request(endpoint, queryPoolById, { id: pool.address });
expect(data.pool.sqrtPrice).to.be.equal(sqrtPrice); expect(data.pool.sqrtPrice).to.be.equal(sqrtPrice);
@ -198,8 +209,12 @@ describe('uni-info-watcher', () => {
}); });
describe('MintEvent', () => { describe('MintEvent', () => {
// Checked entities: Token, Factory, Pool, Transaction, Mint, Tick, UniswapDayData, PoolDayData, TokenDayData, TokenHourData.
const amount = 10; const amount = 10;
const approveAmount = BigInt(1000000000000000000000000); const approveAmount = BigInt(1000000000000000000000000);
let expectedTxID: string;
let expectedTxTimestamp: string;
// Initial entity values // Initial entity values
let oldFactory: any; let oldFactory: any;
@ -248,7 +263,9 @@ describe('uni-info-watcher', () => {
}); });
it('should update Token entities', async () => { it('should update Token entities', async () => {
// Check txCount. // Checked values: txCount.
// Unchecked values: totalValueLocked, totalValueLockedUSD.
let data: any; let data: any;
data = await request(endpoint, queryToken, { id: token0.address }); data = await request(endpoint, queryToken, { id: token0.address });
@ -262,14 +279,18 @@ describe('uni-info-watcher', () => {
}); });
it('should update Factory entity', async () => { it('should update Factory entity', async () => {
// Check txCount. // Checked values: txCount.
// Unchecked values: totalValueLockedUSD.
const data = await request(endpoint, queryFactory); const data = await request(endpoint, queryFactory);
const newFactory = data.factories[0]; const newFactory = data.factories[0];
expect(newFactory.txCount).to.be.equal((BigInt(oldFactory.txCount) + BigInt(1)).toString()); expect(newFactory.txCount).to.be.equal((BigInt(oldFactory.txCount) + BigInt(1)).toString());
}); });
it('should update Pool entity', async () => { it('should update Pool entity', async () => {
// Check txCount, liquidity. // Checked values: txCount, liquidity.
// Unchecked values: totalValueLockedToken0, totalValueLockedToken1, totalValueLockedUSD.
let expectedLiquidity = BigInt(oldPool.liquidity); let expectedLiquidity = BigInt(oldPool.liquidity);
if (oldPool.tick !== null) { if (oldPool.tick !== null) {
if ( if (
@ -287,8 +308,15 @@ describe('uni-info-watcher', () => {
expect(BigInt(newPool.liquidity)).to.be.equal(expectedLiquidity); expect(BigInt(newPool.liquidity)).to.be.equal(expectedLiquidity);
}); });
it('should create a Transaction entity', async () => {
const eventType = 'MintEvent';
({ expectedTxID, expectedTxTimestamp } = await checkTransaction(endpoint, eventType));
});
it('should create a Mint entity', async () => { it('should create a Mint entity', async () => {
// Check id, origin, owner, sender. // Checked values: id, origin, owner, sender, timestamp, pool, transaction.
// Unchecked values: amount0, amount1, amountUSD.
// Get the latest Mint. // Get the latest Mint.
let data: any; let data: any;
const variables = { const variables = {
@ -300,11 +328,9 @@ describe('uni-info-watcher', () => {
data = await request(endpoint, queryMints, variables); data = await request(endpoint, queryMints, variables);
expect(data.mints).to.not.be.empty; expect(data.mints).to.not.be.empty;
const id: string = data.mints[0].id; const mint = data.mints[0];
const txCountID = id.split('#')[1]; const txID = mint.id.split('#')[0];
const origin = data.mints[0].origin; const txCountID = mint.id.split('#')[1];
const owner = data.mints[0].owner;
const sender = data.mints[0].sender;
data = await request(endpoint, queryPoolById, { id: pool.address }); data = await request(endpoint, queryPoolById, { id: pool.address });
const poolTxCount = data.pool.txCount; const poolTxCount = data.pool.txCount;
@ -312,14 +338,21 @@ describe('uni-info-watcher', () => {
const expectedOwner = recipient; const expectedOwner = recipient;
const expectedSender = poolCallee.address; const expectedSender = poolCallee.address;
expect(txID).to.be.equal(expectedTxID);
expect(txCountID).to.be.equal(poolTxCount); expect(txCountID).to.be.equal(poolTxCount);
expect(origin).to.be.equal(expectedOrigin); expect(mint.origin).to.be.equal(expectedOrigin);
expect(owner).to.be.equal(expectedOwner); expect(mint.owner).to.be.equal(expectedOwner);
expect(sender).to.be.equal(expectedSender); expect(mint.sender).to.be.equal(expectedSender);
expect(mint.timestamp).to.be.equal(expectedTxTimestamp);
expect(mint.pool.id).to.be.equal(pool.address);
expect(mint.transaction.id).to.be.equal(expectedTxID);
}); });
it('should create Tick entities', async () => { it('should create Tick entities', async () => {
// Check liquidityGross, liquidityNet. // Checked values: liquidityGross, liquidityNet.
// Unchecked values: id, price0, price1.
const data = await request(endpoint, queryTicks, { pool: pool.address }); const data = await request(endpoint, queryTicks, { pool: pool.address });
expect(data.ticks).to.not.be.empty; expect(data.ticks).to.not.be.empty;
@ -352,7 +385,11 @@ describe('uni-info-watcher', () => {
}); });
describe('BurnEvent', () => { describe('BurnEvent', () => {
// Checked entities: Token, Factory, Pool, Transaction, Burn, Tick, UniswapDayData, PoolDayData, TokenDayData, TokenHourData.
const amount = 10; const amount = 10;
let expectedTxID: string;
let expectedTxTimestamp: string;
// Initial entity values // Initial entity values
let oldFactory: any; let oldFactory: any;
@ -398,7 +435,9 @@ describe('uni-info-watcher', () => {
}); });
it('should update Token entities', async () => { it('should update Token entities', async () => {
// Check txCount. // Checked values: txCount.
// Unchecked values: totalValueLocked, totalValueLockedUSD.
let data: any; let data: any;
data = await request(endpoint, queryToken, { id: token0.address }); data = await request(endpoint, queryToken, { id: token0.address });
@ -412,14 +451,18 @@ describe('uni-info-watcher', () => {
}); });
it('should update Factory entity', async () => { it('should update Factory entity', async () => {
// Check txCount. // Checked values: txCount.
// Unchecked values: totalValueLockedUSD.
const data = await request(endpoint, queryFactory); const data = await request(endpoint, queryFactory);
const newFactory = data.factories[0]; const newFactory = data.factories[0];
expect(newFactory.txCount).to.be.equal((BigInt(oldFactory.txCount) + BigInt(1)).toString()); expect(newFactory.txCount).to.be.equal((BigInt(oldFactory.txCount) + BigInt(1)).toString());
}); });
it('should update Pool entity', async () => { it('should update Pool entity', async () => {
// Check txCount, liquidity. // Checked values: txCount, liquidity.
// Unchecked values: totalValueLockedToken0, totalValueLockedToken1, totalValueLockedUSD.
let expectedLiquidity = BigInt(oldPool.liquidity); let expectedLiquidity = BigInt(oldPool.liquidity);
if (oldPool.tick !== null) { if (oldPool.tick !== null) {
if ( if (
@ -437,8 +480,15 @@ describe('uni-info-watcher', () => {
expect(BigInt(newPool.liquidity)).to.be.equal(expectedLiquidity); expect(BigInt(newPool.liquidity)).to.be.equal(expectedLiquidity);
}); });
it('should create a Transaction entity', async () => {
const eventType = 'BurnEvent';
({ expectedTxID, expectedTxTimestamp } = await checkTransaction(endpoint, eventType));
});
it('should create a Burn entity', async () => { it('should create a Burn entity', async () => {
// Check id, origin, owner. // Checked values: id, origin, owner, timestamp, pool, transaction.
// Unchecked values: amount0, amount1, amountUSD.
// Get the latest Burn. // Get the latest Burn.
let data: any; let data: any;
const variables = { const variables = {
@ -451,23 +501,29 @@ describe('uni-info-watcher', () => {
data = await request(endpoint, queryBurns, variables); data = await request(endpoint, queryBurns, variables);
expect(data.burns).to.not.be.empty; expect(data.burns).to.not.be.empty;
const id: string = data.burns[0].id; const burn = data.burns[0];
const txCountID = id.split('#')[1]; const txID = burn.id.split('#')[0];
const origin = data.burns[0].origin; const txCountID = burn.id.split('#')[1];
const owner = data.burns[0].owner;
data = await request(endpoint, queryPoolById, { id: pool.address }); data = await request(endpoint, queryPoolById, { id: pool.address });
const poolTxCount = data.pool.txCount; const poolTxCount = data.pool.txCount;
const expectedOrigin = recipient; const expectedOrigin = recipient;
const expectedOwner = recipient; const expectedOwner = recipient;
expect(txID).to.be.equal(expectedTxID);
expect(txCountID).to.be.equal(poolTxCount); expect(txCountID).to.be.equal(poolTxCount);
expect(origin).to.be.equal(expectedOrigin); expect(burn.origin).to.be.equal(expectedOrigin);
expect(owner).to.be.equal(expectedOwner); expect(burn.owner).to.be.equal(expectedOwner);
expect(burn.timestamp).to.be.equal(expectedTxTimestamp);
expect(burn.pool.id).to.be.equal(pool.address);
expect(burn.transaction.id).to.be.equal(expectedTxID);
}); });
it('should update Tick entities', async () => { it('should update Tick entities', async () => {
// Check liquidityGross, liquidityNet. // Checked values: liquidityGross, liquidityNet.
// Unchecked values: id, price0, price1.
const data = await request(endpoint, queryTicks, { pool: pool.address }); const data = await request(endpoint, queryTicks, { pool: pool.address });
expect(data.ticks).to.not.be.empty; expect(data.ticks).to.not.be.empty;
@ -505,7 +561,12 @@ describe('uni-info-watcher', () => {
}); });
describe('SwapEvent', () => { describe('SwapEvent', () => {
// Checked entities: Token, Factory, Pool, Transaction, Swap, Tick, UniswapDayData, PoolDayData, TokenDayData, TokenHourData.
// Unchecked entities: Bundle.
const sqrtPrice = '4295128938'; const sqrtPrice = '4295128938';
let expectedTxID: string;
let expectedTxTimestamp: string;
// Initial entity values // Initial entity values
let eventValue: any; let eventValue: any;
@ -544,7 +605,9 @@ describe('uni-info-watcher', () => {
}); });
it('should update Token entities', async () => { it('should update Token entities', async () => {
// Check txCount. // Checked values: txCount.
// Unchecked values: derivedETH, feesUSD, totalValueLocked, totalValueLockedUSD, volume, volumeUSD.
let data: any; let data: any;
data = await request(endpoint, queryToken, { id: token0.address }); data = await request(endpoint, queryToken, { id: token0.address });
@ -558,14 +621,18 @@ describe('uni-info-watcher', () => {
}); });
it('should update Factory entity', async () => { it('should update Factory entity', async () => {
// Check txCount. // Checked values: txCount.
// Unchecked values: totalFeesUSD, totalValueLockedUSD, totalVolumeUSD.
const data = await request(endpoint, queryFactory); const data = await request(endpoint, queryFactory);
const newFactory = data.factories[0]; const newFactory = data.factories[0];
expect(newFactory.txCount).to.be.equal((BigInt(oldFactory.txCount) + BigInt(1)).toString()); expect(newFactory.txCount).to.be.equal((BigInt(oldFactory.txCount) + BigInt(1)).toString());
}); });
it('should update Pool entity', async () => { it('should update Pool entity', async () => {
// Check txCount, liquidity, tick, sqrtPrice. // Checked values: txCount, liquidity, tick, sqrtPrice.
// Unchecked values: token0Price, token1Price, totalValueLockedToken0, totalValueLockedToken1, totalValueLockedUSD, volumeUSD.
const expectedLiquidity = eventValue.event.liquidity; const expectedLiquidity = eventValue.event.liquidity;
const expectedTick = eventValue.event.tick; const expectedTick = eventValue.event.tick;
const expectedSqrtPrice = eventValue.event.sqrtPriceX96; const expectedSqrtPrice = eventValue.event.sqrtPriceX96;
@ -579,9 +646,15 @@ describe('uni-info-watcher', () => {
expect(newPool.sqrtPrice).to.be.equal(expectedSqrtPrice); expect(newPool.sqrtPrice).to.be.equal(expectedSqrtPrice);
}); });
it('should create a Transaction entity', async () => {
const eventType = 'SwapEvent';
({ expectedTxID, expectedTxTimestamp } = await checkTransaction(endpoint, eventType));
});
it('should create a Swap entity', async () => { it('should create a Swap entity', async () => {
// Check id, origin. // Checked values: id, origin, timestamp, pool, transaction.
// Get the latest Swap. // Unchecked values: amount0, amount1, amountUSD.
let data: any; let data: any;
const variables = { const variables = {
first: 1, first: 1,
@ -593,16 +666,21 @@ describe('uni-info-watcher', () => {
data = await request(endpoint, querySwaps, variables); data = await request(endpoint, querySwaps, variables);
expect(data.swaps).to.not.be.empty; expect(data.swaps).to.not.be.empty;
const id: string = data.swaps[0].id; const swap = data.swaps[0];
const txCountID = id.split('#')[1]; const txID = swap.id.split('#')[0];
const origin = data.swaps[0].origin; const txCountID = swap.id.split('#')[1];
data = await request(endpoint, queryPoolById, { id: pool.address }); data = await request(endpoint, queryPoolById, { id: pool.address });
const poolTxCount = data.pool.txCount; const poolTxCount = data.pool.txCount;
const expectedOrigin = recipient; const expectedOrigin = recipient;
expect(txID).to.be.equal(expectedTxID);
expect(txCountID).to.be.equal(poolTxCount); expect(txCountID).to.be.equal(poolTxCount);
expect(origin).to.be.equal(expectedOrigin); expect(swap.origin).to.be.equal(expectedOrigin);
expect(swap.timestamp).to.be.equal(expectedTxTimestamp);
expect(swap.pool.id).to.be.equal(pool.address);
expect(swap.transaction.id).to.be.equal(expectedTxID);
}); });
it('should update UniswapDayData entity', async () => { it('should update UniswapDayData entity', async () => {

View File

@ -139,14 +139,20 @@ query queryMints(
token0: $token0, token0: $token0,
token1: $token1 token1: $token1
}) { }) {
amount0 amount0,
amount1 amount1,
amountUSD amountUSD,
id id,
origin origin,
owner owner,
sender sender,
timestamp timestamp,
pool {
id
},
transaction {
id
}
} }
}`; }`;
@ -168,13 +174,19 @@ query queryBurns(
token0: $token0, token0: $token0,
token1: $token1 token1: $token1
}) { }) {
amount0 amount0,
amount1 amount1,
amountUSD amountUSD,
id id,
origin origin,
owner owner,
timestamp timestamp,
pool {
id
},
transaction {
id
}
} }
}`; }`;
@ -196,11 +208,47 @@ query querySwaps(
token0: $token0, token0: $token0,
token1: $token1 token1: $token1
}) { }) {
amount0 amount0,
amount1 amount1,
amountUSD amountUSD,
id id,
origin origin,
timestamp,
pool {
id
},
transaction {
id
}
}
}`;
// Getting transactions(s) ordered by timestamp.
export const queryTransactions = gql`
query queryTransactions(
$first: Int,
$orderBy: Transaction_orderBy,
$mintOrderBy: Mint_orderBy,
$burnOrderBy: Burn_orderBy,
$swapOrderBy: Swap_orderBy,
$orderDirection: OrderDirection) {
transactions(
first: $first,
orderBy: $orderBy,
orderDirection: $orderDirection) {
id,
mints( first: $first, orderBy: $mintOrderBy, orderDirection: $orderDirection) {
id,
timestamp
},
burns( first: $first, orderBy: $burnOrderBy, orderDirection: $orderDirection) {
id,
timestamp
},
swaps( first: $first, orderBy: $swapOrderBy, orderDirection: $orderDirection) {
id,
timestamp
},
timestamp timestamp
} }
}`; }`;

View File

@ -10,10 +10,14 @@ import {
queryPoolDayData, queryPoolDayData,
queryUniswapDayData, queryUniswapDayData,
queryTokenDayData, queryTokenDayData,
queryTokenHourData queryTokenHourData,
queryTransactions
} from '../test/queries'; } from '../test/queries';
export const checkUniswapDayData = async (endpoint: string): Promise<void> => { export const checkUniswapDayData = async (endpoint: string): Promise<void> => {
// Checked values: date, tvlUSD.
// Unchecked values: volumeUSD.
// Get the latest UniswapDayData. // Get the latest UniswapDayData.
const variables = { const variables = {
first: 1, first: 1,
@ -37,6 +41,9 @@ export const checkUniswapDayData = async (endpoint: string): Promise<void> => {
}; };
export const checkPoolDayData = async (endpoint: string, poolAddress: string): Promise<void> => { export const checkPoolDayData = async (endpoint: string, poolAddress: string): Promise<void> => {
// Checked values: id, date, tvlUSD.
// Unchecked values: volumeUSD.
// Get the latest PoolDayData. // Get the latest PoolDayData.
const variables = { const variables = {
first: 1, first: 1,
@ -63,6 +70,9 @@ export const checkPoolDayData = async (endpoint: string, poolAddress: string): P
}; };
export const checkTokenDayData = async (endpoint: string, tokenAddress: string): Promise<void> => { export const checkTokenDayData = async (endpoint: string, tokenAddress: string): Promise<void> => {
// Checked values: id, date, totalValueLockedUSD.
// Unchecked values: volumeUSD.
// Get the latest TokenDayData. // Get the latest TokenDayData.
const variables = { const variables = {
first: 1, first: 1,
@ -89,6 +99,9 @@ export const checkTokenDayData = async (endpoint: string, tokenAddress: string):
}; };
export const checkTokenHourData = async (endpoint: string, tokenAddress: string): Promise<void> => { export const checkTokenHourData = async (endpoint: string, tokenAddress: string): Promise<void> => {
// Checked values: id, periodStartUnix, low, high, open, close.
// Unchecked values:
// Get the latest TokenHourData. // Get the latest TokenHourData.
const variables = { const variables = {
first: 1, first: 1,
@ -120,3 +133,58 @@ export const checkTokenHourData = async (endpoint: string, tokenAddress: string)
expect(open).to.be.equal(tokenPrice.toString()); expect(open).to.be.equal(tokenPrice.toString());
expect(close).to.be.equal(tokenPrice.toString()); expect(close).to.be.equal(tokenPrice.toString());
}; };
export const checkTransaction = async (endpoint: string, eventType: string): Promise<{expectedTxID: string, expectedTxTimestamp: string}> => {
// Checked values: mints, burns, swaps.
// Get the latest Transaction.
// Get only the latest mint, burn and swap entity in the transaction.
const variables = {
first: 1,
orderBy: 'timestamp',
mintOrderBy: 'timestamp',
burnOrderBy: 'timestamp',
swapOrderBy: 'timestamp',
orderDirection: 'desc'
};
const data = await request(endpoint, queryTransactions, variables);
expect(data.transactions).to.not.be.empty;
const transaction = data.transactions[0];
const expectedTxID = transaction.id;
const expectedTxTimestamp = transaction.timestamp;
let timestamp = '';
switch (eventType) {
case 'MintEvent':
expect(transaction.mints).to.not.be.empty;
expect(transaction.burns).to.be.empty;
expect(transaction.swaps).to.be.empty;
timestamp = transaction.mints[0].timestamp;
break;
case 'BurnEvent':
expect(transaction.mints).to.be.empty;
expect(transaction.burns).to.not.be.empty;
expect(transaction.swaps).to.be.empty;
timestamp = transaction.burns[0].timestamp;
break;
case 'SwapEvent':
expect(transaction.mints).to.be.empty;
expect(transaction.burns).to.be.empty;
expect(transaction.swaps).to.not.be.empty;
timestamp = transaction.swaps[0].timestamp;
break;
default:
break;
}
expect(timestamp).to.be.equal(expectedTxTimestamp);
return { expectedTxID, expectedTxTimestamp };
};