Skip to content

Commit

Permalink
Update notifiy endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed May 22, 2024
1 parent 0659f08 commit b843e86
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 116 deletions.
50 changes: 50 additions & 0 deletions src/db/auctions/auction.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,56 @@ export class AuctionEntity extends BaseEntity {
});
}

static fromAuctionTopics(
topicsAuctionToken: {
collection: string;
nonce: string;
auctionId: string;
nrAuctionTokens: string;
originalOwner: string;
minBid: string;
maxBid: string;
startTime: number;
endTime: number;
paymentToken: string;
paymentNonce: number;
auctionType: string;
},
tags: string,
hash: string,
marketplaceKey: string,
decimals: number = 18,
) {
if (!topicsAuctionToken) {
return null;
}
console.log('topicsAuctionToken', topicsAuctionToken);
return new AuctionEntity({
marketplaceAuctionId: parseInt(topicsAuctionToken.auctionId, 16),
collection: topicsAuctionToken.collection,
nonce: parseInt(topicsAuctionToken.nonce, 16),
nrAuctionedTokens: parseInt(topicsAuctionToken.nrAuctionTokens, 16),
status: AuctionStatusEnum.Running,
type:
topicsAuctionToken.auctionType === '' || parseInt(topicsAuctionToken.auctionType) === ElrondSwapAuctionTypeEnum.Auction
? AuctionTypeEnum.Nft
: AuctionTypeEnum.SftOnePerPayment,
paymentToken: topicsAuctionToken.paymentToken,
paymentNonce: topicsAuctionToken.paymentNonce,
ownerAddress: topicsAuctionToken.originalOwner,
minBid: topicsAuctionToken.minBid,
minBidDenominated: BigNumberUtils.denominateAmount(topicsAuctionToken.minBid, decimals),
maxBid: topicsAuctionToken.maxBid ?? '0',
maxBidDenominated: BigNumberUtils.denominateAmount(topicsAuctionToken.maxBid ?? '0', decimals),
startDate: DateUtils.getCurrentTimestamp(),
endDate: topicsAuctionToken.endTime,
identifier: `${topicsAuctionToken.collection}-${topicsAuctionToken.nonce}`,
tags: tags ? `,${tags},` : '',
blockHash: hash,
marketplaceKey: marketplaceKey,
});
}

