Skip to content

Commit

Permalink
More Reliable Gas Oracle (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored Mar 13, 2024
1 parent 687d39e commit af1b3a7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/chains/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -43,9 +44,8 @@ async function queryGasPrice(): Promise<GasPrices> {
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.
Expand Down
17 changes: 17 additions & 0 deletions src/utils/gasPrice.ts
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit af1b3a7

Please sign in to comment.