getFeeData - SDK

Returns the recommended fee data to use in a transaction. For an EIP-1559 transaction, the maxFeePerGas and maxPriorityFeePerGas should be used.

For legacy transactions and networks which do not support EIP-1559, the gasPrice should be used.

Don’t have an API key?

Start using this method in your app today.

Description

Returns the recommended fee data to use in a transaction. For an EIP-1559 transaction, the maxFeePerGas and maxPriorityFeePerGas should be used.

For legacy transactions and networks which do not support EIP-1559, the gasPrice should be used.

Response

PropertyTypeDescription
Promise<FeeData>objectReturns an object with the fees

FeeData response object parameters

PropertyTypeDescription
maxFeePerGasnumber / stringBigNumber - The maxFeePerGas to use for a transaction. This is based on the most recent block's baseFee.
maxPriorityFeePerGasnumber / stringBigNumber - The maxPriorityFeePerGas to use for a transaction. This accounts for the uncle risk and for the majority of current MEV risk.
gasPricenumber / stringBigNumber - The gasPrice to use for legacy transactions or networks which do not support EIP-1559.

Example Request and Response

Prerequisite: You will need to install the Alchemy SDK before making requests with it.

The commands for installing it using npm or yarn are given below:

npm install alchemy-sdk
yarn add alchemy-sdk

Request

Here is an example of how to make a getFeeData request using the Alchemy SDK:

// Imports the Alchemy SDK
const { Alchemy, Network } = require("alchemy-sdk");

// Configures the Alchemy SDK
const config = {
    apiKey: "alchemy-replit", // Replace with your API key
    network: Network.ETH_MAINNET, // Replace with your network
};

// Creates an Alchemy object instance with the config to use for making requests
const alchemy = new Alchemy(config);

const main = async () => {

    //Call the method to return the recommended fee data to use in a transaction.
    let response = await alchemy.core.getFeeData()

    //Logging the response to the console
    console.log(response)
};

main();

Response

{
  lastBaseFeePerGas: BigNumber { _hex: '0x0d9fbc42bf', _isBigNumber: true },
  maxFeePerGas: BigNumber { _hex: '0x1b98e0b47e', _isBigNumber: true },
  maxPriorityFeePerGas: BigNumber { _hex: '0x59682f00', _isBigNumber: true },
  gasPrice: BigNumber { _hex: '0x0da39585ec', _isBigNumber: true }
}

Code Sandbox

You can test out the getFeeData method using the code sandbox below:

Use Cases

Here are some possible use cases for the getFeeData method:

  • Transaction fee estimation: Developers can use the getFeeData method to estimate the fee required for a particular transaction. This information can be used to determine the optimal fee to set for a transaction, given the current network conditions.

  • Gas price monitoring: The getFeeData method can be used to monitor changes in the gas price of a blockchain network over time. This information can be used to adjust gas price strategies for applications, ensuring they stay competitive and cost-effective.

  • Smart contract development: When developing smart contracts, developers need to ensure that their contracts operate within the gas limits of the network. The getFeeData method can be used to calculate the gas costs associated with specific operations, helping developers to optimize their contract code and stay within network constraints.

Related Methods

Here are the methods related to getFeeData:

  • getGasPrice: Returns the current price per gas in wei.