Module: SolanaRuby::HttpMethods::TransactionMethods

Defined in:
lib/solana_ruby/http_methods/transaction_methods.rb

Overview

Transaction Related HTTP Methods

Constant Summary collapse

DEFAULT_COMMITMENT =
"finalized"
TIMEOUT =

seconds

60
RETRY_INTERVAL =

seconds

2
ENCODED_TRANSACTION_OPTIONS =
{ skipPreflight: false }.freeze
FINALIZED_OPTIONS =
{ commitment: "finalized" }.freeze

Instance Method Summary collapse

Instance Method Details

#confirm_transaction(signature, commitment = DEFAULT_COMMITMENT, timeout = TIMEOUT) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/solana_ruby/http_methods/transaction_methods.rb', line 19

def confirm_transaction(signature, commitment = DEFAULT_COMMITMENT, timeout = TIMEOUT)
  start_time = Time.now

  loop do
    # Fetch transaction status
    options = { searchTransactionHistory: true }
    status_info = get_signature_status(signature, options)

    # Check if the transaction is confirmed based on the commitment level
    if status_info && (status_info["confirmationStatus"] == commitment || status_info["confirmationStatus"] == "confirmed")
      return true
    end

    # Break the loop if timeout is reached
    if Time.now - start_time > timeout
      raise "Transaction #{signature} was not confirmed within #{timeout} seconds."
    end

    sleep(RETRY_INTERVAL)
  end
end

#get_transaction(signature, options = {}) ⇒ Object



41
42
43
44
45
# File 'lib/solana_ruby/http_methods/transaction_methods.rb', line 41

def get_transaction(signature, options = {})
  params = [signature, options]
  response = request("getTransaction", params)
  response["result"]
end

#get_transaction_count(options = FINALIZED_OPTIONS) ⇒ Object



47
48
49
50
# File 'lib/solana_ruby/http_methods/transaction_methods.rb', line 47

def get_transaction_count(options = FINALIZED_OPTIONS)
  result = request("getTransactionCount", [options])
  result["result"]
end

#get_transactions(signatures, options = FINALIZED_OPTIONS) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/solana_ruby/http_methods/transaction_methods.rb', line 52

def get_transactions(signatures, options = FINALIZED_OPTIONS)
  transactions = []
  signatures.each do |signature|
    transaction = get_transaction(signature, options)
    transactions << transaction
  end
  transactions
end

#request_airdrop(pubkey, lamports, options = FINALIZED_OPTIONS) ⇒ Object



61
62
63
64
65
# File 'lib/solana_ruby/http_methods/transaction_methods.rb', line 61

def request_airdrop(pubkey, lamports, options = FINALIZED_OPTIONS)
  params = [pubkey, lamports, options]
  response = request("requestAirdrop", params)
  response["result"]
end

#send_encoded_transaction(encoded_transaction, options = ENCODED_TRANSACTION_OPTIONS.merge(FINALIZED_OPTIONS)) ⇒ Object



73
74
75
# File 'lib/solana_ruby/http_methods/transaction_methods.rb', line 73

def send_encoded_transaction(encoded_transaction, options = ENCODED_TRANSACTION_OPTIONS.merge(FINALIZED_OPTIONS))
  send_transaction(encoded_transaction, options)
end

#send_raw_transaction(raw_transaction, options = ENCODED_TRANSACTION_OPTIONS.merge(FINALIZED_OPTIONS)) ⇒ Object



77
78
79
80
81
82
# File 'lib/solana_ruby/http_methods/transaction_methods.rb', line 77

def send_raw_transaction(raw_transaction, options = ENCODED_TRANSACTION_OPTIONS.merge(FINALIZED_OPTIONS))
  # Convert the raw hexadecimal transaction to base64 encoding
  base64_encoded_transaction = Base64.encode64(raw_transaction)

  send_transaction(base64_encoded_transaction, options)
end

#send_transaction(signed_transaction, options = {}) ⇒ Object



13
14
15
16
17
# File 'lib/solana_ruby/http_methods/transaction_methods.rb', line 13

def send_transaction(signed_transaction, options = {})
  params = [signed_transaction, options]
  result = request("sendTransaction", params)
  result["result"]
end

#simulate_transaction(transaction, options = { encoding: "base64" }) ⇒ Object



67
68
69
70
71
# File 'lib/solana_ruby/http_methods/transaction_methods.rb', line 67

def simulate_transaction(transaction, options = { encoding: "base64" })
  params = [transaction, options]
  response = request("simulateTransaction", params)
  response["result"]
end