Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reindex null ERC1155 metadata #105

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/indexer/indexer/contracts/ContractIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -572,6 +599,7 @@ export class ContractIndexer {
`${this.contractType}_contracts`,
blockTo
)
this.lastUpdateTimestamp = Date.now()

this.l.info(
`Processed [${blockFrom}, ${blockTo}] (${
Expand Down
2 changes: 2 additions & 0 deletions src/store/postgres/filters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`
Expand Down
1 change: 1 addition & 0 deletions src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down