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
-
#create_associated_token_account(**options) ⇒ String
Creates a new associated token account.
-
#get_address(**options) ⇒ Array<String, Integer>
Alias method for get_address.
-
#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.
-
#initialize(connection:) ⇒ AssociatedTokenAccount
constructor
Initializes a new Associated Token Account client.
-
#prepare_create_associated_token_account(payer:, owner:, mint:) ⇒ Solace::Transaction
Prepares a new associated token account and returns the signed transaction.
Constructor Details
#initialize(connection:) ⇒ AssociatedTokenAccount
Initializes a new Associated Token Account client.
48 49 50 |
# File 'lib/solace/programs/associated_token_account.rb', line 48 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.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/solace/programs/associated_token_account.rb', line 33 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
#create_associated_token_account(**options) ⇒ String
Creates a new associated token account.
87 88 89 90 91 92 93 |
# File 'lib/solace/programs/associated_token_account.rb', line 87 def create_associated_token_account(**) tx = prepare_create_associated_token_account(**) tx.sign([:payer]) @connection.send_transaction(tx.serialize) end |
#get_address(**options) ⇒ Array<String, Integer>
Alias method for get_address
56 57 58 |
# File 'lib/solace/programs/associated_token_account.rb', line 56 def get_address(**) self.class.get_address(**) 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.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/solace/programs/associated_token_account.rb', line 67 def get_or_create_address(payer:, owner:, mint:, commitment: 'confirmed') ata_address, _bump = get_address(owner: owner, mint: mint) account_balance = @connection.get_account_info(ata_address) return ata_address unless account_balance.nil? response = create_associated_token_account(payer: payer, owner: owner, mint: mint) raise 'Failed to create associated token account' unless response['result'] @connection.wait_for_confirmed_signature(commitment) { response } ata_address end |
#prepare_create_associated_token_account(payer:, owner:, mint:) ⇒ Solace::Transaction
Prepares a new associated token account and returns the signed transaction.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/solace/programs/associated_token_account.rb', line 102 def prepare_create_associated_token_account( payer:, owner:, mint: ) ata_address, = get_address(owner: owner, mint: mint) TransactionComposer.new(connection: connection).try do |tx_composer| tx_composer.set_fee_payer(payer) tx_composer.add_instruction( Solace::Composers::AssociatedTokenAccountProgramCreateAccountComposer.new( mint: mint, owner: owner, funder: payer, ata_address: ata_address ) ) tx_composer.compose_transaction end end |