From bf5e40e21fcba4ce4efdfcd4286e623af932cd58 Mon Sep 17 00:00:00 2001 From: artemkolodko Date: Sun, 14 May 2023 13:57:55 +0100 Subject: [PATCH] Reindex null ERC1155 metadata --- .../indexer/contracts/ContractIndexer.ts | 30 ++++++++++++++++++- src/store/postgres/filters/index.ts | 2 ++ src/types/api.ts | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/indexer/indexer/contracts/ContractIndexer.ts b/src/indexer/indexer/contracts/ContractIndexer.ts index 6ca1343..c7db477 100644 --- a/src/indexer/indexer/contracts/ContractIndexer.ts +++ b/src/indexer/indexer/contracts/ContractIndexer.ts @@ -41,12 +41,29 @@ const updateTokensFilter: Filter = { ], } +const emptyMetadataFilter: Filter = { + limit: 20, + offset: 0, + filters: [ + { + property: 'meta', + type: 'eq', + value: 'null', + }, + ], + orderBy: 'block_number', + orderDirection: 'desc', +} + +const reindexEmptyERC1155MetaInterval = 60 * 60 * 1000 + export class ContractIndexer { readonly l: LoggerModule readonly store: PostgresStorage readonly contractType: ContractType readonly shardID: ShardID private contractsCache: string[] = [] + private lastUpdateTimestamp = 0 constructor(shardID: ShardID, contractType: ContractType) { this.shardID = shardID @@ -370,7 +387,17 @@ export class ContractIndexer { let count = 0 while (true) { - const assetsNeedUpdate = await this.store.erc1155.getAssets(updateTokensFilter) + let assetsNeedUpdate = await this.store.erc1155.getAssets(updateTokensFilter) + + if (assetsNeedUpdate.length === 0) { + const needToCheckEmptyMetadata = + Date.now() - this.lastUpdateTimestamp > reindexEmptyERC1155MetaInterval + if (needToCheckEmptyMetadata) { + assetsNeedUpdate = await this.store.erc1155.getAssets(emptyMetadataFilter) + this.l.info(`Reindex empty metadata tokens: ${assetsNeedUpdate.length}`) + } + } + if (!assetsNeedUpdate.length) { break } @@ -572,6 +599,7 @@ export class ContractIndexer { `${this.contractType}_contracts`, blockTo ) + this.lastUpdateTimestamp = Date.now() this.l.info( `Processed [${blockFrom}, ${blockTo}] (${ diff --git a/src/store/postgres/filters/index.ts b/src/store/postgres/filters/index.ts index af2c303..5500a0b 100644 --- a/src/store/postgres/filters/index.ts +++ b/src/store/postgres/filters/index.ts @@ -27,6 +27,8 @@ export const buildSQLQuery = (query: Filter) => { .map((f) => { if (f.type === 'startsFrom') { return `${propertyToString(f.property)} like '${safeSQL(f.value)}%'` + } else if (f.type === 'eq' && f.value === 'null') { + return `${propertyToString(f.property)} is null` } return `${propertyToString(f.property)} ${mapFilterTypeToSQL[f.type]} ${safeSQL(f.value)}` diff --git a/src/types/api.ts b/src/types/api.ts index 921addf..eb89092 100644 --- a/src/types/api.ts +++ b/src/types/api.ts @@ -11,6 +11,7 @@ export type FilterProperty = | 'transaction_type' | 'timestamp' | 'to' + | 'meta' export type TransactionQueryField = 'block_number' | 'block_hash' | 'hash' | 'hash_harmony' export type StakingTransactionQueryField = 'block_number' | 'block_hash' | 'hash'