Skip to content

Commit

Permalink
Merge pull request #240 from UniqueNetwork/fix/token-rft-data
Browse files Browse the repository at this point in the history
Fix events for RFT
  • Loading branch information
icehuntmen committed Mar 28, 2023
2 parents c1c6097 + 2e8e6e9 commit 5afbfa9
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 287 deletions.
5 changes: 3 additions & 2 deletions apps/crawler/src/services/collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,8 @@ export class CollectionService {
}: {
events: Event[];
blockCommonData: any;
}): Promise<ItemsBatchProcessingResult> {
}): Promise<any> {
const collectionEvents = this.extractCollectionEvents(events);

const eventChunks = chunk(
collectionEvents,
this.configService.get('scanCollectionsBatchSize'),
Expand Down Expand Up @@ -324,6 +323,8 @@ export class CollectionService {

return {
totalEvents: collectionEvents.length,
collection:
collectionEvents.length === 1 ? collectionEvents[0].values : null,
rejected,
};
}
Expand Down
95 changes: 16 additions & 79 deletions apps/crawler/src/services/event/event.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Repository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { EventMethod, EventSection, SubscriberName } from '@common/constants';
import { Event } from '@entities/Event';
Expand All @@ -14,16 +14,14 @@ import { EventEntity } from '@unique-nft/harvester/src/database/entities';

@Injectable()
export class EventService {
private logCollection = new Logger('EventService_Collection');
private logToken = new Logger('EventService_Token');
constructor(
private eventArgumentsService: EventArgumentsService,
private evmService: EvmService,

private tokenService: TokenService,

private collectionService: CollectionService,

private configService: ConfigService<Config>,

@InjectRepository(Event)
private eventsRepository: Repository<Event>,
) {}
Expand Down Expand Up @@ -98,6 +96,14 @@ export class EventService {
events,
blockCommonData,
});
if (
collectionsResult.totalEvents >= 1 &&
collectionsResult.rejected.length === 0
) {
this.logCollection.log(
`Save event collection: ${collectionsResult.collection.collectionId}`,
);
}
}

let tokensResult = null;
Expand All @@ -106,6 +112,11 @@ export class EventService {
events,
blockCommonData,
});
if (tokensResult.totalEvents >= 4) {
this.logToken.log(
`Save event in collection: ${tokensResult.collection} token: ${tokensResult.token}`,
);
}
}

return {
Expand All @@ -115,78 +126,4 @@ export class EventService {
events,
};
}

// async process({
// blockItems,
// blockCommonData,
// }: {
// blockItems: any;
// blockCommonData: IBlockCommonData;
// }): Promise<any> {
// const eventItems = this.extractEventItems(blockItems);
// //
// // console.dir(eventItems, { depth: 2 });
//
// const events = await this.prepareDataForDb({
// blockCommonData,
// eventItems,
// });
//
// const ethereumEvents = events.filter(
// ({ section, method }) =>
// section === EventSection.ETHEREUM && method === EventMethod.EXECUTED,
// );
//
// await this.evmService.parseEvents(
// ethereumEvents,
// blockCommonData.blockTimestamp,
// );
//
// await this.eventsRepository.upsert(events, ['block_number', 'event_index']);
//
// const subscribersConfig = this.configService.get('subscribers');
//
// let collectionsResult = null;
// if (subscribersConfig[SubscriberName.COLLECTIONS]) {
// collectionsResult = await this.collectionService.batchProcess({
// events,
// blockCommonData,
// });
// }
//
// let tokensResult = null;
// if (subscribersConfig[SubscriberName.TOKENS]) {
// tokensResult = await this.tokenService.batchProcess({
// events,
// blockCommonData,
// });
// }
//
// return {
// events,
// collectionsResult,
// tokensResult,
// };
// }

// async processEventWithAccounts(
// eventName: string,
// rawArgs: EventArgs,
// ): Promise<AccountRecord[]> {
// const eventValues = await this.eventArgumentsService.processEventArguments(
// eventName,
// rawArgs,
// );
//
// const result = [];
//
// // Extract only accounts values.
// EVENT_ARGS_ACCOUNT_KEYS.forEach((k) => {
// if (eventValues[k]) {
// result.push(eventValues[k]);
// }
// });
//
// return result;
// }
}
71 changes: 26 additions & 45 deletions apps/crawler/src/services/token/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,23 @@ import {
} from '@common/utils';
import { Event } from '@entities/Event';
import { ITokenEntities, Tokens, TokenType } from '@entities/Tokens';
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { SdkService } from '../../sdk/sdk.service';
import {
IBlockCommonData,
ItemsBatchProcessingResult,
} from '../../subscribers/blocks.subscriber.service';
import { TokenNestingService } from './nesting.service';
import { TokenData, TokenOwnerData } from './token.types';
import { chunk } from 'lodash';
import { red } from 'cli-color';
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';
import { yellow } from '@nestjs/common/utils/cli-colors.util';

