From f53976e7b92b85def3e342013cc51ec22e757366 Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Wed, 23 Nov 2022 19:27:11 +0530 Subject: [PATCH 1/7] Use fill CLI from cli package --- packages/erc20-watcher/src/fill.ts | 91 +-------------------- packages/uni-info-watcher/src/fill.ts | 109 +++----------------------- packages/uni-watcher/src/fill.ts | 92 +--------------------- 3 files changed, 18 insertions(+), 274 deletions(-) diff --git a/packages/erc20-watcher/src/fill.ts b/packages/erc20-watcher/src/fill.ts index 08615cb6..6290b9b9 100644 --- a/packages/erc20-watcher/src/fill.ts +++ b/packages/erc20-watcher/src/fill.ts @@ -2,17 +2,10 @@ // Copyright 2021 Vulcanize, Inc. // -import assert from 'assert'; import 'reflect-metadata'; -import yargs from 'yargs'; -import { hideBin } from 'yargs/helpers'; import debug from 'debug'; -import { PubSub } from 'graphql-subscriptions'; -import { DEFAULT_CONFIG_PATH, JobQueue, getCustomProvider, getConfig, fillBlocks, DEFAULT_PREFETCH_BATCH_SIZE } from '@cerc-io/util'; -import { EthClient } from '@cerc-io/ipld-eth-client'; -import { getCache } from '@cerc-io/cache'; -import { Config } from '@vulcanize/util'; +import { FillCmd } from '@cerc-io/cli'; import { Database } from './database'; import { Indexer } from './indexer'; @@ -21,86 +14,10 @@ import { EventWatcher } from './events'; const log = debug('vulcanize:server'); export const main = async (): Promise => { - const argv = await yargs(hideBin(process.argv)).parserConfiguration({ - 'parse-numbers': false - }).env( - 'FILL' - ).options({ - configFile: { - alias: 'f', - type: 'string', - require: true, - demandOption: true, - describe: 'configuration file path (toml)', - default: DEFAULT_CONFIG_PATH - }, - startBlock: { - type: 'number', - require: true, - demandOption: true, - describe: 'Block number to start processing at' - }, - endBlock: { - type: 'number', - require: true, - demandOption: true, - describe: 'Block number to stop processing at' - }, - prefetch: { - type: 'boolean', - default: false, - describe: 'Block and events prefetch mode' - }, - batchBlocks: { - type: 'number', - default: DEFAULT_PREFETCH_BATCH_SIZE, - describe: 'Number of blocks prefetched in batch' - }, - blockCid: { - type: 'boolean', - default: false, - describe: 'Only fetch and update block CIDs' - } - }).argv; + const fillCmd = new FillCmd(); + await fillCmd.init(Database, Indexer, EventWatcher); - const config: Config = await getConfig(argv.configFile); - - assert(config.server, 'Missing server config'); - - const { upstream, database: dbConfig, jobQueue: jobQueueConfig } = config; - - assert(dbConfig, 'Missing database config'); - - const db = new Database(dbConfig); - await db.init(); - - assert(upstream, 'Missing upstream config'); - const { ethServer: { gqlApiEndpoint, rpcProviderEndpoint }, cache: cacheConfig } = upstream; - - const cache = await getCache(cacheConfig); - const ethClient = new EthClient({ - gqlEndpoint: gqlApiEndpoint, - cache - }); - - const ethProvider = getCustomProvider(rpcProviderEndpoint); - - // Note: In-memory pubsub works fine for now, as each watcher is a single process anyway. - // Later: https://www.apollographql.com/docs/apollo-server/data/subscriptions/#production-pubsub-libraries - const pubsub = new PubSub(); - - assert(jobQueueConfig, 'Missing job queue config'); - const { dbConnectionString, maxCompletionLagInSecs } = jobQueueConfig; - assert(dbConnectionString, 'Missing job queue db connection string'); - - const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); - await jobQueue.start(); - - const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); - - const eventWatcher = new EventWatcher(ethClient, indexer, pubsub, jobQueue); - - await fillBlocks(jobQueue, indexer, eventWatcher, jobQueueConfig.blockDelayInMilliSecs, argv); + await fillCmd.exec(); }; main().catch(err => { diff --git a/packages/uni-info-watcher/src/fill.ts b/packages/uni-info-watcher/src/fill.ts index 2a7bd219..dbcc449b 100644 --- a/packages/uni-info-watcher/src/fill.ts +++ b/packages/uni-info-watcher/src/fill.ts @@ -2,124 +2,35 @@ // Copyright 2021 Vulcanize, Inc. // -import assert from 'assert'; import 'reflect-metadata'; -import yargs from 'yargs'; -import { hideBin } from 'yargs/helpers'; import debug from 'debug'; -import { PubSub } from 'graphql-subscriptions'; -import { getCache } from '@cerc-io/cache'; -import { DEFAULT_CONFIG_PATH, JobQueue, getCustomProvider, getConfig, fillBlocks, DEFAULT_PREFETCH_BATCH_SIZE } from '@cerc-io/util'; +import { FillCmd } from '@cerc-io/cli'; import { Config } from '@vulcanize/util'; import { Client as UniClient } from '@vulcanize/uni-watcher'; import { Client as ERC20Client } from '@vulcanize/erc20-watcher'; -import { EthClient } from '@cerc-io/ipld-eth-client'; import { Database } from './database'; import { Indexer } from './indexer'; import { EventWatcher } from './events'; -import { fillState } from './cli/fill-state'; -const log = debug('vulcanize:server'); +const log = debug('vulcanize:fill'); export const main = async (): Promise => { - const argv = await yargs(hideBin(process.argv)).parserConfiguration({ - 'parse-numbers': false - }).env( - 'FILL' - ).options({ - configFile: { - alias: 'f', - type: 'string', - require: true, - demandOption: true, - describe: 'configuration file path (toml)', - default: DEFAULT_CONFIG_PATH - }, - startBlock: { - type: 'number', - require: true, - demandOption: true, - describe: 'Block number to start processing at' - }, - endBlock: { - type: 'number', - require: true, - demandOption: true, - describe: 'Block number to stop processing at' - }, - prefetch: { - type: 'boolean', - default: false, - describe: 'Block and events prefetch mode' - }, - batchBlocks: { - type: 'number', - default: DEFAULT_PREFETCH_BATCH_SIZE, - describe: 'Number of blocks prefetched in batch' - }, - state: { - type: 'boolean', - default: false, - describe: 'Fill state for subgraph entities' - }, - blockCid: { - type: 'boolean', - default: false, - describe: 'Only fetch and update block CIDs' - } - }).argv; + const fillCmd = new FillCmd(); - const config: Config = await getConfig(argv.configFile); - - assert(config.server, 'Missing server config'); - - const { upstream, database: dbConfig, jobQueue: jobQueueConfig } = config; - - assert(dbConfig, 'Missing database config'); - - const db = new Database(dbConfig, config.server); - await db.init(); - - assert(upstream, 'Missing upstream config'); - const { ethServer: { gqlApiEndpoint, rpcProviderEndpoint }, cache: cacheConfig, uniWatcher, tokenWatcher } = upstream; - - const cache = await getCache(cacheConfig); - - const ethClient = new EthClient({ - gqlEndpoint: gqlApiEndpoint, - cache - }); + const config: Config = await fillCmd.initConfig(); + const { + uniWatcher, + tokenWatcher + } = config.upstream; const uniClient = new UniClient(uniWatcher); const erc20Client = new ERC20Client(tokenWatcher); - const ethProvider = getCustomProvider(rpcProviderEndpoint); - - // Note: In-memory pubsub works fine for now, as each watcher is a single process anyway. - // Later: https://www.apollographql.com/docs/apollo-server/data/subscriptions/#production-pubsub-libraries - const pubsub = new PubSub(); - - assert(jobQueueConfig, 'Missing job queue config'); - const { dbConnectionString, maxCompletionLagInSecs } = jobQueueConfig; - assert(dbConnectionString, 'Missing job queue db connection string'); - - const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); - await jobQueue.start(); - - const indexer = new Indexer(config.server, db, { uniClient, erc20Client, ethClient }, ethProvider, jobQueue); - await indexer.init(); - - if (argv.state) { - assert(config.server.enableState, 'State creation disabled'); - await fillState(indexer, db, argv); - - return; - } - const eventWatcher = new EventWatcher(ethClient, indexer, pubsub, jobQueue); + await fillCmd.init(Database, Indexer, EventWatcher, { uniClient, erc20Client }); - await fillBlocks(jobQueue, indexer, eventWatcher, jobQueueConfig.blockDelayInMilliSecs, argv); + await fillCmd.exec(); }; main().catch(err => { diff --git a/packages/uni-watcher/src/fill.ts b/packages/uni-watcher/src/fill.ts index 2ec5de67..6c884c97 100644 --- a/packages/uni-watcher/src/fill.ts +++ b/packages/uni-watcher/src/fill.ts @@ -2,17 +2,10 @@ // Copyright 2021 Vulcanize, Inc. // -import assert from 'assert'; import 'reflect-metadata'; -import yargs from 'yargs'; -import { hideBin } from 'yargs/helpers'; import debug from 'debug'; -import { PubSub } from 'graphql-subscriptions'; -import { getCache } from '@cerc-io/cache'; -import { EthClient } from '@cerc-io/ipld-eth-client'; -import { DEFAULT_CONFIG_PATH, JobQueue, getCustomProvider, getConfig, fillBlocks, DEFAULT_PREFETCH_BATCH_SIZE } from '@cerc-io/util'; -import { Config } from '@vulcanize/util'; +import { FillCmd } from '@cerc-io/cli'; import { Database } from './database'; import { Indexer } from './indexer'; @@ -21,87 +14,10 @@ import { EventWatcher } from './events'; const log = debug('vulcanize:server'); export const main = async (): Promise => { - const argv = await yargs(hideBin(process.argv)).parserConfiguration({ - 'parse-numbers': false - }).env( - 'FILL' - ).options({ - configFile: { - alias: 'f', - type: 'string', - require: true, - demandOption: true, - describe: 'configuration file path (toml)', - default: DEFAULT_CONFIG_PATH - }, - startBlock: { - type: 'number', - require: true, - demandOption: true, - describe: 'Block number to start processing at' - }, - endBlock: { - type: 'number', - require: true, - demandOption: true, - describe: 'Block number to stop processing at' - }, - prefetch: { - type: 'boolean', - default: false, - describe: 'Block and events prefetch mode' - }, - batchBlocks: { - type: 'number', - default: DEFAULT_PREFETCH_BATCH_SIZE, - describe: 'Number of blocks prefetched in batch' - }, - blockCid: { - type: 'boolean', - default: false, - describe: 'Only fetch and update block CIDs' - } - }).argv; + const fillCmd = new FillCmd(); + await fillCmd.init(Database, Indexer, EventWatcher); - const config: Config = await getConfig(argv.configFile); - - assert(config.server, 'Missing server config'); - - const { upstream, database: dbConfig, jobQueue: jobQueueConfig } = config; - - assert(dbConfig, 'Missing database config'); - - const db = new Database(dbConfig); - await db.init(); - - assert(upstream, 'Missing upstream config'); - const { ethServer: { gqlApiEndpoint, rpcProviderEndpoint }, cache: cacheConfig } = upstream; - - const cache = await getCache(cacheConfig); - const ethClient = new EthClient({ - gqlEndpoint: gqlApiEndpoint, - cache - }); - - const ethProvider = getCustomProvider(rpcProviderEndpoint); - - // Note: In-memory pubsub works fine for now, as each watcher is a single process anyway. - // Later: https://www.apollographql.com/docs/apollo-server/data/subscriptions/#production-pubsub-libraries - const pubsub = new PubSub(); - - assert(jobQueueConfig, 'Missing job queue config'); - const { dbConnectionString, maxCompletionLagInSecs } = jobQueueConfig; - assert(dbConnectionString, 'Missing job queue db connection string'); - - const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); - await jobQueue.start(); - - const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); - await indexer.init(); - - const eventWatcher = new EventWatcher(ethClient, indexer, pubsub, jobQueue); - - await fillBlocks(jobQueue, indexer, eventWatcher, jobQueueConfig.blockDelayInMilliSecs, argv); + await fillCmd.exec(); }; main().catch(err => { From 3752587d8172af172fe3b7fc76f44ff8ba541cb1 Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Thu, 24 Nov 2022 12:07:09 +0530 Subject: [PATCH 2/7] Satisfy updated indexer interface --- packages/erc20-watcher/src/database.ts | 4 ++++ packages/erc20-watcher/src/indexer.ts | 9 +++++++++ packages/uni-info-watcher/src/database.ts | 10 +--------- packages/uni-info-watcher/src/indexer.ts | 8 ++++---- packages/uni-watcher/src/database.ts | 4 ++++ packages/uni-watcher/src/indexer.ts | 9 +++++++++ 6 files changed, 31 insertions(+), 13 deletions(-) diff --git a/packages/erc20-watcher/src/database.ts b/packages/erc20-watcher/src/database.ts index 9556d7e6..f2c3cfb2 100644 --- a/packages/erc20-watcher/src/database.ts +++ b/packages/erc20-watcher/src/database.ts @@ -291,6 +291,10 @@ export class Database implements DatabaseInterface { return this._baseDatabase.getBlockProgressEntities(repo, where, options); } + async getEntitiesForBlock (blockHash: string, tableName: string): Promise { + return this._baseDatabase.getEntitiesForBlock(blockHash, tableName); + } + async saveBlockProgress (queryRunner: QueryRunner, block: DeepPartial): Promise { const repo = queryRunner.manager.getRepository(BlockProgress); diff --git a/packages/erc20-watcher/src/indexer.ts b/packages/erc20-watcher/src/indexer.ts index 74e385b6..4a6743ab 100644 --- a/packages/erc20-watcher/src/indexer.ts +++ b/packages/erc20-watcher/src/indexer.ts @@ -129,6 +129,10 @@ export class Indexer implements IndexerInterface { ); } + async getEntitiesForBlock (blockHash: string, tableName: string): Promise { + return this._db.getEntitiesForBlock(blockHash, tableName); + } + getStateData (state: State): any { return this._baseIndexer.getStateData(state); } @@ -378,6 +382,11 @@ export class Indexer implements IndexerInterface { return undefined; } + async getStates (where: FindConditions): Promise { + // TODO Implement + return []; + } + // Method to be used by export-state CLI. async createCheckpoint (contractAddress: string, blockHash: string): Promise { // TODO Implement diff --git a/packages/uni-info-watcher/src/database.ts b/packages/uni-info-watcher/src/database.ts index 8a5842c5..011dfed9 100644 --- a/packages/uni-info-watcher/src/database.ts +++ b/packages/uni-info-watcher/src/database.ts @@ -554,15 +554,7 @@ export class Database implements DatabaseInterface { } async getEntitiesForBlock (blockHash: string, tableName: string): Promise { - const repo = this._conn.getRepository(tableName); - - const entities = await repo.find({ - where: { - blockHash - } - }); - - return entities; + return this._baseDatabase.getEntitiesForBlock(blockHash, tableName); } async getModelEntities ( diff --git a/packages/uni-info-watcher/src/indexer.ts b/packages/uni-info-watcher/src/indexer.ts index 307b31d1..286b9d30 100644 --- a/packages/uni-info-watcher/src/indexer.ts +++ b/packages/uni-info-watcher/src/indexer.ts @@ -439,6 +439,10 @@ export class Indexer implements IndexerInterface { return res; } + async getEntitiesForBlock (blockHash: string, tableName: string): Promise { + return this._db.getEntitiesForBlock(blockHash, tableName); + } + getGQLToDBFilter (where: { [key: string]: any } = {}) { return Object.entries(where).reduce((acc: { [key: string]: any }, [fieldWithSuffix, value]) => { const [field, ...suffix] = fieldWithSuffix.split('_'); @@ -470,10 +474,6 @@ export class Indexer implements IndexerInterface { }, {}); } - async getEntitiesForBlock (blockHash: string, tableName: string): Promise { - return this._db.getEntitiesForBlock(blockHash, tableName); - } - async addContracts (): Promise { if (this._serverConfig.mode === 'demo') { return; diff --git a/packages/uni-watcher/src/database.ts b/packages/uni-watcher/src/database.ts index 7bddca27..089d082c 100644 --- a/packages/uni-watcher/src/database.ts +++ b/packages/uni-watcher/src/database.ts @@ -215,6 +215,10 @@ export class Database implements DatabaseInterface { return this._baseDatabase.getBlockProgressEntities(repo, where, options); } + async getEntitiesForBlock (blockHash: string, tableName: string): Promise { + return this._baseDatabase.getEntitiesForBlock(blockHash, tableName); + } + async saveBlockProgress (queryRunner: QueryRunner, block: DeepPartial): Promise { const repo = queryRunner.manager.getRepository(BlockProgress); diff --git a/packages/uni-watcher/src/indexer.ts b/packages/uni-watcher/src/indexer.ts index 38de0558..e6512d84 100644 --- a/packages/uni-watcher/src/indexer.ts +++ b/packages/uni-watcher/src/indexer.ts @@ -92,6 +92,10 @@ export class Indexer implements IndexerInterface { ); } + async getEntitiesForBlock (blockHash: string, tableName: string): Promise { + return this._db.getEntitiesForBlock(blockHash, tableName); + } + getStateData (state: State): any { return this._baseIndexer.getStateData(state); } @@ -159,6 +163,11 @@ export class Indexer implements IndexerInterface { return undefined; } + async getStates (where: FindConditions): Promise { + // TODO Implement + return []; + } + // Method to be used by export-state CLI. async createCheckpoint (contractAddress: string, blockHash: string): Promise { // TODO Implement From 4b46f69e26ad8a1cde0608c039a3c0fb1ba6a590 Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Thu, 24 Nov 2022 12:07:49 +0530 Subject: [PATCH 3/7] Use fill-state CLI from cli package --- .../uni-info-watcher/src/cli/fill-state.ts | 112 ------------------ packages/uni-info-watcher/src/fill.ts | 17 ++- packages/uni-info-watcher/src/indexer.ts | 16 +-- 3 files changed, 23 insertions(+), 122 deletions(-) delete mode 100644 packages/uni-info-watcher/src/cli/fill-state.ts diff --git a/packages/uni-info-watcher/src/cli/fill-state.ts b/packages/uni-info-watcher/src/cli/fill-state.ts deleted file mode 100644 index 114b2470..00000000 --- a/packages/uni-info-watcher/src/cli/fill-state.ts +++ /dev/null @@ -1,112 +0,0 @@ -// -// Copyright 2022 Vulcanize, Inc. -// - -import 'reflect-metadata'; -import debug from 'debug'; -import { Between } from 'typeorm'; - -import { prepareEntityState } from '@cerc-io/graph-node'; - -import { Indexer } from '../indexer'; -import { Database, ENTITIES } from '../database'; -import { FACTORY_ADDRESS } from '../utils/constants'; - -const log = debug('vulcanize:fill-state'); - -export const fillState = async ( - indexer: Indexer, - db: Database, - argv: { - startBlock: number, - endBlock: number - } -): Promise => { - const { startBlock, endBlock } = argv; - if (startBlock > endBlock) { - log('endBlock should be greater than or equal to startBlock'); - process.exit(1); - } - - // NOTE: Assuming all blocks in the given range are in the pruned region - log(`Filling state for subgraph entities in range: [${startBlock}, ${endBlock}]`); - - // Check that there are no existing diffs in this range - const existingStates = await indexer.getStates({ block: { blockNumber: Between(startBlock, endBlock) } }); - if (existingStates.length > 0) { - log('found existing state(s) in the given range'); - process.exit(1); - } - - // Map: contractAddress -> entity names - // Using Factory contract to store state for all entities. - const entityNames = [...ENTITIES].map(entity => entity.name); - const contractEntitiesMap: Map = new Map([ - [ - FACTORY_ADDRESS, - entityNames - ] - ]); - - console.time('time:fill-state'); - - // Fill state for blocks in the given range - for (let blockNumber = startBlock; blockNumber <= endBlock; blockNumber++) { - console.time(`time:fill-state-${blockNumber}`); - - // Get the canonical block hash at current height - const blocks = await indexer.getBlocksAtHeight(blockNumber, false); - - if (blocks.length === 0) { - log(`block not found at height ${blockNumber}`); - process.exit(1); - } else if (blocks.length > 1) { - log(`found more than one non-pruned block at height ${blockNumber}`); - process.exit(1); - } - - const blockHash = blocks[0].blockHash; - - // Create initial state for contracts - await indexer.createInit(blockHash, blockNumber); - - // Fill state for each contract in contractEntitiesMap - const contractStatePromises = Array.from(contractEntitiesMap.entries()) - .map(async ([contractAddress, entities]): Promise => { - // Get all the updated entities at this block - const updatedEntitiesListPromises = entities.map(async (entity): Promise => { - return indexer.getEntitiesForBlock(blockHash, entity); - }); - const updatedEntitiesList = await Promise.all(updatedEntitiesListPromises); - - // Populate state with all the updated entities of each entity type - updatedEntitiesList.forEach((updatedEntities, index) => { - const entityName = entities[index]; - - updatedEntities.forEach((updatedEntity) => { - // Prepare diff data for the entity update - const diffData = prepareEntityState(updatedEntity, entityName, db.relationsMap); - - // Update the in-memory subgraph state - indexer.updateEntityState(contractAddress, diffData); - }); - }); - }); - - await Promise.all(contractStatePromises); - - // Persist subgraph state to the DB - await indexer.dumpEntityState(blockHash, true); - await indexer.updateStateSyncStatusIndexedBlock(blockNumber); - - // Create checkpoints - await indexer.processCheckpoint(blockHash); - await indexer.updateStateSyncStatusCheckpointBlock(blockNumber); - - console.timeEnd(`time:fill-state-${blockNumber}`); - } - - console.timeEnd('time:fill-state'); - - log(`Filled state for subgraph entities in range: [${startBlock}, ${endBlock}]`); -}; diff --git a/packages/uni-info-watcher/src/fill.ts b/packages/uni-info-watcher/src/fill.ts index dbcc449b..998a618b 100644 --- a/packages/uni-info-watcher/src/fill.ts +++ b/packages/uni-info-watcher/src/fill.ts @@ -10,9 +10,10 @@ import { Config } from '@vulcanize/util'; import { Client as UniClient } from '@vulcanize/uni-watcher'; import { Client as ERC20Client } from '@vulcanize/erc20-watcher'; -import { Database } from './database'; +import { Database, ENTITIES } from './database'; import { Indexer } from './indexer'; import { EventWatcher } from './events'; +import { FACTORY_ADDRESS } from './utils/constants'; const log = debug('vulcanize:fill'); @@ -30,7 +31,19 @@ export const main = async (): Promise => { await fillCmd.init(Database, Indexer, EventWatcher, { uniClient, erc20Client }); - await fillCmd.exec(); + await fillCmd.exec(_getContractEntitiesMap()); +}; + +const _getContractEntitiesMap = (): Map => { + // Map: contractAddress -> entity names + // Using Factory contract to store state for all entities. + const entityNames = [...ENTITIES].map(entity => entity.name); + return new Map([ + [ + FACTORY_ADDRESS, + entityNames + ] + ]); }; main().catch(err => { diff --git a/packages/uni-info-watcher/src/indexer.ts b/packages/uni-info-watcher/src/indexer.ts index 286b9d30..3aa6eefb 100644 --- a/packages/uni-info-watcher/src/indexer.ts +++ b/packages/uni-info-watcher/src/indexer.ts @@ -616,7 +616,14 @@ export class Indexer implements IndexerInterface { return this._baseIndexer.updateBlockProgress(block, lastProcessedEventIndex); } - async dumpEntityState (blockHash: string, isStateFinalized = false): Promise { + updateSubgraphState (contractAddress: string, data: any): void { + // Update the subgraph state for a given contract. + const oldData = this._subgraphStateMap.get(contractAddress); + const updatedData = _.merge(oldData, data); + this._subgraphStateMap.set(contractAddress, updatedData); + } + + async dumpSubgraphState (blockHash: string, isStateFinalized = false): Promise { // Create a diff for each contract in the subgraph state map. const createDiffPromises = Array.from(this._subgraphStateMap.entries()) .map(([contractAddress, data]): Promise => { @@ -633,13 +640,6 @@ export class Indexer implements IndexerInterface { this._subgraphStateMap.clear(); } - updateEntityState (contractAddress: string, data: any): void { - // Update the subgraph state for a given contract. - const oldData = this._subgraphStateMap.get(contractAddress); - const updatedData = _.merge(oldData, data); - this._subgraphStateMap.set(contractAddress, updatedData); - } - async resetWatcherToBlock (blockNumber: number): Promise { const entities = [...ENTITIES, FrothyEntity]; await this._baseIndexer.resetWatcherToBlock(blockNumber, entities); From 99ad6ef3415c745724d5d113361871ef6f4cd5c1 Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Thu, 24 Nov 2022 13:45:36 +0530 Subject: [PATCH 4/7] Satisfy updated indexer interface --- packages/erc20-watcher/src/indexer.ts | 9 ++++++++- packages/uni-info-watcher/src/indexer.ts | 22 +++------------------- packages/uni-watcher/src/indexer.ts | 9 ++++++++- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/packages/erc20-watcher/src/indexer.ts b/packages/erc20-watcher/src/indexer.ts index 4a6743ab..d554c582 100644 --- a/packages/erc20-watcher/src/indexer.ts +++ b/packages/erc20-watcher/src/indexer.ts @@ -387,7 +387,14 @@ export class Indexer implements IndexerInterface { return []; } - // Method to be used by export-state CLI. + async createDiffStaged (contractAddress: string, blockHash: string, data: any): Promise { + // TODO Implement + } + + async createDiff (contractAddress: string, blockHash: string, data: any): Promise { + // TODO Implement + } + async createCheckpoint (contractAddress: string, blockHash: string): Promise { // TODO Implement return undefined; diff --git a/packages/uni-info-watcher/src/indexer.ts b/packages/uni-info-watcher/src/indexer.ts index 3aa6eefb..902cec2e 100644 --- a/packages/uni-info-watcher/src/indexer.ts +++ b/packages/uni-info-watcher/src/indexer.ts @@ -8,7 +8,6 @@ import { DeepPartial, FindConditions, FindManyOptions, FindOneOptions, LessThan, import JSONbig from 'json-bigint'; import { providers, utils, BigNumber } from 'ethers'; import { SelectionNode } from 'graphql'; -import _ from 'lodash'; import { Client as UniClient } from '@vulcanize/uni-watcher'; import { Client as ERC20Client } from '@vulcanize/erc20-watcher'; @@ -31,6 +30,7 @@ import { DatabaseInterface, Clients } from '@cerc-io/util'; +import { updateSubgraphState, dumpSubgraphState } from '@cerc-io/graph-node'; import { EthClient } from '@cerc-io/ipld-eth-client'; import { StorageLayout, MappingKey } from '@cerc-io/solidity-mapper'; @@ -617,27 +617,11 @@ export class Indexer implements IndexerInterface { } updateSubgraphState (contractAddress: string, data: any): void { - // Update the subgraph state for a given contract. - const oldData = this._subgraphStateMap.get(contractAddress); - const updatedData = _.merge(oldData, data); - this._subgraphStateMap.set(contractAddress, updatedData); + return updateSubgraphState(this._subgraphStateMap, contractAddress, data); } async dumpSubgraphState (blockHash: string, isStateFinalized = false): Promise { - // Create a diff for each contract in the subgraph state map. - const createDiffPromises = Array.from(this._subgraphStateMap.entries()) - .map(([contractAddress, data]): Promise => { - if (isStateFinalized) { - return this.createDiff(contractAddress, blockHash, data); - } - - return this.createDiffStaged(contractAddress, blockHash, data); - }); - - await Promise.all(createDiffPromises); - - // Reset the subgraph state map. - this._subgraphStateMap.clear(); + return dumpSubgraphState(this, this._subgraphStateMap, blockHash, isStateFinalized); } async resetWatcherToBlock (blockNumber: number): Promise { diff --git a/packages/uni-watcher/src/indexer.ts b/packages/uni-watcher/src/indexer.ts index e6512d84..49068ea9 100644 --- a/packages/uni-watcher/src/indexer.ts +++ b/packages/uni-watcher/src/indexer.ts @@ -168,7 +168,14 @@ export class Indexer implements IndexerInterface { return []; } - // Method to be used by export-state CLI. + async createDiffStaged (contractAddress: string, blockHash: string, data: any): Promise { + // TODO Implement + } + + async createDiff (contractAddress: string, blockHash: string, data: any): Promise { + // TODO Implement + } + async createCheckpoint (contractAddress: string, blockHash: string): Promise { // TODO Implement return undefined; From 1a619bdb01d959090209efec8e7683fa9ae6f084 Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Thu, 24 Nov 2022 14:18:05 +0530 Subject: [PATCH 5/7] Move watcher job-runner --- packages/util/index.ts | 1 - packages/util/src/common.ts | 46 --------------------------------- packages/util/src/job-runner.ts | 33 +++++++++++++++++++++++ 3 files changed, 33 insertions(+), 47 deletions(-) delete mode 100644 packages/util/src/common.ts diff --git a/packages/util/index.ts b/packages/util/index.ts index 25d669e6..a7e023de 100644 --- a/packages/util/index.ts +++ b/packages/util/index.ts @@ -5,4 +5,3 @@ export * from './src/config'; export * from './src/types'; export * from './src/job-runner'; -export * from './src/common'; diff --git a/packages/util/src/common.ts b/packages/util/src/common.ts deleted file mode 100644 index ca960d3b..00000000 --- a/packages/util/src/common.ts +++ /dev/null @@ -1,46 +0,0 @@ -// -// Copyright 2022 Vulcanize, Inc. -// - -import { - JobQueueConfig, - QUEUE_BLOCK_PROCESSING, - QUEUE_EVENT_PROCESSING, - JobQueue -} from '@cerc-io/util'; - -import { IndexerInterface } from './types'; -import { JobRunner as BaseJobRunner } from './job-runner'; - -export class WatcherJobRunner { - _indexer: IndexerInterface - _jobQueue: JobQueue - _baseJobRunner: BaseJobRunner - _jobQueueConfig: JobQueueConfig - - constructor (jobQueueConfig: JobQueueConfig, indexer: IndexerInterface, jobQueue: JobQueue) { - this._jobQueueConfig = jobQueueConfig; - this._indexer = indexer; - this._jobQueue = jobQueue; - this._baseJobRunner = new BaseJobRunner(this._jobQueueConfig, this._indexer, this._jobQueue); - } - - async start (): Promise { - await this._jobQueue.deleteAllJobs(); - await this._baseJobRunner.resetToPrevIndexedBlock(); - await this.subscribeBlockProcessingQueue(); - await this.subscribeEventProcessingQueue(); - } - - async subscribeBlockProcessingQueue (): Promise { - await this._jobQueue.subscribe(QUEUE_BLOCK_PROCESSING, async (job) => { - await this._baseJobRunner.processBlock(job); - }); - } - - async subscribeEventProcessingQueue (): Promise { - await this._jobQueue.subscribe(QUEUE_EVENT_PROCESSING, async (job) => { - await this._baseJobRunner.processEvent(job); - }); - } -} diff --git a/packages/util/src/job-runner.ts b/packages/util/src/job-runner.ts index 38d4d3c6..bbff0a9e 100644 --- a/packages/util/src/job-runner.ts +++ b/packages/util/src/job-runner.ts @@ -441,3 +441,36 @@ export class JobRunner { this._indexer.updateStateStatusMap(contract.address, {}); } } + +export class WatcherJobRunner { + _indexer: IndexerInterface + _jobQueue: JobQueue + _baseJobRunner: JobRunner + _jobQueueConfig: JobQueueConfig + + constructor (jobQueueConfig: JobQueueConfig, indexer: IndexerInterface, jobQueue: JobQueue) { + this._jobQueueConfig = jobQueueConfig; + this._indexer = indexer; + this._jobQueue = jobQueue; + this._baseJobRunner = new JobRunner(this._jobQueueConfig, this._indexer, this._jobQueue); + } + + async start (): Promise { + await this._jobQueue.deleteAllJobs(); + await this._baseJobRunner.resetToPrevIndexedBlock(); + await this.subscribeBlockProcessingQueue(); + await this.subscribeEventProcessingQueue(); + } + + async subscribeBlockProcessingQueue (): Promise { + await this._jobQueue.subscribe(QUEUE_BLOCK_PROCESSING, async (job) => { + await this._baseJobRunner.processBlock(job); + }); + } + + async subscribeEventProcessingQueue (): Promise { + await this._jobQueue.subscribe(QUEUE_EVENT_PROCESSING, async (job) => { + await this._baseJobRunner.processEvent(job); + }); + } +} From 359e088ed6cd17fea2b5c9389f1c415ac5d1dca6 Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Thu, 24 Nov 2022 14:36:18 +0530 Subject: [PATCH 6/7] Remove mock server and data --- packages/erc20-watcher/README.md | 12 - packages/erc20-watcher/package.json | 2 - packages/erc20-watcher/src/mock/data.ts | 50 -- packages/erc20-watcher/src/mock/resolvers.ts | 90 ---- .../erc20-watcher/src/mock/server.test.ts | 173 ------- packages/erc20-watcher/src/server.ts | 3 +- packages/uni-info-watcher/README.md | 10 +- packages/uni-info-watcher/package.json | 4 - packages/uni-info-watcher/src/mock/data.ts | 256 ---------- .../uni-info-watcher/src/mock/resolvers.ts | 462 ------------------ .../uni-info-watcher/src/mock/server.test.ts | 29 -- packages/uni-info-watcher/src/server.ts | 3 +- packages/uni-watcher/package.json | 1 - packages/uni-watcher/src/mock/data.ts | 40 -- packages/uni-watcher/src/mock/resolvers.ts | 33 -- packages/uni-watcher/src/server.ts | 3 +- 16 files changed, 7 insertions(+), 1164 deletions(-) delete mode 100644 packages/erc20-watcher/src/mock/data.ts delete mode 100644 packages/erc20-watcher/src/mock/resolvers.ts delete mode 100644 packages/erc20-watcher/src/mock/server.test.ts delete mode 100644 packages/uni-info-watcher/src/mock/data.ts delete mode 100644 packages/uni-info-watcher/src/mock/resolvers.ts delete mode 100644 packages/uni-info-watcher/src/mock/server.test.ts delete mode 100644 packages/uni-watcher/src/mock/data.ts delete mode 100644 packages/uni-watcher/src/mock/resolvers.ts diff --git a/packages/erc20-watcher/README.md b/packages/erc20-watcher/README.md index 571f5406..ca37df38 100644 --- a/packages/erc20-watcher/README.md +++ b/packages/erc20-watcher/README.md @@ -160,15 +160,3 @@ $ yarn fill --startBlock 1000 --endBlock 2000 } ``` - -## Test - -To run tests (GQL queries) against the mock server: - -``` -yarn run server:mock -``` - -```bash -yarn test -``` diff --git a/packages/erc20-watcher/package.json b/packages/erc20-watcher/package.json index bcfe8cdd..5014340f 100644 --- a/packages/erc20-watcher/package.json +++ b/packages/erc20-watcher/package.json @@ -6,11 +6,9 @@ "main": "dist/index.js", "scripts": { "lint": "eslint .", - "test": "mocha -r ts-node/register src/**/*.test.ts", "build": "tsc", "server": "DEBUG=vulcanize:* node --enable-source-maps dist/server.js", "server:dev": "DEBUG=vulcanize:* nodemon --watch src src/server.ts", - "server:mock": "MOCK=1 nodemon src/server.ts", "job-runner": "DEBUG=vulcanize:* node --enable-source-maps dist/job-runner.js", "job-runner:dev": "DEBUG=vulcanize:* nodemon --watch src src/job-runner.ts", "watch:contract": "node --enable-source-maps dist/cli/watch-contract.js", diff --git a/packages/erc20-watcher/src/mock/data.ts b/packages/erc20-watcher/src/mock/data.ts deleted file mode 100644 index 0160e0fa..00000000 --- a/packages/erc20-watcher/src/mock/data.ts +++ /dev/null @@ -1,50 +0,0 @@ -// -// Copyright 2021 Vulcanize, Inc. -// - -// TODO: Pull mock data for 5 tokens from rinkeby. - -export const tokens: {[address: string]: {[variable: string]: string}} = { - '0xd87fea54f506972e3267239ec8e159548892074a': { - name: 'ChainLink Token', - symbol: 'LINK', - decimals: '18', - totalSupply: '1000000' - } -}; - -export const blocks: {[blockHash: string]: {[address: string]: any}} = { - // Block hash. - '0x77b5479a5856dd8ec63df6aabf9ce0913071a6dda3a3d54f3c9c940574bcb8ab': { - - // ERC20 token address. - '0xd87fea54f506972e3267239ec8e159548892074a': { - ...tokens['0xd87fea54f506972e3267239ec8e159548892074a'], - - balanceOf: { - '0xDC7d7A8920C8Eecc098da5B7522a5F31509b5Bfc': '10000', - '0xCA6D29232D1435D8198E3E5302495417dD073d61': '500' - }, - allowance: { - '0xDC7d7A8920C8Eecc098da5B7522a5F31509b5Bfc': { - '0xCA6D29232D1435D8198E3E5302495417dD073d61': '100', - '0x9273D9437B0bf2F1b7999d8dB72960d6379564d1': '200' - } - }, - events: [ - { - name: 'Transfer', - from: '0xDC7d7A8920C8Eecc098da5B7522a5F31509b5Bfc', - to: '0xCA6D29232D1435D8198E3E5302495417dD073d61', - value: '500' - }, - { - name: 'Approval', - owner: '0xDC7d7A8920C8Eecc098da5B7522a5F31509b5Bfc', - spender: '0xCA6D29232D1435D8198E3E5302495417dD073d61', - value: '100' - } - ] - } - } -}; diff --git a/packages/erc20-watcher/src/mock/resolvers.ts b/packages/erc20-watcher/src/mock/resolvers.ts deleted file mode 100644 index 4bfee4b2..00000000 --- a/packages/erc20-watcher/src/mock/resolvers.ts +++ /dev/null @@ -1,90 +0,0 @@ -// -// Copyright 2021 Vulcanize, Inc. -// - -import debug from 'debug'; -import BigInt from 'apollo-type-bigint'; - -import { blocks } from './data'; - -const log = debug('test'); - -export const createResolvers = async (): Promise => { - return { - BigInt: new BigInt('bigInt'), - - TokenEvent: { - __resolveType: (obj: any) => { - if (obj.owner) { - return 'ApprovalEvent'; - } - - return 'TransferEvent'; - } - }, - - Query: { - - totalSupply: (_: any, { blockHash, token }: { blockHash: string, token: string }) => { - log('totalSupply', blockHash, token); - - return { - value: blocks[blockHash][token].totalSupply, - proof: { data: '' } - }; - }, - - balanceOf: (_: any, { blockHash, token, owner }: { blockHash: string, token: string, owner: string }) => { - log('balanceOf', blockHash, token, owner); - - return { - value: blocks[blockHash][token].balanceOf[owner], - proof: { data: '' } - }; - }, - - allowance: (_: any, { blockHash, token, owner, spender }: { blockHash: string, token: string, owner: string, spender: string }) => { - log('allowance', blockHash, token, owner, spender); - - return { - value: blocks[blockHash][token].allowance[owner][spender], - proof: { data: '' } - }; - }, - - name: (_: any, { blockHash, token }: { blockHash: string, token: string }) => { - log('name', blockHash, token); - - return { - value: blocks[blockHash][token].name, - proof: { data: '' } - }; - }, - - symbol: (_: any, { blockHash, token }: { blockHash: string, token: string }) => { - log('symbol', blockHash, token); - - return { - value: blocks[blockHash][token].symbol, - proof: { data: '' } - }; - }, - - decimals: (_: any, { blockHash, token }: { blockHash: string, token: string }) => { - log('decimals', blockHash, token); - - return { - value: blocks[blockHash][token].decimals, - proof: { data: '' } - }; - }, - - events: (_: any, { blockHash, token, name }: { blockHash: string, token: string, name: string }) => { - log('events', blockHash, token, name); - return blocks[blockHash][token].events - .filter((e: any) => !name || name === e.name) - .map((e: any) => ({ event: e })); - } - } - }; -}; diff --git a/packages/erc20-watcher/src/mock/server.test.ts b/packages/erc20-watcher/src/mock/server.test.ts deleted file mode 100644 index 7e6a8152..00000000 --- a/packages/erc20-watcher/src/mock/server.test.ts +++ /dev/null @@ -1,173 +0,0 @@ -// -// Copyright 2021 Vulcanize, Inc. -// - -import 'mocha'; -import { expect } from 'chai'; -import _ from 'lodash'; - -import { GraphQLClient } from 'graphql-request'; - -import { - queryName, - querySymbol, - queryDecimals, - queryTotalSupply, - queryBalanceOf, - queryAllowance, - queryEvents -} from '../queries'; - -import { blocks, tokens as tokenInfo } from './data'; - -const testCases: { - balanceOf: any[], - allowance: any[], - events: any[], - tokens: any[] -} = { - balanceOf: [], - allowance: [], - events: [], - tokens: [] -}; - -const blockHashes = _.keys(blocks); -blockHashes.forEach(blockHash => { - const block = blocks[blockHash]; - const tokens = _.keys(block); - tokens.forEach(token => { - const tokenObj = block[token]; - - // Token info test cases. - testCases.tokens.push({ - blockHash, - token, - info: tokenInfo[token] - }); - - // Event test cases. - testCases.events.push({ - blockHash, - token, - events: tokenObj.events - }); - - // Balance test cases. - const balanceOfOwners = _.keys(tokenObj.balanceOf); - balanceOfOwners.forEach(owner => { - testCases.balanceOf.push({ - blockHash, - token, - owner, - balance: tokenObj.balanceOf[owner] - }); - }); - - // Allowance test cases. - const allowanceOwners = _.keys(tokenObj.allowance); - allowanceOwners.forEach(owner => { - const allowanceObj = tokenObj.allowance[owner]; - const spenders = _.keys(allowanceObj); - spenders.forEach(spender => { - testCases.allowance.push({ - blockHash, - token, - owner, - spender, - allowance: allowanceObj[spender] - }); - }); - }); - }); -}); - -describe('server', () => { - const client = new GraphQLClient('http://localhost:3001/graphql'); - - it('query token info', async () => { - const tests = testCases.tokens; - expect(tests.length).to.be.greaterThan(0); - - for (let i = 0; i < tests.length; i++) { - const testCase = tests[i]; - - // Token totalSupply. - let result = await client.request(queryTotalSupply, testCase); - expect(result.totalSupply.value).to.equal(testCase.info.totalSupply); - expect(result.totalSupply.proof.data).to.equal(''); - - // Token name. - result = await client.request(queryName, testCase); - expect(result.name.value).to.equal(testCase.info.name); - expect(result.name.proof.data).to.equal(''); - - // Token symbol. - result = await client.request(querySymbol, testCase); - expect(result.symbol.value).to.equal(testCase.info.symbol); - expect(result.symbol.proof.data).to.equal(''); - - // Token decimals. - result = await client.request(queryDecimals, testCase); - expect(result.decimals.value).to.equal(testCase.info.decimals); - expect(result.decimals.proof.data).to.equal(''); - } - }); - - it('query balanceOf', async () => { - const tests = testCases.balanceOf; - expect(tests.length).to.be.greaterThan(0); - - for (let i = 0; i < tests.length; i++) { - const testCase = tests[i]; - const result = await client.request(queryBalanceOf, testCase); - - expect(result.balanceOf.value).to.equal(testCase.balance); - - // TODO: Check proof. - expect(result.balanceOf.proof.data).to.equal(''); - } - }); - - it('query allowance', async () => { - const tests = testCases.allowance; - expect(tests.length).to.be.greaterThan(0); - - for (let i = 0; i < tests.length; i++) { - const testCase = tests[i]; - const result = await client.request(queryAllowance, testCase); - - expect(result.allowance.value).to.equal(testCase.allowance); - - // TODO: Check proof. - expect(result.allowance.proof.data).to.equal(''); - } - }); - - it('query events', async () => { - const tests = testCases.events; - expect(tests.length).to.be.greaterThan(0); - - for (let i = 0; i < tests.length; i++) { - const testCase = tests[i]; - const result = await client.request(queryEvents, testCase); - - const resultEvents = result.events.map((record: any) => record.event); - expect(resultEvents.length).to.equal(testCase.events.length); - - resultEvents.forEach((resultEvent: any, index: number) => { - const { name, ...testCaseEvent } = testCase.events[index]; - - if (name === 'Transfer') { - expect(resultEvent.__typename).to.equal('TransferEvent'); - } else if (name === 'Approval') { - expect(resultEvent.__typename).to.equal('ApprovalEvent'); - } - - expect(resultEvent).to.include(testCaseEvent); - }); - - // TODO: Check proof. - } - }); -}); diff --git a/packages/erc20-watcher/src/server.ts b/packages/erc20-watcher/src/server.ts index ee0cb27e..fe9f6d11 100644 --- a/packages/erc20-watcher/src/server.ts +++ b/packages/erc20-watcher/src/server.ts @@ -8,7 +8,6 @@ import 'graphql-import-node'; import { ServerCmd } from '@cerc-io/cli'; import typeDefs from './schema'; -import { createResolvers as createMockResolvers } from './mock/resolvers'; import { createResolvers } from './resolvers'; import { Indexer } from './indexer'; import { Database } from './database'; @@ -20,7 +19,7 @@ export const main = async (): Promise => { const serverCmd = new ServerCmd(); await serverCmd.init(Database, Indexer, EventWatcher); - return process.env.MOCK ? serverCmd.exec(createMockResolvers, typeDefs) : serverCmd.exec(createResolvers, typeDefs); + return serverCmd.exec(createResolvers, typeDefs); }; main().then(() => { diff --git a/packages/uni-info-watcher/README.md b/packages/uni-info-watcher/README.md index e46d4441..4de373d8 100644 --- a/packages/uni-info-watcher/README.md +++ b/packages/uni-info-watcher/README.md @@ -66,9 +66,7 @@ Update `environments/local.toml` with database connection settings for both the $ yarn job-runner -f environments/local.toml ``` -* Run `yarn server:mock` to run server with mock data. - -## Mock Queries +## Example Queries ```graphql { @@ -77,7 +75,7 @@ Update `environments/local.toml` with database connection settings for both the ethPriceUSD } - bundles(first: 1, block: { number: 2 }) { + bundles(first: 1, block: { number: 2 }) { id ethPriceUSD } @@ -287,13 +285,13 @@ Queries with ID param To run a smoke test: * Start the server in `packages/erc-20-watcher`. - + ```bash yarn server ``` * Start the server and the job-runner in `packages/uni-watcher` with test config. - + ```bash yarn job-runner -f environments/test.toml ``` diff --git a/packages/uni-info-watcher/package.json b/packages/uni-info-watcher/package.json index d315aa62..0d76b713 100644 --- a/packages/uni-info-watcher/package.json +++ b/packages/uni-info-watcher/package.json @@ -28,13 +28,11 @@ "lint": "eslint .", "test": "mocha src/**/*.test.ts", "test:gpev": "mocha src/get-prev-entity.test.ts", - "test:server": "mocha src/server.test.ts", "test:init": "ts-node test/init.ts", "build": "tsc", "server": "DEBUG=vulcanize:* node --enable-source-maps dist/server.js", "server:prof": "DEBUG=vulcanize:* node --require pprof --enable-source-maps dist/server.js", "server:dev": "DEBUG=vulcanize:* nodemon --watch src src/server.ts", - "server:mock": "MOCK=1 nodemon src/server.ts", "job-runner": "DEBUG=vulcanize:* node --enable-source-maps dist/job-runner.js", "job-runner:prof": "DEBUG=vulcanize:* node --require pprof --enable-source-maps dist/job-runner.js", "job-runner:dev": "DEBUG=vulcanize:* nodemon --watch src src/job-runner.ts", @@ -57,14 +55,12 @@ "import-state:dev": "DEBUG=vulcanize:* ts-node src/cli/import-state.ts" }, "devDependencies": { - "@types/chance": "^1.1.2", "@types/debug": "^4.1.7", "@types/express": "^4.17.14", "@typescript-eslint/eslint-plugin": "^4.25.0", "@typescript-eslint/parser": "^4.25.0", "@uniswap/v3-core": "1.0.0", "chai": "^4.3.4", - "chance": "^1.1.7", "eslint": "^7.27.0", "eslint-config-semistandard": "^15.0.1", "eslint-config-standard": "^16.0.3", diff --git a/packages/uni-info-watcher/src/mock/data.ts b/packages/uni-info-watcher/src/mock/data.ts deleted file mode 100644 index a1990947..00000000 --- a/packages/uni-info-watcher/src/mock/data.ts +++ /dev/null @@ -1,256 +0,0 @@ -// -// Copyright 2021 Vulcanize, Inc. -// - -import Chance from 'chance'; -import { ethers } from 'ethers'; - -export const NO_OF_BLOCKS = 3; - -export interface Entity { - blockNumber: number - id: string - [field: string]: any -} - -export class Data { - static _instance: Data; - - _entities: {[key: string]: Array} = { - bundles: [], - burns: [], - transactions: [], - pools: [], - tokens: [], - factories: [], - mints: [], - swaps: [], - poolDayDatas: [], - tokenDayDatas: [], - uniswapDayDatas: [], - ticks: [], - tokenHourDatas: [] - } - - _chance: Chance.Chance - - constructor () { - this._chance = new Chance(); - this._generateData(); - } - - static getInstance (): Data { - if (!this._instance) { - this._instance = new Data(); - } - return this._instance; - } - - get entities (): {[key: string]: Array} { - return this._entities; - } - - _generateData (): void { - const factoryAddress = this._getRandomAddress(); - - // Generate data for each block. - Array.from(Array(NO_OF_BLOCKS)) - .forEach((_, blockNumber) => { - // Generate data for Factory. - this._entities.factories.push({ - blockNumber, - id: factoryAddress, - totalFeesUSD: this._chance.floating({ min: 1, fixed: 2 }), - totalValueLockedUSD: this._chance.floating({ min: 1, fixed: 2 }), - totalVolumeUSD: this._chance.floating({ min: 1, fixed: 2 }), - txCount: this._chance.integer({ min: 1 }) - }); - - // Generate Bundle. - this._entities.bundles.push({ - blockNumber, - id: '1', - ethPriceUSD: this._chance.floating({ min: 1, fixed: 2 }) - }); - - // Generate Pools. - Array.from(Array(3)) - .forEach(() => { - const token0 = { - blockNumber: blockNumber, - id: this._getRandomAddress(), - symbol: this._chance.string({ length: 3, casing: 'upper', alpha: false }), - name: this._chance.word({ syllables: 1 }), - volume: this._chance.integer({ min: 1 }), - volumeUSD: this._chance.floating({ min: 1, fixed: 2 }), - feesUSD: this._chance.floating({ min: 1, fixed: 2 }), - txCount: this._chance.integer({ min: 1 }), - totalValueLocked: this._chance.integer({ min: 1 }), - totalValueLockedUSD: this._chance.floating({ min: 1, fixed: 2 }), - derivedETH: this._chance.floating({ min: 1, fixed: 2 }) - }; - - const token1 = { - blockNumber: blockNumber, - id: this._getRandomAddress(), - symbol: this._chance.string({ length: 3, casing: 'upper', alpha: false }), - name: this._chance.word({ syllables: 1 }), - volume: this._chance.integer({ min: 1 }), - volumeUSD: this._chance.floating({ min: 1, fixed: 2 }), - feesUSD: this._chance.floating({ min: 1, fixed: 2 }), - txCount: this._chance.integer({ min: 1 }), - totalValueLocked: this._chance.integer({ min: 1 }), - totalValueLockedUSD: this._chance.floating({ min: 1, fixed: 2 }), - derivedETH: this._chance.floating({ min: 1, fixed: 2 }) - }; - - const pool = { - blockNumber: blockNumber, - id: this._getRandomAddress(), - token0: token0.id, - token1: token1.id, - feeTier: this._chance.integer({ min: 1 }), - liquidity: this._chance.integer({ min: 1 }), - sqrtPrice: this._chance.integer({ min: 1 }), - token0Price: this._chance.floating({ min: 1, fixed: 2 }), - token1Price: this._chance.floating({ min: 1, fixed: 2 }), - tick: this._chance.integer({ min: 1 }), - volumeUSD: this._chance.floating({ min: 1, fixed: 2 }), - txCount: this._chance.integer({ min: 1 }), - totalValueLockedToken0: this._chance.integer({ min: 1 }), - totalValueLockedToken1: this._chance.integer({ min: 1 }), - totalValueLockedUSD: this._chance.floating({ min: 1, fixed: 2 }) - }; - - const timestamp = this._chance.timestamp(); - - this._entities.poolDayDatas.push({ - blockNumber, - date: timestamp, - id: String(timestamp), - tvlUSD: this._chance.floating({ min: 1, fixed: 2 }), - volumeUSD: this._chance.floating({ min: 1, fixed: 2 }) - }); - - this._entities.tokenDayDatas.push( - { - blockNumber, - date: timestamp, - id: `${token0.id}-${timestamp}`, - totalValueLockedUSD: this._chance.floating({ min: 1, fixed: 2 }), - volumeUSD: this._chance.floating({ min: 1, fixed: 2 }) - }, - { - blockNumber, - date: timestamp, - id: `${token1.id}-${timestamp}`, - totalValueLockedUSD: this._chance.floating({ min: 1, fixed: 2 }), - volumeUSD: this._chance.floating({ min: 1, fixed: 2 }) - } - ); - - this._entities.uniswapDayDatas.push({ - blockNumber, - date: timestamp, - id: String(timestamp), - tvlUSD: this._chance.floating({ min: 1, fixed: 2 }), - volumeUSD: this._chance.floating({ min: 1, fixed: 2 }) - }); - - this._entities.ticks.push({ - blockNumber, - id: `${pool.id}#${this._chance.integer({ min: 1 })}`, - liquidityGross: this._chance.integer({ min: 1 }), - liquidityNet: this._chance.integer({ min: 1 }), - price0: this._chance.floating({ min: 1, fixed: 2 }), - price1: this._chance.floating({ min: 1, fixed: 2 }), - tickIdx: this._chance.integer({ min: 1 }) - }); - - this._entities.tokenHourDatas.push( - { - blockNumber, - close: this._chance.floating({ min: 1, fixed: 2 }), - high: this._chance.floating({ min: 1, fixed: 2 }), - id: `${token0.id}-${timestamp}`, - low: this._chance.floating({ min: 1, fixed: 2 }), - open: this._chance.floating({ min: 1, fixed: 2 }), - periodStartUnix: timestamp - }, - { - blockNumber, - close: this._chance.floating({ min: 1, fixed: 2 }), - high: this._chance.floating({ min: 1, fixed: 2 }), - id: `${token1.id}-${timestamp}`, - low: this._chance.floating({ min: 1, fixed: 2 }), - open: this._chance.floating({ min: 1, fixed: 2 }), - periodStartUnix: timestamp - } - ); - - this._entities.tokens.push(token0, token1); - this._entities.pools.push(pool); - - // Generate Transactions. - Array.from(Array(3)) - .forEach((_, transactionIndex) => { - const transactionHash = ethers.utils.hexlify(ethers.utils.randomBytes(32)); - - const transaction = { - blockNumber, - id: transactionHash, - timestamp: this._chance.timestamp() - }; - - this._entities.transactions.push(transaction); - - // Generate Burns - this._entities.burns.push({ - id: `${transaction.id}#${transactionIndex}`, - blockNumber, - transaction: transaction.id, - pool: pool.id, - timestamp: this._chance.timestamp(), - owner: this._getRandomAddress(), - origin: this._getRandomAddress(), - amount0: this._chance.integer({ min: 1 }), - amount1: this._chance.integer({ min: 1 }), - amountUSD: this._chance.floating({ min: 1, fixed: 2 }) - }); - - // Generate Mints - this._entities.mints.push({ - id: `${transaction.id}#${transactionIndex}`, - blockNumber, - transaction: transaction.id, - pool: pool.id, - timestamp: this._chance.timestamp(), - owner: this._getRandomAddress(), - origin: this._getRandomAddress(), - amount0: this._chance.integer({ min: 1 }), - amount1: this._chance.integer({ min: 1 }), - amountUSD: this._chance.floating({ min: 1, fixed: 2 }), - sender: this._getRandomAddress() - }); - - // Generate Swaps - this._entities.swaps.push({ - id: `${transaction.id}#${transactionIndex}`, - blockNumber, - transaction: transaction.id, - pool: pool.id, - timestamp: this._chance.timestamp(), - origin: this._getRandomAddress(), - amount0: this._chance.integer({ min: 1 }), - amount1: this._chance.integer({ min: 1 }), - amountUSD: this._chance.floating({ min: 1, fixed: 2 }) - }); - }); - }); - }); - } - - _getRandomAddress (): string { - return ethers.utils.hexlify(ethers.utils.randomBytes(20)); - } -} diff --git a/packages/uni-info-watcher/src/mock/resolvers.ts b/packages/uni-info-watcher/src/mock/resolvers.ts deleted file mode 100644 index 6a739aed..00000000 --- a/packages/uni-info-watcher/src/mock/resolvers.ts +++ /dev/null @@ -1,462 +0,0 @@ -// -// Copyright 2021 Vulcanize, Inc. -// - -/* eslint-disable camelcase */ -import debug from 'debug'; -import BigInt from 'apollo-type-bigint'; - -import { BlockHeight, OrderDirection } from '@cerc-io/util'; - -import { Data, Entity, NO_OF_BLOCKS } from './data'; - -const log = debug('vulcanize:test'); - -enum BurnOrderBy { - timestamp -} - -interface BurnFilter { - pool: string; - token0: string; - token1: string; -} - -enum MintOrderBy { - timestamp -} - -interface MintFilter { - pool: string; - token0: string; - token1: string; -} - -enum PoolOrderBy { - totalValueLockedUSD -} - -interface PoolFilter { - id: string; - id_in: [string]; - token0: string; - token0_in: [string]; - token1: string; - token1_in: [string]; -} - -enum TokenOrderBy { - totalValueLockedUSD -} - -interface TokenFilter { - id: string; - id_in: [string]; - name_contains: string; - symbol_contains: string; -} - -enum TransactionOrderBy { - timestamp -} - -interface SwapFilter { - pool: string; - token0: string; - token1: string; -} - -enum SwapOrderBy { - timestamp -} - -enum DayDataOrderBy { - date -} - -interface DayDataFilter { - date_gt: number; - pool: string; -} - -interface TickFilter { - poolAddress: string; - tickIdx_gte: number; - tickIdx_lte: number; -} - -enum TokenHourDataOrderBy { - periodStartUnix -} - -interface TokenHourDataFilter { - periodStartUnix_gt: number; - token: string; -} - -export const createResolvers = async (): Promise => { - const latestBlockNumber = NO_OF_BLOCKS - 1; - const data = Data.getInstance(); - const { bundles, burns, pools, transactions, factories, mints, tokens, swaps, poolDayDatas, tokenDayDatas, uniswapDayDatas, ticks, tokenHourDatas } = data.entities; - - return { - BigInt: new BigInt('bigInt'), - - Query: { - bundle: (_: any, { id: bundleId, block }: { id: string, block: BlockHeight }) => { - log('bundle', bundleId, block); - const res = bundles.find((bundle: Entity) => bundle.blockNumber === block.number && bundle.id === bundleId); - - if (res) { - const { ethPriceUSD, id } = res; - return { ethPriceUSD, id }; - } - }, - - bundles: (_: any, { first, block }: { first: number, block: BlockHeight }) => { - log('bundles', first, block); - - const res = bundles.filter((bundle: Entity) => bundle.blockNumber === block.number) - .slice(0, first) - .map(({ ethPriceUSD, id }) => ({ ethPriceUSD, id })); - - return res; - }, - - burns: (_: any, { first, orderBy, orderDirection, where }: { first: number, orderBy: BurnOrderBy, orderDirection: OrderDirection, where: BurnFilter }) => { - log('burns', first, orderBy, orderDirection, where); - - const res = burns.filter((burn: Entity) => { - if (burn.blockNumber === latestBlockNumber) { - return Object.entries(where || {}) - .every(([field, value]) => burn[field] === value); - } - - return false; - }).slice(0, first) - .sort((a: any, b: any) => { - a = a[orderBy]; - b = b[orderBy]; - return orderDirection === OrderDirection.asc ? (a - b) : (b - a); - }) - .map(burn => { - return { - ...burn, - pool: pools.find(pool => pool.id === burn.pool), - transaction: transactions.find(transaction => transaction.id === burn.transaction) - }; - }); - - return res; - }, - - factories: (_: any, { first, block }: { first: number, block: BlockHeight }) => { - log('factories', first, block); - - const res = factories.filter((factory: Entity) => factory.blockNumber === block.number) - .slice(0, first); - - return res; - }, - - mints: (_: any, { first, orderBy, orderDirection, where }: { first: number, orderBy: MintOrderBy, orderDirection: OrderDirection, where: MintFilter }) => { - log('mints', first, orderBy, orderDirection, where); - - const res = mints.filter((mint: Entity) => { - if (mint.blockNumber === latestBlockNumber) { - return Object.entries(where || {}) - .every(([field, value]) => mint[field] === value); - } - - return false; - }).slice(0, first) - .sort((a: any, b: any) => { - a = a[orderBy]; - b = b[orderBy]; - return orderDirection === OrderDirection.asc ? (a - b) : (b - a); - }) - .map(mint => { - return { - ...mint, - pool: pools.find(pool => pool.id === mint.pool), - transaction: transactions.find(transaction => transaction.id === mint.transaction) - }; - }); - - return res; - }, - - pool: (_: any, { id: poolId }: { id: string }) => { - log('pool', poolId); - const res = pools.find((pool: Entity) => pool.id === poolId); - - if (res) { - return { - ...res, - token0: tokens.find(token => token.id === res.token0), - token1: tokens.find(token => token.id === res.token1) - }; - } - }, - - pools: (_: any, { first, orderBy, orderDirection, where, block }: { first: number, orderBy: PoolOrderBy, orderDirection: OrderDirection, where: PoolFilter, block: BlockHeight }) => { - log('pools', first, orderBy, orderDirection, where, block); - - const res = pools.filter((pool: Entity) => { - if (pool.blockNumber === latestBlockNumber) { - return Object.entries(where || {}) - .every(([filter, value]) => { - if (filter.endsWith('_in')) { - const field = filter.substring(0, filter.length - 3); - - return value.some((el: any) => el === pool[field]); - } - - return pool[filter] === value; - }); - } - - return false; - }).slice(0, first) - .sort((a: any, b: any) => { - a = a[orderBy]; - b = b[orderBy]; - return orderDirection === OrderDirection.asc ? (a - b) : (b - a); - }) - .map(pool => { - return { - ...pool, - token0: tokens.find(token => token.id === pool.token0), - token1: tokens.find(token => token.id === pool.token1) - }; - }); - - return res; - }, - - token: (_: any, { id: tokenId, block }: { id: string, block: BlockHeight }) => { - log('token', tokenId, block); - const res = tokens.find((token: Entity) => token.blockNumber === block.number && token.id === tokenId); - - return res; - }, - - tokens: (_: any, { orderBy, orderDirection, where }: { orderBy: TokenOrderBy, orderDirection: OrderDirection, where: TokenFilter }) => { - log('tokens', orderBy, orderDirection, where); - - const res = tokens.filter((token: Entity) => { - if (token.blockNumber === latestBlockNumber) { - return Object.entries(where || {}) - .every(([filter, value]) => { - if (filter.endsWith('_in')) { - const field = filter.substring(0, filter.length - 3); - - return value.some((el: any) => el === token[field]); - } - - return token[filter] === value; - }); - } - - return false; - }).sort((a: any, b: any) => { - a = a[orderBy]; - b = b[orderBy]; - return orderDirection === OrderDirection.asc ? (a - b) : (b - a); - }); - - return res; - }, - - transactions: (_: any, { first, orderBy, orderDirection }: { first: number, orderBy: TransactionOrderBy, orderDirection: OrderDirection }) => { - log('transactions', first, orderBy, orderDirection); - - const res = transactions.filter((transaction: Entity) => transaction.blockNumber === latestBlockNumber) - .slice(0, first) - .sort((a: any, b: any) => { - a = a[orderBy]; - b = b[orderBy]; - return orderDirection === OrderDirection.asc ? (a - b) : (b - a); - }) - .map(transaction => { - return { - ...transaction, - burns: burns.filter(burn => burn.transaction === transaction.id), - mints: mints.filter(mint => mint.transaction === transaction.id), - swaps: swaps.filter(swap => swap.transaction === transaction.id) - }; - }); - - return res; - }, - - swaps: (_: any, { first, orderBy, orderDirection, where }: { first: number, orderBy: SwapOrderBy, orderDirection: OrderDirection, where: SwapFilter }) => { - log('swaps', first, orderBy, orderDirection, where); - - const res = swaps.filter((swap: Entity) => { - if (swap.blockNumber === latestBlockNumber) { - return Object.entries(where || {}) - .every(([field, value]) => swap[field] === value); - } - - return false; - }).slice(0, first) - .sort((a: any, b: any) => { - a = a[orderBy]; - b = b[orderBy]; - return orderDirection === OrderDirection.asc ? (a - b) : (b - a); - }) - .map(swap => { - return { - ...swap, - pool: pools.find(pool => pool.id === swap.pool), - transaction: transactions.find(transaction => transaction.id === swap.transaction) - }; - }); - - return res; - }, - - poolDayDatas: (_: any, { skip, first, orderBy, orderDirection, where }: { skip: number, first: number, orderBy: DayDataOrderBy, orderDirection: OrderDirection, where: DayDataFilter }) => { - log('poolDayDatas', skip, first, orderBy, orderDirection, where); - - const res = poolDayDatas.filter((poolDayData: Entity) => { - if (poolDayData.blockNumber === latestBlockNumber) { - return Object.entries(where || {}) - .every(([filter, value]) => { - if (filter.endsWith('_gt')) { - const field = filter.substring(0, filter.length - 3); - - return poolDayData[field] > value; - } - - return poolDayData[filter] === value; - }); - } - - return false; - }).slice(skip, skip + first) - .sort((a: any, b: any) => { - a = a[orderBy]; - b = b[orderBy]; - return orderDirection === OrderDirection.asc ? (a - b) : (b - a); - }); - - return res; - }, - - tokenDayDatas: (_: any, { skip, first, orderBy, orderDirection, where }: { skip: number, first: number, orderBy: DayDataOrderBy, orderDirection: OrderDirection, where: DayDataFilter }) => { - log('tokenDayDatas', skip, first, orderBy, orderDirection, where); - - const res = tokenDayDatas.filter((tokenDayData: Entity) => { - if (tokenDayData.blockNumber === latestBlockNumber) { - return Object.entries(where || {}) - .every(([filter, value]) => { - if (filter.endsWith('_gt')) { - const field = filter.substring(0, filter.length - 3); - - return tokenDayData[field] > value; - } - - return tokenDayData[filter] === value; - }); - } - - return false; - }).slice(skip, skip + first) - .sort((a: any, b: any) => { - a = a[orderBy]; - b = b[orderBy]; - return orderDirection === OrderDirection.asc ? (a - b) : (b - a); - }); - - return res; - }, - - uniswapDayDatas: (_: any, { skip, first, orderBy, orderDirection, where }: { skip: number, first: number, orderBy: DayDataOrderBy, orderDirection: OrderDirection, where: DayDataFilter }) => { - log('uniswapDayDatas', skip, first, orderBy, orderDirection, where); - - const res = uniswapDayDatas.filter((uniswapDayData: Entity) => { - if (uniswapDayData.blockNumber === latestBlockNumber) { - return Object.entries(where || {}) - .every(([filter, value]) => { - if (filter.endsWith('_gt')) { - const field = filter.substring(0, filter.length - 3); - - return uniswapDayData[field] > value; - } - - return uniswapDayData[filter] === value; - }); - } - - return false; - }).slice(skip, skip + first) - .sort((a: any, b: any) => { - a = a[orderBy]; - b = b[orderBy]; - return orderDirection === OrderDirection.asc ? (a - b) : (b - a); - }); - - return res; - }, - - ticks: (_: any, { skip, first, where, block }: { skip: number, first: number, where: TickFilter, block: BlockHeight }) => { - log('ticks', skip, first, where, block); - - const res = ticks.filter((tick: Entity) => { - if (tick.blockNumber === block.number) { - return Object.entries(where || {}) - .every(([filter, value]) => { - if (filter.endsWith('_gte')) { - const field = filter.substring(0, filter.length - 3); - - return tick[field] >= value; - } - - if (filter.endsWith('_lte')) { - const field = filter.substring(0, filter.length - 3); - - return tick[field] <= value; - } - - return tick[filter] === value; - }); - } - - return false; - }).slice(skip, skip + first); - - return res; - }, - - tokenHourDatas: (_: any, { skip, first, orderBy, orderDirection, where }: { skip: number, first: number, orderBy: TokenHourDataOrderBy, orderDirection: OrderDirection, where: TokenHourDataFilter }) => { - log('tokenHourDatas', skip, first, orderBy, orderDirection, where); - - const res = tokenHourDatas.filter((tokenHourData: Entity) => { - if (tokenHourData.blockNumber === latestBlockNumber) { - return Object.entries(where || {}) - .every(([filter, value]) => { - if (filter.endsWith('_gt')) { - const field = filter.substring(0, filter.length - 3); - - return tokenHourData[field] > value; - } - - return tokenHourData[filter] === value; - }); - } - - return false; - }).slice(skip, skip + first) - .sort((a: any, b: any) => { - a = a[orderBy]; - b = b[orderBy]; - return orderDirection === OrderDirection.asc ? (a - b) : (b - a); - }); - - return res; - } - } - }; -}; diff --git a/packages/uni-info-watcher/src/mock/server.test.ts b/packages/uni-info-watcher/src/mock/server.test.ts deleted file mode 100644 index 82e0aefa..00000000 --- a/packages/uni-info-watcher/src/mock/server.test.ts +++ /dev/null @@ -1,29 +0,0 @@ -// -// Copyright 2021 Vulcanize, Inc. -// - -import 'mocha'; -import { expect } from 'chai'; -import { GraphQLClient } from 'graphql-request'; - -import { queryBundles } from '../queries'; -import { Data } from './data'; - -describe('server', () => { - const client = new GraphQLClient('http://localhost:3004/graphql'); - const data = Data.getInstance(); - - it('query bundle', async () => { - const { bundles } = data.entities; - expect(bundles.length).to.be.greaterThan(0); - - for (let i = 0; i < bundles.length; i++) { - const { id, blockNumber, ethPriceUSD } = bundles[i]; - - // Bundle query. - const [bundle] = await client.request(queryBundles, { first: 1, block: { number: blockNumber } }); - expect(bundle.id).to.equal(id); - expect(bundle.ethPriceUSD).to.equal(ethPriceUSD); - } - }); -}); diff --git a/packages/uni-info-watcher/src/server.ts b/packages/uni-info-watcher/src/server.ts index 814f1bfb..1f1f71bd 100644 --- a/packages/uni-info-watcher/src/server.ts +++ b/packages/uni-info-watcher/src/server.ts @@ -11,7 +11,6 @@ import { Client as ERC20Client } from '@vulcanize/erc20-watcher'; import { Client as UniClient } from '@vulcanize/uni-watcher'; import typeDefs from './schema'; -import { createResolvers as createMockResolvers } from './mock/resolvers'; import { createResolvers } from './resolvers'; import { Indexer } from './indexer'; import { Database } from './database'; @@ -33,7 +32,7 @@ export const main = async (): Promise => { await serverCmd.init(Database, Indexer, EventWatcher, { uniClient, erc20Client }); - return process.env.MOCK ? serverCmd.exec(createMockResolvers, typeDefs) : serverCmd.exec(createResolvers, typeDefs); + return serverCmd.exec(createResolvers, typeDefs); }; main().then(() => { diff --git a/packages/uni-watcher/package.json b/packages/uni-watcher/package.json index ab675a08..f70e4ccc 100644 --- a/packages/uni-watcher/package.json +++ b/packages/uni-watcher/package.json @@ -13,7 +13,6 @@ "server": "DEBUG=vulcanize:* node --enable-source-maps dist/server.js", "server:prof": "DEBUG=vulcanize:* node --require pprof --enable-source-maps dist/server.js", "server:dev": "DEBUG=vulcanize:* nodemon --watch src src/server.ts", - "server:mock": "MOCK=1 nodemon src/server.ts ", "job-runner": "DEBUG=vulcanize:* node --enable-source-maps dist/job-runner.js", "job-runner:prof": "DEBUG=vulcanize:* node --require pprof --enable-source-maps dist/job-runner.js", "job-runner:dev": "DEBUG=vulcanize:* nodemon --watch src src/job-runner.ts", diff --git a/packages/uni-watcher/src/mock/data.ts b/packages/uni-watcher/src/mock/data.ts deleted file mode 100644 index 533951e5..00000000 --- a/packages/uni-watcher/src/mock/data.ts +++ /dev/null @@ -1,40 +0,0 @@ -// -// Copyright 2021 Vulcanize, Inc. -// - -// TODO: Pull mock data for 5 tokens from rinkeby. - -export const tokens: {[address: string]: {[variable: string]: string}} = { - '0xd87fea54f506972e3267239ec8e159548892074a': { - name: 'ChainLink Token', - symbol: 'LINK', - decimals: '18', - totalSupply: '1000000' - } -}; - -export const blocks: {[blockHash: string]: {[address: string]: any}} = { - // Block hash. - '0x77b5479a5856dd8ec63df6aabf9ce0913071a6dda3a3d54f3c9c940574bcb8ab': { - - // ERC20 token address. - '0xd87fea54f506972e3267239ec8e159548892074a': { - ...tokens['0xd87fea54f506972e3267239ec8e159548892074a'], - - events: [ - { - name: 'Transfer', - from: '0xDC7d7A8920C8Eecc098da5B7522a5F31509b5Bfc', - to: '0xCA6D29232D1435D8198E3E5302495417dD073d61', - value: '500' - }, - { - name: 'Approval', - owner: '0xDC7d7A8920C8Eecc098da5B7522a5F31509b5Bfc', - spender: '0xCA6D29232D1435D8198E3E5302495417dD073d61', - value: '100' - } - ] - } - } -}; diff --git a/packages/uni-watcher/src/mock/resolvers.ts b/packages/uni-watcher/src/mock/resolvers.ts deleted file mode 100644 index f7b8cc4d..00000000 --- a/packages/uni-watcher/src/mock/resolvers.ts +++ /dev/null @@ -1,33 +0,0 @@ -// -// Copyright 2021 Vulcanize, Inc. -// - -import debug from 'debug'; -import BigInt from 'apollo-type-bigint'; - -import { blocks } from './data'; - -const log = debug('test'); - -export const createResolvers = async (): Promise => { - return { - BigInt: new BigInt('bigInt'), - - TokenEvent: { - __resolveType: (obj: any) => { - // TODO: Return correct type. - return obj.__typename; - } - }, - - Query: { - - events: (_: any, { blockHash, token, name }: { blockHash: string, token: string, name: string }) => { - log('events', blockHash, token, name); - return blocks[blockHash][token].events - .filter((e: any) => !name || name === e.name) - .map((e: any) => ({ event: e })); - } - } - }; -}; diff --git a/packages/uni-watcher/src/server.ts b/packages/uni-watcher/src/server.ts index 2f1e3697..ebbc6e79 100644 --- a/packages/uni-watcher/src/server.ts +++ b/packages/uni-watcher/src/server.ts @@ -8,7 +8,6 @@ import 'graphql-import-node'; import { ServerCmd } from '@cerc-io/cli'; import typeDefs from './schema'; -import { createResolvers as createMockResolvers } from './mock/resolvers'; import { createResolvers } from './resolvers'; import { Indexer } from './indexer'; import { Database } from './database'; @@ -20,7 +19,7 @@ export const main = async (): Promise => { const serverCmd = new ServerCmd(); await serverCmd.init(Database, Indexer, EventWatcher); - return process.env.MOCK ? serverCmd.exec(createMockResolvers, typeDefs) : serverCmd.exec(createResolvers, typeDefs); + return serverCmd.exec(createResolvers, typeDefs); }; main().then(() => { From 2001c259358ecae0fb6d3b6f570b99b053de0786 Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Thu, 24 Nov 2022 14:48:20 +0530 Subject: [PATCH 7/7] Update yarn lockfile --- yarn.lock | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/yarn.lock b/yarn.lock index 36544b8d..ccdcc8a4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1621,11 +1621,6 @@ resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.2.18.tgz#0c8e298dbff8205e2266606c1ea5fbdba29b46e4" integrity sha512-rS27+EkB/RE1Iz3u0XtVL5q36MGDWbgYe7zWiodyKNUnthxY0rukK5V36eiUCtCisB7NN8zKYH6DO2M37qxFEQ== -"@types/chance@^1.1.2": - version "1.1.2" - resolved "https://registry.yarnpkg.com/@types/chance/-/chance-1.1.2.tgz#0f397c17e9d5a9e83914e767ca6f419b2ded09dd" - integrity sha512-OYwnnh2D7QAleRpPWnBQBfDZMlapMHoNeuvyEg7WrDiMApgcKOnXgyiVAl+OzBvhyQmfYkx7YtFXOm8E9IYsNw== - "@types/connect@*": version "3.4.34" resolved "https://registry.yarnpkg.com/@types/connect/-/connect-3.4.34.tgz#170a40223a6d666006d93ca128af2beb1d9b1901" @@ -2786,11 +2781,6 @@ chalk@^4.0.0, chalk@^4.1.0: ansi-styles "^4.1.0" supports-color "^7.1.0" -chance@^1.1.7: - version "1.1.7" - resolved "https://registry.yarnpkg.com/chance/-/chance-1.1.7.tgz#e99dde5ac16681af787b5ba94c8277c090d6cfe8" - integrity sha512-bua/2cZEfzS6qPm0vi3JEvGNbriDLcMj9lKxCQOjUcCJRcyjA7umP0zZm6bKWWlBN04vA0L99QGH/CZQawr0eg== - chardet@^0.7.0: version "0.7.0" resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"