Class: Solace::Connection

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

Overview

Connection to a Solana RPC node

This class provides methods for sending JSON-RPC requests to a Solana RPC node and parsing responses. It includes methods for sending transactions, getting account information, and getting blockhashes.

rubocop:disable Metrics/ClassLength

Examples:

# Initialize the connection
connection = Solace::Connection.new('http://localhost:8899', commitment: 'confirmed')

# Get account information
connection.(.address)

# Request an airdrop
result = connection.request_airdrop(.address, 1000000)

# Wait for the transaction to be finalized
connection.wait_for_confirmed_signature('finalized') { result['result'] }

Since:

  • 0.0.1

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

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

    The commitment level for RPC requests

Since:

  • 0.0.1



43
44
45
46
47
48
49
50
51
52
# File 'lib/solace/connection.rb', line 43

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_optionsObject (readonly)

Since:

  • 0.0.1



36
37
38
# File 'lib/solace/connection.rb', line 36

def default_options
  @default_options
end

#rpc_urlObject (readonly)

Since:

  • 0.0.1



32
33
34
# File 'lib/solace/connection.rb', line 32

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

Since:

  • 0.0.1



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/solace/connection.rb', line 102

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

Since:

  • 0.0.1



120
121
122
123
124
125
126
127
128
# File 'lib/solace/connection.rb', line 120

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

Since:

  • 0.0.1



86
87
88
# File 'lib/solace/connection.rb', line 86

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

Since:

  • 0.0.1



94
95
96
# File 'lib/solace/connection.rb', line 94

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

Since:

  • 0.0.1



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

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

Since:

  • 0.0.1



134
135
136
137
138
139
140
141
142
# File 'lib/solace/connection.rb', line 134

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

  • options (Hash{Symbol => Object}) (defaults to: { maxSupportedTransactionVersion: 0 })

Returns:

Since:

  • 0.0.1



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

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

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

    The options for the request

Returns:

  • (String)

    The transaction signature of the airdrop

Since:

  • 0.0.1



72
73
74
75
76
77
78
79
80
81
# File 'lib/solace/connection.rb', line 72

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

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

Sends a JSON-RPC request to the configured Solana RPC server.

Parameters:

  • method (String)

    the JSON-RPC method name

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

    the parameters for the RPC method

Returns:

  • (Hash)

    the parsed JSON response

Raises:

  • (RuntimeError)

    if the response is not successful

Since:

  • 0.0.1



60
61
62
63
64
# File 'lib/solace/connection.rb', line 60

def rpc_request(method, params = [])
  request = build_rpc_request(method, params)
  response = perform_http_request(request)
  handle_rpc_response(response)
end

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

Send a transaction to the Solana node

Parameters:

  • transaction (Solace::Transaction)

    The transaction to send

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

Returns:

  • (String)

    The signature of the transaction

Since:

  • 0.0.1



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

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)

Since:

  • 0.0.1



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

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