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:) ⇒ String
Gets the address of an associated token account.
Instance Method Summary collapse
-
#compose_create_associated_token_account(funder:, owner:, mint:) ⇒ Transaction
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:) ⇒ 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.
58 59 60 |
# File 'lib/solace/programs/associated_token_account.rb', line 58 def initialize(connection:) super(connection: connection, program_id: Solace::Constants::ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID) end |
Class Method Details
.get_address(owner:, mint:) ⇒ String
Gets the address of an associated token account.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/solace/programs/associated_token_account.rb', line 43 def get_address(owner:, mint:) Solace::Utils::PDA.find_program_address( [ owner.to_s, Solace::Constants::TOKEN_PROGRAM_ID, mint.to_s ], Solace::Constants::ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID ) end |
Instance Method Details
#compose_create_associated_token_account(funder:, owner:, mint:) ⇒ Transaction
Prepares a new associated token account and returns the signed transaction.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/solace/programs/associated_token_account.rb', line 140 def compose_create_associated_token_account( funder:, owner:, mint: ) ata_address, = get_address(owner: owner, mint: mint) ix = Solace::Composers::AssociatedTokenAccountProgramCreateAccountComposer.new( mint: mint, owner: owner, funder: funder, ata_address: ata_address ) 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.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/solace/programs/associated_token_account.rb', line 108 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
66 67 68 |
# File 'lib/solace/programs/associated_token_account.rb', line 66 def get_address(**) self.class.get_address(**) end |
#get_or_create_address(payer:, funder:, owner:, mint:) ⇒ String
Gets the address of an associated token account, creating it if it doesn’t exist.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/solace/programs/associated_token_account.rb', line 77 def get_or_create_address( payer:, funder:, owner:, mint: ) ata_address, = get_address(owner: owner, mint: mint) 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 ) connection.wait_for_confirmed_signature { tx.signature } ata_address end |