Skip to content

Commit

Permalink
feat(fees): customizable gas limit and JSON flag (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Sep 26, 2023
1 parent c232c46 commit 54ed4cb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
10 changes: 4 additions & 6 deletions helpers/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import axios from "axios";
import { ethers } from "ethers";
import { formatEther } from "ethers/lib/utils";

const GAS_LIMIT = 350000;

const formatTo18Decimals = (n: any) => parseFloat(formatEther(n)).toFixed(18);

export const fetchZEVMFees = async (network: string) => {
Expand All @@ -33,15 +31,15 @@ export const fetchZEVMFees = async (network: string) => {
};
};

export const fetchCCMFees = async (network: string) => {
export const fetchCCMFees = async (network: string, gas: Number = 500000) => {
const API = getEndpoints("cosmos-http", "zeta_testnet")[0]?.url;
if (!API) {
throw new Error("getEndpoints: API endpoint not found");
}

const chainID = getHardhatConfigNetworks()[network].chainId;

const url = `${API}/zeta-chain/crosschain/convertGasToZeta?chainId=${chainID}&gasLimit=${GAS_LIMIT}`;
const url = `${API}/zeta-chain/crosschain/convertGasToZeta?chainId=${chainID}&gasLimit=${gas}`;
const { data } = await axios.get(url);

const gasFee = ethers.BigNumber.from(data.outboundGasInZeta);
Expand All @@ -55,7 +53,7 @@ export const fetchCCMFees = async (network: string) => {
};
};

export const fetchFees = async () => {
export const fetchFees = async (gas: Number) => {
let fees = {
feesCCM: {} as Record<string, any>,
feesZEVM: {} as Record<string, any>,
Expand All @@ -68,7 +66,7 @@ export const fetchFees = async () => {
try {
const zevmFees = await fetchZEVMFees(n);
if (zevmFees) fees.feesZEVM[n] = zevmFees;
const ccmFees = await fetchCCMFees(n);
const ccmFees = await fetchCCMFees(n, gas);
if (ccmFees) fees.feesCCM[n] = ccmFees;
} catch (err) {}
})
Expand Down
31 changes: 23 additions & 8 deletions tasks/fees.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import { task } from "hardhat/config";
import { task, types } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";

import { fetchCCMFees, fetchFees } from "../helpers/fees";
import { fetchFees } from "../helpers/fees";

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const fees = await fetchFees();
const fees = await fetchFees(args.gas);

console.log("\nOmnichain fees (in native gas tokens of destination chain):");
console.table(fees.feesZEVM);
console.log("\nCross-chain messaging fees (in ZETA):");
console.table(fees.feesCCM);
if (args.json) {
console.log(JSON.stringify(fees, null, 2));
} else {
console.log(
"\nOmnichain fees (in native gas tokens of destination chain):"
);
console.table(fees.feesZEVM);
console.log(
`\nCross-chain messaging fees (in ZETA, gas limit: ${args.gas}):`
);
console.table(fees.feesCCM);
}
};

export const feesTask = task(
"fees",
"Show omnichain and cross-chain messaging fees",
main
);
)
.addOptionalParam(
"gas",
"Gas limit for a cross-chain messaging transaction",
500000,
types.int
)
.addFlag("json", "Print the result in JSON format");

0 comments on commit 54ed4cb

Please sign in to comment.