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/update owner for each cw721 activity ( develop ) #351

Closed
wants to merge 47 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
45fa9b4
docs: cw721
phamphong9981 Jul 5, 2023
b27aa4c
docs: cw20
phamphong9981 Jul 7, 2023
ccc8fbd
Merge branch 'main' into docs/cw721
fibonacci998 Jul 10, 2023
0fa8225
Merge branch 'main' into docs/cw20
fibonacci998 Jul 10, 2023
5e50079
fix: update validator as UNSPECIFIED when not found onchain
fibonacci998 Jul 31, 2023
147fb9b
fix: remove token and delegator_shares when validator is UNRECOGNIZED
fibonacci998 Aug 1, 2023
6fd8005
feat: add test for unrecognized validator
fibonacci998 Aug 3, 2023
d8eb803
feat: add migration to add index time to block, tx
fibonacci998 Aug 7, 2023
49ad0b2
Merge pull request #293 from aura-nw/feat/add-index-by-time-to-block-tx
peara Aug 9, 2023
a901780
Merge pull request #276 from aura-nw/fix/update-validator-not-found
peara Aug 9, 2023
49bcd7c
Merge pull request #249 from aura-nw/docs/cw20
peara Aug 9, 2023
6924407
Merge pull request #243 from aura-nw/docs/cw721
peara Aug 9, 2023
32d103a
fix: allow whitelist query can have depth more than config (#312)
fibonacci998 Aug 15, 2023
685bb4b
feat: reindex cw20 service ( main ) (#277)
phamphong9981 Aug 16, 2023
92795eb
refactor: cw20/cw721 index and add relation ( main ) (#306)
phamphong9981 Aug 16, 2023
b6b007c
fix: duplicate latest migrate contract (#304)
phamphong9981 Aug 16, 2023
887ecfd
Feat/statistics (#272)
peara Aug 22, 2023
8c01c5c
refactor: migrate cw721 activity missing smart contract event ( main …
phamphong9981 Aug 23, 2023
3c325dc
Fix/account stat with feegrant (#337)
fibonacci998 Aug 23, 2023
4adfdf0
Update api_gateway.service.ts (#336)
fibonacci998 Aug 23, 2023
5c86a9f
fix: cw721 activity missing from/to ( main ) (#294)
phamphong9981 Aug 28, 2023
dcec038
feat: crawl ibc tao ( main ) (#278)
phamphong9981 Aug 28, 2023
7e18483
fix: cw721 activity save owner at each time
phamphong9981 Aug 30, 2023
35d094d
fix: cw721 activity missing from/to ( main ) (#294)
phamphong9981 Aug 28, 2023
3c7c287
feat: crawl ibc tao ( main ) (#278)
phamphong9981 Aug 28, 2023
85c4dd9
fix: cw721 activity save owner at each time
phamphong9981 Aug 30, 2023
4873127
fix: resolve conflict
phamphong9981 Aug 30, 2023
8baa6b3
fix: resolve conflict
phamphong9981 Aug 30, 2023
ed03ca3
feat: config json
phamphong9981 Aug 31, 2023
b26cf9c
fix: update owner for each cw721 activity
phamphong9981 Aug 31, 2023
c93f995
fix: update owner for each cw721 activity
phamphong9981 Aug 31, 2023
cbd710f
Merge branch 'fix/approve-cw721-activity' of github.com:aura-nw/horos…
phamphong9981 Aug 31, 2023
0d2c5ef
test: test
phamphong9981 Sep 5, 2023
cbcab6b
Merge branch 'fix/approve-cw721-activity' of github.com:aura-nw/horos…
phamphong9981 Sep 5, 2023
ef4a4ba
Merge branch 'develop' of github.com:aura-nw/horoscope-v2 into fix/ap…
phamphong9981 Sep 5, 2023
b612310
Merge branch 'develop' of github.com:aura-nw/horoscope-v2 into fix/ap…
phamphong9981 Sep 7, 2023
cb66cf6
refactor: code
phamphong9981 Sep 7, 2023
8a07f98
Merge branch 'fix/approve-cw721-activity' of github.com:aura-nw/horos…
phamphong9981 Sep 7, 2023
e1d23ff
refactor: review code
phamphong9981 Sep 7, 2023
ced56b9
refactor: review code
phamphong9981 Sep 7, 2023
cc3467b
refactor: code
phamphong9981 Sep 8, 2023
01e0c70
refactor: migration
phamphong9981 Sep 8, 2023
37963fe
Merge branch 'fix/approve-cw721-activity' of github.com:aura-nw/horos…
phamphong9981 Sep 8, 2023
dcbd97d
refactor: sender
phamphong9981 Sep 8, 2023
4c5931d
Merge branch 'fix/approve-cw721-activity' of github.com:aura-nw/horos…
phamphong9981 Sep 8, 2023
f95072a
refactor: review
phamphong9981 Sep 8, 2023
7ff365a
Merge branch 'fix/approve-cw721-activity' of github.com:aura-nw/horos…
phamphong9981 Sep 8, 2023
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
46 changes: 46 additions & 0 deletions migrations/20230830024631_cw721_activity_add_current_owner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Knex } from 'knex';
import CW721Activity from '../src/models/cw721_tx';
import _, { Dictionary } from 'lodash';
import { CW721_ACTION } from '../src/services/cw721/cw721.service';

export async function up(knex: Knex): Promise<void> {
await knex('cw721_activity').update({
sender: knex.ref('from'),
});
await knex.transaction(async (trx) => {
const activities = await CW721Activity.query()
.joinRelated('event')
.orderBy('event.id', 'asc')
.transacting(trx);
const latestOwners: Dictionary<string> = {};
activities.forEach((activity) => {
const latestOwner =
latestOwners[
activity.cw721_contract_id + '_' + activity.cw721_token_id
];
if (latestOwner) {
activity.from = latestOwner;
} else {
activity.from = null;
}
if (
activity.action === CW721_ACTION.MINT ||
activity.action === CW721_ACTION.TRANSFER ||
activity.action === CW721_ACTION.SEND_NFT
) {
latestOwners[
activity.cw721_contract_id + '_' + activity.cw721_token_id
] = activity.to;
}
});
if (activities.length > 0) {
await CW721Activity.query()
.insert(activities)
.onConflict(['id'])
.merge()
.transacting(trx);
}
});
}

export async function down(knex: Knex): Promise<void> {}
8 changes: 4 additions & 4 deletions src/common/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ export const SERVICE = {
path: 'v1.ReAssignMsgIndexToEvent',
},
},
CrawlIBCTaoService: {
key: 'CrawlIBCTaoService',
name: 'v1.CrawlIBCTaoService',
},
DailyStatisticsService: {
key: 'DailyStatisticsService',
name: 'v1.DailyStatisticsService',
Expand All @@ -258,10 +262,6 @@ export const SERVICE = {
key: 'DailyStatsJobsService',
name: 'v1.DailyStatsJobsService',
},
CrawlIBCTaoService: {
key: 'CrawlIBCTaoService',
name: 'v1.CrawlIBCTaoService',
},
Cw20ReindexingService: {
key: 'Cw20ReindexingService',
name: 'v1.Cw20ReindexingService',
Expand Down
8 changes: 4 additions & 4 deletions src/models/cw721_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ export default class CW721Activity extends BaseModel {

id!: number;

action?: string;
action!: string;

sender?: string;

tx_hash!: string;

cw721_contract_id!: number;

cw721_token_id?: number;
cw721_token_id!: number;

created_at?: Date;

updated_at?: Date;

from?: string;
from!: string | null;

to?: string;
to!: string;

height!: number;

Expand Down
4 changes: 2 additions & 2 deletions src/models/event_attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ export class EventAttribute extends BaseModel {
GRANTER: 'granter',
GRANTEE: 'grantee',
FROM: 'from',
FEE: 'fee',
FEE_PAYER: 'fee_payer',
CLIENT_ID: 'client_id',
CLIENT_TYPE: 'client_type',
CONNECTION_ID: 'connection_id',
Expand All @@ -108,6 +106,8 @@ export class EventAttribute extends BaseModel {
COUNTERPARTY_PORT_ID: 'counterparty_port_id',
COUNTERPARTY_CHANNEL_ID: 'counterparty_channel_id',
MINTER: 'minter',
FEE: 'fee',
FEE_PAYER: 'fee_payer',
DATA_HEX: 'packet_data_hex',
SEQUENCE: 'packet_sequence',
SRC_PORT: 'packet_src_port',
Expand Down
119 changes: 79 additions & 40 deletions src/services/cw721/cw721.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Action,
Service,
} from '@ourparentcenter/moleculer-decorators-extended';
import _ from 'lodash';
import _, { Dictionary } from 'lodash';
import { Context, ServiceBroker } from 'moleculer';
import { Queue } from 'bullmq';
import config from '../../../config.json' assert { type: 'json' };
Expand Down Expand Up @@ -296,9 +296,12 @@ export default class Cw721HandlerService extends BullableService {
),
'address'
);
const orderedEvents = _.orderBy(cw721Events, ['height'], ['asc']);
const cw721Activities: CW721Activity[] = [];
const latestOwners: Dictionary<string> = {};
await knex.transaction(async (trx) => {
const queries: any[] = [];
cw721Events.forEach((cw721Event) => {
// eslint-disable-next-line no-restricted-syntax
for (const cw721Event of orderedEvents) {
// find the cw721 Event's smart contract id
const cw721Contract =
cw721ContractDbRecords[cw721Event.contractAddress];
Expand All @@ -321,49 +324,85 @@ export default class Cw721HandlerService extends BullableService {
);
}
}
queries.push(
CW721Activity.query()
.insert(
CW721Activity.fromJson({
action: cw721Event.action,
sender: cw721Event.sender,
tx_hash: cw721Event.hash,
cw721_contract_id: cw721Contract.id,
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'])
.merge()
.transacting(trx)
// eslint-disable-next-line no-await-in-loop
const from = await this.getLatestOwnerForToken(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

có cái foundToken ngay trên mà nhỉ?

cw721Contract.id,
cw721TokenId,
latestOwners
);
const to =
getAttributeFrom(
cw721Event.attributes,
EventAttribute.ATTRIBUTE_KEY.OWNER
) ||
getAttributeFrom(
cw721Event.attributes,
EventAttribute.ATTRIBUTE_KEY.RECIPIENT
);
const cw721Activity = CW721Activity.fromJson({
action: cw721Event.action,
sender: getAttributeFrom(
cw721Event.attributes,
EventAttribute.ATTRIBUTE_KEY.SENDER
),
tx_hash: cw721Event.hash,
cw721_contract_id: cw721Contract.id,
cw721_token_id: cw721TokenId,
height: cw721Event.height,
smart_contract_event_id: cw721Event.smart_contract_event_id,
to,
from,
});
// push to update records
cw721Activities.push(cw721Activity);
// update latestActivity for this token
if (
cw721Activity.action === CW721_ACTION.MINT ||
cw721Activity.action === CW721_ACTION.TRANSFER ||
cw721Activity.action === CW721_ACTION.SEND_NFT
) {
latestOwners[
`${cw721Activity.cw721_contract_id}_${cw721Activity.cw721_token_id}`
] = cw721Activity.to;
}
}
});
if (queries.length > 0) {
await Promise.all(queries) // Once every query is written
.then(trx.commit) // Try to execute all of them
.catch((e) => {
this.logger.error(e);
trx.rollback();
}); // And rollback in case any of them goes wrong
}
if (cw721Activities.length > 0) {
await CW721Activity.query()
.insert(cw721Activities)
.onConflict(['smart_contract_event_id'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cái này mới tinh mà, làm gì có conflict

.merge()
.transacting(trx);
}
});
}

async getLatestOwnerForToken(
cw721ContractId: number,
cw721TokenId: number | null,
latestOwners: Dictionary<string>
) {
const latestOwner = latestOwners[`${cw721ContractId}_${cw721TokenId}`];
if (latestOwner) {
return latestOwner;
}
const latestActivityDB = await CW721Activity.query()
.joinRelated('event')
.whereIn('cw721_activity.action', [
CW721_ACTION.MINT,
CW721_ACTION.TRANSFER,
CW721_ACTION.SEND_NFT,
])
.andWhere('cw721_contract_id', cw721ContractId)
.andWhere('cw721_token_id', cw721TokenId)
.orderBy('event.block_height', 'desc')
.first();
if (latestActivityDB) {
return latestActivityDB.to;
}
return null;
}

// handle Instantiate Msgs
async handleInstantiateMsgs(msgsInstantiate: SmartContractEvent[]) {
const cw721Contracts: any[] = await SmartContract.query()
Expand Down
Loading
Loading