Test for entities after IncreaseLiquidityEvent. (#198)

Co-authored-by: prathamesh0 <prathamesh.musale0@gmail.com>
This commit is contained in:
Ashwin Phatak 2021-08-10 15:53:59 +05:30 committed by GitHub
parent 0c2efde028
commit 6c05600b41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 171 additions and 97 deletions

View File

@ -9,6 +9,7 @@
}, },
"scripts": { "scripts": {
"test": "lerna run test --stream --no-bail", "test": "lerna run test --stream --no-bail",
"lint": "lerna run lint --stream" "lint": "lerna run lint --stream",
"build:contracts": "lerna run build:contracts"
} }
} }

View File

@ -26,7 +26,7 @@
[upstream.ethServer] [upstream.ethServer]
gqlApiEndpoint = "http://127.0.0.1:8082/graphql" gqlApiEndpoint = "http://127.0.0.1:8082/graphql"
gqlPostgraphileEndpoint = "http://127.0.0.1:5000/graphql" gqlPostgraphileEndpoint = "http://127.0.0.1:5000/graphql"
rpcProviderEndpoint = "http://127.0.0.1:8545" rpcProviderEndpoint = "http://127.0.0.1:8081"
[upstream.cache] [upstream.cache]
name = "requests" name = "requests"

View File

