Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Dec 2, 2024
1 parent 53000b1 commit 69cf889
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 61 deletions.
8 changes: 4 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function getChainConfig(chainId: ChainId): ChainConfig {
nativeWrappedToken,
routeProcessors,
stableTokens,
isL2: L2Chains.is(chain.id),
isSpecialL2: SpecialL2Chains.is(chain.id),
};
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
58 changes: 18 additions & 40 deletions src/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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;
Expand Down
13 changes: 4 additions & 9 deletions src/modes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions src/processOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ export type ChainConfig = {
nativeWrappedToken: Token;
routeProcessors: { [key: string]: `0x${string}` };
stableTokens?: Token[];
isL2: boolean;
isSpecialL2: boolean;
};

export type BotConfig = {
chain: Chain;
nativeWrappedToken: Token;
routeProcessors: { [key: string]: `0x${string}` };
stableTokens?: Token[];
isL2: boolean;
isSpecialL2: boolean;
key?: string;
mnemonic?: string;
rpc: string[];
Expand Down
8 changes: 4 additions & 4 deletions test/gas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 69cf889

Please sign in to comment.