Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve calculation of optimism l1 fee #309

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@axelar-network/axelarjs-sdk",
"version": "0.15.0-alpha.3",
"version": "0.15.0-alpha.4",
"description": "The JavaScript SDK for Axelar Network",
"repository": {
"type": "git",
Expand Down
4 changes: 4 additions & 0 deletions src/constants/index.ts

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions src/libs/AxelarQueryAPI.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AssetConfig } from "../assets/types";
import { parseUnits } from "ethers/lib/utils";
import { loadAssets } from "../assets";
import { EnvironmentConfigs, getConfigs } from "../constants";
import { DEFAULT_L1_EXECUTE_DATA, EnvironmentConfigs, getConfigs } from "../constants";
import { RestService } from "../services";
import {
AxelarQueryAPIConfig,
Expand All @@ -23,7 +23,7 @@ import { throwIfInvalidChainIds } from "../utils";
import { loadChains } from "../chains";
import s3 from "./TransactionRecoveryApi/constants/s3";
import { BigNumber, BigNumberish, ethers } from "ethers";
import { ChainInfo } from "src/chains/types";
import { ChainInfo } from "../chains/types";
import { BigNumberUtils } from "./BigNumberUtils";
import { rpcMap as testnetRpcMap } from "./TransactionRecoveryApi/constants/chain/testnet";
import { rpcMap as mainnetRpcMap } from "./TransactionRecoveryApi/constants/chain/mainnet";
Expand Down Expand Up @@ -298,7 +298,7 @@ export class AxelarQueryAPI {

// Calculate the L1 execution fee. This value is in ETH.
l1ExecutionFee = await this.estimateL1GasFee(destChainId, {
executeData: executeData || "0x",
executeData: executeData || DEFAULT_L1_EXECUTE_DATA,
l1GasPrice: destToken.l1_gas_price_in_units,
l1GasOracleAddress: destToken.l1_gas_oracle_address,
destChain: destChainId,
Expand All @@ -317,9 +317,7 @@ export class AxelarQueryAPI {
l1ExecutionFee = BigNumber.from(actualL1ExecutionFee.toString());

// Calculate the L1 execution fee with the gas multiplier
l1ExecutionFeeWithMultiplier = BigNumber.from(
Math.floor(actualGasMultiplier * actualGasMultiplier)
);
l1ExecutionFeeWithMultiplier = l1ExecutionFee.mul(actualGasMultiplier * 10000).div(10000);
}

return [l1ExecutionFee, l1ExecutionFeeWithMultiplier];
Expand Down Expand Up @@ -418,7 +416,7 @@ export class AxelarQueryAPI {
? {
baseFee,
expressFee,
executionFee: executionFeeWithMultiplier.toString(),
executionFee: executionFee.toString(),
executionFeeWithMultiplier: executionFeeWithMultiplier.toString(),
l1ExecutionFeeWithMultiplier: l1ExecutionFeeWithMultiplier.toString(),
l1ExecutionFee: l1ExecutionFee.toString(),
Expand Down
34 changes: 9 additions & 25 deletions src/libs/fee/getL1Fee.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import { Environment, EstimateL1FeeParams } from "../types";
import { getL1FeeForL2 } from "./getL1Fee";
import { AxelarQueryAPI } from "../AxelarQueryAPI";
import { DEFAULT_L1_EXECUTE_DATA } from "../../constants";

const MOCK_EXECUTE_DATA: `0x${string}` =
"0x1a98b2e0e68ba0eb84262d4bcf91955ec2680b37bcedd59a1f48e18d183dac9961bf9d1400000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000d40000000000000000000000000000000000000000000000000000000000deac2c6000000000000000000000000000000000000000000000000000000000000000762696e616e636500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002a307863653136463639333735353230616230313337376365374238386635424138433438463844363636000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000bc000000000000000000000000000000000000000000000000000000000000000400000000000000000000000004607cad6135d7a119185ebe062d3b369b1b536ef000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000003600000000000000000000000000000000000000000000000000000000000000580000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000009200000000000000000000000000000000000000000000000000000000000000a8000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000eb466342c4d449bc9f53a865d5cb90586f4052150000000000000000000000000000000000000000000000000000000000000001000000000000000000000000eb466342c4d449bc9f53a865d5cb90586f405215000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000044095ea7b300000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc450000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000eb466342c4d449bc9f53a865d5cb90586f4052150000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc45000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000000e404e45aaf000000000000000000000000eb466342c4d449bc9f53a865d5cb90586f405215000000000000000000000000ff970a61a04b1ca14834a43f5de4533ebddb5cc800000000000000000000000000000000000000000000000000000000000000640000000000000000000000004fd39c9e151e50580779bd04b1f7ecc310079fd3000000000000000000000000000000000000000000000000000000000deac2c6000000000000000000000000000000000000000000000000000000000dc647500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000eb466342c4d449bc9f53a865d5cb90586f40521500000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ff970a61a04b1ca14834a43f5de4533ebddb5cc8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000044095ea7b300000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc450000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000ff970a61a04b1ca14834a43f5de4533ebddb5cc80000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc45000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000000e404e45aaf000000000000000000000000ff970a61a04b1ca14834a43f5de4533ebddb5cc800000000000000000000000082af49447d8a07e3bd95bd0d56f35241523fbab100000000000000000000000000000000000000000000000000000000000000640000000000000000000000004fd39c9e151e50580779bd04b1f7ecc310079fd3000000000000000000000000000000000000000000000000000000000de83dbf000000000000000000000000000000000000000000000000015d8c7908dbe7130000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000ff970a61a04b1ca14834a43f5de4533ebddb5cc80000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000100000000000000000000000082af49447d8a07e3bd95bd0d56f35241523fbab1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000242e1a7d4d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000082af49447d8a07e3bd95bd0d56f35241523fbab1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000004607cad6135d7a119185ebe062d3b369b1b536ef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000761786c5553444300000000000000000000000000000000000000000000000000";
const env = Environment.MAINNET;

async function getL1Fee(srcChain: string, destChain: string) {
Expand All @@ -14,9 +13,9 @@
const { destToken, l2_type } = await queryAPI.getNativeGasBaseFee(srcChain, destChain);

const params: EstimateL1FeeParams = {
executeData: MOCK_EXECUTE_DATA,
executeData: DEFAULT_L1_EXECUTE_DATA,
destChain,
l1GasPrice: destToken.l1_gas_price_in_units!,

Check warning on line 18 in src/libs/fee/getL1Fee.spec.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/libs/fee/getL1Fee.spec.ts#L18

Forbidden non-null assertion (@typescript-eslint/no-non-null-assertion)
l2Type: l2_type,
};

Expand All @@ -27,30 +26,15 @@
}

describe("getL1Fee", () => {
it("query optimism l1 fee should work", async () => {
const srcChain = "avalanche";
const destChain = "optimism";
it("query l1 fee for l2 chains should work", async () => {
const srcChain = "ethereum";
const destChains = ["optimism", "blast", "mantle", "fraxtal", "base"];

const fee = await getL1Fee(srcChain, destChain);
const queries = destChains.map((destChain) => getL1Fee(srcChain, destChain));

expect(fee).toBeDefined();
});

it("query blast l1 fee should work", async () => {
const srcChain = "avalanche";
const destChain = "blast";

const fee = await getL1Fee(srcChain, destChain);

expect(fee).toBeDefined();
});

it("query mantle l1 fee should work", async () => {
const srcChain = "avalanche";
const destChain = "mantle";

const fee = await getL1Fee(srcChain, destChain);
const fees = await Promise.all(queries);

expect(fee).toBeDefined();
expect(fees.length).toBe(destChains.length);
expect(fees.every((fee) => fee.gt(0))).toBe(true);
});
});
78 changes: 22 additions & 56 deletions src/libs/fee/getL1Fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import { EstimateL1FeeParams } from "../types";

const ABI = {
Optimism: [
"function getL1GasUsed(bytes executeData) view returns (uint256)",
// "function scalar() view returns (uint256)",
// "function overhead() view returns (uint256)",
// "function getL1Fee(bytes executeData) view returns (uint256)",
// "function getL1GasUsed(bytes executeData) view returns (uint256)",
"function getL1Fee(bytes executeData) view returns (uint256)",
],
Mantle: [
"function overhead() view returns (uint256)",
Expand Down Expand Up @@ -59,78 +57,46 @@ async function getOptimismL1Fee(
provider: ethers.providers.Provider,
estimateL1FeeParams: EstimateL1FeeParams
) {
const { l1GasPrice, executeData, l1GasOracleAddress } = estimateL1FeeParams;
const { executeData, l1GasOracleAddress } = estimateL1FeeParams;

const [callContext] = buildContractCallContext(
"optimism",
l1GasOracleAddress as string,
executeData
);
const callContext = buildContractCallContext("optimism", l1GasOracleAddress as string);

const contract = new ethers.Contract(callContext.contractAddress, callContext.abi, provider);
const l1GasUsed = await contract.getL1GasUsed(executeData);

return l1GasUsed.mul(BigNumber.from(l1GasPrice.value));
return contract.getL1Fee(executeData);
}

async function getMantleL1Fee(
provider: ethers.providers.Provider,
estimateL1FeeParams: EstimateL1FeeParams
) {
const { l1GasPrice, executeData, l1GasOracleAddress } = estimateL1FeeParams;
const { l1GasPrice, l1GasOracleAddress } = estimateL1FeeParams;

const [callContext] = buildContractCallContext(
"mantle",
l1GasOracleAddress as string,
executeData
);
const callContext = buildContractCallContext("mantle", l1GasOracleAddress as string);

const contract = new ethers.Contract(callContext.contractAddress, callContext.abi, provider);
const _scalar = contract.scalar();
const _overhead = contract.overhead();
const [dynamicOverhead, fixedOverhead] = await Promise.all([_scalar, _overhead]);

return calculateL1Fee(
BigNumber.from(0),
fixedOverhead || 2100,
dynamicOverhead || 684000,
BigNumber.from(l1GasPrice.value)
);
const fixedOverhead = await contract.overhead();

return BigNumber.from(l1GasPrice.value).mul(fixedOverhead || 2100);
}

type L1FeeCalculationType = "optimism" | "mantle";

function calculateL1Fee(
gasUsed: BigNumber,
fixedOverhead: BigNumber,
dynamicOverhead: BigNumber,
gasPrice: BigNumber
) {
const totalGas = gasUsed.add(fixedOverhead).mul(dynamicOverhead).div(1_000_000);
return totalGas.mul(gasPrice);
}

function buildContractCallContext(
type: L1FeeCalculationType,
l1GasOracleAddress: string,
executeData: string
): ContractCallContext[] {
l1GasOracleAddress: string
): ContractCallContext {
if (type === "optimism") {
return [
{
reference: "gasOracle",
contractAddress: l1GasOracleAddress,
abi: ABI.Optimism,
},
];
return {
reference: "gasOracle",
contractAddress: l1GasOracleAddress,
abi: ABI.Optimism,
};
} else if (type === "mantle") {
return [
{
reference: "gasOracle",
contractAddress: l1GasOracleAddress,
abi: ABI.Mantle,
},
];
return {
reference: "gasOracle",
contractAddress: l1GasOracleAddress,
abi: ABI.Mantle,
};
}

throw new Error("Invalid contract call type");
Expand Down
5 changes: 2 additions & 3 deletions src/libs/test/AxelarQueryAPI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe("AxelarQueryAPI", () => {
},
l2Type: "op",
});
expect(gasAmount.gt(parseEther("0.00001"))).toBeTruthy();
expect(gasAmount.lt(parseEther("0.005"))).toBeTruthy();
});
});

Expand Down Expand Up @@ -172,8 +172,7 @@ describe("AxelarQueryAPI", () => {
500000,
undefined,
undefined,
undefined,
"0x"
undefined
);
mainnetQueries.push(query);
}
Expand Down
Loading