From 9b60c8ba7a33f641a621f4e8757d5bab4444eaa5 Mon Sep 17 00:00:00 2001 From: danielailie Date: Wed, 17 Jan 2024 22:00:49 +0200 Subject: [PATCH] Update token decoding for multi transfer --- .../blockchain-events/nft-events.service.ts | 28 ++++++++++--------- .../entities/auction/transfer.event.topics.ts | 28 +++++++++++++++++++ .../entities/auction/transfer.event.ts | 15 +++++++++- 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/src/modules/rabbitmq/blockchain-events/nft-events.service.ts b/src/modules/rabbitmq/blockchain-events/nft-events.service.ts index 030bdd416..58d0b05eb 100644 --- a/src/modules/rabbitmq/blockchain-events/nft-events.service.ts +++ b/src/modules/rabbitmq/blockchain-events/nft-events.service.ts @@ -4,7 +4,7 @@ import { NftEventEnum, NftTypeEnum } from 'src/modules/assets/models'; import { CacheEventsPublisherService } from '../cache-invalidation/cache-invalidation-publisher/change-events-publisher.service'; import { CacheEventTypeEnum, ChangedEvent } from '../cache-invalidation/events/changed.event'; import { MintEvent } from '../entities/auction/mint.event'; -import { TransferEvent } from '../entities/auction/transfer.event'; +import { MultiTransferEvent, TransferEvent } from '../entities/auction/transfer.event'; import { FeedEventsSenderService } from './feed-events.service'; import { BurnEvent } from '../entities/auction/burn.event'; @@ -55,21 +55,23 @@ export class NftEventsService { break; case NftEventEnum.MultiESDTNFTTransfer: - const multiTransferEvent = new TransferEvent(event); + const multiTransferEvent = new MultiTransferEvent(event); multiTransferEvent.getAddress(); const multiTransferTopics = multiTransferEvent.getTopics(); - const collectionDetails = await this.mxApiService.getCollectionByIdentifierForQuery( - multiTransferTopics.collection, - 'fields=name,type', - ); - if (collectionDetails?.type === NftTypeEnum.NonFungibleESDT || collectionDetails?.type === NftTypeEnum.SemiFungibleESDT) { - this.triggerCacheInvalidationWithOwner( - `${multiTransferTopics.collection}-${multiTransferTopics.nonce}`, - CacheEventTypeEnum.OwnerChanged, - multiTransferEvent.getAddress(), - multiTransferTopics.receiverAddress.toString(), - ); + for (const pair of multiTransferTopics.pairs) { + if (pair.nonce !== '') { + const collectionDetails = await this.mxApiService.getCollectionByIdentifierForQuery(pair.collection, 'fields=name,type'); + if (collectionDetails?.type === NftTypeEnum.NonFungibleESDT || collectionDetails?.type === NftTypeEnum.SemiFungibleESDT) { + this.triggerCacheInvalidationWithOwner( + `${pair.collection}-${pair.nonce}`, + CacheEventTypeEnum.OwnerChanged, + multiTransferEvent.getAddress(), + multiTransferTopics.receiverAddress.toString(), + ); + } + } } + break; } } diff --git a/src/modules/rabbitmq/entities/auction/transfer.event.topics.ts b/src/modules/rabbitmq/entities/auction/transfer.event.topics.ts index 80f20e063..ec7e5b5a1 100644 --- a/src/modules/rabbitmq/entities/auction/transfer.event.topics.ts +++ b/src/modules/rabbitmq/entities/auction/transfer.event.topics.ts @@ -21,3 +21,31 @@ export class TransferEventsTopics { }; } } + +export class MultiTransferEventsTopics { + private receiverAddress: Address; + private pairs: any[] = []; + + constructor(rawTopics: string[]) { + console.log({ rawTopics }); + for (let index = 0; index < rawTopics.length - 1; index += 3) { + console.log({ index }); + this.pairs.push({ + collection: BinaryUtils.base64Decode(rawTopics[index]), + nonce: BinaryUtils.base64ToHex(rawTopics[index + 1]), + value: Buffer.from(rawTopics[index + 2], 'base64') + .toString('hex') + .hexBigNumberToString(), + }); + } + + this.receiverAddress = new Address(Buffer.from(rawTopics[rawTopics.length - 1], 'base64')); + } + + toPlainObject() { + return { + pairs: this.pairs, + receiverAddress: this.receiverAddress, + }; + } +} diff --git a/src/modules/rabbitmq/entities/auction/transfer.event.ts b/src/modules/rabbitmq/entities/auction/transfer.event.ts index f835cfbb9..18f26db6b 100644 --- a/src/modules/rabbitmq/entities/auction/transfer.event.ts +++ b/src/modules/rabbitmq/entities/auction/transfer.event.ts @@ -1,5 +1,5 @@ import { GenericEvent } from '../generic.event'; -import { TransferEventsTopics } from './transfer.event.topics'; +import { MultiTransferEventsTopics as MultiTransferEventTopics, TransferEventsTopics } from './transfer.event.topics'; export class TransferEvent extends GenericEvent { private decodedTopics: TransferEventsTopics; @@ -13,3 +13,16 @@ export class TransferEvent extends GenericEvent { return this.decodedTopics.toPlainObject(); } } + +export class MultiTransferEvent extends GenericEvent { + private decodedTopics: MultiTransferEventTopics; + + constructor(init?: Partial) { + super(init); + this.decodedTopics = new MultiTransferEventTopics(this.topics); + } + + getTopics() { + return this.decodedTopics.toPlainObject(); + } +}