diff --git a/src/error.ts b/src/error.ts index 83bebcf1..e8576204 100644 --- a/src/error.ts +++ b/src/error.ts @@ -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; } } diff --git a/src/processOrders.ts b/src/processOrders.ts index f17eba8f..68a2a52b 100644 --- a/src/processOrders.ts +++ b/src/processOrders.ts @@ -146,7 +146,7 @@ export const processOrders = async ( span.end(); }); - let avgGasCost: BigNumber | undefined; + const txGasCosts: BigNumber[] = []; const reports: Report[] = []; const results: { settle: () => Promise; @@ -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 @@ -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); @@ -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); @@ -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, + }; }; /** diff --git a/src/tx.ts b/src/tx.ts index 42a436a9..deb0cdc4 100644 --- a/src/tx.ts +++ b/src/tx.ts @@ -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"; @@ -143,21 +143,6 @@ 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, @@ -165,9 +150,6 @@ export async function handleTransaction( buyToken: orderPairObject.buyToken, sellToken: orderPairObject.sellToken, }; - if (actualGasCost) { - result.report.actualGasCost = ethers.utils.formatUnits(actualGasCost); - } result.error = e; spanAttributes["details.rawTx"] = JSON.stringify( { diff --git a/src/utils.ts b/src/utils.ts index 9279537e..fb07a139 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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); @@ -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)); }