Class: Solrengine::Tokens::TokenMetadataService

Inherits:
Object
  • Object
show all
Defined in:
lib/solrengine/tokens/token_metadata_service.rb

Instance Method Summary collapse

Constructor Details

#initializeTokenMetadataService

Returns a new instance of TokenMetadataService.



4
5
6
# File 'lib/solrengine/tokens/token_metadata_service.rb', line 4

def initialize
  @client = Solrengine::Rpc.client
end

Instance Method Details

#token_balances_for(wallet_address) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/solrengine/tokens/token_metadata_service.rb', line 8

def token_balances_for(wallet_address)
  sol_balance = @client.get_balance(wallet_address)
  accounts = @client.get_token_accounts(wallet_address)

  all_mints = [ SOL_MINT ] + accounts.map { |a| a[:mint] }

  token_records = Solrengine::Tokens.token_class.find_or_fetch_many(all_mints)

  prices = Solrengine::Rpc.configuration.mainnet? ? JupiterClient.fetch_prices(all_mints) : {}

  tokens = []

  if sol_balance
    sol_token = token_records[SOL_MINT]
    sol_price = prices[SOL_MINT]
    entry = build_entry(
      token: sol_token,
      ui_amount: sol_balance,
      ui_amount_string: format_amount(sol_balance),
      usd_price: sol_price
    )
    entry[:name] = "Solana"
    entry[:symbol] = "SOL"
    tokens << entry
  end

  accounts.each do ||
    token = token_records[[:mint]]
    price = prices[[:mint]]
    tokens << build_entry(
      token: token,
      ui_amount: [:ui_amount],
      ui_amount_string: [:ui_amount_string],
      usd_price: price
    )
  end

  tokens.sort_by { |t| -(t[:usd_value] || -1) }
end