Class: Solace::Programs::AssociatedTokenAccount

Inherits:
Base
  • Object
show all
Defined in:
lib/solace/programs/associated_token_account.rb

Overview

A client for interacting with the SPL Token Program.

Instance Attribute Summary

Attributes inherited from Base

#connection, #program_id

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection:) ⇒ AssociatedTokenAccount

Initializes a new Associated Token Account client.

Parameters:



28
29
30
# File 'lib/solace/programs/associated_token_account.rb', line 28

def initialize(connection:)
  super(connection:, program_id: Solace::Constants::)
end

Class Method Details

.get_address(owner:, mint:) ⇒ String

Gets the address of an associated token account.

Parameters:

Returns:

  • (String)

    The address of the associated token account.



13
14
15
16
17
18
19
20
21
22
# File 'lib/solace/programs/associated_token_account.rb', line 13

def get_address(owner:, mint:)
  Solace::Utils::PDA.find_program_address(
    [
      owner.address,
      Solace::Constants::TOKEN_PROGRAM_ID,
      mint.address
    ],
    Solace::Constants::
  )
end

Instance Method Details

#create_associated_token_account(**options) ⇒ String

Creates a new associated token account.

Parameters:

  • options (Hash)

    Options for calling the prepare_create_associated_token_account method.

Returns:

  • (String)

    The signature of the transaction.



65
66
67
68
69
# File 'lib/solace/programs/associated_token_account.rb', line 65

def (**options)
  tx = (**options)

  @connection.send_transaction(tx.serialize)
end

#get_address(**options) ⇒ Object

Alias method for get_address

Parameters:

  • oprtions (Hash)

    A hash of options for the get_address class method



35
36
37
# File 'lib/solace/programs/associated_token_account.rb', line 35

def get_address(**options)
  self.class.get_address(**options)
end

#get_or_create_address(payer:, owner:, mint:, commitment: 'confirmed') ⇒ String

Gets the address of an associated token account, creating it if it doesn’t exist.

Parameters:

Returns:

  • (String)

    The address of the associated token account



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/solace/programs/associated_token_account.rb', line 45

def get_or_create_address(payer:, owner:, mint:, commitment: 'confirmed')
  ata_address, _ = get_address(owner:, mint:)
  
   = @connection.(ata_address)
  
  return ata_address if 

  response = (payer:, owner:, mint:)

  raise "Failed to create associated token account" unless response['result']

  @connection.wait_for_confirmed_signature(commitment) { response['result'] }
  
  ata_address
end

#prepare_create_associated_token_account(payer:, owner:, mint:) ⇒ Solace::Transaction

Prepares a new associated token account and returns the signed transaction.

Parameters:

Returns:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/solace/programs/associated_token_account.rb', line 77

def (
  payer:, 
  owner:, 
  mint:
)
  ata_address, _ = get_address(owner:, mint:)

  accounts = [
    payer.address,
    ata_address,
    owner.address,
    mint.address,
    Solace::Constants::SYSTEM_PROGRAM_ID,
    Solace::Constants::TOKEN_PROGRAM_ID,
    Solace::Constants::
  ]

  instruction = Solace::Instructions::AssociatedTokenAccount::CreateAssociatedTokenAccountInstruction.build(
    funder_index: 0,
    associated_token_account_index: 1,
    owner_index: 2,
    mint_index: 3,
    system_program_index: 4,
    token_program_index: 5,
    program_index: 6
  )

  message = Solace::Message.new(
    header: [1, 0, 4],
    accounts: accounts,
    recent_blockhash: @connection.get_latest_blockhash,
    instructions: [instruction]
  )

  tx = Solace::Transaction.new(message: message)
  tx.sign(payer)

  tx
end