Implement resolvers for querying Bundle entity. (#170)

Co-authored-by: nabarun <nabarun@deepstacksoft.com>
This commit is contained in:
Ashwin Phatak 2021-07-29 14:45:38 +05:30 committed by GitHub
parent f93c0e3cb6
commit df01b6539b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 7 deletions

View File

@ -71,7 +71,7 @@ export class Database {
return entity; return entity;
} }
async getBundle ({ id, blockHash }: DeepPartial<Bundle>): Promise<Bundle | undefined> { async getBundle ({ id, blockHash, blockNumber }: DeepPartial<Bundle>): Promise<Bundle | undefined> {
const repo = this._conn.getRepository(Bundle); const repo = this._conn.getRepository(Bundle);
const whereOptions: FindConditions<Bundle> = { id }; const whereOptions: FindConditions<Bundle> = { id };
@ -79,6 +79,10 @@ export class Database {
whereOptions.blockHash = blockHash; whereOptions.blockHash = blockHash;
} }
if (blockNumber) {
whereOptions.blockNumber = LessThanOrEqual(blockNumber);
}
const findOptions = { const findOptions = {
where: whereOptions, where: whereOptions,
order: { order: {
@ -364,6 +368,35 @@ export class Database {
return selectQueryBuilder.getMany(); return selectQueryBuilder.getMany();
} }
async getBundles ({ blockHash, blockNumber }: DeepPartial<Bundle>, queryOptions: { [key: string]: any }): Promise<Array<Bundle>> {
const repo = this._conn.getRepository(Bundle);
let selectQueryBuilder = repo.createQueryBuilder('bundle')
.distinctOn(['id'])
.orderBy('id')
.addOrderBy('block_number', 'DESC');
if (blockHash) {
const { canonicalBlockNumber, blockHashes } = await this._getBranchInfo(blockHash);
selectQueryBuilder = selectQueryBuilder
.where('block_hash IN (:...blockHashes)', { blockHashes })
.orWhere('block_number <= :canonicalBlockNumber', { canonicalBlockNumber });
}
if (blockNumber) {
selectQueryBuilder = selectQueryBuilder.where('block_number <= :blockNumber', { blockNumber });
}
const { limit } = queryOptions;
if (limit) {
selectQueryBuilder = selectQueryBuilder.limit(limit);
}
return selectQueryBuilder.getMany();
}
async saveFactory (factory: Factory, block: Block): Promise<Factory> { async saveFactory (factory: Factory, block: Block): Promise<Factory> {
return this._conn.transaction(async (tx) => { return this._conn.transaction(async (tx) => {
const repo = tx.getRepository(Factory); const repo = tx.getRepository(Factory);

View File

@ -26,6 +26,7 @@ import { Swap } from './entity/Swap';
import { PositionSnapshot } from './entity/PositionSnapshot'; import { PositionSnapshot } from './entity/PositionSnapshot';
import { SyncStatus } from './entity/SyncStatus'; import { SyncStatus } from './entity/SyncStatus';
import { BlockProgress } from './entity/BlockProgress'; import { BlockProgress } from './entity/BlockProgress';
import { BlockHeight } from './resolvers';
const log = debug('vulcanize:indexer'); const log = debug('vulcanize:indexer');
@ -188,6 +189,14 @@ export class Indexer {
return this._db.updateBlockProgress(blockHash, lastProcessedEventIndex); return this._db.updateBlockProgress(blockHash, lastProcessedEventIndex);
} }
async getBundle (id: string, block: BlockHeight): Promise<Bundle | undefined> {
return this._db.getBundle({ id, blockHash: block.hash });
}
async getBundles (first: string, block: BlockHeight): Promise<Bundle[]> {
return this._db.getBundles({ blockHash: block.hash, blockNumber: block.number }, { limit: first });
}
async _fetchAndSaveEvents (block: Block): Promise<void> { async _fetchAndSaveEvents (block: Block): Promise<void> {
const events = await this._uniClient.getEvents(block.hash); const events = await this._uniClient.getEvents(block.hash);
const dbEvents: Array<DeepPartial<Event>> = []; const dbEvents: Array<DeepPartial<Event>> = [];

View File

@ -1,15 +1,12 @@
/* eslint-disable camelcase */ /* eslint-disable camelcase */
import debug from 'debug'; import debug from 'debug';
import BigInt from 'apollo-type-bigint'; import BigInt from 'apollo-type-bigint';
import { Data, Entity, NO_OF_BLOCKS } from './data'; import { Data, Entity, NO_OF_BLOCKS } from './data';
import { BlockHeight } from '../resolvers';
const log = debug('vulcanize:test'); const log = debug('vulcanize:test');
interface BlockHeight {
number: number;
hash: string;
}
enum OrderDirection { enum OrderDirection {
asc, asc,
desc desc

View File

@ -1,14 +1,34 @@
import assert from 'assert'; import assert from 'assert';
import BigInt from 'apollo-type-bigint'; import BigInt from 'apollo-type-bigint';
import debug from 'debug';
import { Indexer } from './indexer'; import { Indexer } from './indexer';
const log = debug('vulcanize:resolver');
export interface BlockHeight {
number?: number;
hash?: string;
}
export const createResolvers = async (indexer: Indexer): Promise<any> => { export const createResolvers = async (indexer: Indexer): Promise<any> => {
assert(indexer); assert(indexer);
return { return {
BigInt: new BigInt('bigInt'), BigInt: new BigInt('bigInt'),
Query: {} Query: {
bundle: async (_: any, { id, block = {} }: { id: string, block: BlockHeight }) => {
log('bundle', id, block);
return indexer.getBundle(id, block);
},
bundles: async (_: any, { first, block = {} }: { first: string, block: BlockHeight }) => {
log('bundles', first, block);
return indexer.getBundles(first, block);
}
}
}; };
}; };