From 69cf8892ec804b16153f8ddf974d7e3ede1a7b9d Mon Sep 17 00:00:00 2001 From: rouzwelt Date: Mon, 2 Dec 2024 03:40:50 +0000 Subject: [PATCH] update --- src/config.ts | 8 +++--- src/gas.ts | 58 ++++++++++++++------------------------------ src/modes/index.ts | 13 +++------- src/processOrders.ts | 4 +-- src/types.ts | 4 +-- test/gas.test.ts | 8 +++--- 6 files changed, 34 insertions(+), 61 deletions(-) diff --git a/src/config.ts b/src/config.ts index c6cc304c..ebd5c47c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -58,7 +58,7 @@ export function getChainConfig(chainId: ChainId): ChainConfig { nativeWrappedToken, routeProcessors, stableTokens, - isL2: L2Chains.is(chain.id), + isSpecialL2: SpecialL2Chains.is(chain.id), }; } @@ -289,13 +289,13 @@ export function getWithdrawEnsureBytecode( * such as Arbitrum and Polygon zkEvm are excluded, these chains' * gas actions are performed the same as usual L1 chains. */ -export enum L2Chains { +export enum SpecialL2Chains { BASE = ChainId.BASE, OPTIMISM = ChainId.OPTIMISM, } -export namespace L2Chains { +export namespace SpecialL2Chains { export function is(chainId: number): boolean { - return Object.values(L2Chains).includes(chainId as any); + return Object.values(SpecialL2Chains).includes(chainId as any); } } diff --git a/src/gas.ts b/src/gas.ts index c65b40ad..81cfb58e 100644 --- a/src/gas.ts +++ b/src/gas.ts @@ -2,11 +2,7 @@ import { BotConfig, RawTx, ViemClient } from "./types"; import { publicActionsL2, walletActionsL2 } from "viem/op-stack"; /** - * Estimates gas cost of the given tx, also takes into account L1 gas cost if operating chain is L2. - * Not all L2 chains need to calculate L1 gas separately, some chains like Arbitrum and Polygon zkEvm, - * dont actually need anything extra other than usual gas estimation and they actually contain L1 gas in - * their usual gas estimation opertaion, but some other L2 chains such as Base and Optimism, do need to - * estimate L1 gas separately, so we will use a try/catch block + * Estimates gas cost of the given tx, also takes into account L1 gas cost if the chain is a special L2. */ export async function estimateGasCost( tx: RawTx, @@ -17,7 +13,15 @@ export async function estimateGasCost( ) { const gasPrice = tx.gasPrice ?? (await signer.getGasPrice()); const gas = await signer.estimateGas(tx); - if (config.isL2) { + const result = { + gas, + gasPrice, + l1Gas: 0n, + l1GasPrice: 0n, + l1Cost: 0n, + totalGasCost: gasPrice * gas, + }; + if (config.isSpecialL2) { try { const l1Signer_ = l1Signer ? l1Signer @@ -29,46 +33,20 @@ export async function estimateGasCost( to: tx.to, data: tx.data, }); - return { - gas, - gasPrice, - l1Gas, - l1GasPrice, - l1Cost: l1Gas * l1GasPrice, - totalGasCost: gasPrice * gas + l1Gas * l1GasPrice, - }; - } catch { - return { - gas, - gasPrice, - l1Gas: 0n, - l1GasPrice: 0n, - l1Cost: 0n, - totalGasCost: gasPrice * gas, - }; - } - } else { - return { - gas, - gasPrice, - l1Gas: 0n, - l1GasPrice: 0n, - l1Cost: 0n, - totalGasCost: gasPrice * gas, - }; + result.l1Gas = l1Gas; + result.l1GasPrice = l1GasPrice; + result.l1Cost = l1Gas * l1GasPrice; + result.totalGasCost += result.l1Cost; + } catch {} } + return result; } /** - * Retruns the L1 gas cost of a transaction if operating chain is L2 else returns 0. - * Not all L2 chains need report the L1 gas separately to the usual transaction receipt, chains - * like Arbitrum and Polygon zkEvm report the tx used gas normally like any other L1 chain, but - * some other L2 chains like Base and Optimism report the L1 gas separately to L2 using the properties - * below, so we need to explicitly check for the, if they are not present in the receipt, then simply - * return 0 + * Retruns the L1 gas cost of a transaction if operating chain is special L2 else returns 0. */ export function getL1Fee(receipt: any, config: BotConfig): bigint { - if (!config.isL2) return 0n; + if (!config.isSpecialL2) return 0n; if (typeof receipt.l1Fee === "bigint") { return receipt.l1Fee; diff --git a/src/modes/index.ts b/src/modes/index.ts index 43dd8e63..a454e1c1 100644 --- a/src/modes/index.ts +++ b/src/modes/index.ts @@ -53,19 +53,14 @@ export async function findOpp({ /**/ } - // if chain is L2, get L1 gas price before dryruns + // if chain is special L2, get L1 gas price just before dryruns let l1Signer; - let l1GasPrice: bigint | undefined; - if (config.isL2) { + let l1GasPrice = 0n; + if (config.isSpecialL2) { try { - // as already known, not all L2 chains support this method such as Arbitrum, - // only certain ones do, such as Base and Optimism, so use try/catch block - // and set to 0 on catch block so it becomes ineffective l1Signer = signer.extend(walletActionsL2()).extend(publicActionsL2()); l1GasPrice = await l1Signer.getL1BaseFee(); - } catch { - l1GasPrice = 0n; - } + } catch {} } const promises = [ diff --git a/src/processOrders.ts b/src/processOrders.ts index ce64db2c..a4ec1b27 100644 --- a/src/processOrders.ts +++ b/src/processOrders.ts @@ -728,7 +728,7 @@ export async function processPair(args: { const netProfit = income ? income.sub(actualGasCost) : undefined; spanAttributes["details.actualGasCost"] = toNumber(actualGasCost); - if (config.isL2 && l1Fee) { + if (config.isSpecialL2 && l1Fee) { spanAttributes["details.gasCostL1"] = toNumber(l1Fee); } if (income) { @@ -814,7 +814,7 @@ export async function processPair(args: { .add(l1Fee); signer.BALANCE = signer.BALANCE.sub(actualGasCost); spanAttributes["details.actualGasCost"] = toNumber(actualGasCost); - if (config.isL2 && l1Fee) { + if (config.isSpecialL2 && l1Fee) { spanAttributes["details.gasCostL1"] = toNumber(l1Fee); } } catch { diff --git a/src/types.ts b/src/types.ts index b0f5ab07..d2067064 100644 --- a/src/types.ts +++ b/src/types.ts @@ -142,7 +142,7 @@ export type ChainConfig = { nativeWrappedToken: Token; routeProcessors: { [key: string]: `0x${string}` }; stableTokens?: Token[]; - isL2: boolean; + isSpecialL2: boolean; }; export type BotConfig = { @@ -150,7 +150,7 @@ export type BotConfig = { nativeWrappedToken: Token; routeProcessors: { [key: string]: `0x${string}` }; stableTokens?: Token[]; - isL2: boolean; + isSpecialL2: boolean; key?: string; mnemonic?: string; rpc: string[]; diff --git a/test/gas.test.ts b/test/gas.test.ts index 5b7c9164..c5dbf777 100644 --- a/test/gas.test.ts +++ b/test/gas.test.ts @@ -21,8 +21,8 @@ describe("Test gas", async function () { to: ("0x" + "1".repeat(40)) as `0x${string}`, }; - // estimate gas as L2 chain - const botconfig = { isL2: true } as any as BotConfig; + // estimate gas as special L2 chain + const botconfig = { isSpecialL2: true } as any as BotConfig; const result1 = await estimateGasCost(tx, signer, botconfig, undefined, l1Signer); const expected1 = { gas: 55n, @@ -34,8 +34,8 @@ describe("Test gas", async function () { }; assert.deepEqual(result1, expected1); - // estimate as none L2 chain - botconfig.isL2 = false; + // estimate as usual chain + botconfig.isSpecialL2 = false; const result2 = await estimateGasCost(tx, signer, botconfig); const expected2 = { gas: 55n,