Skip to content

Commit

Permalink
Improve frontend GQL queries (#390)
Browse files Browse the repository at this point in the history
* Add indexes to swap table and use latest entity table for single pool query

* Add latest entity table for tick entity
  • Loading branch information
nikugogoi authored Nov 11, 2022
1 parent b3c8de9 commit 37b8a25
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
50 changes: 47 additions & 3 deletions packages/uni-info-watcher/src/custom-indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { SelectionNode } from 'graphql';
import assert from 'assert';
import { Brackets, QueryRunner, Repository, SelectQueryBuilder } from 'typeorm';
import { Brackets, FindConditions, LessThanOrEqual, QueryRunner, Repository, SelectQueryBuilder } from 'typeorm';

import { OPERATOR_MAP, Config, QueryOptions, Where, BlockHeight } from '@cerc-io/util';
import { ENTITY_QUERY_TYPE, resolveEntityFieldConflicts } from '@cerc-io/graph-node';
Expand All @@ -23,6 +23,8 @@ import { TokenHourData } from './entity/TokenHourData';
import { LatestTokenHourData } from './entity/LatestTokenHourData';
import { PoolDayData } from './entity/PoolDayData';
import { LatestPoolDayData } from './entity/LatestPoolDayData';
import { Tick } from './entity/Tick';
import { LatestTick } from './entity/LatestTick';

export const entityToLatestEntityMap: Map<any, any> = new Map();
entityToLatestEntityMap.set(Pool, LatestPool);
Expand All @@ -31,6 +33,7 @@ entityToLatestEntityMap.set(UniswapDayData, LatestUniswapDayData);
entityToLatestEntityMap.set(TokenDayData, LatestTokenDayData);
entityToLatestEntityMap.set(TokenHourData, LatestTokenHourData);
entityToLatestEntityMap.set(PoolDayData, LatestPoolDayData);
entityToLatestEntityMap.set(Tick, LatestTick);

export class CustomIndexer {
_config: Config;
Expand All @@ -43,6 +46,47 @@ export class CustomIndexer {
this._indexer = indexer;
}

async getPool (id: string, block: BlockHeight, selections: ReadonlyArray<SelectionNode> = []): Promise<Pool | undefined> {
const dbTx = await this._db.createTransactionRunner();
let res;

try {
const repo = dbTx.manager.getRepository(Pool);
const whereOptions: FindConditions<Pool> = { id };

if (block.hash) {
whereOptions.blockHash = block.hash;
}

if (block.number) {
whereOptions.blockNumber = LessThanOrEqual(block.number);
}

let entity = await this._db.getModelEntity(repo, whereOptions);

if (entity) {
[entity] = await this.loadEntitiesRelations(
dbTx,
block,
this._db.relationsMap,
Pool,
[entity],
selections
);
}

res = entity;
await dbTx.commitTransaction();
} catch (error) {
await dbTx.rollbackTransaction();
throw error;
} finally {
await dbTx.release();
}

return res;
}

async getEntities<Entity> (
entity: new () => Entity,
block: BlockHeight,
Expand Down Expand Up @@ -146,7 +190,7 @@ export class CustomIndexer {
return [];
}

entities = await this.loadLatestEntitiesRelations(queryRunner, block, relationsMap, entity, entities, selections);
entities = await this.loadEntitiesRelations(queryRunner, block, relationsMap, entity, entities, selections);
// Resolve any field name conflicts in the entity result.
entities = entities.map(entity => resolveEntityFieldConflicts(entity));

Expand Down Expand Up @@ -271,7 +315,7 @@ export class CustomIndexer {
return selectQueryBuilder.getMany();
}

async loadLatestEntitiesRelations<Entity> (
async loadEntitiesRelations<Entity> (
queryRunner: QueryRunner,
block: BlockHeight,
relationsMap: Map<any, { [key: string]: any }>,
Expand Down
25 changes: 25 additions & 0 deletions packages/uni-info-watcher/src/entity/LatestTick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Copyright 2022 Vulcanize, Inc.
//

import { Entity, PrimaryColumn, Column, Index } from 'typeorm';
import { bigintTransformer } from '@vulcanize/util';

@Entity()
@Index(['id', 'blockHash'])
export class LatestTick {
@PrimaryColumn('varchar')
id!: string;

@Column('varchar', { length: 66 })
blockHash!: string;

@Column('integer')
blockNumber!: number;

@Column('numeric', { transformer: bigintTransformer })
tickIdx!: bigint;

@Column('varchar', { length: 42 })
poolAddress!: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Entity, PrimaryColumn, Column, Index } from 'typeorm';
@Entity()
@Index(['id', 'blockHash'])
export class LatestUniswapDayData {
@PrimaryColumn('varchar', { length: 42 })
@PrimaryColumn('varchar')
id!: string;

@Column('varchar', { length: 66 })
Expand Down
4 changes: 3 additions & 1 deletion packages/uni-info-watcher/src/entity/Swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { graphDecimalTransformer, GraphDecimal, bigintTransformer } from '@vulca
@Entity()
@Index(['id', 'blockNumber'])
@Index(['transaction'])
@Index(['timestamp'])
@Index(['token0', 'timestamp'])
@Index(['token1', 'timestamp'])
@Index(['pool', 'timestamp'])
export class Swap {
@PrimaryColumn('varchar')
id!: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/uni-info-watcher/src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export const createResolvers = async (indexer: Indexer, customIndexer: CustomInd
gqlQueryCount.labels('pool').inc(1);
assert(info.fieldNodes[0].selectionSet);

return indexer.getPool(id, block, info.fieldNodes[0].selectionSet.selections);
return customIndexer.getPool(id, block, info.fieldNodes[0].selectionSet.selections);
},

poolDayDatas: async (
Expand Down Expand Up @@ -259,7 +259,7 @@ export const createResolvers = async (indexer: Indexer, customIndexer: CustomInd
gqlQueryCount.labels('ticks').inc(1);
assert(info.fieldNodes[0].selectionSet);

return indexer.getEntities(
return customIndexer.getEntities(
Tick,
block,
where,
Expand Down

0 comments on commit 37b8a25

Please sign in to comment.