UNPKG

@axelar-network/axelarjs-sdk

Version:
73 lines 3.79 kB
"use strict"; /* eslint-disable @typescript-eslint/no-unsafe-return */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getL1FeeForL2 = getL1FeeForL2; const ethers_1 = require("ethers"); const ABI = { Optimism: ["function getL1Fee(bytes executeData) view returns (uint256)"], Mantle: [ "function getL1Fee(bytes executeData) view returns (uint256)", "function tokenRatio() view returns (uint256)", ], }; /** * Get the estimated L1 fee for a given L2 chain, denominated in the destination * chain's native token wei. * * - OP Stack chains with ETH as native (optimism, base, blast, fraxtal, scroll): * returns the oracle's getL1Fee(data) result, which is already in ETH wei. * - Mantle (post-Arsia): returns getL1Fee(data) × tokenRatio(), which converts * the oracle's ETH-wei posting cost to MNT wei — matching what Mantle actually * debits from the user's balance at tx inclusion. * - Arbitrum: returns 0. Arbitrum bundles the L1 component into the L2 gasUsed * reported by eth_estimateGas, so there's no separate L1 fee to return here. * * @param provider JSON-RPC provider for the destination chain. * @param params Estimation parameters, including executeData and l2Type. * @returns The estimated L1 fee in the destination chain's native token wei. */ function getL1FeeForL2(provider, params) { const { l1GasOracleAddress } = params; const _l1GasOracleAddress = l1GasOracleAddress || "0x420000000000000000000000000000000000000F"; switch (params.l2Type) { case "op": return getOptimismL1Fee(provider, Object.assign(Object.assign({}, params), { l1GasOracleAddress: _l1GasOracleAddress })); case "mantle": return getMantleL1Fee(provider, Object.assign(Object.assign({}, params), { l1GasOracleAddress: _l1GasOracleAddress })); case "arb": default: return Promise.resolve(ethers_1.BigNumber.from(0)); } } function getOptimismL1Fee(provider, estimateL1FeeParams) { return __awaiter(this, void 0, void 0, function* () { const { executeData, l1GasOracleAddress } = estimateL1FeeParams; const contract = new ethers_1.ethers.Contract(l1GasOracleAddress, ABI.Optimism, provider); return contract.getL1Fee(executeData); }); } function getMantleL1Fee(provider, estimateL1FeeParams) { return __awaiter(this, void 0, void 0, function* () { const { executeData, l1GasOracleAddress } = estimateL1FeeParams; const contract = new ethers_1.ethers.Contract(l1GasOracleAddress, ABI.Mantle, provider); const [l1FeeEthWei, tokenRatio] = yield Promise.all([ contract.getL1Fee(executeData), contract.tokenRatio(), ]); if (tokenRatio.isZero()) { throw new Error(`Mantle L1 fee oracle at ${l1GasOracleAddress} returned tokenRatio=0; unable to compute L1 fee`); } // getL1Fee is ETH wei; tokenRatio is the ETH→MNT conversion factor. Product is MNT wei. return l1FeeEthWei.mul(tokenRatio); }); } //# sourceMappingURL=getL1Fee.js.map