Skip to content

Commit

Permalink
Update event logs model
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed May 22, 2024
1 parent e193568 commit 3864858
Show file tree
Hide file tree
Showing 35 changed files with 161 additions and 132 deletions.
6 changes: 3 additions & 3 deletions src/modules/metrics/metrics.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Body, Controller, Get, HttpStatus, Post } from '@nestjs/common';
import { MetricsCollector } from './metrics.collector';
import { RabbitEvent } from './rabbitEvent';
import { EventLog } from './rabbitEvent';
import { MarketplaceEventsProcessingService } from '../rabbitmq/blockchain-events/marketplace-events-processing.service';

@Controller()
Expand All @@ -17,8 +17,8 @@ export class MetricsController {
}

@Post('/event')
async notify(@Body() payload: RabbitEvent): Promise<HttpStatus> {
await this.marketplaceEvents.handleNftAuctionEvents(payload?.events, payload?.hash);
async notify(@Body() payload: EventLog[]): Promise<HttpStatus> {
await this.marketplaceEvents.handleNftAuctionEvents(payload);
return HttpStatus.OK;
}
}
17 changes: 17 additions & 0 deletions src/modules/metrics/rabbitEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,20 @@ export class RabbitEventLog {
Object.assign(this, init);
}
}

export class EventLog {
eventId: string = '';
txHash: string = '';
shardId: number = 0;
timestamp: number = 0;
address: string = '';
identifier: string = '';
topics: string[] = [];
data: string = '';
additionalData: string[] = [];
txOrder: number = 0;
eventOrder: number = 0;
constructor(init?: Partial<EventLog>) {
Object.assign(this, init);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AuctionStatusEnum } from 'src/modules/auctions/models';
import { XOXNO_KEY } from 'src/utils/constants';
import { AcceptGlobalOfferEvent } from '../../entities/auction-reindex/acceptGlobalOffer.event';
import { Marketplace } from 'src/modules/marketplaces/models';
import { EventLog } from 'src/modules/metrics/rabbitEvent';

@Injectable()
export class AcceptGlobalOfferEventHandler {
Expand All @@ -14,11 +15,11 @@ export class AcceptGlobalOfferEventHandler {
private auctionsService: AuctionsSetterService,
) { }

async handle(event: any, hash: string, marketplace: Marketplace) {
async handle(event: EventLog, marketplace: Marketplace) {
try {
const acceptGlobalOfferEvent = new AcceptGlobalOfferEvent(event);
const topics = acceptGlobalOfferEvent.getTopics();
this.logger.log(`Accept Global Offer event detected for hash '${hash}' and marketplace '${marketplace?.name}'`);
this.logger.log(`Accept Global Offer event detected for marketplace '${marketplace?.name}'`);
if (marketplace.key !== XOXNO_KEY || topics.auctionId <= 0) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { AcceptOfferXoxnoEvent } from '../../entities/auction-reindex/acceptOffe
import { FeedEventsSenderService } from '../feed-events.service';
import { AcceptOfferFrameitEvent } from '../../entities/auction-reindex/acceptOfferFrameit.event';
import { Marketplace } from 'src/modules/marketplaces/models';
import { EventLog } from 'src/modules/metrics/rabbitEvent';

@Injectable()
export class AcceptOfferEventHandler {
Expand All @@ -27,7 +28,7 @@ export class AcceptOfferEventHandler {
private readonly notificationsService: NotificationsService,
) { }

async handle(event: any, hash: string, marketplace: Marketplace) {
async handle(event: EventLog, marketplace: Marketplace) {
try {
if (marketplace?.type === MarketplaceTypeEnum.External) {
let acceptOfferEvent = undefined;
Expand All @@ -43,7 +44,7 @@ export class AcceptOfferEventHandler {
if (!acceptOfferEvent) return;

this.logger.log(
`${acceptOfferEvent.getIdentifier()} event detected for hash '${hash}' and marketplace '${marketplace?.name}'`,
`${acceptOfferEvent.getIdentifier()} event detected for marketplace '${marketplace?.name}'`,
);

if (topics.auctionId || topics.auctionId !== 0) {
Expand All @@ -60,11 +61,11 @@ export class AcceptOfferEventHandler {
const acceptOfferEvent = new AcceptOfferEvent(event);
const topics = acceptOfferEvent.getTopics();
marketplace = await this.marketplaceService.getMarketplaceByType(
acceptOfferEvent.getAddress(),
acceptOfferEvent.address,
marketplace.type,
topics.collection,
);
this.logger.log(`Accept Offer event detected for hash '${hash}' and marketplace '${marketplace?.name}'`);
this.logger.log(`Accept Offer event detected for hash marketplace '${marketplace?.name}'`);

const offer = await this.offersService.getOfferByIdAndMarketplace(topics.offerId, marketplace.key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ELRONDNFTSWAP_KEY } from 'src/utils/constants';
import { BidEvent } from '../../entities/auction-reindex';
import { ElrondSwapBidEvent } from '../../entities/auction-reindex/elrondnftswap/elrondswap-bid.event';
import { FeedEventsSenderService } from '../feed-events.service';
import { EventLog } from 'src/modules/metrics/rabbitEvent';

@Injectable()
export class BidEventHandler {
Expand All @@ -27,7 +28,7 @@ export class BidEventHandler {
private readonly persistenceService: PersistenceService,
) { }

async handle(event: any, hash: string, marketplace: Marketplace) {
async handle(event: EventLog, marketplace: Marketplace) {
try {
let [bidEvent, topics] = [undefined, undefined];
if (marketplace.type === MarketplaceTypeEnum.External) {
Expand All @@ -37,7 +38,7 @@ export class BidEventHandler {
marketplace = await this.marketplaceService.getMarketplaceByType(bidEvent.getAddress(), marketplace.type, topics.collection);
}
if (!marketplace) return;
this.logger.log(`${bidEvent.getIdentifier()} event detected for hash '${hash}' and marketplace '${marketplace?.name}'`);
this.logger.log(`${bidEvent.getIdentifier()} event detected for marketplace '${marketplace?.name}'`);
const auction = await this.auctionsGetterService.getAuctionByIdAndMarketplace(parseInt(topics.auctionId, 16), marketplace.key);
if (!auction) return;

Expand All @@ -53,7 +54,7 @@ export class BidEventHandler {
priceToken: auction.paymentToken,
priceAmount: topics.currentBid,
priceNonce: auction.paymentNonce,
blockHash: hash,
blockHash: '',
status: OrderStatusEnum.Active,
marketplaceKey: marketplace.key,
}),
Expand All @@ -63,7 +64,7 @@ export class BidEventHandler {
if (auction.maxBidDenominated === order.priceAmountDenominated) {
this.notificationsService.updateNotificationStatus([auction?.id]);
this.notificationsService.addNotifications(auction, order);
this.auctionsService.updateAuctionStatus(auction.id, AuctionStatusEnum.Ended, hash, event.identifier);
this.auctionsService.updateAuctionStatus(auction.id, AuctionStatusEnum.Ended, 'hash', event.identifier);
this.persistenceService.updateOrderWithStatus(order, OrderStatusEnum.Bought);
}
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ClaimEvent } from '../../entities/auction-reindex/claim.event';
import { ElrondSwapBuyEvent } from '../../entities/auction-reindex/elrondnftswap/elrondswap-buy.event';
import { FeedEventsSenderService } from '../feed-events.service';
import { Marketplace } from 'src/modules/marketplaces/models';
import { EventLog } from 'src/modules/metrics/rabbitEvent';

@Injectable()
export class BuyEventHandler {
Expand All @@ -24,15 +25,15 @@ export class BuyEventHandler {
private readonly marketplaceService: MarketplacesService,
) { }

async handle(event: any, hash: string, marketplace: Marketplace) {
async handle(event: any, marketplace: Marketplace) {
try {
const { buySftEvent, topics } = this.getEventAndTopics(event, hash);
const { buySftEvent, topics } = this.getEventAndTopics(event, 'hash');
let auction: AuctionEntity;

marketplace = await this.marketplaceService.getMarketplaceByType(buySftEvent.getAddress(), marketplace.type, topics.collection);
marketplace = await this.marketplaceService.getMarketplaceByType(buySftEvent.address, marketplace.type, topics.collection);

if (!marketplace) return;
this.logger.log(`${buySftEvent.getIdentifier()} event detected for hash '${hash}' and marketplace '${marketplace?.name}'`);
this.logger.log(`${buySftEvent.identifier} event detected for marketplace '${marketplace?.name}'`);

if (topics.auctionId) {
auction = await this.auctionsGetterService.getAuctionByIdAndMarketplace(parseInt(topics.auctionId, 16), marketplace.key);
Expand All @@ -45,7 +46,7 @@ export class BuyEventHandler {
const result = await this.auctionsGetterService.getAvailableTokens(auction.id);
const totalRemaining = result ? result[0]?.availableTokens - parseFloat(topics.boughtTokens) : 0;
if (totalRemaining === 0) {
this.auctionsService.updateAuctionStatus(auction.id, AuctionStatusEnum.Ended, hash, AuctionStatusEnum.Ended);
this.auctionsService.updateAuctionStatus(auction.id, AuctionStatusEnum.Ended, 'hash', AuctionStatusEnum.Ended);
}
const orderSft = await this.ordersService.createOrderForSft(
new CreateOrderArgs({
Expand All @@ -54,7 +55,7 @@ export class BuyEventHandler {
priceToken: auction.paymentToken,
priceAmount: auction.minBid,
priceNonce: auction.paymentNonce,
blockHash: hash,
blockHash: 'hash',
status: OrderStatusEnum.Bought,
boughtTokens: topics.boughtTokens,
marketplaceKey: marketplace.key,
Expand All @@ -73,7 +74,7 @@ export class BuyEventHandler {
}
}

private getEventAndTopics(event: any, hash: string) {
private getEventAndTopics(event: EventLog, hash: string) {
if (event.identifier === KroganSwapAuctionEventEnum.Purchase) {
if (Buffer.from(event.topics[0], 'base64').toString() === KroganSwapAuctionEventEnum.UpdateListing) {
this.logger.log(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ export class EndAuctionEventHandler {
private notificationsService: NotificationsService,
) { }

async handle(event: any, hash: string, marketplace: Marketplace) {
async handle(event: any, marketplace: Marketplace) {
try {
const endAuctionEvent = new EndAuctionEvent(event);
const topics = endAuctionEvent.getTopics();
marketplace = await this.marketplaceService.getMarketplaceByType(
endAuctionEvent.getAddress(),
endAuctionEvent.address,
marketplace.type,
topics.collection,
);

if (!marketplace) return;
this.logger.log(`End auction event detected for hash '${hash}' and marketplace '${marketplace?.name}'`);
this.logger.log(`End auction event detected for marketplace '${marketplace?.name}'`);
const auction = await this.auctionsGetterService.getAuctionByIdAndMarketplace(parseInt(topics.auctionId, 16), marketplace.key);

if (!auction) return;

this.auctionsService.updateAuctionStatus(auction.id, AuctionStatusEnum.Ended, hash, AuctionEventEnum.EndAuctionEvent);
this.auctionsService.updateAuctionStatus(auction.id, AuctionStatusEnum.Ended, 'hash', AuctionEventEnum.EndAuctionEvent);
this.notificationsService.updateNotificationStatus([auction.id]);
this.ordersService.updateOrder(auction.id, OrderStatusEnum.Bought);
await this.feedEventsSenderService.sendWonAuctionEvent(topics, auction, marketplace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { OffersService } from 'src/modules/offers/offers.service';
import { SendOfferEvent } from '../../entities/auction-reindex/sendOffer.event';
import { FeedEventsSenderService } from '../feed-events.service';
import { Marketplace } from 'src/modules/marketplaces/models';
import { EventLog } from 'src/modules/metrics/rabbitEvent';

@Injectable()
export class SendOfferEventHandler {
Expand All @@ -20,22 +21,22 @@ export class SendOfferEventHandler {
private readonly marketplaceService: MarketplacesService,
) { }

async handle(event: any, hash: string, marketplace: Marketplace) {
async handle(event: EventLog, marketplace: Marketplace) {
try {
if (marketplace.type === MarketplaceTypeEnum.External) {
return;
}
const sendOffer = new SendOfferEvent(event);
const topics = sendOffer.getTopics();
marketplace = await this.marketplaceService.getMarketplaceByType(sendOffer.getAddress(), marketplace.type, topics.collection);
marketplace = await this.marketplaceService.getMarketplaceByType(sendOffer.address, marketplace.type, topics.collection);

if (!marketplace) {
return;
}

this.logger.log(`Send Offer event detected for hash '${hash}' and marketplace '${marketplace?.name}'`);
this.logger.log(`Send Offer event detected for marketplace '${marketplace?.name}'`);

const offer = await this.offersService.saveOffer(OfferEntity.fromEventTopics(topics, hash, marketplace.key, OfferStatusEnum.Active));
const offer = await this.offersService.saveOffer(OfferEntity.fromEventTopics(topics, 'hash', marketplace.key, OfferStatusEnum.Active));

if (!offer) return;
await this.feedEventsSenderService.sendOfferEvent(offer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { AuctionTokenEvent } from '../../entities/auction-reindex';
import { ElrondSwapAuctionEvent } from '../../entities/auction-reindex/elrondnftswap/elrondswap-auction.event';
import { ListNftEvent } from '../../entities/auction-reindex/listNft.event';
import { FeedEventsSenderService } from '../feed-events.service';
import { EventLog } from 'src/modules/metrics/rabbitEvent';

@Injectable()
export class StartAuctionEventHandler {
Expand All @@ -27,22 +28,22 @@ export class StartAuctionEventHandler {
private readonly marketplaceService: MarketplacesService,
) { }

async handle(event: any, hash: string, marketplace: Marketplace) {
async handle(event: any, marketplace: Marketplace) {
try {
const { auctionTokenEvent, topics } = this.getEventAndTopics(event);
if (!auctionTokenEvent && !topics) return;

marketplace = await this.marketplaceService.getMarketplaceByType(
auctionTokenEvent.getAddress(),
auctionTokenEvent.address,
marketplace.type,
topics.collection,
);

if (!marketplace) return;
this.logger.log(
`${auctionTokenEvent.getIdentifier()} listing event detected for hash '${hash}' and marketplace '${marketplace?.name}'`,
`${auctionTokenEvent.identifier} listing event detected for marketplace '${marketplace?.name}'`,
);
const auction = await this.saveAuction(topics, marketplace, hash);
const auction = await this.saveAuction(topics, marketplace, 'hash');

if (!auction) return;

Expand Down Expand Up @@ -89,7 +90,7 @@ export class StartAuctionEventHandler {
);
}

private getEventAndTopics(event: any) {
private getEventAndTopics(event: EventLog) {
if (event.identifier === KroganSwapAuctionEventEnum.NftSwap) {
const auctionTokenEvent = new ElrondSwapAuctionEvent(event);
const topics = auctionTokenEvent.getTopics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export class SwapUpdateEventHandler {
private usdPriceService: UsdPriceService,
) { }

async handle(event: any, hash: string, marketplace: Marketplace) {
async handle(event: any, marketplace: Marketplace) {
try {
const updateEvent = new ElrondSwapUpdateEvent(event);
const topics = updateEvent.getTopics();
this.logger.log(`${updateEvent.getIdentifier()} auction event detected for hash '${hash}' and marketplace '${marketplace?.name}'`);
this.logger.log(`${updateEvent.identifier} auction event detected for marketplace '${marketplace?.name}'`);
let auction = await this.auctionsGetterService.getAuctionByIdAndMarketplace(parseInt(topics.auctionId, 16), marketplace.key);

if (auction) {
await this.updateAuctionPrice(auction, topics, hash);
await this.updateAuctionPrice(auction, topics);
this.auctionsService.updateAuction(auction, KroganSwapAuctionEventEnum.NftSwapUpdate);
}
} catch (error) {
Expand All @@ -44,7 +44,6 @@ export class SwapUpdateEventHandler {
price: string;
deadline: number;
},
hash: string,
) {
const paymentToken = await this.usdPriceService.getToken(auction.paymentToken);
const decimals = paymentToken?.decimals ?? mxConfig.decimals;
Expand All @@ -54,6 +53,6 @@ export class SwapUpdateEventHandler {
auction.maxBidDenominated = auction.minBidDenominated;
auction.endDate = topics.deadline;
auction.nrAuctionedTokens = topics.nrAuctionTokens;
auction.blockHash = hash;
auction.blockHash = 'hash';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { UsdPriceService } from 'src/modules/usdPrice/usd-price.service';
import { BigNumberUtils } from 'src/utils/bigNumber-utils';
import { UpdateListingEvent } from '../../entities/auction-reindex/updateListing.event';
import { Marketplace } from 'src/modules/marketplaces/models';
import { EventLog } from 'src/modules/metrics/rabbitEvent';

@Injectable()
export class UpdateListingEventHandler {
Expand All @@ -21,20 +22,20 @@ export class UpdateListingEventHandler {
private usdPriceService: UsdPriceService,
) { }

async handle(event: any, hash: string, marketplace: Marketplace) {
async handle(event: EventLog, marketplace: Marketplace) {
try {
const updateListingEvent = new UpdateListingEvent(event);
const topics = updateListingEvent.getTopics();
marketplace = await this.marketplaceService.getMarketplaceByType(updateListingEvent.getAddress(), marketplace.type, topics.collection);
marketplace = await this.marketplaceService.getMarketplaceByType(updateListingEvent.address, marketplace.type, topics.collection);
this.logger.log(
`${updateListingEvent.getIdentifier()} listing event detected for hash '${hash}' and marketplace '${marketplace?.name}'`,
`${updateListingEvent.identifier} listing event detected for marketplace '${marketplace?.name}'`,
);
let auction = await this.auctionsGetterService.getAuctionByIdAndMarketplace(parseInt(topics.auctionId, 16), marketplace.key);

if (auction && marketplace) {
const paymentToken = await this.usdPriceService.getToken(auction.paymentToken);

this.updateAuctionListing(auction, updateListingEvent, paymentToken, hash);
this.updateAuctionListing(auction, updateListingEvent, paymentToken, 'hash');

this.auctionsService.updateAuction(auction, ExternalAuctionEventEnum.UpdateListing);
}
Expand Down
Loading

0 comments on commit 3864858

Please sign in to comment.