Skip to content

Commit

Permalink
Merge pull request #289 from UniqueNetwork/feature/schemas-v2
Browse files Browse the repository at this point in the history
Schemas v2 - new feilds +  all_collections_rescan event
  • Loading branch information
ashkuc authored Mar 6, 2024
2 parents 05e3eff + c536306 commit 0499dc3
Show file tree
Hide file tree
Showing 10 changed files with 416 additions and 96 deletions.
19 changes: 18 additions & 1 deletion apps/crawler/src/services/collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class CollectionService {
*/
private async getCollectionData(
collectionId: number,
at: string
at?: string
): Promise<CollectionData | null> {
let collectionDecoded = await this.sdkService.getCollection(
collectionId,
Expand Down Expand Up @@ -291,6 +291,10 @@ export class CollectionService {
owner_normalized: normalizeSubstrateAddress(owner),
collection_cover: collectionCover,
burned: collection?.burned ?? false,
default_token_image: collectionDecodedV2?.info.default_token_image,
original_schema_version: collectionDecodedV2?.info?.originalSchemaVersion,
potential_attributes: collectionDecodedV2?.info?.potential_attributes,
customizing: collectionDecodedV2?.info?.customizing,
};
}

Expand Down Expand Up @@ -352,6 +356,19 @@ export class CollectionService {
};
}

async updateWithoutBlock(collectionId: number) {
const collectionData = await this.getCollectionData(collectionId);

if (collectionData) {
const preparedData = await this.prepareDataForDb(collectionData);

await this.collectionsRepository.upsert(preparedData, ['collection_id']);
} else {
// No entity returned from sdk. Most likely it was destroyed in a future block.
await this.burn(collectionId);
}
}

async update({
collectionId,
eventName,
Expand Down
71 changes: 32 additions & 39 deletions apps/crawler/src/services/token/nesting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ export class TokenNestingService {
@InjectRepository(TokensOwners)
private tokensOwnersRepository: Repository<TokensOwners>,
private dataSource: DataSource,
private readonly sentry: SentryService,
private readonly sentry: SentryService
) {
this.sentry.setContext(TokenNestingService.name);
}

async handleNesting(
tokenData: TokenData,
blockHash: string,
blockTimestamp?: number,
) {
async handleNesting(tokenData: TokenData, blockTimestamp?: number) {
const { tokenDecoded, isBundle } = tokenData;

const {
Expand All @@ -51,21 +47,20 @@ export class TokenNestingService {
if (isBundle) {
const nestingBundle = await this.sdkService.getTokenBundle(
collection_id,
token_id,
token_id
);

children = this.getTokenChildren(
collection_id,
token_id,
nestingBundle,
nestingBundle
);

await this.updateTokenParents(
collection_id,
token_id,
nestingBundle,
blockHash,
blockTimestamp,
blockTimestamp
);
}

Expand Down Expand Up @@ -109,7 +104,7 @@ export class TokenNestingService {

private async unnestBundle(
token: Tokens,
childrenToBeDeleted: ITokenEntities[],
childrenToBeDeleted: ITokenEntities[]
) {
const { parent_id, children } = token;
if (parent_id && children.length) {
Expand All @@ -123,18 +118,18 @@ export class TokenNestingService {
if (parent) {
const childrenSet = new Set<string>(
childrenToBeDeleted.map(
({ collection_id, token_id }) => `${collection_id}_${token_id}`,
),
({ collection_id, token_id }) => `${collection_id}_${token_id}`
)
);

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

if (parent.parent_id) {
Expand All @@ -146,7 +141,7 @@ export class TokenNestingService {

private async unnestBundleOwners(
token: TokensOwners,
childrenToBeDeleted: ITokenEntities[],
childrenToBeDeleted: ITokenEntities[]
) {
const { parent_id, children } = token;

Expand All @@ -161,18 +156,18 @@ export class TokenNestingService {
if (parent) {
const childrenSet = new Set<string>(
childrenToBeDeleted.map(
({ collection_id, token_id }) => `${collection_id}_${token_id}`,
),
({ 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}`),
!childrenSet.has(`${collection_id}_${token_id}`)
),
},
}
);

if (parent.parent_id) {
Expand All @@ -186,29 +181,27 @@ export class TokenNestingService {
collection_id: number,
token_id: number,
nestingBundle: NestedToken,
blockHash: string,
blockTimestamp?: number,
blockTimestamp?: number
) {
try {
const parent = await this.sdkService.getTokenParents(
collection_id,
token_id,
token_id
);

if (parent) {
await this.updateParent(
parent.collectionId,
parent.tokenId,
nestingBundle,
blockTimestamp,
blockTimestamp
);

await this.updateTokenParents(
parent.collectionId,
parent.tokenId,
nestingBundle,
blockHash,
blockTimestamp,
blockTimestamp
);
}
} catch (error) {
Expand All @@ -220,12 +213,12 @@ export class TokenNestingService {
collection_id: number,
token_id: number,
nestingBundle: NestedToken,
blockTimestamp?: number,
blockTimestamp?: number
) {
const children = this.getTokenChildren(
collection_id,
token_id,
nestingBundle,
nestingBundle
);

if (children.length) {
Expand All @@ -240,7 +233,7 @@ export class TokenNestingService {
bundle_created: blockTimestamp
? normalizeTimestamp(blockTimestamp)
: undefined,
},
}
);

await this.tokensOwnersRepository.update(
Expand All @@ -251,7 +244,7 @@ export class TokenNestingService {
{
children,
nested: true,
},
}
);
}
}
Expand All @@ -266,7 +259,7 @@ export class TokenNestingService {

private async getParentsByChildren(
collection_id: number,
token_id: number,
token_id: number
): Promise<
{ collection_id: number; token_id: number; children: ITokenEntities[] }[]
> {
Expand All @@ -286,11 +279,11 @@ export class TokenNestingService {
children: ITokenEntities[];
},
child_collection_id: number,
child_token_id: number,
child_token_id: number
) {
const children = parent.children.filter(
({ token_id, collection_id }) =>
!(child_collection_id === collection_id && child_token_id === token_id),
!(child_collection_id === collection_id && child_token_id === token_id)
);
return this.tokensRepository.update(
{
Expand All @@ -300,23 +293,23 @@ export class TokenNestingService {
{
children,
nested: children.length > 0,
},
}
);
}

private getTokenChildren(
collectionId: number,
tokenId: number,
nestingBundle: NestedToken,
nestingBundle: NestedToken
) {
const nestedToken = this.getNestedTokenFromBundle(
collectionId,
tokenId,
nestingBundle,
nestingBundle
);

const mapNestingBundle = (
tree: NestedToken,
tree: NestedToken
): { collection_id: number; token_id: number }[] => {
let result = [];
if (tree?.nestingChildTokens) {
Expand All @@ -342,13 +335,13 @@ export class TokenNestingService {
private getNestedTokenFromBundle(
collectionId: number,
tokenId: number,
nestingBundle: NestedToken,
nestingBundle: NestedToken
) {
let token: NestedToken | null = null;
const findTokenByArgs = (
nestingBundle: NestedToken,
collectionId: number,
tokenId: number,
tokenId: number
) => {
if (
nestingBundle.tokenId === tokenId &&
Expand Down
Loading

0 comments on commit 0499dc3

Please sign in to comment.