Class: Solace::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/solace/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rpc_url = 'http://localhost:8899', commitment: 'confirmed') ⇒ Solace::Connection

Initialize the connection with a default or custom RPC URL

Parameters:

  • rpc_url (String) (defaults to: 'http://localhost:8899')

    The URL of the Solana RPC node



25
26
27
28
29
30
31
32
33
34
# File 'lib/solace/connection.rb', line 25

def initialize(rpc_url = 'http://localhost:8899', commitment: 'confirmed')
  @request_id = nil
  @rpc_url = rpc_url

  # Set default options
  @default_options = { 
    commitment: commitment,
    encoding: 'base64'
  }
end

Instance Attribute Details

#default_optionsHash (readonly)

Returns The default options for RPC requests.

Returns:

  • (Hash)

    The default options for RPC requests



19
20
21
# File 'lib/solace/connection.rb', line 19

def default_options
  @default_options
end

#rpc_urlString (readonly)

Returns The URL of the Solana RPC node.

Returns:

  • (String)

    The URL of the Solana RPC node



13
14
15
# File 'lib/solace/connection.rb', line 13

def rpc_url
  @rpc_url
end

Instance Method Details

#get_account_info(pubkey) ⇒ Object

Get the account information from the Solana node

Parameters:

  • pubkey (String)

    The public key of the account

Returns:

  • (Object)

    The account information



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/solace/connection.rb', line 105

def (pubkey)
  response = rpc_request(
    'getAccountInfo',
    [
      pubkey,
      default_options
    ]
  )['result']

  return if response.nil?

  response['value']
end

#get_balance(pubkey) ⇒ Integer

Get the balance of a specific account

Parameters:

  • pubkey (String)

    The public key of the account

Returns:

  • (Integer)

    The balance of the account



123
124
125
126
127
128
129
130
131
# File 'lib/solace/connection.rb', line 123

def get_balance(pubkey)
  rpc_request(
    'getBalance',
    [
      pubkey,
      default_options
    ]
  )['result']['value']
end

#get_latest_blockhashString

Get the latest blockhash from the Solana node

Returns:

  • (String)

    The latest blockhash



89
90
91
# File 'lib/solace/connection.rb', line 89

def get_latest_blockhash
  rpc_request('getLatestBlockhash')['result']['value']['blockhash']
end

#get_minimum_lamports_for_rent_exemption(space) ⇒ Integer

Get the minimum required lamports for rent exemption

Parameters:

  • space (Integer)

    Number of bytes to allocate for the account

Returns:

  • (Integer)

    The minimum required lamports



97
98
99
# File 'lib/solace/connection.rb', line 97

def get_minimum_lamports_for_rent_exemption(space)
  rpc_request('getMinimumBalanceForRentExemption', [space])['result']
end

#get_signature_status(signatures) ⇒ Object

Get the signature status

Parameters:

  • signatures (Array)

    The signatures of the transactions

Returns:

  • (Object)

    The signature status



165
166
167
168
169
170
171
172
173
# File 'lib/solace/connection.rb', line 165

def get_signature_status(signatures)
  rpc_request(
    'getSignatureStatuses',
    [
      signatures,
      default_options.merge({ 'searchTransactionHistory' => true })
    ]
  )['result']
end

#get_token_account_balance(token_account) ⇒ Hash

Get the balance of a token account

Parameters:

  • token_account (String)

    The public key of the token account

Returns:

  • (Hash)

    Token account balance information with amount and decimals



137
138
139
140
141
142
143
144
145
# File 'lib/solace/connection.rb', line 137

def ()
  rpc_request(
    'getTokenAccountBalance',
    [
      ,
      default_options
    ]
  )['result']['value']
end

#get_transaction(signature, options = { maxSupportedTransactionVersion: 0 }) ⇒ Solace::Transaction

Get the transaction by signature

Parameters:

  • signature (String)

    The signature of the transaction

Returns:



151
152
153
154
155
156
157
158
159
# File 'lib/solace/connection.rb', line 151

def get_transaction(signature, options = { maxSupportedTransactionVersion: 0 })
  rpc_request(
    'getTransaction',
    [
      signature,
      default_options.merge(options)
    ]
  )['result']
end

#request_airdrop(pubkey, lamports, options = {}) ⇒ String

Request an airdrop of lamports to a given address

Parameters:

  • pubkey (String)

    The public key of the account to receive the airdrop

  • lamports (Integer)

    Amount of lamports to airdrop

Returns:

  • (String)

    The transaction signature of the airdrop



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

def request_airdrop(pubkey, lamports, options = {})
  rpc_request(
    'requestAirdrop',
    [
      pubkey,
      lamports,
      default_options.merge(options)
    ]
  )
end

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

Make an RPC request to the Solana node

Parameters:

  • method (String)

    The RPC method to call

  • params (Array) (defaults to: [])

    Parameters for the RPC method

Returns:

  • (Object)

    Result of the RPC call



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/solace/connection.rb', line 41

def rpc_request(method, params = [])
  uri = URI(rpc_url)

  req = Net::HTTP::Post.new(uri)
  req['Accept'] = 'application/json'
  req['Content-Type'] = 'application/json'

  @request_id = SecureRandom.uuid

  req.body = {
    jsonrpc: '2.0',
    id: @request_id,
    method: method,
    params: params
  }.to_json

  res = Net::HTTP.start(
    uri.hostname,
    uri.port,
    use_ssl: uri.scheme == 'https'
  ) do |http|
    http.request(req)
  end

  raise "RPC error: #{res.body}" unless res.is_a?(Net::HTTPSuccess)

  JSON.parse(res.body)
end

#send_transaction(transaction, options = {}) ⇒ String

Send a transaction to the Solana node

Parameters:

Returns:

  • (String)

    The signature of the transaction



179
180
181
182
183
184
185
186
187
# File 'lib/solace/connection.rb', line 179

def send_transaction(transaction, options = {})
  rpc_request(
    'sendTransaction',
    [
      transaction,
      default_options.merge(options)
    ]
  )
end

#wait_for_confirmed_signature(commitment = 'confirmed') ⇒ Boolean

Wait for a confirmed signature from the transaction

Parameters:

  • commitment (String) (defaults to: 'confirmed')

    The commitment level to wait for

Returns:

  • (Boolean)

    True if the transaction was confirmed, false otherwise

Raises:

  • (ArgumentError)


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/solace/connection.rb', line 193

def wait_for_confirmed_signature(commitment = 'confirmed')
  raise ArgumentError, 'Block required' unless block_given?

  # Get the signature from the block
  signature = yield

  interval = 0.1

  # Wait for confirmation
  loop do
    status = get_signature_status([signature]).dig('value', 0)
    
    break if status && status['confirmationStatus'] == commitment
    
    sleep interval
  end
  
  signature
end