JS SDK - Tatum Fee Submodule
getCurrentFee() in the Tatum JS SDK returns the current slow, medium and fast gas fees for Ethereum, Bitcoin, Litecoin or Dogecoin.
The getCurrentFee() function is a method provided by the Tatum Fee Submodule in the Tatum SDK. It enables developers to fetch the current gas fee for a specific blockchain network. This function facilitates access to real-time fee information, allowing users to make informed decisions about transaction costs and optimize their blockchain interactions.
Syntax
const fee = await tatum.fee.getCurrentFee();
NoteThis function calls the v3 endpoint
GET /v3/blockchain/fee/{chain}. The SDK uses v4 by default, so initialize it withversion: ApiVersion.V3to use this function. For the REST endpoints, including gas estimation for a specific transaction, see Fee Estimation.
import { TatumSDK, Network, ApiVersion } from '@tatumio/tatum'
const tatum = await TatumSDK.init(
{ network: Network.ETHEREUM, version: ApiVersion.V3, apiKey: '{YOUR_API_KEY}' }
);Response DTO
interface CurrentEvmFee {
/* Chain for the Response */
chain: Network
/* gasPrice for slow, medium, fast, baseFee & unit */
gasPrice: {
slow: string
medium: string
fast: string
baseFee: string
unit: string
}
/* Last time when the gas fees was Recalculated */
lastRecalculated: string
/* This property specifies the block number or block
identifier on the blockchain network on which the current
gas fee data is based. */
basedOnBlockNumber: string
}On Bitcoin, Litecoin and Dogecoin, the response (CurrentUtxoFee) has slow, medium, fast and unit at the top level instead of the gasPrice object.
Return Value
The getCurrentFee() function returns a Promise that resolves to an object containing the current gas fee data for the selected network. The specific structure of the returned object may vary depending on the blockchain network and API version being used.
Usage
- Initialize the Tatum SDK with the desired network and API version using
TatumSDK.init().
const tatum = await TatumSDK.init({ network: Network.ETHEREUM, version: ApiVersion.V3, apiKey: '{YOUR_API_KEY}' });- Call the
getCurrentFee()function to fetch the current gas fee for the selected network.
const fee = await tatum.fee.getCurrentFee();- Access the fee data returned by the function to retrieve the gas fee information.
console.log(fee.data.gasPrice.fast); // Display the fast gas feeSupported blockchains
- ETHEREUM
- BITCOIN
- LITECOIN
- DOGECOIN
Example
The following example demonstrates the usage of the getCurrentFee() function within an event listener:
const button = document.getElementById("fetch-fee");
const feeContainer = document.getElementById("fee-container");
button.addEventListener("click", async () => {
const tatum = await TatumSDK.init({ network: Network.ETHEREUM, version: ApiVersion.V3, apiKey: '{YOUR_API_KEY}' });
const fee = await tatum.fee.getCurrentFee();
feeContainer.textContent = `Current Fee: ${fee.data.gasPrice.fast}`;
await tatum.destroy();
});In this example, when the "Fetch Fee" button is clicked, the getCurrentFee() function is called to fetch the current gas fee for the Ethereum network. The returned fee data is then used to update the content of the feeContainer element with the fast gas fee value.
Note: The example assumes that the HTML structure and event listeners are properly set up as described in the previous sections. Adjust the code according to your specific requirements and chosen blockchain network.
Updated 1 day ago