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/v1.7.2 rft tokens nested #204

Merged
merged 2 commits into from
Feb 17, 2023
Merged
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
37 changes: 29 additions & 8 deletions apps/crawler/src/services/extrinsic.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
ExtrinsicMethod,
ExtrinsicSection,
} from '@common/constants';
import { normalizeSubstrateAddress, normalizeTimestamp } from '@common/utils';
import {
getParentCollectionAndToken,
normalizeSubstrateAddress,
normalizeTimestamp,
} from '@common/utils';
import { Event } from '@entities/Event';
import { Extrinsic } from '@entities/Extrinsic';
import { Injectable } from '@nestjs/common';
Expand All @@ -20,6 +24,7 @@ import { EventValues } from './event/event.types';
import { TokensOwners } from '@entities/TokensOwners';
import { SdkService } from '../sdk/sdk.service';
import { Tokens } from '@entities/Tokens';
import * as console from 'console';

const EXTRINSICS_TRANSFER_METHODS = [
ExtrinsicMethod.TRANSFER,
Expand Down Expand Up @@ -192,7 +197,6 @@ export class ExtrinsicService {
}

const { to, from, tokenId, collectionId } = event?.values as any;

substrateAddress.push({
owner: from.value,
owner_normalized: normalizeSubstrateAddress(from.value),
Expand All @@ -201,13 +205,22 @@ export class ExtrinsicService {
date_created: String(normalizeTimestamp(blockTimestamp)),
block_number: blockNumber,
});
let parentId = null;
const toNestedAddress =
getParentCollectionAndToken(to.value) || undefined;
if (toNestedAddress) {
const { collectionId, tokenId } = toNestedAddress;
parentId = `${collectionId}_${tokenId}`;
}
substrateAddress.push({
owner: to.value,
owner_normalized: normalizeSubstrateAddress(to.value),
collection_id: collectionId,
token_id: tokenId,
date_created: String(normalizeTimestamp(blockTimestamp)),
block_number: blockNumber,
parent_id: parentId,
nested: true,
});
});

Expand Down Expand Up @@ -237,12 +250,20 @@ export class ExtrinsicService {
tokenId: extrinsic.token_id,
});
const updateTokenTransfer = { ...extrinsic, ...pieceToken };
//await this.updateOrSaveTokenOwnerPart(extrinsic, updateTokenTransfer);
await this.tokensOwnersRepository.upsert({ ...updateTokenTransfer }, [
'collection_id',
'token_id',
'owner',
]);

if (pieceToken.amount === 0) {
await this.tokensOwnersRepository.delete({
collection_id: extrinsic.collection_id,
token_id: extrinsic.token_id,
owner: extrinsic.owner,
});
} else {
await this.tokensOwnersRepository.upsert({ ...updateTokenTransfer }, [
'collection_id',
'token_id',
'owner',
]);
}
}
}

Expand Down
67 changes: 67 additions & 0 deletions apps/crawler/src/services/token/nesting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import { SentryService } from '@ntegral/nestjs-sentry';
import { normalizeTimestamp } from '@common/utils';
import { SdkService } from '../../sdk/sdk.service';
import { TokenData } from './token.types';
import { TokensOwners } from '@entities/TokensOwners';

@Injectable()
export class TokenNestingService {
constructor(
private sdkService: SdkService,
@InjectRepository(Tokens)
private tokensRepository: Repository<Tokens>,
@InjectRepository(TokensOwners)
private tokensOwnersRepository: Repository<TokensOwners>,
private dataSource: DataSource,
private readonly sentry: SentryService,
) {
Expand All @@ -36,6 +39,11 @@ export class TokenNestingService {
collection_id,
token_id,
});
const tokenOwnersFromDb = await this.tokensOwnersRepository.findOneBy({
collection_id,
token_id,
});
debugger;
let children: ITokenEntities[] = [];
try {
// token nested. Update children. Update parent.
Expand Down Expand Up @@ -82,6 +90,16 @@ export class TokenNestingService {
]);
}

if (tokenOwnersFromDb?.parent_id && isBundle && !parentId) {
await this.unnestBundleOwners(tokenOwnersFromDb, [
...tokenOwnersFromDb.children,
{
token_id,
collection_id,
},
]);
}

return children;
} catch {
return children;
Expand Down Expand Up @@ -125,6 +143,44 @@ export class TokenNestingService {
}
}

private async unnestBundleOwners(
token: TokensOwners,
childrenToBeDeleted: ITokenEntities[],
) {
const { parent_id, children } = token;
debugger;
if (parent_id && children.length) {
const [collectionId, tokenId] = parent_id?.split('_');

const parent = await this.tokensOwnersRepository.findOneBy({
collection_id: Number(collectionId),
token_id: Number(tokenId),
});

if (parent) {
const childrenSet = new Set<string>(
childrenToBeDeleted.map(
({ collection_id, token_id }) => `${collection_id}_${token_id}`,
),
);

await this.tokensOwnersRepository.update(
{ id: parent.id },
{
children: parent.children.filter(
({ collection_id, token_id }) =>
!childrenSet.has(`${collection_id}_${token_id}`),
),
},
);

if (parent.parent_id) {
await this.unnestBundleOwners(parent, childrenToBeDeleted);
}
}
}
}

private async updateTokenParents(
collection_id: number,
token_id: number,
Expand Down Expand Up @@ -185,6 +241,17 @@ export class TokenNestingService {
: undefined,
},
);

await this.tokensOwnersRepository.update(
{
token_id,
collection_id,
},
{
children,
nested: true,
},
);
}
}

