Skip to content

Commit

Permalink
Merge pull request #780 from ElrondNetwork/development
Browse files Browse the repository at this point in the history
Development to main
  • Loading branch information
danielailie authored Dec 20, 2022
2 parents e3e2ef1 + 13d747e commit 9a92f5c
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 65 deletions.
22 changes: 22 additions & 0 deletions src/common/persistence/persistence.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,15 @@ export class PersistenceService {
);
}

async getLastAuctionIdForMarketplace(
marketplaceKey: string,
): Promise<number> {
return await this.execute(
this.getLastAuctionIdForMarketplace.name,
this.auctionsRepository.getLastAuctionIdForMarketplace(marketplaceKey),
);
}

async getBulkAuctions(auctionsIds: number[]): Promise<AuctionEntity[]> {
return await this.execute(
this.getBulkAuctions.name,
Expand All @@ -785,6 +794,19 @@ export class PersistenceService {
);
}

async getAuctionByIdentifierAndMarketplace(
identifier: string,
marketplaceKey: string,
): Promise<AuctionEntity> {
return await this.execute(
this.getAuctionByIdentifierAndMarketplace.name,
this.auctionsRepository.getAuctionByIdentifierAndMarketplace(
identifier,
marketplaceKey,
),
);
}

async getAuctionCountForIdentifiers(
identifiers: string[],
): Promise<AuctionEntity[]> {
Expand Down
61 changes: 27 additions & 34 deletions src/db/auctions/auctions.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,18 @@ export class AuctionsRepository {
return null;
}

async getLastAuctionIdForMarketplace(
marketplaceKey: string,
): Promise<number> {
const { marketplaceAuctionId } = await this.auctionsRepository
.createQueryBuilder('a')
.select('a.marketplaceAuctionId as marketplaceAuctionId')
.where({ marketplaceKey: marketplaceKey })
.orderBy('id', 'DESC')
.getRawOne();
return marketplaceAuctionId;
}

async getBulkAuctions(auctionsIds: number[]): Promise<AuctionEntity[]> {
return await this.auctionsRepository
.createQueryBuilder('auctions')
Expand All @@ -429,6 +441,21 @@ export class AuctionsRepository {
return null;
}

async getAuctionByIdentifierAndMarketplace(
identifier: string,
marketplaceKey: string,
): Promise<AuctionEntity> {
return await this.auctionsRepository.findOne({
where: [
{
identifier: identifier,
marketplaceKey: marketplaceKey,
status: AuctionStatusEnum.Running,
},
],
});
}

async getAuctionCountForIdentifiers(
identifiers: string[],
): Promise<AuctionEntity[]> {
Expand Down Expand Up @@ -527,40 +554,6 @@ export class AuctionsRepository {
await this.rollbackCreateAuction(auctions);
}

private getDefaultQueryRequest(
startDate: number,
endDate: number = null,
): QueryRequest {
if (endDate) {
return new QueryRequest({
filters: {
childExpressions: undefined,
filters: [
{ field: 'status', values: ['Running'], op: Operation.EQ },
{ field: 'startDate', values: [`${startDate}`], op: Operation.LE },
{
field: 'endDate',
values: [`${endDate ? endDate : startDate}`],
op: Operation.LE,
},
{ field: 'endDate', values: ['1'], op: Operation.GE },
],
operator: Operator.AND,
},
});
}
return new QueryRequest({
filters: {
childExpressions: undefined,
filters: [
{ field: 'status', values: ['Running'], op: Operation.EQ },
{ field: 'startDate', values: [`${startDate}`], op: Operation.LE },
],
operator: Operator.AND,
},
});
}

private async getAuctionsForIdentifierSortByPrice(
queryRequest: QueryRequest,
identifier: string,
Expand Down
2 changes: 2 additions & 0 deletions src/modules/assets/models/AuctionEvent.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export enum AuctionEventEnum {

export enum ExternalAuctionEventEnum {
Listing = 'listing',
ListNftOnMarketplace = 'listNftOnMarketplace',
Buy = 'buy',
BuyNft = 'buyNft',
BulkBuy = 'bulkBuy',
Expand All @@ -16,6 +17,7 @@ export enum ExternalAuctionEventEnum {
AcceptOffer = 'acceptOffer',
UpdateOffer = 'update_offer_event',
AcceptGlobalOffer = 'acceptGlobalOffer',
ClaimBackNft = 'claimBackNft',
}

export enum ElrondNftsSwapAuctionEventEnum {
Expand Down
18 changes: 18 additions & 0 deletions src/modules/auctions/auctions-getter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ export class AuctionsGetterService {
return await this.persistenceService.getAuction(id);
}

async getLastAuctionIdForMarketplace(
marketplaceKey: string,
): Promise<number> {
return await this.persistenceService.getLastAuctionIdForMarketplace(
marketplaceKey,
);
}

async getAuctionByIdAndMarketplace(
id: number,
marketplaceKey: string,
Expand All @@ -145,6 +153,16 @@ export class AuctionsGetterService {
);
}

async getAuctionByIdentifierAndMarketplace(
identifier: string,
marketplaceKey: string,
): Promise<AuctionEntity> {
return await this.persistenceService.getAuctionByIdentifierAndMarketplace(
identifier,
marketplaceKey,
);
}

async getAvailableTokens(id: number): Promise<number> {
return await this.persistenceService.getAvailableTokensByAuctionId(id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { Injectable, Logger } from '@nestjs/common';
import { ElrondNftsSwapAuctionEventEnum } from 'src/modules/assets/models';
import { AuctionEntity } from 'src/db/auctions';
import {
ElrondNftsSwapAuctionEventEnum,
ExternalAuctionEventEnum,
} from 'src/modules/assets/models';
import {
AuctionsGetterService,
AuctionsSetterService,
} from 'src/modules/auctions';
import { AuctionStatusEnum } from 'src/modules/auctions/models';
import { MarketplacesService } from 'src/modules/marketplaces/marketplaces.service';
import { Marketplace } from 'src/modules/marketplaces/models';
import { MarketplaceTypeEnum } from 'src/modules/marketplaces/models/MarketplaceType.enum';
import { CreateOrderArgs, OrderStatusEnum } from 'src/modules/orders/models';
import { OrdersService } from 'src/modules/orders/order.service';
import { BuySftEvent } from '../../entities/auction';
import { ClaimEvent } from '../../entities/auction/claim.event';
import { ElrondSwapBuyEvent } from '../../entities/auction/elrondnftswap/elrondswap-buy.event';
import { FeedEventsSenderService } from '../feed-events.service';

Expand All @@ -27,6 +31,7 @@ export class BuyEventHandler {

async handle(event: any, hash: string, marketplaceType: MarketplaceTypeEnum) {
const { buySftEvent, topics } = this.getEventAndTopics(event, hash);
let auction: AuctionEntity;

const marketplace = await this.marketplaceService.getMarketplaceByType(
buySftEvent.getAddress(),
Expand All @@ -38,11 +43,20 @@ export class BuyEventHandler {
this.logger.log(
`Buy event detected for hash '${hash}' and marketplace '${marketplace?.name}'`,
);
const auction =
await this.auctionsGetterService.getAuctionByIdAndMarketplace(

if (topics.auctionId) {
auction = await this.auctionsGetterService.getAuctionByIdAndMarketplace(
parseInt(topics.auctionId, 16),
marketplace.key,
);
} else {
const auctionIdentifier = `${topics.collection}-${topics.nonce}`;
auction =
await this.auctionsGetterService.getAuctionByIdentifierAndMarketplace(
auctionIdentifier,
marketplace.key,
);
}
if (!auction) return;

const result = await this.auctionsGetterService.getAvailableTokens(
Expand All @@ -64,7 +78,7 @@ export class BuyEventHandler {
ownerAddress: topics.currentWinner,
auctionId: auction.id,
priceToken: auction.paymentToken,
priceAmount: topics.bid,
priceAmount: auction.minBid,
priceNonce: auction.paymentNonce,
blockHash: hash,
status: OrderStatusEnum.Bought,
Expand All @@ -74,7 +88,7 @@ export class BuyEventHandler {
);
await this.feedEventsSenderService.sendBuyEvent(
topics.currentWinner,
topics.bid,
auction.minBid,
topics.boughtTokens,
orderSft,
auction,
Expand All @@ -97,6 +111,12 @@ export class BuyEventHandler {
const topics = buySftEvent.getTopics();
return { buySftEvent, topics };
}

if (event.identifier === ExternalAuctionEventEnum.BuyNft) {
const buySftEvent = new ClaimEvent(event);
const topics = buySftEvent.getTopics();
return { buySftEvent, topics };
}
const buySftEvent = new BuySftEvent(event);
const topics = buySftEvent.getTopics();
return { buySftEvent, topics };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import { Injectable, Logger } from '@nestjs/common';
import { elrondConfig } from 'src/config';
import { AuctionEntity } from 'src/db/auctions';
import { AssetByIdentifierService } from 'src/modules/assets';
import { ElrondNftsSwapAuctionEventEnum } from 'src/modules/assets/models';
import { AuctionsSetterService } from 'src/modules/auctions';
import {
ElrondNftsSwapAuctionEventEnum,
ExternalAuctionEventEnum,
} from 'src/modules/assets/models';
import {
AuctionsGetterService,
AuctionsSetterService,
} from 'src/modules/auctions';
import { ElrondSwapAuctionTypeEnum } from 'src/modules/auctions/models';
import { MarketplacesService } from 'src/modules/marketplaces/marketplaces.service';
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 } from 'src/utils/constants';
import { ELRONDNFTSWAP_KEY, ENEFTOR_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';
import { FeedEventsSenderService } from '../feed-events.service';

@Injectable()
export class StartAuctionEventHandler {
private readonly logger = new Logger(StartAuctionEventHandler.name);
constructor(
private auctionsService: AuctionsSetterService,
private auctionsSetterService: AuctionsSetterService,
private auctionsGetterService: AuctionsGetterService,
private feedEventsSenderService: FeedEventsSenderService,
private assetByIdentifierService: AssetByIdentifierService,
private usdPriceService: UsdPriceService,
Expand All @@ -28,7 +37,6 @@ export class StartAuctionEventHandler {
const { auctionTokenEvent, topics } = this.getEventAndTopics(event);
if (!auctionTokenEvent && !topics) return;

const auctionIdentifier = `${topics.collection}-${topics.nonce}`;
const marketplace = await this.marketplaceService.getMarketplaceByType(
auctionTokenEvent.getAddress(),
marketplaceType,
Expand All @@ -39,12 +47,7 @@ export class StartAuctionEventHandler {
this.logger.log(
`Auction listing event detected for hash '${hash}' and marketplace '${marketplace?.name}'`,
);
const auction = await this.saveAuction(
topics,
auctionIdentifier,
marketplace,
hash,
);
const auction = await this.saveAuction(topics, marketplace, hash);

if (!auction) return;

Expand All @@ -57,46 +60,60 @@ export class StartAuctionEventHandler {

private async saveAuction(
topics: any,
auctionIdentifier: string,
marketplace: Marketplace,
hash: string,
) {
if (marketplace.key === ELRONDNFTSWAP_KEY) {
return await this.handleElrondSwapAuction(
const auctionIdentifier = `${topics.collection}-${topics.nonce}`;
if (
marketplace.key === ELRONDNFTSWAP_KEY ||
marketplace.key === ENEFTOR_KEY
) {
if (topics.auctionId === '0') {
let auctionId =
await this.auctionsGetterService.getLastAuctionIdForMarketplace(
marketplace.key,
);
topics.auctionId = auctionId && auctionId > 0 ? auctionId++ : 1;
}
return await this.handleSaveAuctionFromTopics(
auctionIdentifier,
topics,
hash,
marketplace,
);
}
return await this.auctionsService.saveAuction(

return await this.auctionsSetterService.saveAuction(
parseInt(topics.auctionId, 16),
auctionIdentifier,
marketplace,
hash,
);
}

private async handleElrondSwapAuction(
private async handleSaveAuctionFromTopics(
auctionIdentifier: string,
topics: any,
hash: string,
auctionTokenEventMarketplace: Marketplace,
) {
let decimals = elrondConfig.decimals;
const asset = await this.assetByIdentifierService.getAsset(
auctionIdentifier,
);

const paymentToken = await this.usdPriceService.getToken(
topics.paymentToken,
);
return await this.auctionsService.saveAuctionEntity(
if (topics.paymentToken !== elrondConfig.egld) {
const paymentToken = await this.usdPriceService.getToken(
topics.paymentToken,
);
decimals = paymentToken.decimals;
}
return await this.auctionsSetterService.saveAuctionEntity(
AuctionEntity.fromWithdrawTopics(
topics,
asset.tags?.toString(),
hash,
auctionTokenEventMarketplace.key,
paymentToken?.decimals,
decimals,
),
asset.tags,
);
Expand All @@ -111,6 +128,12 @@ export class StartAuctionEventHandler {
}
return { auctionTokenEvent, topics };
}

if (event.identifier === ExternalAuctionEventEnum.ListNftOnMarketplace) {
const auctionTokenEvent = new ListNftEvent(event);
const topics = auctionTokenEvent.getTopics();
return { auctionTokenEvent, topics };
}
const auctionTokenEvent = new AuctionTokenEvent(event);
const topics = auctionTokenEvent.getTopics();
return { auctionTokenEvent, topics };
Expand Down
Loading

0 comments on commit 9a92f5c

Please sign in to comment.