mirror of
https://github.com/cerc-io/watcher-ts
synced 2024-11-20 12:56:20 +00:00
123 lines
4.3 KiB
TypeScript
123 lines
4.3 KiB
TypeScript
|
import { expect } from 'chai';
|
||
|
import { request } from 'graphql-request';
|
||
|
import Decimal from 'decimal.js';
|
||
|
|
||
|
import {
|
||
|
queryFactory,
|
||
|
queryBundle,
|
||
|
queryToken,
|
||
|
queryPoolById,
|
||
|
queryPoolDayData,
|
||
|
queryUniswapDayData,
|
||
|
queryTokenDayData,
|
||
|
queryTokenHourData
|
||
|
} from '../test/queries';
|
||
|
|
||
|
export const checkUniswapDayData = async (endpoint: string): Promise<void> => {
|
||
|
// Get the latest UniswapDayData.
|
||
|
const variables = {
|
||
|
first: 1,
|
||
|
orderBy: 'date',
|
||
|
orderDirection: 'desc'
|
||
|
};
|
||
|
const data = await request(endpoint, queryUniswapDayData, variables);
|
||
|
expect(data.uniswapDayDatas).to.not.be.empty;
|
||
|
|
||
|
const id: string = data.uniswapDayDatas[0].id;
|
||
|
const dayID = Number(id);
|
||
|
const date = data.uniswapDayDatas[0].date;
|
||
|
const tvlUSD = data.uniswapDayDatas[0].tvlUSD;
|
||
|
|
||
|
const dayStartTimestamp = dayID * 86400;
|
||
|
const factoryData = await request(endpoint, queryFactory);
|
||
|
const totalValueLockedUSD: string = factoryData.factories[0].totalValueLockedUSD;
|
||
|
|
||
|
expect(date).to.be.equal(dayStartTimestamp);
|
||
|
expect(tvlUSD).to.be.equal(totalValueLockedUSD);
|
||
|
};
|
||
|
|
||
|
export const checkPoolDayData = async (endpoint: string, poolAddress: string): Promise<void> => {
|
||
|
// Get the latest PoolDayData.
|
||
|
const variables = {
|
||
|
first: 1,
|
||
|
orderBy: 'date',
|
||
|
orderDirection: 'desc',
|
||
|
pool: poolAddress
|
||
|
};
|
||
|
const data = await request(endpoint, queryPoolDayData, variables);
|
||
|
expect(data.poolDayDatas).to.not.be.empty;
|
||
|
|
||
|
const dayPoolID: string = data.poolDayDatas[0].id;
|
||
|
const poolID: string = dayPoolID.split('-')[0];
|
||
|
const dayID = Number(dayPoolID.split('-')[1]);
|
||
|
const date = data.poolDayDatas[0].date;
|
||
|
const tvlUSD = data.poolDayDatas[0].tvlUSD;
|
||
|
|
||
|
const dayStartTimestamp = dayID * 86400;
|
||
|
const poolData = await request(endpoint, queryPoolById, { id: poolAddress });
|
||
|
const totalValueLockedUSD: string = poolData.pool.totalValueLockedUSD;
|
||
|
|
||
|
expect(poolID).to.be.equal(poolAddress);
|
||
|
expect(date).to.be.equal(dayStartTimestamp);
|
||
|
expect(tvlUSD).to.be.equal(totalValueLockedUSD);
|
||
|
};
|
||
|
|
||
|
export const checkTokenDayData = async (endpoint: string, tokenAddress: string): Promise<void> => {
|
||
|
// Get the latest TokenDayData.
|
||
|
const variables = {
|
||
|
first: 1,
|
||
|
orderBy: 'date',
|
||
|
orderDirection: 'desc',
|
||
|
token: tokenAddress
|
||
|
};
|
||
|
const data = await request(endpoint, queryTokenDayData, variables);
|
||
|
expect(data.tokenDayDatas).to.not.be.empty;
|
||
|
|
||
|
const tokenDayID: string = data.tokenDayDatas[0].id;
|
||
|
const tokenID: string = tokenDayID.split('-')[0];
|
||
|
const dayID = Number(tokenDayID.split('-')[1]);
|
||
|
const date = data.tokenDayDatas[0].date;
|
||
|
const tvlUSD = data.tokenDayDatas[0].totalValueLockedUSD;
|
||
|
|
||
|
const dayStartTimestamp = dayID * 86400;
|
||
|
const tokenData = await request(endpoint, queryToken, { id: tokenAddress });
|
||
|
const totalValueLockedUSD: string = tokenData.token.totalValueLockedUSD;
|
||
|
|
||
|
expect(tokenID).to.be.equal(tokenAddress);
|
||
|
expect(date).to.be.equal(dayStartTimestamp);
|
||
|
expect(tvlUSD).to.be.equal(totalValueLockedUSD);
|
||
|
};
|
||
|
|
||
|
export const checkTokenHourData = async (endpoint: string, tokenAddress: string): Promise<void> => {
|
||
|
// Get the latest TokenHourData.
|
||
|
const variables = {
|
||
|
first: 1,
|
||
|
orderBy: 'periodStartUnix',
|
||
|
orderDirection: 'desc',
|
||
|
token: tokenAddress
|
||
|
};
|
||
|
const data = await request(endpoint, queryTokenHourData, variables);
|
||
|
expect(data.tokenHourDatas).to.not.be.empty;
|
||
|
|
||
|
const tokenHourID: string = data.tokenHourDatas[0].id;
|
||
|
const tokenID: string = tokenHourID.split('-')[0];
|
||
|
const hourIndex = Number(tokenHourID.split('-')[1]);
|
||
|
const periodStartUnix = data.tokenHourDatas[0].periodStartUnix;
|
||
|
const low = data.tokenHourDatas[0].low;
|
||
|
const high = data.tokenHourDatas[0].high;
|
||
|
const open = data.tokenHourDatas[0].open;
|
||
|
const close = data.tokenHourDatas[0].close;
|
||
|
|
||
|
const hourStartUnix = hourIndex * 3600;
|
||
|
const tokenData = await request(endpoint, queryToken, { id: tokenAddress });
|
||
|
const bundleData = await request(endpoint, queryBundle);
|
||
|
const tokenPrice = new Decimal(tokenData.token.derivedETH).times(bundleData.bundles[0].ethPriceUSD);
|
||
|
|
||
|
expect(tokenID).to.be.equal(tokenAddress);
|
||
|
expect(periodStartUnix).to.be.equal(hourStartUnix);
|
||
|
expect(low).to.be.equal(tokenPrice.toString());
|
||
|
expect(high).to.be.equal(tokenPrice.toString());
|
||
|
expect(open).to.be.equal(tokenPrice.toString());
|
||
|
expect(close).to.be.equal(tokenPrice.toString());
|
||
|
};
|