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

feat: reindex specific cw721 tokens #665

Open
wants to merge 1 commit into
base: develop
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
4 changes: 4 additions & 0 deletions src/common/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ export const SERVICE = {
key: 'reindexing',
path: 'v1.Cw721ReindexingService.reindexing',
},
ReindexingTokens: {
key: 'reindexingTokens',
path: 'v1.Cw721ReindexingService.reindexingTokens',
},
},
JobService: {
CreateBlockPartition: {
Expand Down
36 changes: 36 additions & 0 deletions src/services/api-gateways/cw721_admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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<string, unknown>
>
) {
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,
}
);
}
}
36 changes: 36 additions & 0 deletions src/services/cw721/cw721-reindexing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand All @@ -29,6 +30,10 @@ interface ICw721ReindexingServiceParams {
type: string;
}

interface ICw721ReindexingTokensParams {
ids: number[];
}

export const REINDEX_TYPE = {
ALL: 'all',
HISTORY: 'history',
Expand Down Expand Up @@ -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<ICw721ReindexingTokensParams>
): Promise<void> {
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<void> {
await this.broker.waitForServices(SERVICE.V1.Cw721.name);
return super._start();
Expand Down
27 changes: 27 additions & 0 deletions test/unit/services/cw721/cw721-reindexing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 = {
Expand Down
Loading