Class: Solrengine::Rpc::Client

Inherits:
Object
  • Object
show all
Includes:
SslHttp
Defined in:
lib/solrengine/rpc/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(rpc_url: nil) ⇒ Client

Returns a new instance of Client.



8
9
10
# File 'lib/solrengine/rpc/client.rb', line 8

def initialize(rpc_url: nil)
  @rpc_url = rpc_url || Solrengine::Rpc.configuration.rpc_url
end

Instance Method Details

#get_balance(wallet_address, commitment: "confirmed") ⇒ Object



12
13
14
15
16
17
18
# File 'lib/solrengine/rpc/client.rb', line 12

def get_balance(wallet_address, commitment: "confirmed")
  result = rpc_request("getBalance", [ wallet_address, { "commitment" => commitment } ])
  lamports = result.dig("result", "value")
  return nil unless lamports

  lamports.to_f / 1_000_000_000
end

#get_latest_blockhash(commitment: "finalized") ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/solrengine/rpc/client.rb', line 67

def get_latest_blockhash(commitment: "finalized")
  result = rpc_request("getLatestBlockhash", [ { "commitment" => commitment } ])
  value = result.dig("result", "value")
  return nil unless value

  {
    blockhash: value["blockhash"],
    last_valid_block_height: value["lastValidBlockHeight"]
  }
end

#get_recent_signatures(wallet_address, limit: 10) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/solrengine/rpc/client.rb', line 47

def get_recent_signatures(wallet_address, limit: 10)
  result = rpc_request("getSignaturesForAddress", [
    wallet_address,
    { "limit" => limit }
  ])

  signatures = result.dig("result") || []

  signatures.map do |sig|
    {
      signature: sig["signature"],
      slot: sig["slot"],
      block_time: sig["blockTime"] ? Time.at(sig["blockTime"]) : nil,
      error: sig["err"],
      memo: sig["memo"],
      confirmation_status: sig["confirmationStatus"]
    }
  end
end

#get_signature_status(signature) ⇒ Object



78
79
80
81
# File 'lib/solrengine/rpc/client.rb', line 78

def get_signature_status(signature)
  result = rpc_request("getSignatureStatuses", [ [ signature ] ])
  result.dig("result", "value", 0)
end

#get_token_accounts(wallet_address, program_id: "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA") ⇒ Object



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
# File 'lib/solrengine/rpc/client.rb', line 20

def get_token_accounts(wallet_address, program_id: "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
  result = rpc_request("getTokenAccountsByOwner", [
    wallet_address,
    { "programId" => program_id },
    { "encoding" => "jsonParsed" }
  ])

  accounts = result.dig("result", "value") || []

  accounts.filter_map do ||
    info = .dig("account", "data", "parsed", "info")
    next unless info

    token_amount = info["tokenAmount"]
    amount = token_amount["uiAmount"].to_f
    next if amount.zero?

    {
      mint: info["mint"],
      balance: token_amount["amount"],
      decimals: token_amount["decimals"],
      ui_amount: amount,
      ui_amount_string: token_amount["uiAmountString"]
    }
  end
end

#get_transaction(signature) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/solrengine/rpc/client.rb', line 83

def get_transaction(signature)
  result = rpc_request("getTransaction", [
    signature,
    { "encoding" => "jsonParsed", "maxSupportedTransactionVersion" => 0 }
  ])

  result["result"]
end

#request(method, params = []) ⇒ Object

Generic RPC call for methods not covered above



93
94
95
# File 'lib/solrengine/rpc/client.rb', line 93

def request(method, params = [])
  rpc_request(method, params)
end