diff --git a/packages/eden-watcher/src/indexer.ts b/packages/eden-watcher/src/indexer.ts index c6b88b44..0b7e450a 100644 --- a/packages/eden-watcher/src/indexer.ts +++ b/packages/eden-watcher/src/indexer.ts @@ -261,8 +261,10 @@ export class Indexer implements IndexerInterface { const checkpoint = await this.getLatestIPLDBlock(contractAddress, 'checkpoint'); // There should be an initial checkpoint at least. - // Assumption: There should be no events for the contract at the starting block. - assert(checkpoint, 'Initial checkpoint doesn\'t exist'); + // Return if initial checkpoint doesn't exist. + if (!checkpoint) { + return; + } // Check if the latest checkpoint is in the same block. assert(checkpoint.block.blockHash !== block.blockHash, 'Checkpoint already created for the block hash.'); @@ -289,7 +291,7 @@ export class Indexer implements IndexerInterface { const checkpointBlock = await this.getLatestIPLDBlock(contract.address, 'checkpoint'); if (!checkpointBlock) { - if (blockNumber === contract.startingBlock) { + if (blockNumber >= contract.startingBlock) { // Call initial checkpoint hook. await createInitialCheckpoint(this, contract.address, blockHash); } diff --git a/packages/eden-watcher/src/job-runner.ts b/packages/eden-watcher/src/job-runner.ts index 60087c69..0a1f8e82 100644 --- a/packages/eden-watcher/src/job-runner.ts +++ b/packages/eden-watcher/src/job-runner.ts @@ -146,6 +146,9 @@ export const main = async (): Promise => { graphWatcher.setIndexer(indexer); await graphWatcher.init(); + // Watching all the contracts in the subgraph. + await graphWatcher.addContracts(); + const jobQueueConfig = config.jobQueue; assert(jobQueueConfig, 'Missing job queue config'); diff --git a/packages/graph-node/src/database.ts b/packages/graph-node/src/database.ts index f2c2191a..70328750 100644 --- a/packages/graph-node/src/database.ts +++ b/packages/graph-node/src/database.ts @@ -42,27 +42,35 @@ export class Database { async getEntity (entity: (new () => Entity) | string, id: string, blockHash: string): Promise { const queryRunner = this._conn.createQueryRunner(); - const repo = queryRunner.manager.getRepository(entity); - const whereOptions: { [key: string]: any } = { id }; - if (blockHash) { - whereOptions.blockHash = blockHash; - } + try { + const repo = queryRunner.manager.getRepository(entity); - const findOptions = { - where: whereOptions, - order: { - blockNumber: 'DESC' + const whereOptions: { [key: string]: any } = { id }; + + if (blockHash) { + whereOptions.blockHash = blockHash; } - }; - let entityData = await repo.findOne(findOptions as FindOneOptions); + const findOptions = { + where: whereOptions, + order: { + blockNumber: 'DESC' + } + }; - if (!entityData && findOptions.where.blockHash) { - entityData = await this._baseDatabase.getPrevEntityVersion(queryRunner, repo, findOptions); + let entityData = await repo.findOne(findOptions as FindOneOptions); + + if (!entityData && findOptions.where.blockHash) { + entityData = await this._baseDatabase.getPrevEntityVersion(queryRunner, repo, findOptions); + } + + return entityData; + } catch (error) { + console.log(error); + } finally { + await queryRunner.release(); } - - return entityData; } async saveEntity (entity: string, data: any): Promise { @@ -129,7 +137,7 @@ export class Database { // Get blockNumber as _blockNumber and blockHash as _blockHash from the entityInstance (wasm). if (['_blockNumber', '_blockHash'].includes(propertyName)) { - return fromEntityValue(instanceExports, entityInstance, propertyName); + return fromEntityValue(instanceExports, entityInstance, propertyName.slice(1)); } return fromEntityValue(instanceExports, entityInstance, propertyName); diff --git a/packages/graph-node/src/utils.ts b/packages/graph-node/src/utils.ts index 57a37bb1..3f021271 100644 --- a/packages/graph-node/src/utils.ts +++ b/packages/graph-node/src/utils.ts @@ -303,6 +303,9 @@ export const createBlock = async (instanceExports: any, blockData: Block): Promi const authorPtr = await Address.zero(); + const sizePtr = await __newString('0'); + const size = await BigInt.fromString(sizePtr); + // Missing fields from watcher in block data: // author // size @@ -320,7 +323,7 @@ export const createBlock = async (instanceExports: any, blockData: Block): Promi blockTimestamp, difficulty, totalDifficulty, - null + size ); }; diff --git a/packages/graph-node/src/watcher.ts b/packages/graph-node/src/watcher.ts index 37d6a2b0..3a431bdf 100644 --- a/packages/graph-node/src/watcher.ts +++ b/packages/graph-node/src/watcher.ts @@ -95,6 +95,16 @@ export class GraphWatcher { }, {}); } + async addContracts () { + assert(this._indexer?.watchContract); + + // Watching the contract(s). + for (const dataSource of this._dataSources) { + const { source: { address, startBlock }, name } = dataSource; + await this._indexer.watchContract(address, name, true, startBlock); + } + } + async handleEvent (eventData: any) { const { contract, event, eventSignature, block, tx, eventIndex } = eventData; @@ -147,6 +157,8 @@ export class GraphWatcher { async handleBlock (blockHash: string) { const blockData = await getFullBlock(this._postgraphileClient, blockHash); + this._context.event.block = blockData; + // Call block handler(s) for each contract. for (const dataSource of this._dataSources) { // Check if block handler(s) are configured. diff --git a/packages/graph-node/test/subgraph/eden/EdenNetwork/EdenNetwork.wasm b/packages/graph-node/test/subgraph/eden/EdenNetwork/EdenNetwork.wasm index 75f5f60a..8a084cc5 100644 Binary files a/packages/graph-node/test/subgraph/eden/EdenNetwork/EdenNetwork.wasm and b/packages/graph-node/test/subgraph/eden/EdenNetwork/EdenNetwork.wasm differ diff --git a/packages/graph-node/test/subgraph/eden/EdenNetworkDistribution/EdenNetworkDistribution.wasm b/packages/graph-node/test/subgraph/eden/EdenNetworkDistribution/EdenNetworkDistribution.wasm index 422c1bf7..7445c27a 100644 Binary files a/packages/graph-node/test/subgraph/eden/EdenNetworkDistribution/EdenNetworkDistribution.wasm and b/packages/graph-node/test/subgraph/eden/EdenNetworkDistribution/EdenNetworkDistribution.wasm differ diff --git a/packages/graph-node/test/subgraph/eden/EdenNetworkGovernance/EdenNetworkGovernance.wasm b/packages/graph-node/test/subgraph/eden/EdenNetworkGovernance/EdenNetworkGovernance.wasm index d1fb16d6..89efa85b 100644 Binary files a/packages/graph-node/test/subgraph/eden/EdenNetworkGovernance/EdenNetworkGovernance.wasm and b/packages/graph-node/test/subgraph/eden/EdenNetworkGovernance/EdenNetworkGovernance.wasm differ diff --git a/packages/util/src/types.ts b/packages/util/src/types.ts index 60be5724..85d11d80 100644 --- a/packages/util/src/types.ts +++ b/packages/util/src/types.ts @@ -73,6 +73,7 @@ export interface IndexerInterface { isWatchedContract?: (address: string) => Promise; cacheContract?: (contract: ContractInterface) => void; createDiffStaged?: (contractAddress: string, blockHash: string, data: any) => Promise + watchContract?: (address: string, kind: string, checkpoint: boolean, startingBlock?: number) => Promise } export interface EventWatcherInterface {