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



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.

Parameters:

Returns:

  • (String)

    The address of the associated token account.

Since:

  • 0.0.2



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.

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.

Returns:

Since:

  • 0.0.2



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

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



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



66
67
68
# File 'lib/solace/programs/associated_token_account.rb', line 66

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

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.

Returns:

  • (String)

    The address of the associated token account

Since:

  • 0.0.2



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)

   = connection.(ata_address)

  return ata_address unless .nil?

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

  connection.wait_for_confirmed_signature { tx.signature }

  ata_address
end