Skip to content

Commit

Permalink
Merge pull request #201 from UniqueNetwork/fix/v1.7.2-index-tokens-owner
Browse files Browse the repository at this point in the history
Fix/v1.7.2 index tokens owner
  • Loading branch information
icehuntmen authored Feb 14, 2023
2 parents c74b6e8 + 3280479 commit 705d474
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm --no-git-tag-version version patch && git add package.json
28 changes: 22 additions & 6 deletions apps/crawler/src/sdk/sdk.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export class SdkService {
tokenId: number,
at?: string,
): Promise<TokenByIdResult | null> {
return await this.sdk.tokens.get({ collectionId, tokenId });
if (at) {
return await this.sdk.tokens.get({ collectionId, tokenId, at });
} else {
return await this.sdk.tokens.get({ collectionId, tokenId });
}
}

@SdkCache('isTokenBundle')
Expand Down Expand Up @@ -111,10 +115,22 @@ export class SdkService {
}

@SdkCache('getTotalPieces')
async getTotalPieces(tokenId, collectionId): Promise<any> {
return await this.sdk.refungible.totalPieces({
tokenId: tokenId,
collectionId: collectionId,
});
async getTotalPieces(
tokenId: number,
collectionId: number,
at?: string,
): Promise<any> {
if (at) {
return await this.sdk.refungible.totalPieces({
tokenId,
collectionId,
at,
});
} else {
return await this.sdk.refungible.totalPieces({
tokenId,
collectionId,
});
}
}
}
7 changes: 6 additions & 1 deletion apps/crawler/src/services/extrinsic.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,12 @@ export class ExtrinsicService {
tokenId: extrinsic.token_id,
});
const updateTokenTransfer = { ...extrinsic, ...pieceToken };
await this.updateOrSaveTokenOwnerPart(extrinsic, updateTokenTransfer);
//await this.updateOrSaveTokenOwnerPart(extrinsic, updateTokenTransfer);
await this.tokensOwnersRepository.upsert({ ...updateTokenTransfer }, [
'collection_id',
'token_id',
'owner',
]);
}
}

Expand Down
58 changes: 48 additions & 10 deletions apps/crawler/src/services/token/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export class TokenService {
);
let rejected = [];
let data = [];

for (const chunk of eventChunks) {
const result = await Promise.allSettled(
chunk.map((event) => {
Expand All @@ -143,10 +144,7 @@ export class TokenService {
const { blockHash, blockTimestamp, blockNumber } = blockCommonData;
const eventName = `${section}.${method}`;

if (
eventName === 'Common.ItemCreated' ||
eventName === 'Common.Transfer'
) {
if (eventName === 'Common.ItemCreated') {
data = JSON.parse(event.data);
}

Expand All @@ -165,6 +163,9 @@ export class TokenService {
}

if (TOKEN_UPDATE_EVENTS.includes(eventName)) {
if (eventName === 'Common.Transfer') {
data = JSON.parse(event.data);
}
return this.update({
blockNumber,
collectionId,
Expand Down Expand Up @@ -211,15 +212,16 @@ export class TokenService {
data: any;
}): Promise<SubscriberAction> {
const tokenData = await this.getTokenData(collectionId, tokenId, blockHash);

let result;

if (tokenData) {
const { tokenDecoded } = tokenData;
const needCheckNesting = eventName === EventName.TRANSFER;

const pieces = await this.sdkService.getTotalPieces(
tokenId,
collectionId,
blockHash,
);

if (data.length != 0) {
Expand All @@ -237,7 +239,11 @@ export class TokenService {
block_number: blockNumber,
};

await this.checkAndSaveOrUpdateTokenOwnerPart(tokenOwner);
await this.tokensOwnersRepository.upsert({ ...tokenOwner }, [
'collection_id',
'token_id',
'owner',
]);
}

const preparedData = await this.prepareDataForDb(
Expand All @@ -259,9 +265,17 @@ export class TokenService {
},
['collection_id', 'token_id'],
);

result = SubscriberAction.UPSERT;
} else {
const ownerToken = normalizeSubstrateAddress(data[2].value);

await this.burnTokenOwnerPart({
collection_id: collectionId,
token_id: tokenId,
owner: ownerToken,
owner_normalized: ownerToken,
block_number: blockNumber,
});
// No entity returned from sdk. Most likely it was destroyed in a future block.
await this.burn(collectionId, tokenId);

Expand All @@ -271,7 +285,11 @@ export class TokenService {
return result;
}

async burn(collectionId: number, tokenId: number) {
async burn(
collectionId: number,
tokenId: number,
owner?: string,
): Promise<any> {
await this.nestingService.removeTokenFromParents(collectionId, tokenId);

return this.tokensRepository.update(
Expand All @@ -298,7 +316,6 @@ export class TokenService {
blockHash,
);
}

if (!tokenDecoded) {
return null;
}
Expand All @@ -325,7 +342,7 @@ export class TokenService {
});
}

private async checkAndSaveOrUpdateTokenOwnerPart(tokenOwner: TokenOwnerData) {
private async burnTokenOwnerPart(tokenOwner: TokenOwnerData) {
const ownerToken = await this.tokensOwnersRepository.findOne({
where: {
owner: tokenOwner.owner,
Expand All @@ -341,6 +358,27 @@ export class TokenService {
collection_id: tokenOwner.collection_id,
token_id: tokenOwner.token_id,
},
{
amount: 0,
block_number: tokenOwner.block_number,
},
);
}
}

private async checkAndSaveOrUpdateTokenOwnerPart(tokenOwner: TokenOwnerData) {
const ownerToken = await this.tokensOwnersRepository.findOne({
where: {
owner: tokenOwner.owner,
collection_id: tokenOwner.collection_id,
token_id: tokenOwner.token_id,
},
});
if (ownerToken != null) {
await this.tokensOwnersRepository.update(
{
id: ownerToken.id,
},
{
amount: tokenOwner.amount,
block_number: tokenOwner.block_number,
Expand Down
10 changes: 5 additions & 5 deletions apps/crawler/src/services/token/token.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export interface TokenData {

export interface TokenOwnerData {
owner: string;
owner_normalized: string;
owner_normalized?: string;
collection_id: number;
token_id: number;
date_created: string;
amount: number;
type: TokenType;
block_number: number;
date_created?: string;
amount?: number;
type?: TokenType;
block_number?: number;
}
1 change: 0 additions & 1 deletion apps/crawler/src/subscribers/blocks.subscriber.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export class BlocksSubscriberService implements ISubscriberService {
blockCommonData,
blockItems,
});
//console.dir(events);
const [itemCounts] = await Promise.all([
this.blockService.upsert({
block,
Expand Down
18 changes: 18 additions & 0 deletions migrations/1676377784283-unqindex-tokens-owners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class unqindextokensowners1676377784283 implements MigrationInterface {
name = 'unqindextokensowners1676377784283';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`TRUNCATE TABLE tokens_owners RESTART IDENTITY CASCADE`,
);
await queryRunner.query(
`CREATE UNIQUE INDEX "tokens_owners_idx" ON "tokens_owners" ("token_id", "collection_id","owner")`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "public"."tokens_owners_idx"`);
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "polkastats-backend-uniquenetwork",
"version": "1.7.1",
"version": "1.7.4",
"description": "",
"author": "Unique Network Team",
"private": true,
Expand Down

0 comments on commit 705d474

Please sign in to comment.