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.

]

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'] }

Raises:

Since:

  • 0.0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rpc_url = 'http://localhost:8899', commitment: 'confirmed', http_open_timeout: 30, http_read_timeout: 60) ⇒ 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

  • http_open_timeout (Integer) (defaults to: 30)

    The timeout for opening an HTTP connection

  • http_read_timeout (Integer) (defaults to: 60)

    The timeout for reading an HTTP response

Since:

  • 0.0.1



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/solace/connection.rb', line 52

def initialize(
  rpc_url = 'http://localhost:8899',
  commitment: 'confirmed',
  http_open_timeout: 30,
  http_read_timeout: 60
)
  # Initialize the RPC client
  @rpc_client = Utils::RPCClient.new(
    rpc_url,
    open_timeout: http_open_timeout,
    read_timeout: http_read_timeout
  )

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

Instance Attribute Details

#default_optionsObject (readonly)

Since:

  • 0.0.1



43
44
45
# File 'lib/solace/connection.rb', line 43

def default_options
  @default_options
end

#rpc_clientObject (readonly)

Since:

  • 0.0.1



39
40
41
# File 'lib/solace/connection.rb', line 39

def rpc_client
  @rpc_client
end

#rpc_urlObject (readonly)

The URL of the Solana RPC node



39
# File 'lib/solace/connection.rb', line 39

attr_reader :rpc_client

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



125
126
127
# File 'lib/solace/connection.rb', line 125

def (pubkey)
  @rpc_client.rpc_request('getAccountInfo', [pubkey, default_options]).dig('result', '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



133
134
135
# File 'lib/solace/connection.rb', line 133

def get_balance(pubkey)
  @rpc_client.rpc_request('getBalance', [pubkey, default_options]).dig('result', 'value')
end

#get_latest_blockhashString

Get the latest blockhash from the Solana node

Returns:

  • (String)

    The latest blockhash

Since:

  • 0.0.1



109
110
111
# File 'lib/solace/connection.rb', line 109

def get_latest_blockhash
  @rpc_client.rpc_request('getLatestBlockhash').dig('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



117
118
119
# File 'lib/solace/connection.rb', line 117

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

#get_signature_status(signature) ⇒ Object

Get the signature status

Parameters:

  • signature (String)

    The signature of the transaction

Returns:

  • (Object)

    The signature status

Since:

  • 0.0.1



167
168
169
# File 'lib/solace/connection.rb', line 167

def get_signature_status(signature)
  get_signature_statuses([signature])
end

#get_signature_statuses(signatures) ⇒ Object

Get the signature status

Parameters:

  • signatures (Array)

    The signatures of the transactions

Returns:

  • (Object)

    The signature status

Since:

  • 0.0.1



158
159
160
161
# File 'lib/solace/connection.rb', line 158

def get_signature_statuses(signatures)
  @rpc_client.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



141
142
143
# File 'lib/solace/connection.rb', line 141

def ()
  @rpc_client.rpc_request('getTokenAccountBalance', [, default_options]).dig('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



150
151
152
# File 'lib/solace/connection.rb', line 150

def get_transaction(signature, options = { maxSupportedTransactionVersion: 0 })
  @rpc_client.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



95
96
97
98
99
100
101
102
103
104
# File 'lib/solace/connection.rb', line 95

def request_airdrop(pubkey, lamports, options = {})
  @rpc_client.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:

Since:

  • 0.0.1



83
84
85
86
87
# File 'lib/solace/connection.rb', line 83

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



176
177
178
# File 'lib/solace/connection.rb', line 176

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

#wait_for_confirmed_signature(commitment = 'confirmed', timeout: 60, interval: 0.1) ⇒ String

Wait until the yielded signature reaches the desired commitment or timeout.

Parameters:

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

    One of “processed”, “confirmed”, “finalized”

  • timeout (Numeric) (defaults to: 60)

    seconds to wait before raising

  • interval (Numeric) (defaults to: 0.1)

    polling interval in seconds

Yield Returns:

  • (String, Hash)

    a signature string or a JSON-RPC hash with “result”

Returns:

  • (String)

    the signature when the commitment is reached

Raises:

Since:

  • 0.0.1



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/solace/connection.rb', line 188

def wait_for_confirmed_signature(
  commitment = 'confirmed',
  timeout: 60,
  interval: 0.1
)
  raise ArgumentError, 'Block required' unless block_given?

  signature = extract_signature_from(yield)
  deadline = monotonic_deadline(timeout)

  # Wait for confirmation
  until dealine_passed?(deadline)
    return signature if commitment_reached?(signature, commitment)

    sleep interval
  end

  raise Errors::ConfirmationTimeout.format(signature, commitment, timeout)
end