Class: Solace::Programs::AssociatedTokenAccount

Inherits:
Base
  • Object
show all
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.

Examples:

Create an associated token account

# Initialize the program with a connection
program = Solace::Programs::AssociatedTokenAccount.new(connection: connection)

# Create an associated token account
result = program.(
  payer: payer,
  funder: funder,
  owner: owner,
  mint: mint
)

# Wait for the transaction to be finalized
connection.wait_for_confirmed_signature('finalized') { result['result'] }

Since:

  • 0.0.2

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:

Since:

  • 0.0.2



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.

Parameters:

  • owner (Keypair, PublicKey)

    The keypair of the owner.

  • mint (Keypair, PublicKey)

    The keypair of the mint.

  • token_program_id (String) (defaults to: Solace::Constants::TOKEN_PROGRAM_ID)

    The token program that owns the mint (defaults to legacy SPL Token).

Returns:

  • (Array<String, Integer>)

    The associated token account address and bump seed.

Since:

  • 0.0.2



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.

Parameters:

  • owner (#to_s, PublicKey)

    The keypair of the owner.

  • mint (#to_s, PublicKey)

    The keypair of the mint.

  • funder (#to_s, PublicKey)

    The keypair that will pay for rent of the new associated token account.

  • token_program_id (String) (defaults to: Solace::Constants::TOKEN_PROGRAM_ID)

    The token program that owns the mint (defaults to legacy SPL Token).

Returns:

Since:

  • 0.0.2



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 (
  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.

Parameters:

  • payer (#to_s, Keypair)

    The keypair that will pay for fees and rent.

  • sign (Boolean) (defaults to: true)

    Whether to sign the transaction before sending it.

  • execute (Boolean) (defaults to: true)

    Whether to send the transaction to the cluster.

  • composer_opts (Hash)

    Options for calling the compose_create_associated_token_account method.

Yields:

  • (composer)

Returns:

Since:

  • 0.0.2



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 (
  payer:,
  sign: true,
  execute: true,
  **composer_opts
)
  composer = (**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

Parameters:

  • options (Hash)

    A hash of options for the get_address class method

Returns:

  • (Array<String, Integer>)

    The address of the associated token account and the bump seed

Since:

  • 0.0.2



77
78
79
# File 'lib/solace/programs/associated_token_account.rb', line 77

def get_address(**options)
  self.class.get_address(**options)
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.

Parameters:

  • payer (Keypair)

    The keypair that will pay for fees and rent.

  • funder (Keypair)

    The keypair that will pay for rent of the new associated token account.

  • owner (Keypair, PublicKey)

    The keypair of the owner.

  • mint (Keypair, PublicKey)

    The keypair of the mint.

  • token_program_id (String) (defaults to: Solace::Constants::TOKEN_PROGRAM_ID)

    The token program that owns the mint (defaults to legacy SPL Token).

Returns:

  • (String)

    The address of the associated token account

Since:

  • 0.0.2



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)

   = connection.(ata_address)

  return ata_address unless .nil?

  tx = (
    payer: payer,
    funder: funder,
    owner: owner,
    mint: mint,
    token_program_id: token_program_id
  )

  connection.wait_for_confirmed_signature { tx.signature }

  ata_address
end