Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Jan 27, 2025
1 parent 7cfd60c commit 224dc45
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 186 deletions.
19 changes: 6 additions & 13 deletions src/modes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { findOpp as findInterObOpp } from "./interOrderbook";
import { findOpp as findIntraObOpp } from "./intraOrderbook";
import { findOppWithRetries as findRpOpp } from "./routeProcessor";
import { BotConfig, BundledOrders, ViemClient, DryrunResult, SpanAttrs } from "../types";
import { extendSpanAttributes } from "../utils";

/**
* The main entrypoint for the main logic to find opps.
Expand Down Expand Up @@ -117,24 +116,18 @@ export async function findOpp({
noneNodeError: undefined,
};
if ((allResults[0] as any)?.reason?.spanAttributes) {
extendSpanAttributes(
spanAttributes,
JSON.stringify((allResults[0] as any).reason.spanAttributes),
"routeProcessor",
spanAttributes["routeProcessor"] = JSON.stringify(
(allResults[0] as any).reason.spanAttributes,
);
}
if ((allResults[1] as any)?.reason?.spanAttributes) {
extendSpanAttributes(
spanAttributes,
JSON.stringify((allResults[1] as any).reason.spanAttributes),
"intraOrderbook",
spanAttributes["intraOrderbook"] = JSON.stringify(
(allResults[1] as any).reason.spanAttributes["intraOrderbook"],
);
}
if ((allResults[2] as any)?.reason?.spanAttributes) {
extendSpanAttributes(
spanAttributes,
JSON.stringify((allResults[2] as any).reason.spanAttributes),
"interOrderbook",
spanAttributes["interOrderbook"] = JSON.stringify(
(allResults[2] as any).reason.spanAttributes,
);
}
if ((allResults[0] as any)?.reason?.value?.noneNodeError) {
Expand Down
61 changes: 19 additions & 42 deletions src/modes/interOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ import { BigNumber, Contract, ethers } from "ethers";
import { containsNodeError, errorSnapshot } from "../error";
import { getBountyEnsureRainlang, parseRainlang } from "../task";
import { BaseError, ExecutionRevertedError, PublicClient } from "viem";
import { ONE18, scale18To, estimateProfit, withBigintSerializer } from "../utils";
import { BotConfig, BundledOrders, ViemClient, DryrunResult, SpanAttrs } from "../types";
import {
ONE18,
scale18To,
estimateProfit,
withBigintSerializer,
extendSpanAttributes,
} from "../utils";

/**
* Executes a extimateGas call for an inter-orderbook arb() tx, to determine if the tx is successfull ot not
Expand Down Expand Up @@ -127,21 +121,13 @@ export async function dryrun({
gasLimit = ethers.BigNumber.from(estimation.gas).mul(config.gasLimitMultiplier).div(100);

// include dryrun headroom gas estimation in otel logs
extendSpanAttributes(
spanAttributes,
JSON.stringify({
gasLimit: estimation.gas.toString(),
totalCost: estimation.totalGasCost.toString(),
gasPrice: estimation.gasPrice.toString(),
...(config.isSpecialL2
? {
l1Cost: estimation.l1Cost.toString(),
l1GasPrice: estimation.l1GasPrice.toString(),
}
: {}),
}),
"gasEst.headroom",
);
spanAttributes["gasEst.headroom.gasLimit"] = estimation.gas.toString();
spanAttributes["gasEst.headroom.totalCost"] = estimation.totalGasCost.toString();
spanAttributes["gasEst.headroom.gasPrice"] = estimation.gasPrice.toString();
if (config.isSpecialL2) {
spanAttributes["gasEst.headroom.l1Cost"] = estimation.l1Cost.toString();
spanAttributes["gasEst.headroom.l1GasPrice"] = estimation.l1GasPrice.toString();
}
} catch (e) {
const isNodeError = containsNodeError(e as BaseError);
const errMsg = errorSnapshot("", e);
Expand Down Expand Up @@ -211,21 +197,13 @@ export async function dryrun({
gasCost = gasLimit.mul(gasPrice).add(estimation.l1Cost);

// include dryrun final gas estimation in otel logs
extendSpanAttributes(
spanAttributes,
JSON.stringify({
gasLimit: estimation.gas.toString(),
totalCost: estimation.totalGasCost.toString(),
gasPrice: estimation.gasPrice.toString(),
...(config.isSpecialL2
? {
l1Cost: estimation.l1Cost.toString(),
l1GasPrice: estimation.l1GasPrice.toString(),
}
: {}),
}),
"gasEst.final",
);
spanAttributes["gasEst.final.gasLimit"] = estimation.gas.toString();
spanAttributes["gasEst.final.totalCost"] = estimation.totalGasCost.toString();
spanAttributes["gasEst.final.gasPrice"] = estimation.gasPrice.toString();
if (config.isSpecialL2) {
spanAttributes["gasEst.final.l1Cost"] = estimation.l1Cost.toString();
spanAttributes["gasEst.final.l1GasPrice"] = estimation.l1GasPrice.toString();
}
task.evaluable.bytecode = await parseRainlang(
await getBountyEnsureRainlang(
ethers.utils.parseUnits(inputToEthPrice),
Expand Down Expand Up @@ -422,13 +400,12 @@ export async function findOpp({
// } catch {
// /**/
// }
const allOrderbooksAttributes: any = {};
for (let i = 0; i < e.errors.length; i++) {
extendSpanAttributes(
spanAttributes,
JSON.stringify(e.errors[i].spanAttributes),
"againstOrderbooks." + opposingOrderbookOrders[i].orderbook,
);
allOrderbooksAttributes[opposingOrderbookOrders[i].orderbook] =
e.errors[i].spanAttributes;
}
spanAttributes["againstOrderbooks"] = JSON.stringify(allOrderbooksAttributes);
const noneNodeErrors = allNoneNodeErrors.filter((v) => !!v);
if (allNoneNodeErrors.length && noneNodeErrors.length / allNoneNodeErrors.length > 0.5) {
result.value = {
Expand Down
54 changes: 18 additions & 36 deletions src/modes/intraOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { estimateGasCost } from "../gas";
import { BigNumber, ethers } from "ethers";
import { containsNodeError, errorSnapshot } from "../error";
import { getWithdrawEnsureRainlang, parseRainlang } from "../task";
import { estimateProfit, scale18, withBigintSerializer } from "../utils";
import { BaseError, erc20Abi, ExecutionRevertedError, PublicClient } from "viem";
import { estimateProfit, scale18, withBigintSerializer, extendSpanAttributes } from "../utils";
import {
SpanAttrs,
BotConfig,
Expand Down Expand Up @@ -119,21 +119,13 @@ export async function dryrun({
gasLimit = ethers.BigNumber.from(estimation.gas).mul(config.gasLimitMultiplier).div(100);

// include dryrun headroom gas estimation in otel logs
extendSpanAttributes(
spanAttributes,
JSON.stringify({
gasLimit: estimation.gas.toString(),
totalCost: estimation.totalGasCost.toString(),
gasPrice: estimation.gasPrice.toString(),
...(config.isSpecialL2
? {
l1Cost: estimation.l1Cost.toString(),
l1GasPrice: estimation.l1GasPrice.toString(),
}
: {}),
}),
"gasEst.headroom",
);
spanAttributes["gasEst.headroom.gasLimit"] = estimation.gas.toString();
spanAttributes["gasEst.headroom.totalCost"] = estimation.totalGasCost.toString();
spanAttributes["gasEst.headroom.gasPrice"] = estimation.gasPrice.toString();
if (config.isSpecialL2) {
spanAttributes["gasEst.headroom.l1Cost"] = estimation.l1Cost.toString();
spanAttributes["gasEst.headroom.l1GasPrice"] = estimation.l1GasPrice.toString();
}
} catch (e) {
// reason, code, method, transaction, error, stack, message
const isNodeError = containsNodeError(e as BaseError);
Expand Down Expand Up @@ -213,21 +205,13 @@ export async function dryrun({
gasCost = gasLimit.mul(gasPrice).add(estimation.l1Cost);

// include dryrun final gas estimation in otel logs
extendSpanAttributes(
spanAttributes,
JSON.stringify({
gasLimit: estimation.gas.toString(),
totalCost: estimation.totalGasCost.toString(),
gasPrice: estimation.gasPrice.toString(),
...(config.isSpecialL2
? {
l1Cost: estimation.l1Cost.toString(),
l1GasPrice: estimation.l1GasPrice.toString(),
}
: {}),
}),
"gasEst.final",
);
spanAttributes["gasEst.final.gasLimit"] = estimation.gas.toString();
spanAttributes["gasEst.final.totalCost"] = estimation.totalGasCost.toString();
spanAttributes["gasEst.final.gasPrice"] = estimation.gasPrice.toString();
if (config.isSpecialL2) {
spanAttributes["gasEst.final.l1Cost"] = estimation.l1Cost.toString();
spanAttributes["gasEst.final.l1GasPrice"] = estimation.l1GasPrice.toString();
}
task.evaluable.bytecode = await parseRainlang(
await getWithdrawEnsureRainlang(
signer.account.address,
Expand Down Expand Up @@ -357,6 +341,7 @@ export async function findOpp({
);
if (!opposingOrders || !opposingOrders.length) throw undefined;

const allErrorAttributes: string[] = [];
const allNoneNodeErrors: (string | undefined)[] = [];
const inputBalance = scale18(
await viemClient.readContract({
Expand Down Expand Up @@ -393,13 +378,10 @@ export async function findOpp({
});
} catch (e: any) {
allNoneNodeErrors.push(e?.value?.noneNodeError);
extendSpanAttributes(
spanAttributes,
JSON.stringify(e.spanAttributes),
"intraOrderbook." + i,
);
allErrorAttributes.push(JSON.stringify(e.spanAttributes));
}
}
spanAttributes["intraOrderbook"] = allErrorAttributes;
const noneNodeErrors = allNoneNodeErrors.filter((v) => !!v);
if (allNoneNodeErrors.length && noneNodeErrors.length / allNoneNodeErrors.length > 0.5) {
result.value = {
Expand Down
49 changes: 16 additions & 33 deletions src/modes/routeProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
estimateProfit,
visualizeRoute,
withBigintSerializer,
extendSpanAttributes,
} from "../utils";

/**
Expand Down Expand Up @@ -191,21 +190,13 @@ export async function dryrun({
.div(100);

// include dryrun headroom gas estimation in otel logs
extendSpanAttributes(
spanAttributes,
JSON.stringify({
gasLimit: estimation.gas.toString(),
totalCost: estimation.totalGasCost.toString(),
gasPrice: estimation.gasPrice.toString(),
...(config.isSpecialL2
? {
l1Cost: estimation.l1Cost.toString(),
l1GasPrice: estimation.l1GasPrice.toString(),
}
: {}),
}),
"gasEst.headroom",
);
spanAttributes["gasEst.headroom.gasLimit"] = estimation.gas.toString();
spanAttributes["gasEst.headroom.totalCost"] = estimation.totalGasCost.toString();
spanAttributes["gasEst.headroom.gasPrice"] = estimation.gasPrice.toString();
if (config.isSpecialL2) {
spanAttributes["gasEst.headroom.l1Cost"] = estimation.l1Cost.toString();
spanAttributes["gasEst.headroom.l1GasPrice"] = estimation.l1GasPrice.toString();
}
} catch (e) {
// reason, code, method, transaction, error, stack, message
const isNodeError = containsNodeError(e as BaseError);
Expand Down Expand Up @@ -277,21 +268,13 @@ export async function dryrun({
gasCost = gasLimit.mul(gasPrice).add(estimation.l1Cost);

// include dryrun final gas estimation in otel logs
extendSpanAttributes(
spanAttributes,
JSON.stringify({
gasLimit: estimation.gas.toString(),
totalCost: estimation.totalGasCost.toString(),
gasPrice: estimation.gasPrice.toString(),
...(config.isSpecialL2
? {
l1Cost: estimation.l1Cost.toString(),
l1GasPrice: estimation.l1GasPrice.toString(),
}
: {}),
}),
"gasEst.final",
);
spanAttributes["gasEst.final.gasLimit"] = estimation.gas.toString();
spanAttributes["gasEst.final.totalCost"] = estimation.totalGasCost.toString();
spanAttributes["gasEst.final.gasPrice"] = estimation.gasPrice.toString();
if (config.isSpecialL2) {
spanAttributes["gasEst.final.l1Cost"] = estimation.l1Cost.toString();
spanAttributes["gasEst.final.l1GasPrice"] = estimation.l1GasPrice.toString();
}
task.evaluable.bytecode = await parseRainlang(
await getBountyEnsureRainlang(
ethers.utils.parseUnits(ethPrice),
Expand Down Expand Up @@ -430,7 +413,7 @@ export async function findOpp({
// the fail reason can only be no route in case all hops fail reasons are no route
if (e.reason !== RouteProcessorDryrunHaltReason.NoRoute) noRoute = false;
allNoneNodeErrors.push(e?.value?.noneNodeError);
extendSpanAttributes(spanAttributes, JSON.stringify(e.spanAttributes), "full");
spanAttributes["full"] = JSON.stringify(e.spanAttributes);
}
if (!hasPriceMatch.value) {
const maxTradeSize = findMaxInput({
Expand Down Expand Up @@ -463,7 +446,7 @@ export async function findOpp({
// the fail reason can only be no route in case all hops fail reasons are no route
if (e.reason !== RouteProcessorDryrunHaltReason.NoRoute) noRoute = false;
allNoneNodeErrors.push(e?.value?.noneNodeError);
extendSpanAttributes(spanAttributes, JSON.stringify(e.spanAttributes), "partial");
spanAttributes["partial"] = JSON.stringify(e.spanAttributes);
}
}
}
Expand Down
35 changes: 33 additions & 2 deletions src/processOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,40 @@ export async function processPair(args: {
}
}
} catch (e: any) {
// record all span attributes
// record all span attributes in their scopes
for (const attrKey in e.spanAttributes) {
spanAttributes["details." + attrKey] = e.spanAttributes[attrKey];
if (attrKey === "routeProcessor") {
const rpAttrs = JSON.parse(e.spanAttributes[attrKey]);
for (const key in rpAttrs) {
const innerAttrs = JSON.parse(rpAttrs[key]);
for (const innerKey in innerAttrs) {
spanAttributes["details.routeProcessor." + key + "." + innerKey] =
innerAttrs[innerKey];
}
}
} else if (attrKey === "intraOrderbook") {
const intraAttrs = JSON.parse(e.spanAttributes[attrKey]);
for (let i = 0; i < intraAttrs.length; i++) {
const innerAttrs = JSON.parse(intraAttrs[i]);
for (const innerKey in innerAttrs) {
spanAttributes["details.intraOrderbook." + i + "." + innerKey] =
innerAttrs[innerKey];
}
}
} else if (attrKey === "interOrderbook") {
const interAttrs = JSON.parse(
JSON.parse(e.spanAttributes[attrKey])["againstOrderbooks"],
);
for (const key in interAttrs) {
for (const innerKey in interAttrs[key]) {
spanAttributes[
"details.interOrderbook.againstOrderbooks." + key + "." + innerKey
] = interAttrs[key][innerKey];
}
}
} else {
spanAttributes["details." + attrKey] = e.spanAttributes[attrKey];
}
}
if (e.noneNodeError) {
spanAttributes["details.noneNodeError"] = true;
Expand Down
10 changes: 4 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1405,17 +1405,15 @@ export function scale18To(value: BigNumberish, targetDecimals: BigNumberish): Bi
*/
export function extendSpanAttributes(
spanAttributes: Record<string, any>,
newAttributes: string,
newAttributes: Record<string, any>,
header: string,
excludeHeaderForKeys: string[] = [],
) {
const attrs = JSON.parse(newAttributes);
for (const attrKey in attrs) {
for (const attrKey in newAttributes) {
if (!excludeHeaderForKeys.includes(attrKey)) {
spanAttributes[header + "." + attrKey] = attrs[attrKey];
spanAttributes[header + "." + attrKey] = newAttributes[attrKey];
} else {
spanAttributes[attrKey] = attrs[attrKey];
spanAttributes[attrKey] = newAttributes[attrKey];
}
delete attrs[attrKey];
}
}
Loading

0 comments on commit 224dc45

Please sign in to comment.