Skip to content

Commit

Permalink
Merge pull request #49 from decentraland/fix/multiple-open-orders-eth
Browse files Browse the repository at this point in the history
fix: add close active order logic for eth
  • Loading branch information
juanmahidalgo authored Jan 14, 2025
2 parents 978103a + 7773653 commit 40b0eef
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
29 changes: 24 additions & 5 deletions src/eth/handlers/marketplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,34 @@ export function handleOrderCreated(
order.createdAt = timestamp;
order.updatedAt = timestamp;

if (nft.activeOrder) {
const oldOrder = orders.get(nft.activeOrder.id);
const currentOpenOrder = 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 && !currentOpenOrder) {
console.log(
`ERROR: Active order not found for NFT ${nft.id} and order ${nft.activeOrder.id}`
);
}

if (nft.activeOrder || currentOpenOrder) {
const oldOrder = currentOpenOrder || nft.activeOrder;

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}`);
}
}
nft.updatedAt = timestamp;
Expand Down
9 changes: 4 additions & 5 deletions src/polygon/handlers/marketplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function handleOrderCreated(
order.updatedAt = timestamp;

// 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>(
const currentOpenOrder = Array.from(orders.values()).reduce<Order | null>(
(newestOrder, currentOrder) => {
if (
currentOrder.nftAddress === nft.contractAddress &&
Expand All @@ -94,15 +94,14 @@ export function handleOrderCreated(
null
);

if (nft.activeOrder && !nftActiveOrder) {
if (nft.activeOrder && !currentOpenOrder) {
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;
const oldOrder = id ? orders.get(id) : null;
if (nft.activeOrder || currentOpenOrder) {
const oldOrder = currentOpenOrder || nft.activeOrder;

if (oldOrder) {
cancelActiveOrder(oldOrder, timestamp);
Expand Down

0 comments on commit 40b0eef

Please sign in to comment.