Class: Solace::Programs::AssociatedTokenAccount
- Defined in:
- lib/solace/programs/associated_token_account.rb
Overview
Client for interacting with the Associated Token Account Program.
This client provides methods for interacting with the Associated Token Account Program. It is a wrapper around the SPL Token Program and provides a more convenient interface for creating and managing associated token accounts.
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
-
.get_address(owner:, mint:, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID) ⇒ Array<String, Integer>
Gets the address of an associated token account.
Instance Method Summary collapse
-
#compose_create_associated_token_account(funder:, owner:, mint:, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID) ⇒ TransactionComposer
Prepares a new associated token account and returns the signed transaction.
-
#create_associated_token_account(payer:, sign: true, execute: true, **composer_opts) {|composer| ... } ⇒ Transaction
Creates a new associated token account.
-
#get_address(**options) ⇒ Array<String, Integer>
Alias method for get_address.
-
#get_or_create_address(payer:, funder:, owner:, mint:, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID) ⇒ String
Gets the address of an associated token account, creating it if it doesn’t exist.
-
#initialize(connection:) ⇒ AssociatedTokenAccount
constructor
Initializes a new Associated Token Account client.
Constructor Details
#initialize(connection:) ⇒ AssociatedTokenAccount
Initializes a new Associated Token Account client.
69 70 71 |
# File 'lib/solace/programs/associated_token_account.rb', line 69 def initialize(connection:) super(connection: connection, program_id: Solace::Constants::ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID) end |
Class Method Details
.get_address(owner:, mint:, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID) ⇒ Array<String, Integer>
Gets the address of an associated token account.
The token program ID is part of the ATA derivation seed. For mints owned by the legacy SPL Token program this is Constants::TOKEN_PROGRAM_ID (the default). For mints owned by the Token-2022 program (e.g. PYUSD on Solana), pass token_program_id: Solace::Constants::TOKEN_2022_PROGRAM_ID to derive the correct address. Deriving with the wrong program ID returns an address that does not exist on chain.
Use Connection#get_mint_program_id to discover which token program owns a given mint at runtime.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/solace/programs/associated_token_account.rb', line 54 def get_address(owner:, mint:, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID) Solace::Utils::PDA.find_program_address( [ owner.to_s, token_program_id.to_s, mint.to_s ], Solace::Constants::ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID ) end |
Instance Method Details
#compose_create_associated_token_account(funder:, owner:, mint:, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID) ⇒ TransactionComposer
Prepares a new associated token account and returns the signed transaction.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/solace/programs/associated_token_account.rb', line 155 def compose_create_associated_token_account( funder:, owner:, mint:, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID ) ata_address, = get_address(owner: owner, mint: mint, token_program_id: token_program_id) ix = Solace::Composers::AssociatedTokenAccountProgramCreateAccountComposer.new( mint: mint, owner: owner, funder: funder, ata_address: ata_address, token_program_id: token_program_id ) TransactionComposer .new(connection: connection) .add_instruction(ix) end |
#create_associated_token_account(payer:, sign: true, execute: true, **composer_opts) {|composer| ... } ⇒ Transaction
Creates a new associated token account.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/solace/programs/associated_token_account.rb', line 122 def create_associated_token_account( payer:, sign: true, execute: true, **composer_opts ) composer = compose_create_associated_token_account(**composer_opts) yield composer if block_given? tx = composer .set_fee_payer(payer) .compose_transaction if sign tx.sign( payer, composer_opts[:funder] ) connection.send_transaction(tx.serialize) if execute end tx end |
#get_address(**options) ⇒ Array<String, Integer>
Alias method for get_address
77 78 79 |
# File 'lib/solace/programs/associated_token_account.rb', line 77 def get_address(**) self.class.get_address(**) end |
#get_or_create_address(payer:, funder:, owner:, mint:, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID) ⇒ String
Gets the address of an associated token account, creating it if it doesn’t exist.
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 |
# File 'lib/solace/programs/associated_token_account.rb', line 89 def get_or_create_address( payer:, funder:, owner:, mint:, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID ) ata_address, = get_address(owner: owner, mint: mint, token_program_id: token_program_id) account_balance = connection.get_account_info(ata_address) return ata_address unless account_balance.nil? tx = create_associated_token_account( payer: payer, funder: funder, owner: owner, mint: mint, token_program_id: token_program_id ) connection.wait_for_confirmed_signature { tx.signature } ata_address end |