【问题标题】:Arbitrum and Optimism gas price oracle apiArbitrum 和 Optimism gas price oracle api
【发布时间】:2022-12-30 02:41:12
【问题描述】:

有一个代码,其中通过 gasstation api 提供汽油价格数据。现在它适用于多边形。想在 Arbitrum 和 Optimism 上开始这个,但找不到任何有 gas price 的 api。据我了解,Arbitrum 和 Optimism 上的气体有两部分,l1 和 l2。如果有人知道如何解决这个问题,请帮忙

import got from "got"
import { BigNumber, ethers } from "ethers";
import { CurrencyAmount } from "@uniswap/sdk-core";
import { Pool } from "@uniswap/v3-sdk";
import { WETH_ADDRESS } from "./constants";

interface GasPriceData {
fast:{
maxPriorityFee: number;
maxFee: number;
}
 }

export async function getmaxFeePerGas(): Promise<BigNumber> {
const gasPriceData: GasPriceData = await got("https://gasstation-mainnet.matic.network/v2").json();
return ethers.utils.parseUnits(gasPriceData.fast.maxFee.toFixed(9).toString(), 9);
 }

export async function getmaxPriorityFee(): Promise<BigNumber> {
const gasPriceData: GasPriceData = await got("https://gasstation-mainnet.matic.network/v2").json();
return ethers.utils.parseUnits(gasPriceData.fast.maxPriorityFee.toFixed(9).toString(), 9);
}

【问题讨论】:

    标签: typescript ethers.js uniswap


    【解决方案1】:

    虽然此解决方案不会为您提供与您当前使用的 API 完全相同的数据,但 Ethers 具有内置的汽油费功能。

    const provider = new ethers.providers.JsonRpcProvider($RPC_ENDPOINT);
    const feeData = await provider.getFeeData();
    

    这将返回一个看起来像这样的对象:

    {
      lastBaseFeePerGas: BigNumber { _hex: '0x047c5067ef', _isBigNumber: true },
      maxFeePerGas: BigNumber { _hex: '0x095208fede', _isBigNumber: true },
      maxPriorityFeePerGas: BigNumber { _hex: '0x59682f00', _isBigNumber: true },
      gasPrice: BigNumber { _hex: '0x0488a084b7', _isBigNumber: true }
    }
    

    值得注意的是,返回的数据会因不同的链而异,尤其是 L2。对于 Optimism,您需要考虑 L1 和 L2 的汽油费。 Ethers 返回的 gasPrice 将仅涵盖交易费用的 L2 部分。 L1 费用必须从 Optimism 的gas price oracle 获取。 Optimism的docs有更详细的解释。

    【讨论】:

      猜你喜欢
      • 2018-11-30
      • 1970-01-01
      • 2017-08-15
      • 2018-05-03
      • 2021-11-19
      • 2018-03-20
      • 2019-01-03
      • 2019-04-18
      • 2022-06-11
      相关资源
      最近更新 更多