-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Update listing event * SERVICES-1172 handle changeListing marketplace event for XOXNO Co-authored-by: danielailie <[email protected]>
- Loading branch information
1 parent
72e0d0c
commit b941133
Showing
7 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/modules/rabbitmq/blockchain-events/handlers/updateListing-event.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { BinaryUtils } from '@elrondnetwork/erdnest'; | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { Token } from 'src/common/services/mx-communication/models/Token.model'; | ||
import { AuctionEntity } from 'src/db/auctions'; | ||
import { ExternalAuctionEventEnum } from 'src/modules/assets/models'; | ||
import { | ||
AuctionsGetterService, | ||
AuctionsSetterService, | ||
} from 'src/modules/auctions'; | ||
import { MarketplacesService } from 'src/modules/marketplaces/marketplaces.service'; | ||
import { MarketplaceTypeEnum } from 'src/modules/marketplaces/models/MarketplaceType.enum'; | ||
import { UsdPriceService } from 'src/modules/usdPrice/usd-price.service'; | ||
import { BigNumberUtils } from 'src/utils/bigNumber-utils'; | ||
import { UpdateListingEvent } from '../../entities/auction/updateListing.event'; | ||
|
||
@Injectable() | ||
export class UpdateListingEventHandler { | ||
private readonly logger = new Logger(UpdateListingEventHandler.name); | ||
constructor( | ||
private auctionsGetterService: AuctionsGetterService, | ||
private auctionsService: AuctionsSetterService, | ||
private readonly marketplaceService: MarketplacesService, | ||
private usdPriceService: UsdPriceService, | ||
) {} | ||
|
||
async handle(event: any, hash: string, marketplaceType: MarketplaceTypeEnum) { | ||
const updateListingEvent = new UpdateListingEvent(event); | ||
const topics = updateListingEvent.getTopics(); | ||
const marketplace = await this.marketplaceService.getMarketplaceByType( | ||
updateListingEvent.getAddress(), | ||
marketplaceType, | ||
topics.collection, | ||
); | ||
this.logger.log( | ||
`Update listing event detected for hash '${hash}' and 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.auctionsService.updateAuction( | ||
auction, | ||
ExternalAuctionEventEnum.UpdateListing, | ||
); | ||
} | ||
} | ||
|
||
private updateAuctionListing( | ||
auction: AuctionEntity, | ||
event: UpdateListingEvent, | ||
paymentToken: Token, | ||
hash: string, | ||
) { | ||
const eventTopics = event.getTopics(); | ||
|
||
if (eventTopics.newBid) { | ||
auction.minBid = eventTopics.newBid; | ||
auction.minBidDenominated = BigNumberUtils.denominateAmount( | ||
eventTopics.newBid, | ||
paymentToken.decimals, | ||
); | ||
} | ||
|
||
if (eventTopics.deadline) { | ||
auction.endDate = eventTopics.deadline; | ||
} | ||
|
||
if (eventTopics.paymentToken) { | ||
auction.paymentToken = eventTopics.paymentToken; | ||
auction.paymentNonce = BinaryUtils.hexToNumber( | ||
eventTopics.paymentTokenNonce, | ||
); | ||
} | ||
|
||
auction.blockHash = hash; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/modules/rabbitmq/entities/auction/updateListing.event.topics.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Address } from '@elrondnetwork/erdjs/out'; | ||
import { BinaryUtils } from '@elrondnetwork/erdnest'; | ||
export class UpdateListingEventsTopics { | ||
private collection: string; | ||
private nonce: string; | ||
private auctionId: string; | ||
private ownerAddress: Address; | ||
private oldPrice: string; | ||
private newBid: string; | ||
private paymentToken: string; | ||
private paymentTokenNonce: string; | ||
private deadline: number = 0; | ||
|
||
constructor(rawTopics: string[]) { | ||
this.collection = Buffer.from(rawTopics[1], 'base64').toString(); | ||
this.nonce = Buffer.from(rawTopics[2], 'base64').toString('hex'); | ||
this.auctionId = Buffer.from(rawTopics[3], 'base64').toString('hex'); | ||
this.ownerAddress = new Address(Buffer.from(rawTopics[4], 'base64')); | ||
this.oldPrice = Buffer.from(rawTopics[5], 'base64') | ||
.toString('hex') | ||
.hexBigNumberToString(); | ||
this.newBid = Buffer.from(rawTopics[6], 'base64') | ||
.toString('hex') | ||
.hexBigNumberToString(); | ||
this.paymentToken = BinaryUtils.base64Decode(rawTopics[7]); | ||
this.paymentTokenNonce = | ||
BinaryUtils.tryBase64ToBigInt(rawTopics[8])?.toString() ?? '0'; | ||
this.deadline = parseInt( | ||
Buffer.from(rawTopics[9] ?? '0', 'base64').toString('hex'), | ||
16, | ||
); | ||
} | ||
|
||
toPlainObject() { | ||
return { | ||
collection: this.collection, | ||
nonce: this.nonce, | ||
auctionId: this.auctionId, | ||
ownerAddress: this.ownerAddress, | ||
oldPrice: this.oldPrice, | ||
newBid: this.newBid, | ||
paymentToken: this.paymentToken, | ||
paymentTokenNonce: this.paymentTokenNonce, | ||
deadline: this.deadline, | ||
}; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/modules/rabbitmq/entities/auction/updateListing.event.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { GenericEvent } from '../generic.event'; | ||
import { UpdateListingEventsTopics } from './updateListing.event.topics'; | ||
|
||
export class UpdateListingEvent extends GenericEvent { | ||
private decodedTopics: UpdateListingEventsTopics; | ||
|
||
constructor(init?: Partial<GenericEvent>) { | ||
super(init); | ||
this.decodedTopics = new UpdateListingEventsTopics(this.topics); | ||
} | ||
|
||
getTopics() { | ||
return this.decodedTopics.toPlainObject(); | ||
} | ||
} |