gleeth/provider

Provider is the main entry point for interacting with an Ethereum network. It wraps a validated JSON-RPC URL and an optional chain ID, giving library consumers a safe handle they can pass to RPC calls, contract interactions, and transaction builders.

Warning: gleeth has not been audited. Recommended for testnet and development use only.

Because Provider is opaque, the only way to construct one is through new, mainnet, or sepolia - all of which guarantee the URL is valid.

Examples

let assert Ok(local) = provider.new("http://localhost:8545")
let url = provider.rpc_url(local)

let eth = provider.mainnet()
let eth = provider.with_chain_id(eth, 1)

Types

An opaque handle representing a connection to an Ethereum JSON-RPC endpoint.

Holds a validated RPC URL, an optional chain ID, and retry configuration. Use new to create one from an arbitrary URL, or the convenience constructors mainnet and sepolia for well-known networks.

pub opaque type Provider

Retry configuration for transient RPC errors.

pub type RetryConfig {
  RetryConfig(
    max_retries: Int,
    initial_backoff_ms: Int,
    max_backoff_ms: Int,
  )
}

Constructors

  • RetryConfig(
      max_retries: Int,
      initial_backoff_ms: Int,
      max_backoff_ms: Int,
    )

    Arguments

    max_retries

    Maximum number of retry attempts. 0 means no retries.

    initial_backoff_ms

    Initial backoff in milliseconds before the first retry.

    max_backoff_ms

    Maximum backoff in milliseconds (backoff is capped at this value).

Values

pub fn chain_id(provider: Provider) -> option.Option(Int)

Return the chain ID if one has been set, or None otherwise.

Examples

let assert Ok(p) = provider.new("http://localhost:8545")
provider.chain_id(p)
// -> None
pub fn default_retry() -> RetryConfig

Default retry config: 3 retries, 1s initial backoff, 8s max.

pub fn mainnet() -> Provider

Convenience constructor for Ethereum mainnet using a public RPC endpoint.

Examples

let eth = provider.mainnet()
provider.rpc_url(eth)
// -> "https://eth.llamarpc.com"
pub fn new(
  rpc_url: String,
) -> Result(Provider, types.GleethError)

Create a new provider from the given RPC URL.

The URL must be non-empty and start with http:// or https://. Returns Error(InvalidRpcUrl(...)) if validation fails.

Examples

let assert Ok(p) = provider.new("http://localhost:8545")
let assert Error(_) = provider.new("")
let assert Error(_) = provider.new("ws://bad-scheme")
pub fn no_retry() -> RetryConfig

No retries - fail immediately on error.

pub fn retry_config(provider: Provider) -> RetryConfig

Get the retry configuration from the provider.

pub fn rpc_url(provider: Provider) -> String

Return the RPC URL held by this provider.

Examples

let assert Ok(p) = provider.new("http://localhost:8545")
provider.rpc_url(p)
// -> "http://localhost:8545"
pub fn sepolia() -> Provider

Convenience constructor for the Sepolia testnet using a public RPC endpoint.

Examples

let sep = provider.sepolia()
provider.rpc_url(sep)
// -> "https://ethereum-sepolia.publicnode.com"
pub fn with_chain_id(provider: Provider, id: Int) -> Provider

Return a new provider with the given chain ID attached.

This does not mutate the original provider - a fresh copy is returned.

Examples

let assert Ok(p) = provider.new("http://localhost:8545")
let p = provider.with_chain_id(p, 1)
provider.chain_id(p)
// -> Some(1)
pub fn with_retry(
  provider: Provider,
  config: RetryConfig,
) -> Provider

Attach retry configuration to the provider.

Examples

let p = provider.new("http://localhost:8545")
  |> result.map(provider.with_retry(_, provider.default_retry()))
Search Document