@Injectable()
export class TokenService {
private logger = new Logger(TokenService.name);
constructor(
private sdkService: SdkService,
private nestingService: TokenNestingService,
Expand Down Expand Up @@ -69,8 +66,6 @@ export class TokenService {
token_id,
});

//const tokenBalance = await this.sdkService.getRFTBalances();

let tokenType =
tokenDecoded.collection.mode === 'NFT' ? TokenType.NFT : TokenType.RFT;
let parentId = null;
Expand Down Expand Up @@ -126,7 +121,7 @@ export class TokenService {
}: {
events: Event[];
blockCommonData: any;
}): Promise<ItemsBatchProcessingResult> {
}): Promise<any> {
const tokenEvents = this.extractTokenEvents(events);

const eventChunks = chunk(
Expand Down Expand Up @@ -194,6 +189,8 @@ export class TokenService {

return {
totalEvents: tokenEvents.length,
collection: data.length >= 4 ? data[0] : null,
token: data.length >= 4 ? data[1] : null,
rejected,
};
}
Expand Down Expand Up @@ -302,7 +299,8 @@ export class TokenService {
}
result = SubscriberAction.UPSERT;
} else {
const ownerToken = normalizeSubstrateAddress(data[2].value);
const tokenOwnSelect = data[2].substrate || data[2].ethereum;
const ownerToken = normalizeSubstrateAddress(tokenOwnSelect);

await this.burnTokenOwnerPart({
collection_id: collectionId,
Expand All @@ -319,7 +317,7 @@ export class TokenService {
});

if (pieceToken.amount === 0) {
console.error(
this.logger.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.
Expand Down Expand Up @@ -354,17 +352,19 @@ export class TokenService {
},
);

const ownerAddress = data[2].substrate || data[2].ethereum;

const pieceFrom = await this.sdkService.getRFTBalances(
{
address: normalizeSubstrateAddress(data[2].value),
address: normalizeSubstrateAddress(ownerAddress),
collectionId: collectionId,
tokenId: tokenId,
},
blockHash,
);
arrayToken.push({
owner: normalizeSubstrateAddress(data[2].value),
owner_normalized: normalizeSubstrateAddress(data[2].value),
owner: normalizeSubstrateAddress(ownerAddress),
owner_normalized: normalizeSubstrateAddress(ownerAddress),
collection_id: collectionId,
token_id: tokenId,
date_created: String(normalizeTimestamp(blockTimestamp)),
Expand All @@ -374,18 +374,9 @@ export class TokenService {
parent_id: preparedData.parent_id,
children: preparedData.children,
});
console.log(
blockNumber,
eventName,
typeMode,
collectionId,
tokenId,
data.length === 4 ? data[3] : null,
pieceFrom.amount,
normalizeSubstrateAddress(data[2].value),
);
if (data.length === 5) {
const owner = normalizeSubstrateAddress(data[3].value);
const ownerAddressTo = data[3].substrate || data[3].ethereum;
const owner = normalizeSubstrateAddress(ownerAddressTo);
const pieceTo = await this.sdkService.getRFTBalances(
{
address: owner,
Expand All @@ -403,7 +394,7 @@ export class TokenService {
}
arrayToken.push({
owner: owner,
owner_normalized: normalizeSubstrateAddress(data[3].value),
owner_normalized: normalizeSubstrateAddress(ownerAddressTo),
collection_id: collectionId,
token_id: tokenId,
date_created: String(normalizeTimestamp(blockTimestamp)),
Expand All @@ -414,18 +405,6 @@ export class TokenService {
children: preparedData.children,
nested: nested,
});
console.log(
blockNumber,
eventName,
typeMode,
collectionId,
tokenId,
data.length === 5 ? data[4] : null,
'-',
pieceTo.amount,
normalizeSubstrateAddress(data[2].value),
normalizeSubstrateAddress(data[3].value),
);
}

for (const tokenOwnerData of arrayToken) {
Expand All @@ -441,6 +420,9 @@ export class TokenService {
'token_id',
'owner',
]);
this.logger.log(
`${eventName} token: ${tokenOwnerData.token_id} collection: ${tokenOwnerData.collection_id} in ${tokenOwnerData.block_number}`,
);
}
}
}
Expand Down Expand Up @@ -478,13 +460,12 @@ export class TokenService {
'token_id',
'owner',
]);
console.log(
blockNumber,
eventName,
typeMode,
collectionId,
tokenId,
tokenDecoded.owner || tokenDecoded.collection.owner,
this.logger.log(
`${eventName} token: ${yellow(
String(tokenOwner.token_id),
)} collection: ${yellow(String(tokenOwner.collection_id))} in ${
tokenOwner.block_number
}`,
);
}

Expand Down
Loading

0 comments on commit 5afbfa9

Please sign in to comment.