Implemented query resolvers for entities Burn and Factory. (#171)

Co-authored-by: nabarun <nabarun@deepstacksoft.com>
This commit is contained in:
Ashwin Phatak 2021-07-29 18:12:35 +05:30 committed by GitHub
parent 175fa48d71
commit d4a19d15a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 17 deletions

View File

@ -116,8 +116,7 @@ export class Indexer {
if (this._serverMode === ETH_CALL_MODE) {
const contract = new ethers.Contract(token, this._abi, this._ethProvider);
const { block } = await this._ethClient.getBlockByHash(blockHash);
const { number } = block;
const { block: { number } } = await this._ethClient.getBlockByHash(blockHash);
const blockNumber = BigNumber.from(number).toNumber();
// eth_call doesnt support calling method by blockHash https://eth.wiki/json-rpc/API#the-default-block-parameter
@ -154,8 +153,7 @@ export class Indexer {
if (this._serverMode === ETH_CALL_MODE) {
const contract = new ethers.Contract(token, this._abi, this._ethProvider);
const { block } = await this._ethClient.getBlockByHash(blockHash);
const { number } = block;
const { block: { number } } = await this._ethClient.getBlockByHash(blockHash);
const blockNumber = BigNumber.from(number).toNumber();
const value = await contract.allowance(owner, spender, { blockTag: blockNumber });

View File

@ -25,6 +25,17 @@ import { BlockProgress } from './entity/BlockProgress';
import { Block } from './events';
import { SyncStatus } from './entity/SyncStatus';
export enum OrderDirection {
asc = 'asc',
desc = 'desc'
}
export interface QueryOptions {
limit?: number;
orderBy?: string;
orderDirection?: OrderDirection;
}
export class Database {
_config: ConnectionOptions
_conn!: Connection
@ -343,7 +354,7 @@ export class Database {
return entity;
}
async getFactories ({ blockHash }: DeepPartial<Factory>, queryOptions: { [key: string]: any }): Promise<Array<Factory>> {
async getFactories ({ blockHash, blockNumber }: Partial<Factory>, queryOptions: QueryOptions): Promise<Array<Factory>> {
const repo = this._conn.getRepository(Factory);
let selectQueryBuilder = repo.createQueryBuilder('factory')
@ -359,6 +370,10 @@ export class Database {
.orWhere('block_number <= :canonicalBlockNumber', { canonicalBlockNumber });
}
if (blockNumber) {
selectQueryBuilder = selectQueryBuilder.where('block_number <= :blockNumber', { blockNumber });
}
const { limit } = queryOptions;
if (limit) {
@ -368,7 +383,7 @@ export class Database {
return selectQueryBuilder.getMany();
}
async getBundles ({ blockHash, blockNumber }: DeepPartial<Bundle>, queryOptions: { [key: string]: any }): Promise<Array<Bundle>> {
async getBundles ({ blockHash, blockNumber }: Partial<Bundle>, queryOptions: QueryOptions): Promise<Array<Bundle>> {
const repo = this._conn.getRepository(Bundle);
let selectQueryBuilder = repo.createQueryBuilder('bundle')
@ -397,6 +412,32 @@ export class Database {
return selectQueryBuilder.getMany();
}
async getBurns (where: Partial<Burn> = {}, queryOptions: QueryOptions): Promise<Array<Burn>> {
const repo = this._conn.getRepository(Burn);
let selectQueryBuilder = repo.createQueryBuilder('burn')
.distinctOn(['burn.id'])
.orderBy('burn.id')
.addOrderBy('burn.block_number', 'DESC')
.innerJoinAndSelect('burn.pool', 'pool');
Object.entries(where).forEach(([field, value]) => {
selectQueryBuilder = selectQueryBuilder.andWhere(`burn.${field} = :value`, { value });
});
const { limit, orderBy, orderDirection } = queryOptions;
if (limit) {
selectQueryBuilder = selectQueryBuilder.limit(limit);
}
if (orderBy) {
selectQueryBuilder = selectQueryBuilder.addOrderBy(orderBy, orderDirection === 'desc' ? 'DESC' : 'ASC');
}
return selectQueryBuilder.getMany();
}
async saveFactory (factory: Factory, block: Block): Promise<Factory> {
return this._conn.transaction(async (tx) => {
const repo = tx.getRepository(Factory);

View File

@ -14,7 +14,7 @@ import { convertTokenToDecimal, loadTransaction, safeDiv } from './utils';
import { createTick } from './utils/tick';
import Decimal from 'decimal.js';
import { Position } from './entity/Position';
import { Database } from './database';
import { Database, QueryOptions } from './database';
import { Event } from './entity/Event';
import { ResultEvent, Block, Transaction, PoolCreatedEvent, InitializeEvent, MintEvent, BurnEvent, SwapEvent, IncreaseLiquidityEvent, DecreaseLiquidityEvent, CollectEvent, TransferEvent } from './events';
import { Factory } from './entity/Factory';
@ -193,8 +193,16 @@ export class Indexer {
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 getBundles (block: BlockHeight, queryOptions: QueryOptions): Promise<Bundle[]> {
return this._db.getBundles({ blockHash: block.hash, blockNumber: block.number }, queryOptions);
}
async getBurns (where: Partial<Burn>, queryOptions: QueryOptions): Promise<Burn[]> {
return this._db.getBurns(where, queryOptions);
}
async getFactories (block: BlockHeight, queryOptions: QueryOptions): Promise<Factory[]> {
return this._db.getFactories({ blockHash: block.hash, blockNumber: block.number }, queryOptions);
}
async _fetchAndSaveEvents (block: Block): Promise<void> {

View File

@ -4,14 +4,10 @@ import BigInt from 'apollo-type-bigint';
import { Data, Entity, NO_OF_BLOCKS } from './data';
import { BlockHeight } from '../resolvers';
import { OrderDirection } from '../database';
const log = debug('vulcanize:test');
enum OrderDirection {
asc,
desc
}
enum BurnOrderBy {
timestamp
}

View File

@ -3,9 +3,13 @@ import BigInt from 'apollo-type-bigint';
import debug from 'debug';
import { Indexer } from './indexer';
import { Burn } from './entity/Burn';
import { OrderDirection } from './database';
const log = debug('vulcanize:resolver');
const DEFAULT_LIMIT = 100;
export interface BlockHeight {
number?: number;
hash?: string;
@ -24,10 +28,22 @@ export const createResolvers = async (indexer: Indexer): Promise<any> => {
return indexer.getBundle(id, block);
},
bundles: async (_: any, { first, block = {} }: { first: string, block: BlockHeight }) => {
log('bundles', first, block);
bundles: async (_: any, { block = {}, first = DEFAULT_LIMIT }: { first: number, block: BlockHeight }) => {
log('bundles', block, first);
return indexer.getBundles(first, block);
return indexer.getBundles(block, { limit: first });
},
burns: async (_: any, { first = DEFAULT_LIMIT, orderBy, orderDirection, where }: { first: number, orderBy: string, orderDirection: OrderDirection, where: Partial<Burn> }) => {
log('burns', first, orderBy, orderDirection, where);
return indexer.getBurns(where, { limit: first, orderBy, orderDirection });
},
factories: async (_: any, { block = {}, first = DEFAULT_LIMIT }: { first: number, block: BlockHeight }) => {
log('factories', block, first);
return indexer.getFactories(block, { limit: first });
}
}
};