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.

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. Get started for free

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

Request

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

getfeedata.js
1// Imports the Alchemy SDK
2const { Alchemy, Network } = require("alchemy-sdk");
3
4// Configures the Alchemy SDK
5const config = {
6 apiKey: "alchemy-replit", // Replace with your API key
7 network: Network.ETH_MAINNET, // Replace with your network
8};
9
10// Creates an Alchemy object instance with the config to use for making requests
11const alchemy = new Alchemy(config);
12
13const main = async () => {
14
15 //Call the method to return the recommended fee data to use in a transaction.
16 let response = await alchemy.core.getFeeData()
17
18 //Logging the response to the console
19 console.log(response)
20};
21
22main();

Response

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

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.