Skip to content

Commit

Permalink
feat: use l2_type from getFees api instead of hardcoded chain
Browse files Browse the repository at this point in the history
  • Loading branch information
npty committed Feb 29, 2024
1 parent 8247d79 commit 956540f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
12 changes: 9 additions & 3 deletions src/libs/AxelarQueryAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export class AxelarQueryAPI {
destination_native_token,
express_fee_string,
express_supported,
l2_type,
} = response.result;
const execute_gas_multiplier = response.result.execute_gas_multiplier as number;
const { decimals: sourceTokenDecimals } = source_token;
Expand All @@ -251,6 +252,7 @@ export class AxelarQueryAPI {
symbol: destination_native_token.symbol,
l1_gas_price_in_units: destination_native_token.l1_gas_price_in_units,
},
l2_type,
ethereumToken: ethereum_token as BaseFeeResponse["ethereumToken"],
apiResponse: response,
success: true,
Expand All @@ -270,7 +272,7 @@ export class AxelarQueryAPI {

const provider = new ethers.providers.JsonRpcProvider(rpcMap[destChainId]);

return getL1FeeForL2(provider, destChainId, l1FeeParams);
return getL1FeeForL2(provider, l1FeeParams);
}

public async calculateL1FeeForDestL2(
Expand All @@ -279,7 +281,8 @@ export class AxelarQueryAPI {
executeData: `0x${string}` | undefined,
sourceToken: FeeToken,
ethereumToken: BaseFeeResponse["ethereumToken"],
actualGasMultiplier: number
actualGasMultiplier: number,
l2Type: BaseFeeResponse["l2_type"]
): Promise<[BigNumber, BigNumber]> {
let l1ExecutionFee = BigNumber.from(0);
let l1ExecutionFeeWithMultiplier = BigNumber.from(0);
Expand All @@ -296,6 +299,7 @@ export class AxelarQueryAPI {
l1ExecutionFee = await this.estimateL1GasFee(destChainId, {
executeData: executeData || "0x",
l1GasPrice: destToken.l1_gas_price_in_units,
l2Type,
});

// Convert the L1 execution fee to the source token
Expand Down Expand Up @@ -353,6 +357,7 @@ export class AxelarQueryAPI {
executeGasMultiplier,
destToken,
apiResponse,
l2_type,
success,
expressSupported,
} = await this.getNativeGasBaseFee(
Expand Down Expand Up @@ -400,7 +405,8 @@ export class AxelarQueryAPI {
executeData,
sourceToken,
ethereumToken,
actualGasMultiplier
actualGasMultiplier,
l2_type
);

return gmpParams?.showDetailedFees
Expand Down
10 changes: 6 additions & 4 deletions src/libs/fee/getL1Fee.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ describe("getL1Fee", () => {

const queryAPI = new AxelarQueryAPI({ environment: env });

const { destToken } = await queryAPI.getNativeGasBaseFee(srcChain, destChain);
const { destToken, l2_type } = await queryAPI.getNativeGasBaseFee(srcChain, destChain);

const provider = new ethers.providers.JsonRpcProvider(rpcMap[destChain]);

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

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

View workflow job for this annotation

GitHub Actions / ESLint

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

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

const fee = await getL1FeeForL2(provider, destChain, params);
const fee = await getL1FeeForL2(provider, params);

expect(fee).toBeDefined();
});
Expand All @@ -36,16 +37,17 @@ describe("getL1Fee", () => {

const queryAPI = new AxelarQueryAPI({ environment: env });

const { destToken } = await queryAPI.getNativeGasBaseFee(srcChain, destChain);
const { destToken, l2_type } = await queryAPI.getNativeGasBaseFee(srcChain, destChain);

const provider = new ethers.providers.JsonRpcProvider(rpcMap[destChain]);

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

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

View workflow job for this annotation

GitHub Actions / ESLint

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

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

const fee = await getL1FeeForL2(provider, destChain, params);
const fee = await getL1FeeForL2(provider, params);

expect(fee).toBeDefined();
});
Expand Down
10 changes: 3 additions & 7 deletions src/libs/fee/getL1Fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,17 @@ const ABI = {
*/
export function getL1FeeForL2(
provider: ethers.providers.JsonRpcProvider,
chain: string,
params: EstimateL1FeeParams
): Promise<BigNumber> {
const multicall = new Multicall({ ethersProvider: provider, tryAggregate: true });

switch (chain) {
switch (params.l2Type) {
case "mantle":
return getMantleL1Fee(multicall, params);
case "optimism":
case "scroll":
case "base":
case "op":
return getOptimismL1Fee(multicall, params);
// Most of the ethereum clients are already included L1 fee in the gas estimation for Arbitrum.
case "arbitrum":
case "arbitrum-sepolia":
case "arb":
default:
return Promise.resolve(BigNumber.from(0));
}
Expand Down
3 changes: 2 additions & 1 deletion src/libs/test/AxelarQueryAPI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ describe("AxelarQueryAPI", () => {
decimals: 18,
value: "32534506865",
},
l2Type: "op",
});
expect(gasAmount.gt(parseEther("0.00001"))).toBeTruthy();
});
Expand All @@ -128,7 +129,7 @@ describe("AxelarQueryAPI", () => {
expect(ethers.utils.parseEther("10000").gt(gasAmount as BigNumberish)).toBeTruthy();
});

test("It should include L1 fee for L2 destination chain", async () => {
test.only("It should include L1 fee for L2 destination chain", async () => {
const mainnetApi = new AxelarQueryAPI({ environment: Environment.MAINNET });
const gasAmount = await mainnetApi.estimateGasFee(
EvmChain.ETHEREUM,
Expand Down
2 changes: 2 additions & 0 deletions src/libs/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export interface BaseFeeResponse {
executeGasMultiplier: number;
sourceToken: FeeToken;
destToken: FeeToken;
l2_type: "op" | "arb" | "mantle" | undefined;
ethereumToken: {
name: string;
symbol: string;
Expand Down Expand Up @@ -245,4 +246,5 @@ export type TokenUnit = {
export type EstimateL1FeeParams = {
executeData: `0x${string}`;
l1GasPrice: TokenUnit;
l2Type?: "op" | "arb" | "mantle" | undefined;
};

0 comments on commit 956540f

Please sign in to comment.