diff --git a/src/chains/ethereum.ts b/src/chains/ethereum.ts index 2978f63..8d25b17 100644 --- a/src/chains/ethereum.ts +++ b/src/chains/ethereum.ts @@ -14,6 +14,7 @@ import { import { NO_DEPOSIT, getNearAccount, provider as nearProvider } from "./near"; import { GasPriceResponse, GasPrices, TxPayload } from "../types"; import { getMultichainContract } from "../mpc_contract"; +import { getFirstNonZeroGasPrice } from "../utils/gasPrice"; const config = { chainId: 11155111, @@ -43,9 +44,8 @@ async function queryGasPrice(): Promise { const res = await fetch( "https://sepolia.beaconcha.in/api/v1/execution/gasnow" ); - const json = await res.json() as GasPriceResponse; - console.log("Response", json); - const maxPriorityFeePerGas = BigInt(json.data.rapid); + const gasPrices = await res.json() as GasPriceResponse; + const maxPriorityFeePerGas = BigInt(getFirstNonZeroGasPrice(gasPrices)!); // Since we don't have a direct `baseFeePerGas`, we'll use a workaround. // Ideally, you should fetch the current `baseFeePerGas` from the network. diff --git a/src/utils/gasPrice.ts b/src/utils/gasPrice.ts new file mode 100644 index 0000000..2f9627f --- /dev/null +++ b/src/utils/gasPrice.ts @@ -0,0 +1,17 @@ +import { GasPriceResponse } from "../types"; + + +export const getFirstNonZeroGasPrice = (response: GasPriceResponse): number | undefined => { + // List the properties we're interested in + const properties: (keyof GasPriceResponse["data"])[] = ["rapid", "fast", "standard", "slow"]; + + // Iterate through the properties and return the first non-zero value + for (const property of properties) { + if (response.data[property] !== 0) { + return response.data[property]; + } + } + + // Return undefined if all values are zero or if none are found + return undefined; +}; \ No newline at end of file