Class: Solace::Programs::AssociatedTokenAccount
- 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
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) ⇒ Object
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.
28 29 30 |
# File 'lib/solace/programs/associated_token_account.rb', line 28 def initialize(connection:) super(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.
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::ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID ) end |
Instance Method Details
#create_associated_token_account(**options) ⇒ String
Creates a new associated token account.
65 66 67 68 69 |
# File 'lib/solace/programs/associated_token_account.rb', line 65 def create_associated_token_account(**) tx = prepare_create_associated_token_account(**) @connection.send_transaction(tx.serialize) end |
#get_address(**options) ⇒ Object
Alias method for get_address
35 36 37 |
# File 'lib/solace/programs/associated_token_account.rb', line 35 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.
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:) account_info = @connection.get_account_info(ata_address) return ata_address if account_info response = create_associated_token_account(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.
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 prepare_create_associated_token_account( 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::ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID ] 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 ) = Solace::Message.new( header: [1, 0, 4], accounts: accounts, recent_blockhash: @connection.get_latest_blockhash, instructions: [instruction] ) tx = Solace::Transaction.new(message: ) tx.sign(payer) tx end |