Expand Down
62 changes: 39 additions & 23 deletions apps/crawler/src/services/token/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
TOKEN_UPDATE_EVENTS,
} from '@common/constants';
import {
getParentCollectionAndToken,
normalizeSubstrateAddress,
normalizeTimestamp,
sanitizePropertiesValues,
Expand All @@ -26,6 +27,7 @@ import { ConfigService } from '@nestjs/config';
import { Config } from '../../config/config.module';
import { CollectionService } from '../collection.service';
import { TokensOwners } from '@entities/TokensOwners';
import * as console from 'console';

@Injectable()
export class TokenService {
Expand Down Expand Up @@ -77,14 +79,15 @@ export class TokenService {
nestedType = true;
}

debugger;
const children: ITokenEntities[] = needCheckNesting
? await this.nestingService.handleNesting(
tokenData,
blockHash,
blockTimestamp,
)
: token?.children ?? [];

debugger;
if (isBundle) {
nestedType = true;
}
Expand Down Expand Up @@ -213,17 +216,25 @@ export class TokenService {
}): Promise<SubscriberAction> {
const tokenData = await this.getTokenData(collectionId, tokenId, blockHash);
let result;

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

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

const preparedData = await this.prepareDataForDb(
tokenData,
blockHash,
pieces?.amount,
blockTimestamp,
needCheckNesting,
);

if (data.length != 0) {
const typeMode = tokenDecoded.collection.mode;
const tokenOwner: TokenOwnerData = {
Expand All @@ -237,6 +248,8 @@ export class TokenService {
amount: pieces.amount,
type: typeMode === 'ReFungible' ? 'RFT' : typeMode,
block_number: blockNumber,
parent_id: preparedData.parent_id,
children: preparedData.children,
};

await this.tokensOwnersRepository.upsert({ ...tokenOwner }, [
Expand All @@ -246,14 +259,6 @@ export class TokenService {
]);
}

const preparedData = await this.prepareDataForDb(
tokenData,
blockHash,
pieces?.amount,
blockTimestamp,
needCheckNesting,
);

// Write token data into db
await this.tokensRepository.upsert(
{
Expand All @@ -276,6 +281,17 @@ export class TokenService {
owner_normalized: ownerToken,
block_number: blockNumber,
});

const pieceToken = await this.sdkService.getRFTBalances({
address: ownerToken,
collectionId: collectionId,
tokenId: tokenId,
});
if (pieceToken.amount === 0) {
console.error(
`Destroy token full amount: ${pieceToken.amount} / collection: ${collectionId} / token: ${tokenId}`,
);
}
// No entity returned from sdk. Most likely it was destroyed in a future block.
await this.burn(collectionId, tokenId);

Expand Down Expand Up @@ -319,12 +335,11 @@ export class TokenService {
if (!tokenDecoded) {
return null;
}

const [tokenProperties, isBundle] = await Promise.all([
this.sdkService.getTokenProperties(collectionId, tokenId),
this.sdkService.isTokenBundle(collectionId, tokenId),
]);

debugger;
return {
tokenDecoded,
tokenProperties,
Expand Down Expand Up @@ -352,17 +367,18 @@ export class TokenService {
});

if (ownerToken) {
await this.tokensOwnersRepository.update(
{
owner: tokenOwner.owner,
collection_id: tokenOwner.collection_id,
token_id: tokenOwner.token_id,
},
{
amount: 0,
block_number: tokenOwner.block_number,
},
);
// await this.tokensOwnersRepository.update(
// {
// id: ownerToken.id,
// },
// {
// amount: 0,
// block_number: tokenOwner.block_number,
// },
// );
await this.tokensOwnersRepository.delete({
id: ownerToken.id,
});
}
}

Expand Down
6 changes: 5 additions & 1 deletion apps/crawler/src/services/token/token.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
TokenByIdResult,
TokenPropertiesResult,
} from '@unique-nft/substrate-client/tokens';
import { TokenType } from '@entities/Tokens';
import { ITokenEntities, TokenType } from '@entities/Tokens';

export interface TokenData {
tokenDecoded: TokenByIdResult;
Expand All @@ -19,4 +19,8 @@ export interface TokenOwnerData {
amount?: number;
type?: TokenType;
block_number?: number;

parent_id?: string;

children?: ITokenEntities[];
}
3 changes: 3 additions & 0 deletions apps/web-api/src/tokens/token.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ export class TokenDTO extends SimpleTokenDTO implements Partial<Tokens> {
@Field(() => String, { nullable: true })
tokens_amount?: string;

@Field(() => String, { nullable: true })
tokens_parent?: string;

@Field(() => String, { nullable: true })
collection_description?: string;

Expand Down
6 changes: 6 additions & 0 deletions apps/web-api/src/tokens/token.resolver.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export class TokenWhereParams implements TWhereParams<TokenDTO> {
@Field(() => GQLWhereOpsString, { nullable: true })
tokens_amount?: GQLWhereOpsString;

@Field(() => GQLWhereOpsString, { nullable: true })
tokens_parent?: GQLWhereOpsString;

@Field(() => GQLWhereOpsString, { nullable: true })
collection_owner?: GQLWhereOpsString;

Expand Down Expand Up @@ -123,6 +126,9 @@ export class TokenOrderByParams implements TOrderByParams<TokenDTO> {
@Field(() => GQLOrderByParamsArgs, { nullable: true })
tokens_amount?: GQLOrderByParamsArgs;

@Field(() => GQLOrderByParamsArgs, { nullable: true })
tokens_parent?: GQLOrderByParamsArgs;

@Field(() => GQLOrderByParamsArgs, { nullable: true })
owner_normalized?: GQLOrderByParamsArgs;

Expand Down
Loading