Skip to content

Commit

Permalink
Merge branch '2024-12-02-l1-gas' into 2024-12-03-quote-gas-limit
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt authored Dec 19, 2024
2 parents 655d57b + d27b3ed commit 3d5cd29
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 46 deletions.
20 changes: 11 additions & 9 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,18 +337,20 @@ export async function hasFrontrun(
}
})();
if (orderConfig) {
const logs = await viemClient.getLogs({
event: TakeOrderV2EventAbi[0],
address: orderbook,
blockHash: receipt.blockHash,
});
const otherLogs = logs.filter(
const txHash = receipt.transactionHash.toLowerCase();
const logs = (
await viemClient.getLogs({
event: TakeOrderV2EventAbi[0],
address: orderbook,
blockHash: receipt.blockHash,
})
).filter(
(v) =>
receipt.transactionIndex > v.transactionIndex &&
v.transactionHash.toLowerCase() !== receipt.transactionHash.toLowerCase(),
v.transactionHash.toLowerCase() !== txHash,
);
if (otherLogs.length) {
for (const log of otherLogs) {
if (logs.length) {
for (const log of logs) {
if (isDeepStrictEqual(log.args.config, orderConfig)) return log.transactionHash;
}
}
Expand Down
25 changes: 9 additions & 16 deletions src/processOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const processOrders = async (
span.end();
});

let avgGasCost: BigNumber | undefined;
const txGasCosts: BigNumber[] = [];
const reports: Report[] = [];
const results: {
settle: () => Promise<ProcessPairResult>;
Expand Down Expand Up @@ -228,6 +228,7 @@ export const processOrders = async (
for (const { settle, pair, orderPairObject } of results) {
// instantiate a span for this pair
const span = tracer.startSpan(`order_${pair}`, undefined, ctx);
span.setAttribute("details.owner", orderPairObject.takeOrders[0].takeOrder.order.owner);
try {
// settle the process results
// this will return the report of the operation and in case
Expand All @@ -237,11 +238,7 @@ export const processOrders = async (

// keep track of avg gas cost
if (result.gasCost) {
if (!avgGasCost) {
avgGasCost = result.gasCost;
} else {
avgGasCost = avgGasCost.add(result.gasCost).div(2);
}
txGasCosts.push(result.gasCost);
}

reports.push(result.report);
Expand All @@ -266,15 +263,6 @@ export const processOrders = async (
span.setStatus({ code: SpanStatusCode.ERROR, message: "unexpected error" });
}
} catch (e: any) {
// keep track of avg gas cost
if (e.gasCost) {
if (!avgGasCost) {
avgGasCost = e.gasCost;
} else {
avgGasCost = avgGasCost.add(e.gasCost).div(2);
}
}

// set the span attributes with the values gathered at processPair()
span.setAttributes(e.spanAttributes);

Expand Down Expand Up @@ -408,7 +396,12 @@ export const processOrders = async (
}
span.end();
}
return { reports, avgGasCost };
return {
reports,
avgGasCost: txGasCosts.length
? txGasCosts.reduce((a, b) => a.add(b), ethers.constants.Zero).div(txGasCosts.length)
: undefined,
};
};

/**
Expand Down
20 changes: 1 addition & 19 deletions src/tx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getL1Fee } from "./gas";
import { Token } from "sushi/currency";
import { Contract, ethers } from "ethers";
import { addWatchedToken } from "./account";
import { BigNumber, Contract, ethers } from "ethers";
import { containsNodeError, handleRevert } from "./error";
import { ProcessPairHaltReason, ProcessPairReportStatus } from "./processOrders";
import { BotConfig, BundledOrders, ProcessPairResult, RawTx, ViemClient } from "./types";
Expand Down Expand Up @@ -143,31 +143,13 @@ export async function handleTransaction(
);
}
} catch {}
// keep track of gas consumption of the account
let actualGasCost;
try {
const l1Fee = getL1Fee(e.receipt, config);
actualGasCost = BigNumber.from(e.receipt.effectiveGasPrice)
.mul(e.receipt.gasUsed)
.add(l1Fee);
signer.BALANCE = signer.BALANCE.sub(actualGasCost);
spanAttributes["details.actualGasCost"] = toNumber(actualGasCost);
if (config.isSpecialL2 && l1Fee) {
spanAttributes["details.gasCostL1"] = toNumber(l1Fee);
}
} catch {
/**/
}
result.report = {
status: ProcessPairReportStatus.FoundOpportunity,
txUrl,
tokenPair: pair,
buyToken: orderPairObject.buyToken,
sellToken: orderPairObject.sellToken,
};
if (actualGasCost) {
result.report.actualGasCost = ethers.utils.formatUnits(actualGasCost);
}
result.error = e;
spanAttributes["details.rawTx"] = JSON.stringify(
{
Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,9 @@ export const visualizeRoute = (fromToken: Token, toToken: Token, legs: RouteLeg[
(e) =>
e.tokenFrom.address.toLowerCase() ===
portions.at(-1)?.tokenTo.address.toLowerCase() &&
!portions.includes(e),
portions.every(
(k) => k.poolAddress.toLowerCase() !== e.poolAddress.toLowerCase(),
),
);
if (legPortion) {
portions.push(legPortion);
Expand Down Expand Up @@ -1271,7 +1273,6 @@ export async function checkOwnedOrders(
* Converts to a float number
*/
export function toNumber(value: BigNumberish): number {
// valueString.substring(0, valueString.includes(".") ? 18 : 17)
return Number.parseFloat(ethers.utils.formatUnits(value));
}

Expand Down

0 comments on commit 3d5cd29

Please sign in to comment.