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

Update token decoding for multi transfer #1062

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
28 changes: 28 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,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,
};
}
}
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();
}
}