项目作者: hrharder

项目描述 :
A simple Golang client for the ETH Gas Station API.
高级语言: Go
项目地址: git://github.com/hrharder/go-gas.git
创建时间: 2020-01-29T23:09:40Z
项目社区:https://github.com/hrharder/go-gas

开源协议:MIT License

下载


Module: go-gas

A simple Golang client for the ETH Gas Station API.

It provides a simple set of methods for loading appropriate gas prices for submitting Ethereum transactions in base units.

Built only on the standard library, it has no external dependencies other than stretchr/testify which is used for testing.

Usage

Install

The go-gas module supports Go modules. Add to your project with the following command.

  1. go get -u github.com/hrharder/go-gas

Usage

Package gas provides two main ways to fetch a gas price from the ETH Gas Station API.

  1. Fetch the current recommended price for a given priority level with a new API call each time
    • Use gas.SuggestGasPrice for a specific priority level
    • Use gas.SuggestFastGasPrice to fetch the fast priority level (no arguments)
  2. Create a new GasPriceSuggester which maintains a cache of results for a user-defined duration
    • Use gas.NewGasPriceSuggester and specify a max result age
    • Use the returned function to fetch new gas prices, or use the cache based on how old the results are

Example

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. "github.com/hrharder/go-gas"
  7. )
  8. func main() {
  9. // get a gas price in base units with one of the exported priorities (fast, fastest, safeLow, average)
  10. fastestGasPrice, err := gas.SuggestGasPrice(gas.GasPriorityFastest)
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. // convenience wrapper for getting the fast gas price
  15. fastGasPrice, err := gas.SuggestFastGasPrice()
  16. if err != nil {
  17. log.Fatal(err)
  18. }
  19. fmt.Println(fastestGasPrice)
  20. fmt.Println(fastGasPrice)
  21. // alternatively, use the NewGasPriceSuggester which maintains a cache of results until they are older than max age
  22. suggestGasPrice, err := gas.NewGasPriceSuggester(5 * time.Minute)
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. fastGasPriceFromCache, err := suggestGasPrice(gas.GasPriorityFast)
  27. if err != nil {
  28. return nil, err
  29. }
  30. // after 5 minutes, the cache will be invalidated and new results will be fetched
  31. time.Sleep(5 * time.Minute)
  32. fasGasPriceFromAPI, err := suggestGasPrice(gas.GasPriorityFast)
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. fmt.Println(fastGasPriceFromCache)
  37. fmt.Println(fasGasPriceFromAPI)
  38. }