From 628b532879e8fef80104fd24de00414e863fa8b1 Mon Sep 17 00:00:00 2001 From: phamphong9981 Date: Tue, 27 Feb 2024 11:22:44 +0700 Subject: [PATCH] feat: reindex specific cw721 tokens --- src/common/constant.ts | 4 +++ .../api-gateways/cw721_admin.service.ts | 36 +++++++++++++++++++ .../cw721/cw721-reindexing.service.ts | 36 +++++++++++++++++++ .../services/cw721/cw721-reindexing.spec.ts | 27 ++++++++++++++ 4 files changed, 103 insertions(+) diff --git a/src/common/constant.ts b/src/common/constant.ts index 2c51bff8f..713414aba 100644 --- a/src/common/constant.ts +++ b/src/common/constant.ts @@ -271,6 +271,10 @@ export const SERVICE = { key: 'reindexing', path: 'v1.Cw721ReindexingService.reindexing', }, + ReindexingTokens: { + key: 'reindexingTokens', + path: 'v1.Cw721ReindexingService.reindexingTokens', + }, }, JobService: { CreateBlockPartition: { diff --git a/src/services/api-gateways/cw721_admin.service.ts b/src/services/api-gateways/cw721_admin.service.ts index 10ca84530..dff3b4e9f 100644 --- a/src/services/api-gateways/cw721_admin.service.ts +++ b/src/services/api-gateways/cw721_admin.service.ts @@ -3,6 +3,7 @@ import { Context, ServiceBroker } from 'moleculer'; import networks from '../../../network.json' assert { type: 'json' }; import BaseService from '../../base/base.service'; import { REINDEX_TYPE } from '../cw721/cw721-reindexing.service'; +import { SERVICE } from '../../common'; @Service({ name: 'cw721-admin', @@ -54,4 +55,39 @@ export default class Cw721AdminService extends BaseService { } ); } + + @Post('/cw721-reindexing-tokens', { + name: 'cw721ReindexingTokens', + params: { + chainid: { + type: 'string', + optional: false, + enum: networks.map((network) => network.chainId), + }, + ids: { + type: 'array', + optional: false, + items: 'number', + }, + }, + }) + async cw721ReindexingTokens( + ctx: Context< + { + chainid: string; + ids: number[]; + }, + Record + > + ) { + const selectedChain = networks.find( + (network) => network.chainId === ctx.params.chainid + ); + return this.broker.call( + `${SERVICE.V1.CW721ReindexingService.ReindexingTokens.path}@${selectedChain?.moleculerNamespace}`, + { + ids: ctx.params.ids, + } + ); + } } diff --git a/src/services/cw721/cw721-reindexing.service.ts b/src/services/cw721/cw721-reindexing.service.ts index b5f279662..4272ebe37 100644 --- a/src/services/cw721/cw721-reindexing.service.ts +++ b/src/services/cw721/cw721-reindexing.service.ts @@ -17,6 +17,7 @@ import CW721ContractStats from '../../models/cw721_stats'; import CW721Token from '../../models/cw721_token'; import CW721Activity from '../../models/cw721_tx'; import { ICw721ReindexingHistoryParams } from './cw721.service'; +import knex from '../../common/utils/db_connection'; export interface IAddressParam { contractAddresses: string[]; @@ -29,6 +30,10 @@ interface ICw721ReindexingServiceParams { type: string; } +interface ICw721ReindexingTokensParams { + ids: number[]; +} + export const REINDEX_TYPE = { ALL: 'all', HISTORY: 'history', @@ -212,6 +217,37 @@ export default class CW721ReindexingService extends BullableService { ); } + @Action({ + name: SERVICE.V1.CW721ReindexingService.ReindexingTokens.key, + params: { + ids: { + type: 'array', + items: 'number', + optional: false, + }, + }, + }) + public async reindexCw721Tokens( + ctx: Context + ): Promise { + const { ids } = ctx.params; + await knex.transaction(async (trx) => { + const tokens = await CW721Token.query() + .delete() + .whereIn('id', ids) + .returning('*') + .transacting(trx); + if (tokens.length > 0) { + const newTokens = tokens.map((token) => CW721Token.fromJson({ + ...token, + id: undefined, + media_info: null, + })); + await CW721Token.query().insert(newTokens).transacting(trx); + } + }); + } + async _start(): Promise { await this.broker.waitForServices(SERVICE.V1.Cw721.name); return super._start(); diff --git a/test/unit/services/cw721/cw721-reindexing.spec.ts b/test/unit/services/cw721/cw721-reindexing.spec.ts index 0725b8f58..d918a5071 100644 --- a/test/unit/services/cw721/cw721-reindexing.spec.ts +++ b/test/unit/services/cw721/cw721-reindexing.spec.ts @@ -6,6 +6,7 @@ import { Test, } from '@jest-decorated/core'; import { Errors, ServiceBroker } from 'moleculer'; +import _ from 'lodash'; import { BULL_JOB_NAME, SERVICE } from '../../../../src/common'; import knex from '../../../../src/common/utils/db_connection'; import { BlockCheckpoint, Code, EventAttribute } from '../../../../src/models'; @@ -352,6 +353,32 @@ export default class TestCw721MissingContractService { }); } + @Test('Test ReindexingTokensService function') + public async testReindexingTokensService() { + const tokens = await CW721Token.query() + .whereIn('token_id', [ + this.cw721Contract.tokens[0].token_id, + this.cw721Contract.tokens[1].token_id, + ]) + .orderBy('id'); + await this.broker.call( + SERVICE.V1.CW721ReindexingService.ReindexingTokens.path, + { + ids: tokens.map((token) => token.id), + } + ); + const newToken = await CW721Token.query().whereIn( + 'token_id', + tokens.map((token) => token.token_id) + ); + expect(newToken).toMatchObject( + tokens.map((token) => ({ + ..._.omit(token, 'id'), + media_info: null, + })) + ); + } + @Test('Test ReindexingService function') public async testReindexingService() { const mockContractInfo = {