@ -46,7 +46,7 @@ import {
checkPoolDayData, checkPoolDayData,
checkTokenDayData, checkTokenDayData,
checkTokenHourData, checkTokenHourData,
checkTransaction fetchTransaction
} from '../test/utils'; } from '../test/utils';
const NETWORK_RPC_URL = 'http://localhost:8545'; const NETWORK_RPC_URL = 'http://localhost:8545';
@ -130,13 +130,12 @@ describe('uni-info-watcher', () => {
}); });
it('should trigger PoolCreatedEvent', async () => { it('should trigger PoolCreatedEvent', async () => {
// Create Pool. // Create Pool and wait for PoolCreatedEvent.
// Not doing tx.wait() here as we are waiting for the event.
createPool(factory, token0Address, token1Address, fee);
// Wait for PoolCreatedEvent.
const eventType = 'PoolCreatedEvent'; const eventType = 'PoolCreatedEvent';
await watchEvent(uniClient, eventType); await Promise.all([
createPool(factory, token0Address, token1Address, fee),
watchEvent(uniClient, eventType)
]);
// Sleeping for 10 sec for the event to be processed. // Sleeping for 10 sec for the event to be processed.
await wait(10000); await wait(10000);
@ -190,12 +189,12 @@ describe('uni-info-watcher', () => {
}); });
it('should trigger InitializeEvent', async () => { it('should trigger InitializeEvent', async () => {
// Initialize Pool. // Initialize Pool and wait for InitializeEvent
initializePool(pool, sqrtPrice);
// Wait for InitializeEvent.
const eventType = 'InitializeEvent'; const eventType = 'InitializeEvent';
await watchEvent(uniClient, eventType); await Promise.all([
initializePool(pool, sqrtPrice),
watchEvent(uniClient, eventType)
]);
// Sleeping for 5 sec for the event to be processed. // Sleeping for 5 sec for the event to be processed.
await wait(5000); await wait(5000);
@ -257,12 +256,12 @@ describe('uni-info-watcher', () => {
}); });
it('should trigger MintEvent', async () => { it('should trigger MintEvent', async () => {
// Pool mint. // Pool mint and wait for MintEvent.
poolCallee.mint(pool.address, recipient, BigInt(tickLower), BigInt(tickUpper), BigInt(amount));
// Wait for MintEvent.
const eventType = 'MintEvent'; const eventType = 'MintEvent';
await watchEvent(uniClient, eventType); await Promise.all([
poolCallee.mint(pool.address, recipient, BigInt(tickLower), BigInt(tickUpper), BigInt(amount)),
watchEvent(uniClient, eventType)
]);
// Sleeping for 20 sec for the event to be processed. // Sleeping for 20 sec for the event to be processed.
await wait(20000); await wait(20000);
@ -315,8 +314,19 @@ describe('uni-info-watcher', () => {
}); });
it('should create a Transaction entity', async () => { it('should create a Transaction entity', async () => {
const eventType = 'MintEvent'; // Checked values: mints, burns, swaps.
({ expectedTxID, expectedTxTimestamp } = await checkTransaction(endpoint, eventType));
const transaction: any = await fetchTransaction(endpoint);
expectedTxID = transaction.id;
expectedTxTimestamp = transaction.timestamp;
expect(transaction.mints).to.not.be.empty;
expect(transaction.burns).to.be.empty;
expect(transaction.swaps).to.be.empty;
const timestamp = transaction.mints[0].timestamp;
expect(timestamp).to.be.equal(expectedTxTimestamp);
}); });
it('should create a Mint entity', async () => { it('should create a Mint entity', async () => {
@ -429,12 +439,12 @@ describe('uni-info-watcher', () => {
}); });
it('should trigger BurnEvent', async () => { it('should trigger BurnEvent', async () => {
// Pool burn. // Pool burn and wait for BurnEvent.
pool.burn(BigInt(tickLower), BigInt(tickUpper), BigInt(amount));
// Wait for BurnEvent.
const eventType = 'BurnEvent'; const eventType = 'BurnEvent';
await watchEvent(uniClient, eventType); await Promise.all([
pool.burn(BigInt(tickLower), BigInt(tickUpper), BigInt(amount)),
watchEvent(uniClient, eventType)
]);
// Sleeping for 15 sec for the event to be processed. // Sleeping for 15 sec for the event to be processed.
await wait(15000); await wait(15000);
@ -487,8 +497,19 @@ describe('uni-info-watcher', () => {
}); });
it('should create a Transaction entity', async () => { it('should create a Transaction entity', async () => {
const eventType = 'BurnEvent'; // Checked values: mints, burns, swaps.
({ expectedTxID, expectedTxTimestamp } = await checkTransaction(endpoint, eventType));
const transaction: any = await fetchTransaction(endpoint);
expectedTxID = transaction.id;
expectedTxTimestamp = transaction.timestamp;
expect(transaction.mints).to.be.empty;
expect(transaction.burns).to.not.be.empty;
expect(transaction.swaps).to.be.empty;
const timestamp = transaction.burns[0].timestamp;
expect(timestamp).to.be.equal(expectedTxTimestamp);
}); });
it('should create a Burn entity', async () => { it('should create a Burn entity', async () => {
@ -599,12 +620,13 @@ describe('uni-info-watcher', () => {
}); });
it('should trigger SwapEvent', async () => { it('should trigger SwapEvent', async () => {
// Pool swap. // Pool swap and wait for SwapEvent.
poolCallee.swapToLowerSqrtPrice(pool.address, BigInt(sqrtPrice), recipient);
// Wait for SwapEvent.
const eventType = 'SwapEvent'; const eventType = 'SwapEvent';
eventValue = await watchEvent(uniClient, eventType); const values = await Promise.all([
poolCallee.swapToLowerSqrtPrice(pool.address, BigInt(sqrtPrice), recipient),
watchEvent(uniClient, eventType)
]);
eventValue = values[1];
// Sleeping for 5 sec for the event to be processed. // Sleeping for 5 sec for the event to be processed.
await wait(5000); await wait(5000);
@ -653,8 +675,19 @@ describe('uni-info-watcher', () => {
}); });
it('should create a Transaction entity', async () => { it('should create a Transaction entity', async () => {
const eventType = 'SwapEvent'; // Checked values: mints, burns, swaps.
({ expectedTxID, expectedTxTimestamp } = await checkTransaction(endpoint, eventType));
const transaction: any = await fetchTransaction(endpoint);
expectedTxID = transaction.id;
expectedTxTimestamp = transaction.timestamp;
expect(transaction.mints).to.be.empty;
expect(transaction.burns).to.be.empty;
expect(transaction.swaps).to.not.be.empty;
const timestamp = transaction.swaps[0].timestamp;
expect(timestamp).to.be.equal(expectedTxTimestamp);
}); });
it('should create a Swap entity', async () => { it('should create a Swap entity', async () => {
@ -767,7 +800,8 @@ describe('uni-info-watcher', () => {
}); });
it('should trigger TransferEvent', async () => { it('should trigger TransferEvent', async () => {
nfpm.mint({ // NFPM mint and wait for MintEvent.
const transaction = nfpm.mint({
token0: token0Address, token0: token0Address,
token1: token1Address, token1: token1Address,
tickLower, tickLower,
@ -781,9 +815,11 @@ describe('uni-info-watcher', () => {
fee fee
}); });
// Wait for MintEvent.
eventType = 'MintEvent'; eventType = 'MintEvent';
await watchEvent(uniClient, eventType); await Promise.all([
transaction,
watchEvent(uniClient, eventType)
]);
// Wait for TransferEvent. // Wait for TransferEvent.
eventType = 'TransferEvent'; eventType = 'TransferEvent';
@ -798,8 +834,19 @@ describe('uni-info-watcher', () => {
}); });
it('should create a Transaction entity', async () => { it('should create a Transaction entity', async () => {
const eventType = 'MintEvent'; // Checked values: mints, burns, swaps.
({ expectedTxID } = await checkTransaction(endpoint, eventType));
const transaction: any = await fetchTransaction(endpoint);
expectedTxID = transaction.id;
const expectedTxTimestamp = transaction.timestamp;
expect(transaction.mints).to.not.be.empty;
expect(transaction.burns).to.be.empty;
expect(transaction.swaps).to.be.empty;
const timestamp = transaction.mints[0].timestamp;
expect(timestamp).to.be.equal(expectedTxTimestamp);
}); });
it('should createa a Position entity', async () => { it('should createa a Position entity', async () => {
@ -825,4 +872,80 @@ describe('uni-info-watcher', () => {
expect(position.owner).to.be.equal(expectedOwner); expect(position.owner).to.be.equal(expectedOwner);
}); });
}); });
describe('IncreaseLiquidityEvent', () => {
// Checked entities: Transaction, Position.
let oldPosition: any;
let eventValue: any;
let eventType: string;
const tokenId = 1;
const amount0Desired = 15;
const amount1Desired = 15;
const amount0Min = 0;
const amount1Min = 0;
const deadline = 1634367993;
before(async () => {
// Get initial entity values.
const data = await request(endpoint, queryPositions, { id: Number(tokenId) });
oldPosition = data.positions[0];
});
it('should trigger IncreaseLiquidityEvent', async () => {
// Position manger increase liquidity and wait for MintEvent.
const transaction = nfpm.increaseLiquidity({
tokenId,
amount0Desired,
amount1Desired,
amount0Min,
amount1Min,
deadline
});
eventType = 'MintEvent';
await Promise.all([
transaction,
watchEvent(uniClient, eventType)
]);
// Wait for IncreaseLiquidityEvent.
eventType = 'IncreaseLiquidityEvent';
eventValue = await watchEvent(uniClient, eventType);
// Sleeping for 10 sec for the events to be processed.
await wait(10000);
});
it('should create a Transaction entity', async () => {
// Checked values: mints, burns, swaps.
const transaction: any = await fetchTransaction(endpoint);
const expectedTxTimestamp = transaction.timestamp;
expect(transaction.mints).to.not.be.empty;
expect(transaction.burns).to.be.empty;
expect(transaction.swaps).to.be.empty;
const timestamp = transaction.mints[0].timestamp;
expect(timestamp).to.be.equal(expectedTxTimestamp);
});
it('should update Position entity', async () => {
// Checked values: liquidity.
// Unchecked values: depositedToken0, depositedToken1, feeGrowthInside0LastX128, feeGrowthInside0LastX128.
// Get the Position using tokenId.
const data = await request(endpoint, queryPositions, { id: Number(eventValue.event.tokenId) });
expect(data.positions).to.not.be.empty;
const position = data.positions[0];
const expectedLiquidity = BigInt(oldPosition.liquidity) + BigInt(eventValue.event.liquidity);
expect(position.liquidity).to.be.equal(expectedLiquidity.toString());
});
});
}); });

View File

@ -134,9 +134,7 @@ export const checkTokenHourData = async (endpoint: string, tokenAddress: string)
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}> => { export const fetchTransaction = async (endpoint: string): Promise<{transaction: any}> => {
// Checked values: mints, burns, swaps.
// Get the latest Transaction. // Get the latest Transaction.
// Get only the latest mint, burn and swap entity in the transaction. // Get only the latest mint, burn and swap entity in the transaction.
@ -151,40 +149,11 @@ export const checkTransaction = async (endpoint: string, eventType: string): Pro
const data = await request(endpoint, queryTransactions, variables); const data = await request(endpoint, queryTransactions, variables);
expect(data.transactions).to.not.be.empty; expect(data.transactions).to.not.be.empty;
const transaction = data.transactions[0]; const transaction = data.transactions[0];
const expectedTxID = transaction.id;
const expectedTxTimestamp = transaction.timestamp;
let timestamp = ''; expect(transaction.mints).to.be.an.instanceOf(Array);
expect(transaction.burns).to.be.an.instanceOf(Array);
expect(transaction.swaps).to.be.an.instanceOf(Array);
switch (eventType) { return transaction;
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 };
}; };

View File

@ -1,18 +0,0 @@
import { HardhatUserConfig } from 'hardhat/config';
import '@nomiclabs/hardhat-waffle';
const config: HardhatUserConfig = {
defaultNetwork: 'localhost',
solidity: {
compilers: [
{
version: '0.7.6'
}
]
},
paths: {
sources: './test/contracts'
}
};
export default config;

View File

@ -13,8 +13,7 @@
"lint": "eslint .", "lint": "eslint .",
"build": "tsc", "build": "tsc",
"watch:contract": "ts-node src/cli/watch-contract.ts --configFile environments/local.toml", "watch:contract": "ts-node src/cli/watch-contract.ts --configFile environments/local.toml",
"test:compile": "hardhat compile", "smoke-test": "mocha src/smoke.test.ts"
"smoke-test": "yarn test:compile && mocha src/smoke.test.ts"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -31,6 +31,6 @@
"scripts": { "scripts": {
"lint": "eslint .", "lint": "eslint .",
"build": "tsc", "build": "tsc",
"test:compile": "hardhat compile" "build:contracts": "hardhat compile"
} }
} }