sushiswap-watcher-ts/packages/v2-watcher/src/resolvers.ts
Prathamesh Musale b1b79c844a Generate watcher for sushiswap v2 subgraph (#2)
Part of [Generate watchers for sushiswap subgraphs deployed in graph-node](https://www.notion.so/Generate-watchers-for-sushiswap-subgraphs-deployed-in-graph-node-b3f2e475373d4ab1887d9f8720bd5ae6)

Reviewed-on: cerc-io/sushiswap-watcher-ts#2
Co-authored-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
Co-committed-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
2024-06-12 04:15:03 +00:00

947 lines
30 KiB
TypeScript

//
// Copyright 2021 Vulcanize, Inc.
//
import assert from 'assert';
import debug from 'debug';
import { GraphQLResolveInfo } from 'graphql';
import { ExpressContext } from 'apollo-server-express';
import winston from 'winston';
import {
gqlTotalQueryCount,
gqlQueryCount,
gqlQueryDuration,
getResultState,
IndexerInterface,
GraphQLBigInt,
GraphQLBigDecimal,
BlockHeight,
OrderDirection,
jsonBigIntStringReplacer,
EventWatcher,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
setGQLCacheHints
} from '@cerc-io/util';
import { Indexer } from './indexer';
import { UniswapFactory } from './entity/UniswapFactory';
import { Token } from './entity/Token';
import { Pair } from './entity/Pair';
import { User } from './entity/User';
import { LiquidityPosition } from './entity/LiquidityPosition';
import { LiquidityPositionSnapshot } from './entity/LiquidityPositionSnapshot';
import { Transaction } from './entity/Transaction';
import { Mint } from './entity/Mint';
import { Burn } from './entity/Burn';
import { Swap } from './entity/Swap';
import { Bundle } from './entity/Bundle';
import { UniswapDayData } from './entity/UniswapDayData';
import { PairHourData } from './entity/PairHourData';
import { PairDayData } from './entity/PairDayData';
import { TokenDayData } from './entity/TokenDayData';
const log = debug('vulcanize:resolver');
const executeAndRecordMetrics = async (
indexer: Indexer,
gqlLogger: winston.Logger,
opName: string,
expressContext: ExpressContext,
operation: () => Promise<any>
) => {
gqlTotalQueryCount.inc(1);
gqlQueryCount.labels(opName).inc(1);
const endTimer = gqlQueryDuration.labels(opName).startTimer();
try {
const [result, syncStatus] = await Promise.all([
operation(),
indexer.getSyncStatus()
]);
gqlLogger.info({
opName,
query: expressContext.req.body.query,
variables: expressContext.req.body.variables,
latestIndexedBlockNumber: syncStatus?.latestIndexedBlockNumber,
urlPath: expressContext.req.path,
apiKey: expressContext.req.header('x-api-key'),
origin: expressContext.req.headers.origin
});
return result;
} catch (error) {
gqlLogger.error({
opName,
error,
query: expressContext.req.body.query,
variables: expressContext.req.body.variables,
urlPath: expressContext.req.path,
apiKey: expressContext.req.header('x-api-key'),
origin: expressContext.req.headers.origin
});
throw error;
} finally {
endTimer();
}
};
export const createResolvers = async (
indexerArg: IndexerInterface,
eventWatcher: EventWatcher,
gqlLogger: winston.Logger
): Promise<any> => {
const indexer = indexerArg as Indexer;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const gqlCacheConfig = indexer.serverConfig.gql.cache;
return {
BigInt: GraphQLBigInt,
BigDecimal: GraphQLBigDecimal,
Event: {
__resolveType: (obj: any) => {
assert(obj.__typename);
return obj.__typename;
}
},
Subscription: {
onEvent: {
subscribe: () => eventWatcher.getEventIterator()
}
},
Mutation: {
watchContract: async (_: any, { address, kind, checkpoint, startingBlock = 1 }: { address: string, kind: string, checkpoint: boolean, startingBlock: number }): Promise<boolean> => {
log('watchContract', address, kind, checkpoint, startingBlock);
await indexer.watchContract(address, kind, checkpoint, startingBlock);
return true;
}
},
Query: {
uniswapFactory: async (
_: any,
{ id, block = {} }: { id: string, block: BlockHeight },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('uniswapFactory', id, JSON.stringify(block, jsonBigIntStringReplacer));
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'uniswapFactory',
expressContext,
async () => indexer.getSubgraphEntity(UniswapFactory, id, block, info)
);
},
uniswapFactories: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('uniswapFactories', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'uniswapFactories',
expressContext,
async () => indexer.getSubgraphEntities(
UniswapFactory,
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info
)
);
},
token: async (
_: any,
{ id, block = {} }: { id: string, block: BlockHeight },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('token', id, JSON.stringify(block, jsonBigIntStringReplacer));
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'token',
expressContext,
async () => indexer.getSubgraphEntity(Token, id, block, info)
);
},
tokens: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('tokens', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'tokens',
expressContext,
async () => indexer.getSubgraphEntities(
Token,
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info
)
);
},
pair: async (
_: any,
{ id, block = {} }: { id: string, block: BlockHeight },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('pair', id, JSON.stringify(block, jsonBigIntStringReplacer));
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'pair',
expressContext,
async () => indexer.getSubgraphEntity(Pair, id, block, info)
);
},
pairs: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('pairs', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'pairs',
expressContext,
async () => indexer.getSubgraphEntities(
Pair,
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info
)
);
},
user: async (
_: any,
{ id, block = {} }: { id: string, block: BlockHeight },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('user', id, JSON.stringify(block, jsonBigIntStringReplacer));
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'user',
expressContext,
async () => indexer.getSubgraphEntity(User, id, block, info)
);
},
users: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('users', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'users',
expressContext,
async () => indexer.getSubgraphEntities(
User,
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info
)
);
},
liquidityPosition: async (
_: any,
{ id, block = {} }: { id: string, block: BlockHeight },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('liquidityPosition', id, JSON.stringify(block, jsonBigIntStringReplacer));
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'liquidityPosition',
expressContext,
async () => indexer.getSubgraphEntity(LiquidityPosition, id, block, info)
);
},
liquidityPositions: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('liquidityPositions', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'liquidityPositions',
expressContext,
async () => indexer.getSubgraphEntities(
LiquidityPosition,
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info
)
);
},
liquidityPositionSnapshot: async (
_: any,
{ id, block = {} }: { id: string, block: BlockHeight },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('liquidityPositionSnapshot', id, JSON.stringify(block, jsonBigIntStringReplacer));
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'liquidityPositionSnapshot',
expressContext,
async () => indexer.getSubgraphEntity(LiquidityPositionSnapshot, id, block, info)
);
},
liquidityPositionSnapshots: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('liquidityPositionSnapshots', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'liquidityPositionSnapshots',
expressContext,
async () => indexer.getSubgraphEntities(
LiquidityPositionSnapshot,
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info
)
);
},
transaction: async (
_: any,
{ id, block = {} }: { id: string, block: BlockHeight },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('transaction', id, JSON.stringify(block, jsonBigIntStringReplacer));
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'transaction',
expressContext,
async () => indexer.getSubgraphEntity(Transaction, id, block, info)
);
},
transactions: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('transactions', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'transactions',
expressContext,
async () => indexer.getSubgraphEntities(
Transaction,
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info
)
);
},
mint: async (
_: any,
{ id, block = {} }: { id: string, block: BlockHeight },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('mint', id, JSON.stringify(block, jsonBigIntStringReplacer));
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'mint',
expressContext,
async () => indexer.getSubgraphEntity(Mint, id, block, info)
);
},
mints: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('mints', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'mints',
expressContext,
async () => indexer.getSubgraphEntities(
Mint,
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info
)
);
},
burn: async (
_: any,
{ id, block = {} }: { id: string, block: BlockHeight },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('burn', id, JSON.stringify(block, jsonBigIntStringReplacer));
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'burn',
expressContext,
async () => indexer.getSubgraphEntity(Burn, id, block, info)
);
},
burns: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('burns', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'burns',
expressContext,
async () => indexer.getSubgraphEntities(
Burn,
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info
)
);
},
swap: async (
_: any,
{ id, block = {} }: { id: string, block: BlockHeight },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('swap', id, JSON.stringify(block, jsonBigIntStringReplacer));
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'swap',
expressContext,
async () => indexer.getSubgraphEntity(Swap, id, block, info)
);
},
swaps: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('swaps', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'swaps',
expressContext,
async () => indexer.getSubgraphEntities(
Swap,
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info
)
);
},
bundle: async (
_: any,
{ id, block = {} }: { id: string, block: BlockHeight },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('bundle', id, JSON.stringify(block, jsonBigIntStringReplacer));
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'bundle',
expressContext,
async () => indexer.getSubgraphEntity(Bundle, id, block, info)
);
},
bundles: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('bundles', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'bundles',
expressContext,
async () => indexer.getSubgraphEntities(
Bundle,
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info
)
);
},
uniswapDayData: async (
_: any,
{ id, block = {} }: { id: string, block: BlockHeight },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('uniswapDayData', id, JSON.stringify(block, jsonBigIntStringReplacer));
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'uniswapDayData',
expressContext,
async () => indexer.getSubgraphEntity(UniswapDayData, id, block, info)
);
},
uniswapDayDatas: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('uniswapDayDatas', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'uniswapDayDatas',
expressContext,
async () => indexer.getSubgraphEntities(
UniswapDayData,
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info
)
);
},
pairHourData: async (
_: any,
{ id, block = {} }: { id: string, block: BlockHeight },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('pairHourData', id, JSON.stringify(block, jsonBigIntStringReplacer));
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'pairHourData',
expressContext,
async () => indexer.getSubgraphEntity(PairHourData, id, block, info)
);
},
pairHourDatas: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('pairHourDatas', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'pairHourDatas',
expressContext,
async () => indexer.getSubgraphEntities(
PairHourData,
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info
)
);
},
pairDayData: async (
_: any,
{ id, block = {} }: { id: string, block: BlockHeight },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('pairDayData', id, JSON.stringify(block, jsonBigIntStringReplacer));
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'pairDayData',
expressContext,
async () => indexer.getSubgraphEntity(PairDayData, id, block, info)
);
},
pairDayDatas: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('pairDayDatas', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'pairDayDatas',
expressContext,
async () => indexer.getSubgraphEntities(
PairDayData,
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info
)
);
},
tokenDayData: async (
_: any,
{ id, block = {} }: { id: string, block: BlockHeight },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('tokenDayData', id, JSON.stringify(block, jsonBigIntStringReplacer));
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'tokenDayData',
expressContext,
async () => indexer.getSubgraphEntity(TokenDayData, id, block, info)
);
},
tokenDayDatas: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
expressContext: ExpressContext,
info: GraphQLResolveInfo
) => {
log('tokenDayDatas', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'tokenDayDatas',
expressContext,
async () => indexer.getSubgraphEntities(
TokenDayData,
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info
)
);
},
events: async (
_: any,
{ blockHash, contractAddress, name }: { blockHash: string, contractAddress: string, name?: string },
expressContext: ExpressContext
) => {
log('events', blockHash, contractAddress, name);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'events',
expressContext,
async () => {
const block = await indexer.getBlockProgress(blockHash);
if (!block || !block.isComplete) {
throw new Error(`Block hash ${blockHash} number ${block?.blockNumber} not processed yet`);
}
const events = await indexer.getEventsByFilter(blockHash, contractAddress, name);
return events.map(event => indexer.getResultEvent(event));
}
);
},
eventsInRange: async (
_: any,
{ fromBlockNumber, toBlockNumber }: { fromBlockNumber: number, toBlockNumber: number },
expressContext: ExpressContext
) => {
log('eventsInRange', fromBlockNumber, toBlockNumber);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'eventsInRange',
expressContext,
async () => {
const syncStatus = await indexer.getSyncStatus();
if (!syncStatus) {
throw new Error('No blocks processed yet');
}
if ((fromBlockNumber < syncStatus.initialIndexedBlockNumber) || (toBlockNumber > syncStatus.latestProcessedBlockNumber)) {
throw new Error(`Block range should be between ${syncStatus.initialIndexedBlockNumber} and ${syncStatus.latestProcessedBlockNumber}`);
}
const events = await indexer.getEventsInRange(fromBlockNumber, toBlockNumber);
return events.map(event => indexer.getResultEvent(event));
}
);
},
getStateByCID: async (
_: any,
{ cid }: { cid: string },
expressContext: ExpressContext
) => {
log('getStateByCID', cid);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'getStateByCID',
expressContext,
async () => {
const state = await indexer.getStateByCID(cid);
return state && state.block.isComplete ? getResultState(state) : undefined;
}
);
},
getState: async (
_: any,
{ blockHash, contractAddress, kind }: { blockHash: string, contractAddress: string, kind: string },
expressContext: ExpressContext
) => {
log('getState', blockHash, contractAddress, kind);
return executeAndRecordMetrics(
indexer,
gqlLogger,
'getState',
expressContext,
async () => {
const state = await indexer.getPrevState(blockHash, contractAddress, kind);
return state && state.block.isComplete ? getResultState(state) : undefined;
}
);
},
_meta: async (
_: any,
{ block = {} }: { block: BlockHeight },
expressContext: ExpressContext
) => {
log('_meta');
return executeAndRecordMetrics(
indexer,
gqlLogger,
'_meta',
expressContext,
async () => indexer.getMetaData(block)
);
},
getSyncStatus: async (
_: any,
__: Record<string, never>,
expressContext: ExpressContext
) => {
log('getSyncStatus');
return executeAndRecordMetrics(
indexer,
gqlLogger,
'getSyncStatus',
expressContext,
async () => indexer.getSyncStatus()
);
}
}
};
};