Skip to content

Commit

Permalink
fix: multiple open orders
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmahidalgo committed Jan 13, 2025
1 parent 1fda408 commit e5dc2c0
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/polygon/handlers/marketplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,39 @@ export function handleOrderCreated(
order.createdAt = timestamp;
order.updatedAt = timestamp;

if (nft.activeOrder) {
const oldOrder = orders.get(nft.activeOrder.id);
// get open order for the nft since nft.activeOrder can be undefined but an order could still be open
const nftActiveOrder = Array.from(orders.values()).reduce<Order | null>(
(newestOrder, currentOrder) => {
if (
currentOrder.nftAddress === nft.contractAddress &&
currentOrder.tokenId === nft.tokenId &&
currentOrder.status === OrderStatus.open &&
(!newestOrder || currentOrder.createdAt > newestOrder.createdAt)
) {
return currentOrder;
}
return newestOrder;
},
null
);

if (nft.activeOrder && !nftActiveOrder) {
console.log(
`ERROR: Active order not found for NFT ${nft.id} and order ${nft.activeOrder.id}`
);
}

if (nft.activeOrder || nftActiveOrder) {
const id = nft.activeOrder ? nft.activeOrder.id : nftActiveOrder?.id;
// console.log("id", id);
// console.log("id gotten from nft.activeOrder", nft.activeOrder?.id);
// console.log("id gotten from nftActiveOrder", nftActiveOrder?.id);
const oldOrder = id ? orders.get(id) : null;

if (oldOrder) {
cancelActiveOrder(oldOrder, timestamp);
} else {
console.log(
`ERROR: Order not found when trying to cancel order ${nft.activeOrder.id}`
);
console.log(`ERROR: Order not found when trying to cancel order ${id}`);
}
}

Expand Down

0 comments on commit e5dc2c0

Please sign in to comment.