/gas-price-utils

Utility for fetching current Ethereum gas prices and performing conversions.

Primary LanguageTypeScriptMIT LicenseMIT

Gas Price Utilities

Simple functions for fetching current reasonable gas prices from the ETH Gas Station API.

See docs/ for full documentation.

Index

Listed below are all exports from this package and links to their documentation.

Usage

Install

# with yarn
yarn add @habsyr/gas-price-utils

# with npm
npm i --save @habsyr/gas-price-utils

Add to your project

  • ES6 and TypeScript:
    import {
        ETH_GAS_STATION_API_URL,
        ETH_GAS_STATION_PRIORITIES,
    
        fetchGasPriceData,
        fetchGasPriceInWei,
    
        gasStationUnitsToGwei,
        gweiToWei,
        weiToGwei,
    } from "@habsyr/gas-price-utils";
  • CommonJS and ES5, etc:
    const {
        ETH_GAS_STATION_API_URL,
        ETH_GAS_STATION_PRIORITIES,
    
        fetchGasPriceData,
        fetchGasPriceInWei,
    
        gasStationUnitsToGwei,
        gweiToWei,
        weiToGwei,
    } = require("@habsyr/gas-price-utils");

Functions

Primary exports of this package (see the docs/ folder for documentation for all exports) used to fetch gas price data from the ETH Gas Station API and perform conversions.

fetchGasPriceData

fetchGasPriceData(): Promise‹EthGasStationApiResponse

Defined in utils.ts:44

Fetch the current gas price data from ETH Gas Station.

This function returns the object directly from the API, in the format described in their official docs.

Returns: Promise‹EthGasStationApiResponse

A promise that resolves to the raw ETH gas station API data.


fetchGasPriceInWei

fetchGasPriceInWei(priority: GasPriority): Promise‹BigNumber›

Defined in utils.ts:27

Fetch a current gas price from ETH Gas Station in base units for a given priority.

This function returns a BigNumber with the gas price corresponding to the specified priority level in ETH base units (wei).

To use gwei value, convert with the weiToGwei method.

Parameters:

Name Type Default
priority GasPriority "fast"

Returns: Promise‹BigNumber›

A promise that resolves to a BigNumber with gas price in wei.


gasStationUnitsToGwei

gasStationUnitsToGwei(fromValue: string | number | BigNumber): BigNumber

Defined in utils.ts:84

Convert a value in the units used by ETH Gas Station (gwei times 10) to the more common gwei (1e9 wei) unit.

Parameters:

Name Type Description
fromValue string | number | BigNumber The value from ETH Gas Station API to convert.

Returns: BigNumber

The same value as a BigNumber in units of gwei (1e9 wei).


gweiToWei

gweiToWei(gweiValue: string | number | BigNumber): BigNumber

Defined in utils.ts:59

Convert a value in gwei (1e9 wei) units to wei (base units).

Parameters:

Name Type Description
gweiValue string | number | BigNumber A positive gwei value as a string, number or BigNumber.

Returns: BigNumber

The value in wei as a BigNumber.


weiToGwei

weiToGwei(weiValue: string | number | BigNumber): BigNumber

Defined in utils.ts:71

Convert a value in wei (base units) to gwei (1e9 wei).

Parameters:

Name Type Description
weiValue string | number | BigNumber A positive wei value as a string, number or BigNumber.

Returns: BigNumber

The value in wei as a BigNumber.