Module: X402::Payments::Networks

Defined in:
lib/x402/payments/networks.rb

Constant Summary collapse

NETWORK_MAPPING =
{
  "base-sepolia" => "eip155:84532",
  "base" => "eip155:8453",
  "avalanche-fuji" => "eip155:43113",
  "avalanche" => "eip155:43114",
  "solana-devnet" => "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1",
  "solana" => "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"
}.freeze
CAIP2_MAPPING =
NETWORK_MAPPING.invert.freeze

Class Method Summary collapse

Class Method Details

.caip2_format?(network) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/x402/payments/networks.rb', line 42

def caip2_format?(network)
  network.to_s.include?(":")
end

.chain_id_from_caip2(caip2_network) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/x402/payments/networks.rb', line 46

def chain_id_from_caip2(caip2_network)
  return nil unless caip2_format?(caip2_network)

  namespace, reference = caip2_network.split(":")
  return nil unless namespace == "eip155"

  reference.to_i
end

.from_caip2(network) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/x402/payments/networks.rb', line 30

def from_caip2(network)
  return network unless caip2_format?(network)

  # Check custom chains first
  X402::Payments.configuration.custom_chains.each do |name, config|
    caip2 = "#{config[:standard]}:#{config[:chain_id]}"
    return name if caip2 == network
  end

  CAIP2_MAPPING[network] || raise(Error, "Unknown CAIP-2 network: #{network}")
end

.supported_caip2_networksObject



59
60
61
# File 'lib/x402/payments/networks.rb', line 59

def supported_caip2_networks
  CAIP2_MAPPING.keys
end

.supported_networksObject



55
56
57
# File 'lib/x402/payments/networks.rb', line 55

def supported_networks
  NETWORK_MAPPING.keys
end

.to_caip2(network) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/x402/payments/networks.rb', line 18

def to_caip2(network)
  return network if caip2_format?(network)

  # Check custom chains first
  custom = X402::Payments.configuration.chain_config(network)
  if custom
    return "#{custom[:standard]}:#{custom[:chain_id]}"
  end

  NETWORK_MAPPING[network] || raise(Error, "Unknown network: #{network}")
end