mirror of
https://github.com/cerc-io/watcher-ts
synced 2024-11-19 20:36:19 +00:00
Implement resolvers for querying Bundle entity. (#170)
Co-authored-by: nabarun <nabarun@deepstacksoft.com>
This commit is contained in:
parent
f93c0e3cb6
commit
df01b6539b
@ -71,7 +71,7 @@ export class Database {
|
||||
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 whereOptions: FindConditions<Bundle> = { id };
|
||||
|
||||
@ -79,6 +79,10 @@ export class Database {
|
||||
whereOptions.blockHash = blockHash;
|
||||
}
|
||||
|
||||
if (blockNumber) {
|
||||
whereOptions.blockNumber = LessThanOrEqual(blockNumber);
|
||||
}
|
||||
|
||||
const findOptions = {
|
||||
where: whereOptions,
|
||||
order: {
|
||||
@ -364,6 +368,35 @@ export class Database {
|
||||
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> {
|
||||
return this._conn.transaction(async (tx) => {
|
||||
const repo = tx.getRepository(Factory);
|
||||
|
@ -26,6 +26,7 @@ import { Swap } from './entity/Swap';
|
||||
import { PositionSnapshot } from './entity/PositionSnapshot';
|
||||
import { SyncStatus } from './entity/SyncStatus';
|
||||
import { BlockProgress } from './entity/BlockProgress';
|
||||
import { BlockHeight } from './resolvers';
|
||||
|
||||
const log = debug('vulcanize:indexer');
|
||||
|
||||
@ -188,6 +189,14 @@ export class Indexer {
|
||||
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> {
|
||||
const events = await this._uniClient.getEvents(block.hash);
|
||||
const dbEvents: Array<DeepPartial<Event>> = [];
|
||||
|
@ -1,15 +1,12 @@
|
||||
/* eslint-disable camelcase */
|
||||
import debug from 'debug';
|
||||
import BigInt from 'apollo-type-bigint';
|
||||
|
||||
import { Data, Entity, NO_OF_BLOCKS } from './data';
|
||||
import { BlockHeight } from '../resolvers';
|
||||
|
||||
const log = debug('vulcanize:test');
|
||||
|
||||
interface BlockHeight {
|
||||
number: number;
|
||||
hash: string;
|
||||
}
|
||||
|
||||
enum OrderDirection {
|
||||
asc,
|
||||
desc
|
||||
|
@ -1,14 +1,34 @@
|
||||
import assert from 'assert';
|
||||
import BigInt from 'apollo-type-bigint';
|
||||
import debug from 'debug';
|
||||
|
||||
import { Indexer } from './indexer';
|
||||
|
||||
const log = debug('vulcanize:resolver');
|
||||
|
||||
export interface BlockHeight {
|
||||
number?: number;
|
||||
hash?: string;
|
||||
}
|
||||
|
||||
export const createResolvers = async (indexer: Indexer): Promise<any> => {
|
||||
assert(indexer);
|
||||
|
||||
return {
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user