gleeth/chain

Multi-chain configuration registry.

Pre-configured entries for common EVM networks with chain ID, name, RPC URL, block explorer, and native currency. Data sourced from ethereum-lists/chains (the canonical chain registry used by chainlist.org).

Examples

let assert Ok(config) = chain.by_name("arbitrum")
let assert Ok(p) = chain.to_provider(config)

// Or look up by chain ID
let assert Ok(config) = chain.by_id(137)
config.name  // "Polygon"

Types

Configuration for an EVM chain.

pub type ChainConfig {
  ChainConfig(
    id: Int,
    name: String,
    rpc_url: String,
    explorer_url: String,
    native_currency: String,
    testnet: Bool,
  )
}

Constructors

  • ChainConfig(
      id: Int,
      name: String,
      rpc_url: String,
      explorer_url: String,
      native_currency: String,
      testnet: Bool,
    )

    Arguments

    id

    Chain ID (e.g. 1 for mainnet, 42161 for Arbitrum).

    name

    Human-readable chain name.

    rpc_url

    Default public RPC URL. Empty if none available.

    explorer_url

    Block explorer URL. Empty if none.

    native_currency

    Native currency symbol (e.g. “ETH”, “POL”).

    testnet

    Whether this is a testnet.

A chain registry. Start with default_registry() and add custom chains with add.

pub type Registry {
  Registry(chains: dict.Dict(Int, ChainConfig))
}

Constructors

Values

pub fn add(registry: Registry, config: ChainConfig) -> Registry

Add a custom chain to a registry.

Examples

let registry = chain.default_registry()
  |> chain.add(chain.ChainConfig(
    id: 56, name: "BNB Smart Chain",
    rpc_url: "https://bsc-dataseed.binance.org",
    explorer_url: "https://bscscan.com",
    native_currency: "BNB", testnet: False,
  ))
pub fn by_id(id: Int) -> Result(ChainConfig, String)

Look up a chain by ID from the default registry.

Examples

let assert Ok(config) = chain.by_id(1)
config.name  // "Ethereum Mainnet"
pub fn by_id_in(
  registry: Registry,
  id: Int,
) -> Result(ChainConfig, String)

Look up a chain by ID in a custom registry.

pub fn by_name(name: String) -> Result(ChainConfig, String)

Look up a chain by name from the default registry (case-insensitive). Matches against the full name or common short names.

Examples

let assert Ok(config) = chain.by_name("arbitrum")
config.id  // 42161
pub fn by_name_in(
  registry: Registry,
  name: String,
) -> Result(ChainConfig, String)

Look up a chain by name in a custom registry (case-insensitive).

pub fn default_registry() -> Registry

The default registry with common EVM networks. RPC URLs and metadata sourced from ethereum-lists/chains.

pub fn list(registry: Registry) -> List(ChainConfig)

List all chains in a registry.

pub fn to_provider(
  config: ChainConfig,
) -> Result(provider.Provider, String)

Create a Provider from a chain config. Uses the chain’s default RPC URL with retry enabled and chain ID set.

Examples

let assert Ok(config) = chain.by_name("base")
let assert Ok(p) = chain.to_provider(config)
Search Document