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 / stringThe maxFeePerGas to use for a transaction. This is based on the most recent block's baseFee.
maxPriorityFeePerGasnumber / stringThe maxPriorityFeePerGas to use for a transaction. This accounts for the uncle risk and the majority of current MEV risk.
gasPricenumber / stringThe 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@latest
yarn add alchemy-sdk@latest

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": {
        "type": "BigNumber",
        "hex": "0x06bff1b5ca"
    },
    "maxFeePerGas": {
        "type": "BigNumber",
        "hex": "0x0dd94b9a94"
    },
    "maxPriorityFeePerGas": {
        "type": "BigNumber",
        "hex": "0x59682f00"
    },
    "gasPrice": {
        "type": "BigNumber",
        "hex": "0x06c1241635"
    }
}

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. Given the current network conditions, this information can be used to determine the optimal fee to set for a transaction.

  • 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 application gas price strategies, 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.
ReadMe