mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-12-04 18:54:08 +00:00
* Add ipld-blocks entity generation * Populate ipld-blocks table * Rename ipld-block entity and update after each event * Move ipld-hook to hooks.ts * Change IPLD block structure * Add cid field in blocks * Fetch prev. IPLDBlock for a contract * GQL API to query IPLDBlock by CID * Save cid in blocks in existing watchers * Update codegen docs * GQL API for getting last derived state (#3) * GQL API for getting last derived state * Rename query to getState * Change query names to getState and getStateByCid * Save BigInt as string * Move function to prepare IPLDBlock to indexer * Refactor IPLDBlock hook * Add genesis hook * Call post-block hook after a block is marked as complete * Add IPLDBlock checkpointing * Use queryRunner instead of a new repo for queries * Add a query to get block in ipld-eth-client * Get latest checkpoints for all contracts for checkpointing. * Call post-block hook in a queue * Pass server config to Indexer in watch-contract cli Co-authored-by: nikugogoi <nabarun@deepstacksoft.com>
97 lines
3.4 KiB
Handlebars
97 lines
3.4 KiB
Handlebars
//
|
|
// Copyright 2021 Vulcanize, Inc.
|
|
//
|
|
|
|
import assert from 'assert';
|
|
import BigInt from 'apollo-type-bigint';
|
|
import debug from 'debug';
|
|
|
|
import { ValueResult } from '@vulcanize/util';
|
|
|
|
import { Indexer } from './indexer';
|
|
import { EventWatcher } from './events';
|
|
|
|
const log = debug('vulcanize:resolver');
|
|
|
|
export const createResolvers = async (indexer: Indexer, eventWatcher: EventWatcher): Promise<any> => {
|
|
assert(indexer);
|
|
|
|
return {
|
|
BigInt: new BigInt('bigInt'),
|
|
|
|
Event: {
|
|
__resolveType: (obj: any) => {
|
|
assert(obj.__typename);
|
|
|
|
return obj.__typename;
|
|
}
|
|
},
|
|
|
|
Subscription: {
|
|
onEvent: {
|
|
subscribe: () => eventWatcher.getEventIterator()
|
|
}
|
|
},
|
|
|
|
Mutation: {
|
|
watchContract: (_: any, { contractAddress, kind, startingBlock = 1 }: { contractAddress: string, kind: string, startingBlock: number }): Promise<boolean> => {
|
|
log('watchContract', contractAddress, kind, startingBlock);
|
|
return indexer.watchContract(contractAddress, kind, startingBlock);
|
|
}
|
|
},
|
|
|
|
Query: {
|
|
{{#each queries}}
|
|
{{this.name}}: (_: any, { blockHash, contractAddress
|
|
{{~#each this.params}}, {{this.name~}} {{/each}} }: { blockHash: string, contractAddress: string
|
|
{{~#each this.params}}, {{this.name}}: {{this.type~}} {{/each}} }): Promise<ValueResult> => {
|
|
log('{{this.name}}', blockHash, contractAddress
|
|
{{~#each this.params}}, {{this.name~}} {{/each}});
|
|
return indexer.{{this.name}}(blockHash, contractAddress
|
|
{{~#each this.params}}, {{this.name~}} {{/each}});
|
|
},
|
|
|
|
{{/each}}
|
|
events: async (_: any, { blockHash, contractAddress, name }: { blockHash: string, contractAddress: string, name?: string }) => {
|
|
log('events', blockHash, contractAddress, name);
|
|
|
|
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 }) => {
|
|
log('eventsInRange', fromBlockNumber, toBlockNumber);
|
|
|
|
const { expected, actual } = await indexer.getProcessedBlockCountForRange(fromBlockNumber, toBlockNumber);
|
|
if (expected !== actual) {
|
|
throw new Error(`Range not available, expected ${expected}, got ${actual} blocks in range`);
|
|
}
|
|
|
|
const events = await indexer.getEventsInRange(fromBlockNumber, toBlockNumber);
|
|
return events.map(event => indexer.getResultEvent(event));
|
|
},
|
|
|
|
getStateByCID: async (_: any, { cid }: { cid: string }) => {
|
|
log('getStateByCID', cid);
|
|
|
|
const ipldBlock = await indexer.getIPLDBlockByCid(cid);
|
|
|
|
return ipldBlock && ipldBlock.block.isComplete ? indexer.getResultIPLDBlock(ipldBlock) : undefined;
|
|
},
|
|
|
|
getState: async (_: any, { blockHash, contractAddress }: { blockHash: string, contractAddress: string }) => {
|
|
log('getState', blockHash, contractAddress);
|
|
|
|
const ipldBlock = await indexer.getPrevIPLDBlock(blockHash, contractAddress);
|
|
|
|
return ipldBlock && ipldBlock.block.isComplete ? indexer.getResultIPLDBlock(ipldBlock) : undefined;
|
|
}
|
|
}
|
|
};
|
|
};
|