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

fix: cw721 activity missing from/to ( main ) #294

Merged
merged 10 commits into from
Aug 28, 2023
55 changes: 55 additions & 0 deletions migrations/20230808072704_update_data_cw721_activity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Knex } from 'knex';
import config from '../config.json' assert { type: 'json' };
import CW721Activity from '../src/models/cw721_tx';
import { getAttributeFrom } from '../src/common/utils/smart_contract';
import { EventAttribute } from '../src/models';
const UPDATE_CW721_ACTIONS = ['mint', 'burn', 'transfer_nft', 'send_nft'];
export async function up(knex: Knex): Promise<void> {
await knex.transaction(async (trx) => {
let prevId = 0;
while (true) {
const handleRecords = await CW721Activity.query()
.withGraphFetched('smart_contract_event.attributes')
.where('cw721_activity.from', null)
.andWhere('cw721_activity.to', null)
.andWhere('cw721_activity.id', '>', prevId)
.whereIn('cw721_activity.action', UPDATE_CW721_ACTIONS)
.whereNotNull('smart_contract_event_id')
.orderBy('cw721_activity.id')
.limit(100)
.transacting(trx);

if (handleRecords.length > 0) {
const patchQueries: any[] = [];
handleRecords.forEach((record) => {
patchQueries.push(
CW721Activity.query()
.patch({
from: getAttributeFrom(
record.smart_contract_event.attributes,
EventAttribute.ATTRIBUTE_KEY.SENDER
),
to:
getAttributeFrom(
record.smart_contract_event.attributes,
EventAttribute.ATTRIBUTE_KEY.OWNER
) ||
getAttributeFrom(
record.smart_contract_event.attributes,
EventAttribute.ATTRIBUTE_KEY.RECIPIENT
),
})
.where('id', record.id)
.transacting(trx)
);
});
await Promise.all(patchQueries);
prevId = handleRecords[handleRecords.length - 1].id;
} else {
break;
}
}
});
}

export async function down(knex: Knex): Promise<void> {}
4 changes: 3 additions & 1 deletion src/models/cw721_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { Event } from './event';
export default class CW721Activity extends BaseModel {
static softDelete = false;

id?: number;
smart_contract_event!: SmartContractEvent;

id!: number;

action?: string;

Expand Down
1 change: 1 addition & 0 deletions src/models/event_attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class EventAttribute extends BaseModel {
GRANTER: 'granter',
GRANTEE: 'grantee',
FROM: 'from',
MINTER: 'minter',
FEE: 'fee',
FEE_PAYER: 'fee_payer',
};
Expand Down
2 changes: 2 additions & 0 deletions src/models/smart_contract_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { TransactionMessage } from './transaction_message';
export class SmartContractEvent extends BaseModel {
[relation: string]: any;

attributes!: SmartContractEventAttribute;

id!: number;

smart_contract_id!: number;
Expand Down
13 changes: 13 additions & 0 deletions src/services/cw721/cw721.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,19 @@ export default class Cw721HandlerService extends BullableService {
cw721_token_id: cw721TokenId,
height: cw721Event.height,
smart_contract_event_id: cw721Event.smart_contract_event_id,
from: getAttributeFrom(
cw721Event.attributes,
EventAttribute.ATTRIBUTE_KEY.SENDER
),
to:
getAttributeFrom(
cw721Event.attributes,
EventAttribute.ATTRIBUTE_KEY.OWNER
) ||
getAttributeFrom(
cw721Event.attributes,
EventAttribute.ATTRIBUTE_KEY.RECIPIENT
),
})
)
.onConflict(['smart_contract_event_id'])
Expand Down
37 changes: 35 additions & 2 deletions test/unit/services/cw721/cw721.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ import { AfterAll, BeforeAll, Describe, Test } from '@jest-decorated/core';
import { ServiceBroker } from 'moleculer';
import { BULL_JOB_NAME } from '../../../../src/common';
import knex from '../../../../src/common/utils/db_connection';
import { getContractActivities } from '../../../../src/common/utils/smart_contract';
import { Block, BlockCheckpoint, Transaction } from '../../../../src/models';
import {
getAttributeFrom,
getContractActivities,
} from '../../../../src/common/utils/smart_contract';
import {
Block,
BlockCheckpoint,
EventAttribute,
Transaction,
} from '../../../../src/models';
import { Code } from '../../../../src/models/code';
import CW721Contract from '../../../../src/models/cw721_contract';
import CW721Token from '../../../../src/models/cw721_token';
Expand Down Expand Up @@ -1296,6 +1304,31 @@ export default class AssetIndexerTest {
expect(cw721Activities[0].cw721_token_id).toEqual(1);
expect(cw721Activities[1].cw721_token_id).toEqual(0);
expect(cw721Activities[2].cw721_token_id).toEqual(2);
expect(cw721Activities[0].from).toEqual(null);
expect(cw721Activities[0].to).toEqual(
getAttributeFrom(
mockActivityMsgs[0].attributes,
EventAttribute.ATTRIBUTE_KEY.OWNER
)
);
expect(cw721Activities[1].from).toEqual(
getAttributeFrom(
mockActivityMsgs[1].attributes,
EventAttribute.ATTRIBUTE_KEY.SENDER
)
);
expect(cw721Activities[1].to).toEqual(
getAttributeFrom(
mockActivityMsgs[1].attributes,
EventAttribute.ATTRIBUTE_KEY.RECIPIENT
)
);
expect(cw721Activities[2].from).toEqual(
getAttributeFrom(
mockActivityMsgs[2].attributes,
EventAttribute.ATTRIBUTE_KEY.SENDER
)
);
}

@Test('test handle multi contract events')
Expand Down