Skip to content

Commit

Permalink
Refactor/cw721 ( develop ) (#386)
Browse files Browse the repository at this point in the history
* refactor: cw721

* test: full test

* feat: migration

* fix: lint

* fix: test

* fix: test

* refactor: test

* refactor: code

* refactor: test

* fix: test

* test: code

* test: code

* refactor: code

* refactor: review

* refactor: review

* refactor: clean redundance get DB field
  • Loading branch information
phamphong9981 authored Sep 26, 2023
1 parent 62d6da3 commit 192caf3
Show file tree
Hide file tree
Showing 9 changed files with 1,657 additions and 987 deletions.
46 changes: 46 additions & 0 deletions migrations/20230915032739_cw721_activity_update_owner_for_each.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_handler';

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 | null> = {};
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> {}
16 changes: 16 additions & 0 deletions src/models/cw721_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fromBase64, fromUtf8, toHex, toUtf8 } from '@cosmjs/encoding';
import { JsonRpcSuccessResponse } from '@cosmjs/json-rpc';
import { createJsonRpcRequest } from '@cosmjs/tendermint-rpc/build/jsonrpc';
import { Model } from 'objection';
import { Knex } from 'knex';
import { getHttpBatchClient } from '../common';
import BaseModel from './base';
// eslint-disable-next-line import/no-cycle
Expand Down Expand Up @@ -38,6 +39,8 @@ export default class CW721Contract extends BaseModel {

updated_at?: Date;

smart_contract!: SmartContract;

static get tableName() {
return 'cw721_contract';
}
Expand Down Expand Up @@ -235,4 +238,17 @@ export default class CW721Contract extends BaseModel {
});
return tokensOwner;
}

static async getCw721TrackedContracts(
addresses: string[],
trx: Knex.Transaction
) {
return CW721Contract.query()
.transacting(trx)
.alias('cw721_contract')
.withGraphJoined('smart_contract')
.whereIn('smart_contract.address', addresses)
.andWhere('track', true)
.select('smart_contract.address as address', 'cw721_contract.id as id');
}
}
4 changes: 3 additions & 1 deletion src/models/cw721_token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class CW721Token extends BaseModel {

owner?: string;

id?: number;
id!: number;

cw721_contract_id!: number;

Expand All @@ -24,6 +24,8 @@ export default class CW721Token extends BaseModel {

burned?: boolean;

contract!: CW721Contract;

static get tableName() {
return 'cw721_token';
}
Expand Down
47 changes: 41 additions & 6 deletions src/models/cw721_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,35 @@ import BaseModel from './base';
// eslint-disable-next-line import/no-cycle
import CW721Contract from './cw721_contract';
import CW721Token from './cw721_token';
import { SmartContractEvent } from './smart_contract_event';
import { Event } from './event';
import { SmartContractEvent } from './smart_contract_event';

export default class CW721Activity extends BaseModel {
static softDelete = false;

[relation: string]: any;

smart_contract_event!: SmartContractEvent;

id!: number;

action?: string;
action!: string;

sender?: 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 | null;

height!: number;

Expand Down Expand Up @@ -94,4 +96,37 @@ export default class CW721Activity extends BaseModel {
},
};
}

static async getCw721ContractEvents(
startBlock: number,
endBlock: number,
smartContractId?: number,
page?: { prevId: number; limit: number }
) {
return SmartContractEvent.query()
.withGraphFetched('attributes(selectAttribute)')
.joinRelated('[message, tx, smart_contract.code]')
.where('smart_contract:code.type', 'CW721')
.where('tx.height', '>', startBlock)
.andWhere('tx.height', '<=', endBlock)
.modify((builder) => {
if (smartContractId) {
builder.andWhere('smart_contract.id', smartContractId);
}
if (page) {
builder
.andWhere('smart_contract_event.id', '>', page.prevId)
.orderBy('smart_contract_event.id', 'asc')
.limit(page.limit);
}
})
.select(
'smart_contract.address as contractAddress',
'smart_contract_event.action',
'smart_contract_event.id as smart_contract_event_id',
'tx.hash',
'tx.height'
)
.orderBy('smart_contract_event.id');
}
}
17 changes: 10 additions & 7 deletions src/services/cw721/cw721-reindexing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class CW721ReindexingService extends BullableService {
}

async handleReindexAll(smartContractId: number, contractAddress: string) {
const cw721Contract = await CW721Contract.query()
let cw721Contract = await CW721Contract.query()
.withGraphJoined('smart_contract')
.where('smart_contract.address', contractAddress)
.select(['cw721_contract.id'])
Expand Down Expand Up @@ -89,24 +89,27 @@ export default class CW721ReindexingService extends BullableService {
.where('cw721_contract_id', cw721Contract.id);
await CW721Contract.query().deleteById(cw721Contract.id);
}
await CW721Contract.query().insertGraph({
...CW721Contract.fromJson({
cw721Contract = await CW721Contract.query().insert(
CW721Contract.fromJson({
contract_id: smartContractId,
symbol: contractInfo?.symbol,
minter: contractInfo?.minter,
name: contractInfo?.name,
track: true,
}),
tokens: currentTokensOwner.map((tokenOwner) =>
})
);
await CW721Token.query().insert(
currentTokensOwner.map((tokenOwner) =>
CW721Token.fromJson({
cw721_contract_id: cw721Contract?.id,
token_id: tokenOwner.token_id,
media_info: null,
owner: tokenOwner.owner,
last_updated_height: tokenOwner.last_updated_height,
burned: false,
})
),
});
)
);
// handle from minUpdatedHeightOwner to blockHeight
await this.broker.call(
SERVICE.V1.Cw721.HandleRangeBlockMissingContract.path,
Expand Down
Loading

0 comments on commit 192caf3

Please sign in to comment.