Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Oct 25, 2024
1 parent ce82066 commit ed00cba
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 213 deletions.
138 changes: 52 additions & 86 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,85 +246,47 @@ export const arbRound = async (
) => {
return await tracer.startActiveSpan("process-orders", {}, roundCtx, async (span) => {
const ctx = trace.setSpan(context.active(), span);
// let ordersDetails;
options;
try {
// try {
// ordersDetails = await getOrderDetails(
// options.subgraph,
// {
// orderHash: options.orderHash,
// orderOwner: options.orderOwner,
// orderbook: options.orderbookAddress,
// },
// span,
// config.timeout,
// );
// if (!ordersDetails.length) {
// span.setStatus({ code: SpanStatusCode.OK, message: "found no orders" });
// span.end();
// return { txs: [], foundOpp: false, avgGasCost: undefined };
// }
// } catch (e: any) {
// const snapshot = errorSnapshot("", e);
// span.setStatus({ code: SpanStatusCode.ERROR, message: snapshot });
// span.recordException(e);
// span.setAttribute("didClear", false);
// span.setAttribute("foundOpp", false);
// span.end();
// return { txs: [], foundOpp: false, avgGasCost: undefined };
// }

try {
let txs;
let foundOpp = false;
const { reports = [], avgGasCost = undefined } = await clear(
config,
bundledOrders,
tracer,
ctx,
);
if (reports && reports.length) {
txs = reports.map((v) => v.txUrl).filter((v) => !!v);
if (txs.length) {
foundOpp = true;
span.setAttribute("txUrls", txs);
span.setAttribute("didClear", true);
span.setAttribute("foundOpp", true);
} else if (
reports.some((v) => v.status === ProcessPairReportStatus.FoundOpportunity)
) {
foundOpp = true;
span.setAttribute("foundOpp", true);
}
} else {
span.setAttribute("didClear", false);
}
if (avgGasCost) {
span.setAttribute("avgGasCost", avgGasCost.toString());
}
span.setStatus({ code: SpanStatusCode.OK });
span.end();
return { txs, foundOpp, avgGasCost };
} catch (e: any) {
if (e?.startsWith?.("Failed to batch quote orders")) {
span.setAttribute("severity", ErrorSeverity.LOW);
span.setStatus({ code: SpanStatusCode.ERROR, message: e });
} else {
const snapshot = errorSnapshot("Unexpected error occured", e);
span.setAttribute("severity", ErrorSeverity.HIGH);
span.setStatus({ code: SpanStatusCode.ERROR, message: snapshot });
let txs;
let foundOpp = false;
const { reports = [], avgGasCost = undefined } = await clear(
config,
bundledOrders,
tracer,
ctx,
);
if (reports && reports.length) {
txs = reports.map((v) => v.txUrl).filter((v) => !!v);
if (txs.length) {
foundOpp = true;
span.setAttribute("txUrls", txs);
span.setAttribute("didClear", true);
span.setAttribute("foundOpp", true);
} else if (
reports.some((v) => v.status === ProcessPairReportStatus.FoundOpportunity)
) {
foundOpp = true;
span.setAttribute("foundOpp", true);
}
span.recordException(e);
} else {
span.setAttribute("didClear", false);
span.setAttribute("foundOpp", false);
span.end();
return { txs: [], foundOpp: false, avgGasCost: undefined };
}
if (avgGasCost) {
span.setAttribute("avgGasCost", avgGasCost.toString());
}
span.setStatus({ code: SpanStatusCode.OK });
span.end();
return { txs, foundOpp, avgGasCost };
} catch (e: any) {
const snapshot = errorSnapshot("Unexpected error occured", e);
span.setAttribute("severity", ErrorSeverity.HIGH);
span.setStatus({ code: SpanStatusCode.ERROR, message: snapshot });
if (e?.startsWith?.("Failed to batch quote orders")) {
span.setAttribute("severity", ErrorSeverity.LOW);
span.setStatus({ code: SpanStatusCode.ERROR, message: e });
} else {
const snapshot = errorSnapshot("Unexpected error occured", e);
span.setAttribute("severity", ErrorSeverity.HIGH);
span.setStatus({ code: SpanStatusCode.ERROR, message: snapshot });
}
span.recordException(e);
span.setAttribute("didClear", false);
span.setAttribute("foundOpp", false);
Expand Down Expand Up @@ -403,7 +365,7 @@ export async function startup(argv: any, version?: string, tracer?: Tracer, ctx?
}
}
const tokens = getOrdersTokens(ordersDetails);
options.tokens = tokens;
options.tokens = [...tokens];

// get config
const config = await getConfig(
Expand Down Expand Up @@ -497,12 +459,7 @@ export const main = async (argv: any, version?: string) => {
orderbooksOwnersProfileMap.forEach((_, ob) => {
obs.push(ob.toLowerCase());
});
const orderbooksUnwatchers = watchAllOrderbooks(
obs,
config.watchClient,
watchedOrderbooksOrders,
);
orderbooksUnwatchers;
watchAllOrderbooks(obs, config.watchClient, watchedOrderbooksOrders);

const day = 24 * 60 * 60 * 1000;
let lastGasReset = Date.now() + day;
Expand Down Expand Up @@ -576,13 +533,9 @@ export const main = async (argv: any, version?: string) => {
update = true;
}
try {
const bundledOrders = prepareRoundProcessingOrders(orderbooksOwnersProfileMap);
await handleNewLogs(
const bundledOrders = prepareRoundProcessingOrders(
orderbooksOwnersProfileMap,
watchedOrderbooksOrders,
config.viemClient as any as ViemClient,
tokens,
(options as CliOptions).ownerProfile,
true,
);
await rotateProviders(config, update);
const roundResult = await arbRound(
Expand Down Expand Up @@ -704,6 +657,19 @@ export const main = async (argv: any, version?: string) => {
if (avgGasCost) {
roundSpan.setAttribute("avgGasCost", ethers.utils.formatUnits(avgGasCost));
}
try {
// check for new orders
await handleNewLogs(
orderbooksOwnersProfileMap,
watchedOrderbooksOrders,
config.viemClient as any as ViemClient,
tokens,
options.ownerProfile,
roundSpan,
);
} catch {
/**/
}
// eslint-disable-next-line no-console
console.log(`Starting next round in ${roundGap / 1000} seconds...`, "\n");
roundSpan.end();
Expand Down
3 changes: 0 additions & 3 deletions src/processOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ export const processOrders = async (
genericArb = new ethers.Contract(config.genericArbAddress, arbAbis);
}

// prepare orders
// const bundledOrders = bundleOrders(ordersDetails, false, true);

// check owned vaults and top them up if necessary
await tracer.startActiveSpan("handle-owned-vaults", {}, ctx, async (span) => {
try {
Expand Down
19 changes: 14 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,7 @@ export async function getTokenSymbol(address: string, viemClient: ViemClient): P

export function prepareRoundProcessingOrders(
orderbooksOwnersProfileMap: OrderbooksOwnersProfileMap,
shuffle = true,
): BundledOrders[][] {
const result: BundledOrders[][] = [];
for (const [orderbook, ownersProfileMap] of orderbooksOwnersProfileMap) {
Expand Down Expand Up @@ -1338,8 +1339,20 @@ export function prepareRoundProcessingOrders(
}
}
}
if (shuffle) {
// shuffle orders
for (const bundledOrders of orderbookBundledOrders) {
shuffleArray(bundledOrders.takeOrders);
}
// shuffle pairs
shuffleArray(orderbookBundledOrders);
}
result.push(orderbookBundledOrders);
}
if (shuffle) {
// shuffle orderbooks
shuffleArray(result);
}
return result;
}

Expand All @@ -1353,11 +1366,7 @@ function gatherPairs(
const bundleOrder = bundledOrders.find(
(v) =>
v.buyToken.toLowerCase() === pair.buyToken.toLowerCase() &&
v.buyTokenDecimals === pair.buyTokenDecimals &&
v.buyTokenSymbol === pair.buyTokenSymbol &&
v.sellToken.toLowerCase() === pair.sellToken.toLowerCase() &&
v.sellTokenDecimals === pair.sellTokenDecimals &&
v.sellTokenSymbol === pair.sellTokenSymbol,
v.sellToken.toLowerCase() === pair.sellToken.toLowerCase(),
);
if (bundleOrder) {
if (
Expand Down
Loading

0 comments on commit ed00cba

Please sign in to comment.