static fromWithdrawTopics(
topicsAuctionToken: {
originalOwner: string;
Expand Down
3 changes: 1 addition & 2 deletions src/modules/metrics/metrics.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ export class MetricsController {

@Post('/event')
async notify(@Body() payload: RabbitEvent): Promise<HttpStatus> {
const resp = await this.marketplaceEvents.handleNftEvents(payload?.events, payload?.hash, MarketplaceTypeEnum.External);
console.log('resp', resp);
await this.marketplaceEvents.handleNftAuctionEvents(payload?.events, payload?.hash, MarketplaceTypeEnum.External);
return HttpStatus.OK;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { MarketplacesService } from 'src/modules/marketplaces/marketplaces.servi
import { Marketplace } from 'src/modules/marketplaces/models';
import { MarketplaceTypeEnum } from 'src/modules/marketplaces/models/MarketplaceType.enum';
import { UsdPriceService } from 'src/modules/usdPrice/usd-price.service';
import { ELRONDNFTSWAP_KEY, ENEFTOR_KEY } from 'src/utils/constants';
import { ELRONDNFTSWAP_KEY, ENEFTOR_KEY, XOXNO_KEY } from 'src/utils/constants';
import { AuctionTokenEvent } from '../../entities/auction';
import { ElrondSwapAuctionEvent } from '../../entities/auction/elrondnftswap/elrondswap-auction.event';
import { ListNftEvent } from '../../entities/auction/listNft.event';
Expand Down Expand Up @@ -55,7 +55,7 @@ export class StartAuctionEventHandler {

private async saveAuction(topics: any, marketplace: Marketplace, hash: string) {
const auctionIdentifier = `${topics.collection}-${topics.nonce}`;
if (marketplace.key === ELRONDNFTSWAP_KEY || marketplace.key === ENEFTOR_KEY) {
if (marketplace.key === ELRONDNFTSWAP_KEY || marketplace.key === ENEFTOR_KEY || marketplace.key === XOXNO_KEY) {
if (topics.auctionId === '0') {
let auctionId = await this.auctionsGetterService.getLastAuctionIdForMarketplace(marketplace.key);
topics.auctionId = (auctionId && auctionId > 0 ? auctionId + 1 : 1).toString(16);
Expand All @@ -78,6 +78,12 @@ export class StartAuctionEventHandler {
const paymentToken = await this.usdPriceService.getToken(topics.paymentToken);
decimals = paymentToken.decimals;
}
if (auctionTokenEventMarketplace.key === XOXNO_KEY) {
return await this.auctionsSetterService.saveAuctionEntity(
AuctionEntity.fromAuctionTopics(topics, asset.tags?.toString(), hash, auctionTokenEventMarketplace.key, decimals),
asset.tags,
);
}
return await this.auctionsSetterService.saveAuctionEntity(
AuctionEntity.fromWithdrawTopics(topics, asset.tags?.toString(), hash, auctionTokenEventMarketplace.key, decimals),
asset.tags,
Expand Down
106 changes: 0 additions & 106 deletions src/modules/rabbitmq/blockchain-events/marketplace-events.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,110 +124,4 @@ export class MarketplaceEventsService {
}
}
}

public async handleNftEvents(auctionEvents: any[], hash: string, marketplaceType: MarketplaceTypeEnum) {
for (let event of auctionEvents) {
console.log({ identifier: event.identifier });
switch (event.identifier) {
case AuctionEventEnum.BidEvent:
case KroganSwapAuctionEventEnum.Bid:
await this.bidEventHandler.handle(event, hash, marketplaceType);
console.log({ identifier: event.identifier });

break;
case AuctionEventEnum.BuySftEvent:
case ExternalAuctionEventEnum.Buy:
case ExternalAuctionEventEnum.BulkBuy:
case ExternalAuctionEventEnum.BuyFor:
case ExternalAuctionEventEnum.BuyNft:
case KroganSwapAuctionEventEnum.Purchase:
const eventName = Buffer.from(event.topics[0], 'base64').toString();
if (eventName === ExternalAuctionEventEnum.UpdateOffer || eventName === KroganSwapAuctionEventEnum.UpdateListing) {
this.logger.log(`${eventName} event detected for hash '${hash}' for marketplace ${event.address}, ignore it for the moment`);
continue;
}
console.log({ identifier: event.identifier });
await this.buyEventHandler.handle(event, hash, marketplaceType);
break;
case AuctionEventEnum.WithdrawEvent:
case KroganSwapAuctionEventEnum.WithdrawSwap:
case ExternalAuctionEventEnum.ClaimBackNft:
case ExternalAuctionEventEnum.ReturnListing:
if (Buffer.from(event.topics[0], 'base64').toString() === ExternalAuctionEventEnum.UpdateOffer) {
this.logger.log(
`${event.topics[0]} event detected for hash '${hash}' for marketplace ${event.addreses}, ignore it for the moment`,
);
continue;
}
console.log({ identifier: event.identifier });
await this.withdrawAuctionEventHandler.handle(event, hash, marketplaceType);
break;
case AuctionEventEnum.EndAuctionEvent:
console.log({ identifier: event.identifier });
await this.endAuctionEventHandler.handle(event, hash, marketplaceType);
break;
case AuctionEventEnum.AuctionTokenEvent:
case ExternalAuctionEventEnum.Listing:
case ExternalAuctionEventEnum.ListNftOnMarketplace:
case KroganSwapAuctionEventEnum.NftSwap:
console.log({ identifier: event.identifier });
await this.startAuctionEventHandler.handle(event, hash, marketplaceType);
break;
case ExternalAuctionEventEnum.ChangePrice:
case ExternalAuctionEventEnum.UpdatePrice:
console.log({ identifier: event.identifier });
await this.updatePriceEventHandler.handle(event, hash, marketplaceType);
break;
case ExternalAuctionEventEnum.UpdateListing: {
console.log({ identifier: event.identifier });
await this.updateListingEventHandler.handle(event, hash, marketplaceType);
break;
}
case ExternalAuctionEventEnum.AcceptOffer:
case ExternalAuctionEventEnum.AcceptOfferFromAuction:
const acceptOfferEventName = Buffer.from(event.topics[0], 'base64').toString();
if (acceptOfferEventName === ExternalAuctionEventEnum.UserDeposit) {
continue;
}

console.log({ identifier: event.identifier });
if (acceptOfferEventName === ExternalAuctionEventEnum.EndTokenEvent) {
await this.withdrawAuctionEventHandler.handle(event, hash, marketplaceType);
} else {
await this.acceptOfferEventHandler.handle(event, hash, marketplaceType);
}

break;
case AuctionEventEnum.WithdrawAuctionAndAcceptOffer:
console.log({ identifier: event.identifier });
if (Buffer.from(event.topics[0], 'base64').toString() === AuctionEventEnum.Accept_offer_token_event) {
await this.acceptOfferEventHandler.handle(event, hash, marketplaceType);
} else {
await this.withdrawAuctionEventHandler.handle(event, hash, marketplaceType);
}
break;
case ExternalAuctionEventEnum.AcceptGlobalOffer:
console.log({ identifier: event.identifier });
await this.acceptGlobalOfferEventHandler.handle(event, hash, marketplaceType);
break;
case AuctionEventEnum.SendOffer:
console.log({ identifier: event.identifier });
await this.sendOfferEventHandler.handle(event, hash, marketplaceType);
break;
case AuctionEventEnum.WithdrawOffer:
console.log({ identifier: event.identifier });
await this.withdrawOfferEventHandler.handle(event, hash, marketplaceType);
break;
case KroganSwapAuctionEventEnum.NftSwapUpdate:
case KroganSwapAuctionEventEnum.NftSwapExtend:
console.log({ identifier: event.identifier });
await this.swapUpdateEventHandler.handle(event, hash, marketplaceType);
break;
case MarketplaceEventEnum.SCUpgrade: {
console.log({ identifier: event.identifier });
await this.slackReportService.sendScUpgradeNotification(event.address);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class AuctionTokenEventsTopics {
private nonce: string;
private auctionId: string;
private nrAuctionTokens: string;
private originalOwner: Address;
private originalOwner: string;
private minBid: string;
private maxBid: string;
private startTime: number;
Expand All @@ -20,14 +20,14 @@ export class AuctionTokenEventsTopics {
this.nonce = Buffer.from(rawTopics[2], 'base64').toString('hex');
this.auctionId = Buffer.from(rawTopics[3], 'base64').toString('hex');
this.nrAuctionTokens = parseInt(Buffer.from(rawTopics[4], 'base64').toString('hex'), 16).toString();
this.originalOwner = new Address(Buffer.from(rawTopics[5], 'base64'));
this.originalOwner = new Address(Buffer.from(rawTopics[5], 'base64')).bech32();

this.minBid = BinaryUtils.hexToNumber(BinaryUtils.base64ToHex(rawTopics[6])).toString();
this.maxBid = BinaryUtils.hexToNumber(BinaryUtils.base64ToHex(rawTopics[7])).toString();
this.maxBid = rawTopics[7] ? BinaryUtils.hexToNumber(BinaryUtils.base64ToHex(rawTopics[7])).toString() : '0';
this.startTime = BinaryUtils.hexToNumber(BinaryUtils.base64ToHex(rawTopics[8]));
this.endTime = BinaryUtils.hexToNumber(BinaryUtils.base64ToHex(rawTopics[9]));
this.endTime = rawTopics[9] ? parseInt(BinaryUtils.hexToNumber(BinaryUtils.base64ToHex(rawTopics[9])).toString()) : 0;
this.paymentToken = BinaryUtils.base64Decode(rawTopics[10]);
this.paymentNonce = BinaryUtils.hexToNumber(BinaryUtils.base64ToHex(rawTopics[11]));
this.paymentNonce = rawTopics[11] ? parseInt(BinaryUtils.hexToNumber(rawTopics[11]).toString()) : 0;
this.auctionType = BinaryUtils.hexToNumber(BinaryUtils.base64ToHex(rawTopics[12])).toString();

if (this.startTime.toString().length > 10) {
Expand All @@ -40,7 +40,7 @@ export class AuctionTokenEventsTopics {

toPlainObject() {
return {
originalOwner: this.originalOwner.bech32(),
originalOwner: this.originalOwner,
collection: this.collection,
nonce: this.nonce,
auctionId: this.auctionId,
Expand Down

0 comments on commit b843e86

Please sign in to comment.