Module: Solace::Tokens

Defined in:
lib/solace/tokens.rb,
lib/solace/tokens/token.rb

Overview

Represents a Solana token with its metadata.

A tokens object encapsulates the symbol and associated metadata for a Solana token. It provides dynamic access to metadata attributes via method calls. This class is used within the Solace::Tokens module to represent individual tokens loaded from a YAML configuration file.

Examples:

Load tokens for the ‘mainnet’ network

Solace::Tokens.load(path: 'path/to/tokens.yml', network: 'mainnet')

Access a specific token

usdc = Solace::Tokens.fetch('USDC')
puts usdc.address

Query tokens by criteria

stablecoins = Solace::Tokens.where(type: 'stablecoin')

Since:

  • 0.1.0

Defined Under Namespace

Classes: Token

Class Method Summary collapse

Class Method Details

.allArray<Solace::Tokens::Token>

Get all loaded tokens

Returns:

Since:

  • 0.1.0



60
61
62
# File 'lib/solace/tokens.rb', line 60

def self.all
  @registry.values
end

.clear!void

This method returns an undefined value.

Clear loaded tokens

Since:

  • 0.1.0



52
53
54
55
# File 'lib/solace/tokens.rb', line 52

def self.clear!
  (constants - [:Token]).each { |c| remove_const(c) }
  @registry = {}
end

.fetch(symbol) ⇒ Solace::Tokens::Token?

Fetch a token by its symbol

Parameters:

  • symbol (String)

    The symbol of the token

Returns:

Since:

  • 0.1.0



68
69
70
# File 'lib/solace/tokens.rb', line 68

def self.fetch(symbol)
  @registry[symbol.to_s]
end

.load(path:, network:) ⇒ void

This method returns an undefined value.

Load tokens from a YAML file for a specific network

Parameters:

  • path (String)

    Path to the YAML file

  • network (String, Symbol)

    Network name (e.g., ‘mainnet’, ‘testnet’)

Since:

  • 0.1.0



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/solace/tokens.rb', line 31

def self.load(path:, network:)
  data = YAML.load_file(path)
  tokens = data.fetch(network.to_s) do
    raise ArgumentError, "Network '#{network}' not found in config"
  end

  # Clear previous constants and registry
  clear!

  @registry = {}

  tokens.each do |symbol, attrs|
    token = Solace::Tokens::Token.new(symbol, attrs)
    const_set(symbol, token)
    @registry[symbol.to_s] = token
  end
end

.where(criteria = {}) ⇒ Array<Solace::Tokens::Token>

Query tokens based on criteria

Parameters:

  • criteria (Hash{Symbol => Object}) (defaults to: {})

    Key-value pairs to filter tokens

Returns:

Since:

  • 0.1.0



76
77
78
79
80
81
82
83
84
# File 'lib/solace/tokens.rb', line 76

def self.where(criteria = {})
  return all if criteria.empty?

  normalized = criteria.transform_keys(&:to_sym)

  all.select do |token|
    normalized.all? { |k, v| token.[k] == v }
  end
end