2021-08-12 09:58:13 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 Vulcanize, Inc.
|
|
|
|
//
|
|
|
|
|
2021-08-06 06:42:52 +00:00
|
|
|
import { expect } from 'chai';
|
2021-08-16 10:12:34 +00:00
|
|
|
import { ethers } from 'ethers';
|
2021-08-06 06:42:52 +00:00
|
|
|
import Decimal from 'decimal.js';
|
2021-08-16 10:12:34 +00:00
|
|
|
import _ from 'lodash';
|
2021-08-20 06:56:37 +00:00
|
|
|
|
|
|
|
import { insertNDummyBlocks } from '@vulcanize/util/test';
|
2021-08-06 06:42:52 +00:00
|
|
|
|
2021-08-24 06:25:29 +00:00
|
|
|
import { Database, OrderDirection } from '../src/database';
|
2021-08-16 10:12:34 +00:00
|
|
|
import { Block } from '../src/events';
|
|
|
|
import { Token } from '../src/entity/Token';
|
2021-08-24 06:25:29 +00:00
|
|
|
import { Client } from '../src/client';
|
2021-08-06 06:42:52 +00:00
|
|
|
|
2021-08-24 06:25:29 +00:00
|
|
|
export const checkUniswapDayData = async (client: Client): Promise<void> => {
|
2021-08-09 06:38:37 +00:00
|
|
|
// Checked values: date, tvlUSD.
|
|
|
|
// Unchecked values: volumeUSD.
|
|
|
|
|
2021-08-06 06:42:52 +00:00
|
|
|
// Get the latest UniswapDayData.
|
2021-08-24 06:25:29 +00:00
|
|
|
const uniswapDayDatas = await client.getUniswapDayDatas({}, 0, 1, 'date', OrderDirection.desc);
|
|
|
|
expect(uniswapDayDatas).to.not.be.empty;
|
|
|
|
|
|
|
|
const id: string = uniswapDayDatas[0].id;
|
2021-08-06 06:42:52 +00:00
|
|
|
const dayID = Number(id);
|
2021-08-24 06:25:29 +00:00
|
|
|
const date = uniswapDayDatas[0].date;
|
|
|
|
const tvlUSD = uniswapDayDatas[0].tvlUSD;
|
2021-08-06 06:42:52 +00:00
|
|
|
|
|
|
|
const dayStartTimestamp = dayID * 86400;
|
2021-08-24 06:25:29 +00:00
|
|
|
const factories = await client.getFactories(1);
|
|
|
|
const totalValueLockedUSD: string = factories[0].totalValueLockedUSD;
|
2021-08-06 06:42:52 +00:00
|
|
|
|
|
|
|
expect(date).to.be.equal(dayStartTimestamp);
|
|
|
|
expect(tvlUSD).to.be.equal(totalValueLockedUSD);
|
|
|
|
};
|
|
|
|
|
2021-08-24 06:25:29 +00:00
|
|
|
export const checkPoolDayData = async (client: Client, poolAddress: string): Promise<void> => {
|
2021-08-09 06:38:37 +00:00
|
|
|
// Checked values: id, date, tvlUSD.
|
|
|
|
// Unchecked values: volumeUSD.
|
|
|
|
|
2021-08-06 06:42:52 +00:00
|
|
|
// Get the latest PoolDayData.
|
2021-08-24 06:25:29 +00:00
|
|
|
const poolDayDatas = await client.getPoolDayDatas({ pool: poolAddress }, 0, 1, 'date', OrderDirection.desc);
|
|
|
|
expect(poolDayDatas).to.not.be.empty;
|
|
|
|
|
|
|
|
const dayPoolID: string = poolDayDatas[0].id;
|
2021-08-06 06:42:52 +00:00
|
|
|
const poolID: string = dayPoolID.split('-')[0];
|
|
|
|
const dayID = Number(dayPoolID.split('-')[1]);
|
2021-08-24 06:25:29 +00:00
|
|
|
const date = poolDayDatas[0].date;
|
|
|
|
const tvlUSD = poolDayDatas[0].tvlUSD;
|
2021-08-06 06:42:52 +00:00
|
|
|
|
|
|
|
const dayStartTimestamp = dayID * 86400;
|
2021-08-24 06:25:29 +00:00
|
|
|
const poolData = await client.getPoolById(poolAddress);
|
2021-08-06 06:42:52 +00:00
|
|
|
const totalValueLockedUSD: string = poolData.pool.totalValueLockedUSD;
|
|
|
|
|
|
|
|
expect(poolID).to.be.equal(poolAddress);
|
|
|
|
expect(date).to.be.equal(dayStartTimestamp);
|
|
|
|
expect(tvlUSD).to.be.equal(totalValueLockedUSD);
|
|
|
|
};
|
|
|
|
|
2021-08-24 06:25:29 +00:00
|
|
|
export const checkTokenDayData = async (client: Client, tokenAddress: string): Promise<void> => {
|
2021-08-09 06:38:37 +00:00
|
|
|
// Checked values: id, date, totalValueLockedUSD.
|
|
|
|
// Unchecked values: volumeUSD.
|
|
|
|
|
2021-08-06 06:42:52 +00:00
|
|
|
// Get the latest TokenDayData.
|
2021-08-24 06:25:29 +00:00
|
|
|
const tokenDayDatas = await client.getTokenDayDatas({ token: tokenAddress }, 0, 1, 'date', OrderDirection.desc);
|
|
|
|
expect(tokenDayDatas).to.not.be.empty;
|
|
|
|
|
|
|
|
const tokenDayID: string = tokenDayDatas[0].id;
|
2021-08-06 06:42:52 +00:00
|
|
|
const tokenID: string = tokenDayID.split('-')[0];
|
|
|
|
const dayID = Number(tokenDayID.split('-')[1]);
|
2021-08-24 06:25:29 +00:00
|
|
|
const date = tokenDayDatas[0].date;
|
|
|
|
const tvlUSD = tokenDayDatas[0].totalValueLockedUSD;
|
2021-08-06 06:42:52 +00:00
|
|
|
|
|
|
|
const dayStartTimestamp = dayID * 86400;
|
2021-08-24 06:25:29 +00:00
|
|
|
const tokenData = await client.getToken(tokenAddress);
|
2021-08-06 06:42:52 +00:00
|
|
|
const totalValueLockedUSD: string = tokenData.token.totalValueLockedUSD;
|
|
|
|
|
|
|
|
expect(tokenID).to.be.equal(tokenAddress);
|
|
|
|
expect(date).to.be.equal(dayStartTimestamp);
|
|
|
|
expect(tvlUSD).to.be.equal(totalValueLockedUSD);
|
|
|
|
};
|
|
|
|
|
2021-08-24 06:25:29 +00:00
|
|
|
export const checkTokenHourData = async (client: Client, tokenAddress: string): Promise<void> => {
|
2021-08-09 06:38:37 +00:00
|
|
|
// Checked values: id, periodStartUnix, low, high, open, close.
|
|
|
|
// Unchecked values:
|
|
|
|
|
2021-08-06 06:42:52 +00:00
|
|
|
// Get the latest TokenHourData.
|
2021-08-24 06:25:29 +00:00
|
|
|
const tokenHourDatas = await client.getTokenHourDatas({ token: tokenAddress }, 0, 1, 'periodStartUnix', OrderDirection.desc);
|
|
|
|
expect(tokenHourDatas).to.not.be.empty;
|
|
|
|
|
|
|
|
const tokenHourID: string = tokenHourDatas[0].id;
|
2021-08-06 06:42:52 +00:00
|
|
|
const tokenID: string = tokenHourID.split('-')[0];
|
|
|
|
const hourIndex = Number(tokenHourID.split('-')[1]);
|
2021-08-24 06:25:29 +00:00
|
|
|
const periodStartUnix = tokenHourDatas[0].periodStartUnix;
|
|
|
|
const low = tokenHourDatas[0].low;
|
|
|
|
const high = tokenHourDatas[0].high;
|
|
|
|
const open = tokenHourDatas[0].open;
|
|
|
|
const close = tokenHourDatas[0].close;
|
2021-08-06 06:42:52 +00:00
|
|
|
|
|
|
|
const hourStartUnix = hourIndex * 3600;
|
2021-08-24 06:25:29 +00:00
|
|
|
const tokenData = await client.getToken(tokenAddress);
|
|
|
|
const bundles = await client.getBundles(1);
|
|
|
|
const tokenPrice = new Decimal(tokenData.token.derivedETH).times(bundles[0].ethPriceUSD);
|
2021-08-06 06:42:52 +00:00
|
|
|
|
|
|
|
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());
|
|
|
|
};
|
2021-08-09 06:38:37 +00:00
|
|
|
|
2021-08-24 06:25:29 +00:00
|
|
|
export const fetchTransaction = async (client: Client): Promise<{transaction: any}> => {
|
2021-08-09 06:38:37 +00:00
|
|
|
// Get the latest Transaction.
|
|
|
|
// Get only the latest mint, burn and swap entity in the transaction.
|
2021-08-24 06:25:29 +00:00
|
|
|
const transactions = await client.getTransactions(
|
|
|
|
1,
|
|
|
|
{
|
|
|
|
orderBy: 'timestamp',
|
|
|
|
mintOrderBy: 'timestamp',
|
|
|
|
burnOrderBy: 'timestamp',
|
|
|
|
swapOrderBy: 'timestamp'
|
|
|
|
},
|
|
|
|
OrderDirection.desc
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(transactions).to.not.be.empty;
|
|
|
|
const transaction = transactions[0];
|
2021-08-10 10:23:59 +00:00
|
|
|
|
|
|
|
expect(transaction.mints).to.be.an.instanceOf(Array);
|
|
|
|
expect(transaction.burns).to.be.an.instanceOf(Array);
|
|
|
|
expect(transaction.swaps).to.be.an.instanceOf(Array);
|
|
|
|
|
|
|
|
return transaction;
|
2021-08-09 06:38:37 +00:00
|
|
|
};
|
2021-08-16 10:12:34 +00:00
|
|
|
|
2021-08-20 06:56:37 +00:00
|
|
|
export const createTestBlockTree = async (db: Database): Promise<Block[][]> => {
|
2021-08-16 10:12:34 +00:00
|
|
|
// Create BlockProgress test data.
|
|
|
|
//
|
|
|
|
// +---+
|
|
|
|
// head----->| 21|
|
|
|
|
// +---+
|
|
|
|
// |
|
|
|
|
// |
|
|
|
|
// +---+ +---+
|
2021-08-20 06:56:37 +00:00
|
|
|
// | 20| | 15|
|
2021-08-16 10:12:34 +00:00
|
|
|
// +---+ +---+
|
|
|
|
// | /
|
|
|
|
// | /
|
|
|
|
// 8 Blocks 3 Blocks
|
|
|
|
// | /
|
|
|
|
// | /
|
|
|
|
// +---+ +---+ +---+
|
|
|
|
// | 11| | 11| | 11|
|
|
|
|
// +---+ +---+ +---+
|
|
|
|
// \ | /
|
|
|
|
// \ | /
|
|
|
|
// +---+ +---+
|
|
|
|
// | 10| | 10|
|
|
|
|
// +---+ +---+
|
|
|
|
// \ |
|
|
|
|
// \ |
|
|
|
|
// +---+
|
|
|
|
// | 9 |
|
|
|
|
// +---+
|
|
|
|
// |
|
|
|
|
// |
|
|
|
|
// 7 Blocks
|
|
|
|
// |
|
|
|
|
// |
|
|
|
|
// +---+
|
2021-08-20 06:56:37 +00:00
|
|
|
// tail----->| 1 |
|
|
|
|
// +---+
|
2021-08-16 10:12:34 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
const blocks: Block[][] = [];
|
|
|
|
|
|
|
|
const firstSeg = await insertNDummyBlocks(db, 9);
|
|
|
|
const secondSeg = await insertNDummyBlocks(db, 2, _.last(firstSeg));
|
|
|
|
const thirdSeg = await insertNDummyBlocks(db, 1, _.last(firstSeg));
|
|
|
|
const fourthSeg = await insertNDummyBlocks(db, 11, _.last(thirdSeg));
|
|
|
|
const fifthSeg = await insertNDummyBlocks(db, 5, _.last(thirdSeg));
|
|
|
|
|
|
|
|
blocks.push(firstSeg);
|
|
|
|
blocks.push(secondSeg);
|
|
|
|
blocks.push(thirdSeg);
|
|
|
|
blocks.push(fourthSeg);
|
|
|
|
blocks.push(fifthSeg);
|
|
|
|
|
|
|
|
return blocks;
|
|
|
|
};
|
|
|
|
|
2021-08-20 06:56:37 +00:00
|
|
|
export const insertDummyToken = async (db: Database, block: Block, token?: Token): Promise<Token> => {
|
2021-08-16 10:12:34 +00:00
|
|
|
// Insert a dummy Token entity at block.
|
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
const randomByte = ethers.utils.randomBytes(20);
|
|
|
|
const tokenAddress = ethers.utils.hexValue(randomByte);
|
|
|
|
|
|
|
|
token = new Token();
|
|
|
|
token.symbol = 'TEST';
|
|
|
|
token.name = 'TestToken';
|
|
|
|
token.id = tokenAddress;
|
|
|
|
token.totalSupply = new Decimal(0);
|
|
|
|
token.decimals = BigInt(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const dbTx = await db.createTransactionRunner();
|
|
|
|
|
|
|
|
try {
|
|
|
|
token = await db.saveToken(dbTx, token, block);
|
|
|
|
dbTx.commitTransaction();
|
|
|
|
return token;
|
|
|
|
} catch (error) {
|
|
|
|
await dbTx.rollbackTransaction();
|
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
await dbTx.release();
|
|
|
|
}
|
|
|
|
};
|