Skip to content

Commit

Permalink
refactor(order-synchronizer): remove deprecated method
Browse files Browse the repository at this point in the history
bludnic committed May 6, 2024
1 parent fc780af commit 9e15c52
Showing 1 changed file with 1 addition and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { NetworkError, OrderNotFound, RequestTimeout } from "ccxt";
import { subHours } from "date-fns";
import { exchangeProvider } from "@opentrader/exchanges";
import { NetworkError, RequestTimeout } from "ccxt";
import { ExchangeAccountProcessor } from "@opentrader/processing";
import { xprisma } from "@opentrader/db";
import { OrderSynchronizerWatcher } from "./order-synchronizer-watcher.abstract";

/**
@@ -52,76 +49,4 @@ export class OrderSynchronizerPollingWatcher extends OrderSynchronizerWatcher {
}
}
}

private async syncOrders__deprecated() {
// Find orders with `status == Placed` and the last sync was >1 hour ago.
const ONE_HOUR_AGO = subHours(new Date(), 1);

console.debug("Querying orders which last sync was done >1 hour ago");
const orders = await xprisma.order.findMany({
where: {
status: "Placed",
syncedAt: {
lt: ONE_HOUR_AGO,
},
},
orderBy: {
syncedAt: "asc",
},
include: {
smartTrade: {
include: {
exchangeAccount: true,
},
},
},
});

for (const order of orders) {
if (!order.exchangeOrderId) {
throw new Error("Order: Missing `exchangeOrderId`");
}

const { smartTrade } = order;
const { exchangeAccount } = smartTrade;

const exchange = exchangeProvider.fromAccount(exchangeAccount);

console.debug(
`Synchronize order #${order.id}: exchangeOrderId "${order.exchangeOrderId}": price: ${order.price}: status: ${order.status}`,
);
try {
const exchangeOrder = await exchange.getLimitOrder({
orderId: order.exchangeOrderId,
symbol: smartTrade.exchangeSymbolId,
});

await xprisma.order.updateSyncedAt(order.id);

if (exchangeOrder.status === "filled") {
const statusChanged = order.status !== "Filled";
if (statusChanged) {
this.emit("onFilled", [exchangeOrder, order]);
}
} else if (exchangeOrder.status === "canceled") {
const statusChanged = order.status !== "Canceled";
if (statusChanged) {
this.emit("onCanceled", [exchangeOrder, order]);
}
}
} catch (err) {
if (err instanceof OrderNotFound) {
await xprisma.order.updateStatus("Deleted", order.id);

console.debug(
`Order not found on the exchange. Change status to "Deleted"`,
);
} else {
throw err;
}
}
}

return orders.length;
}
}

0 comments on commit 9e15c52

Please sign in to comment.