Skip to main content

Ethereum gasprice: Actual gasprice for ethereum blockchain

PyPI Build Status Documentation Status

Library for fetching actual ethereum blockchain gasprice from different sources: Etherscan Gas Tracker, Eth Gas Station, Etherchain Gasprice Oracle, Web3 RPC Method.

Read more about gas and fee from this article

Installation

poetry add ethereum-gasprice

or

pip3 install ethereum-gasprice

Quickstart

from ethereum_gasprice import GaspriceController, GaspriceStrategy, EthereumUnit
from ethereum_gasprice.providers import EtherscanProvider

ETHERSCAN_API_KEY = "..."

# Pass api key to GaspriceController to initialize provider
controller = GaspriceController(
    settings={EtherscanProvider.title: ETHERSCAN_API_KEY},
)

# Get gasprice by one of these strategies:
gasprice = controller.get_gasprice_by_strategy(GaspriceStrategy.FAST)
print(gasprice)  # output: 69

Docs

Read base API references and other part documentation on ethereum-gasprice.readthedocs.io

Usage

Gasprice controller

Main entrypoint to fetching gasprice from providers. Has sync and async implementations.

It is recommended to initialize controller with with ... as controller: method

from ethereum_gasprice import GaspriceController, AsyncGaspriceController

Parameters:

  • return_unit - return gasprice in given ethereum unit. It is recommended to use EthereumUnit class from ethereum_gasprice.consts to choose unit
  • providers - tuple of providers what will be used in fetching gasprice. Order of providers is important
  • gasprice will be fetch in given priority. Providers must be a subclass of BaseGaspriceProvider
  • settings - dict containing secrets for providers. Key is provider title slug, value is a secret for provider.
from ethereum_gasprice.consts import EthereumUnit
from ethereum_gasprice.providers import (
    EtherscanProvider, EthGasStationProvider, AsyncEtherscanProvider, AsyncEthGasStationProvider
)

settings = {
    EtherscanProvider.title: "API_KEY",
    EthGasStationProvider.title: "API_KEY"
}

sync_providers = (EtherscanProvider, EthGasStationProvider)
async_providers = (AsyncEtherscanProvider, AsyncEthGasStationProvider)

with GaspriceStrategy(
        return_unit=EthereumUnit.GWEI,
        providers=sync_providers,
        settings=settings
) as controller:
    # Do something
    pass

async with AsyncGaspriceController(
        return_unit=EthereumUnit.WEI,
        providers=async_providers,
        settings=settings
) as async_controller:
    # Do something
    pass

Methods:

  • .get_gasprice_by_strategy() - get gasprices from first available provider and return only one gasprice strategy.

available strategies: slow (GaspriceStrategy.SLOW), regular (GaspriceStrategy.REGULAR), fast (GaspriceStrategy.FAST), fastest (GaspriceStrategy.FASTEST).

Some providers does not have info for some strategies. For example, Etherscan does not provide gasprice for slow strategy.

In any case method will return dict with these for strategies. If fail case strategy (when all provides is unavailable) dict with None values will be returned.

from ethereum_gasprice.consts import GaspriceStrategy

gasprice = controller.get_gasprice_by_strategy(GaspriceStrategy.FAST)  # type: int, example: 69
  • .get_gasprices() - gets gasprices for all strategies from first available provider. Returns a dict.
gasprices = await async_controller.get_gasprices()  # type: dict
print(gasprices)

# {'slow': None, 'regular': 17, 'fast': 19, 'fastest': 20}
  • .get_gasprice_from_all_sources() - get gasprices for all strategies from all available provider.

It can be useful to calculate an average gasprice value from all providers to get the most objective gasprice value.

gasprices = controller.get_gasprice_from_all_sources()  # type: dict
print(gasprices)
# {
#   'etherscan': {'slow': None, 'regular': 17, 'fast': 19, 'fastest': 29},
#   'ethgasstation': {'slow': 16, 'regular': 17, 'fast': 19, 'fastest': 20}
# }

Providers

Provider wrapper

from ethereum_gasprice.providers import EtherscanProvider, AsyncEtherscanProvider

Parameters:

Methods:

  • .request() - make request to api, returns status and response data.
  • .get_gasprice() - get gasprices from provider, returns gasprices in GWEI.
from httpx import Client

provider = EtherscanProvider(
    secret="API-KEY",
    client=Client()
)
print(provider.get_gasprice())
# {'slow': None, 'regular': 17, 'fast': 19, 'fastest': 20}

TODO

  • Initital release
  • Async implementation of controller, providers
  • Write documentation
  • Write unit tests with pytest
  • Integrate tests, docs and auto publishing to pypi in github pipeline

Changelog

see CHANGELOG.md file

License

Ethereum gasprice is licensed under the terms of the MIT License (see the file LICENSE).

Special thanks

Metadata

Release files for ethereum-gasprice 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ethereum-gasprice 1.3.0
File Size Uploaded Provenance Actions
ethereum-gasprice-1.3.0.tar.gz 11.1 kB Without provenance Details for ethereum-gasprice-1.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ethereum-gasprice 1.3.0
File Interpreter ABI Platform Actions
ethereum_gasprice-1.3.0-py3-none-any.whl Python 3 none any Details for ethereum_gasprice-1.3.0-py3-none-any.whl

Total release size: 26.6 kB

Release files / ethereum-gasprice-1.3.0.tar.gz

Download URL ethereum-gasprice-1.3.0.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
614d67c46eb4206e92f79765f6e6ec4210d4586572cf0b119dbdb6433fb15aff
BLAKE2b-256 checksum
How to use checksums
acd189aae708da7fd27f2486816f7d04bd3a462bb0bbe84c854cb36c6fe27907
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.0 Darwin/17.7.0

Release files / ethereum_gasprice-1.3.0-py3-none-any.whl

Download URL ethereum_gasprice-1.3.0-py3-none-any.whl
Size 15.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6cf6d99fd78f0051a7abe41a83a0160ba98a22cf8ea5b46d9bbaf85c28871fea
BLAKE2b-256 checksum
How to use checksums
5ed93717068a9f9b057b5fe3b7be88a6609a06ab7e356960729853d104cbfa95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.0 Darwin/17.7.0

Security for ethereum-gasprice 1.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page