oracle

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package oracle provides a blockchain oracle for gas price queries.

The oracle fetches gas prices on-demand with a short TTL, using request coalescing to minimize RPC calls when multiple goroutines request the price simultaneously.

Chain Support

The oracle works with any EVM chain:

  • Ethereum mainnet and testnets
  • Arbitrum One, Nova, and Orbit L3 chains
  • Optimism, Base, and other OP Stack chains
  • Any EVM-compatible chain

Built-in Gas Price Tracking

The oracle includes gas price tracking with automatic chain detection:

  • Arbitrum chains: Uses ArbGasInfo precompile (0x6C) for accurate L2 pricing
  • Other chains: Uses eth_gasPrice RPC call

Usage

oracle, err := oracle.New(ctx, logger, "wss://your-rpc-endpoint")
if err != nil {
    return err
}
defer oracle.Close()

// Get gas price (fetches if stale, coalesces concurrent requests)
gasPrice := oracle.GetGasPrice()

Thread Safety

All methods are safe for concurrent use. GetGasPrice() uses singleflight to coalesce concurrent requests, ensuring only one RPC call is made when multiple goroutines request the price simultaneously.

Staleness Handling

Gas prices are cached for 250ms. If the cached value is stale, GetGasPrice() fetches a fresh value. If the fetch fails, it returns a safe default (0.1 gwei).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockchainOracle

type BlockchainOracle interface {
	GetGasPrice() int64
	Close()
}

type GasPriceSource

type GasPriceSource int

func (GasPriceSource) String

func (g GasPriceSource) String() string

type Oracle

type Oracle struct {
	// contains filtered or unexported fields
}

func New

func New(
	ctx context.Context,
	logger *zap.Logger,
	wsURL string,
) (*Oracle, error)

func (*Oracle) Close

func (o *Oracle) Close()

func (*Oracle) GetGasPrice

func (o *Oracle) GetGasPrice() int64

GetGasPrice returns the current gas price. If the cached price is stale (older than 250ms), it fetches a fresh value. Concurrent requests are coalesced to minimize RPC calls. If fetching fails, returns a safe default value (0.1 gwei).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL