Skip to content

Commit

Permalink
Merge pull request #1062 from multiversx/UpdateTopicsForMultiTransfer
Browse files Browse the repository at this point in the history
Update token decoding for multi transfer
  • Loading branch information
danielailie authored Jan 18, 2024
2 parents 456befd + b2f5c4e commit 2421d26
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
28 changes: 15 additions & 13 deletions src/modules/rabbitmq/blockchain-events/nft-events.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/modules/rabbitmq/entities/auction/transfer.event.topics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,29 @@ export class TransferEventsTopics {
};
}
}

export class MultiTransferEventsTopics {
private receiverAddress: Address;
private pairs: any[] = [];

constructor(rawTopics: string[]) {
for (let index = 0; index < rawTopics.length - 1; index += 3) {
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,
};
}
}
15 changes: 14 additions & 1 deletion src/modules/rabbitmq/entities/auction/transfer.event.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,3 +13,16 @@ export class TransferEvent extends GenericEvent {
return this.decodedTopics.toPlainObject();
}
}

export class MultiTransferEvent extends GenericEvent {
private decodedTopics: MultiTransferEventTopics;

constructor(init?: Partial<GenericEvent>) {
super(init);
this.decodedTopics = new MultiTransferEventTopics(this.topics);
}

getTopics() {
return this.decodedTopics.toPlainObject();
}
}

0 comments on commit 2421d26

Please sign in to comment.