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



48
49
50
# File 'lib/solace/programs/associated_token_account.rb', line 48

def initialize(connection:)
  super(connection: connection, program_id: Solace::Constants::)
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



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::
  )
end

Instance Method Details

#create_associated_token_account(**options) ⇒ String

Creates a new associated token account.

Parameters:

  • options (Hash)

    Options for calling the prepare_create_associated_token_account method.

Returns:

  • (String)

    The signature of the transaction.

Since:

  • 0.0.2



87
88
89
90
91
92
93
# File 'lib/solace/programs/associated_token_account.rb', line 87

def (**options)
  tx = (**options)

  tx.sign(options[:payer])

  @connection.send_transaction(tx.serialize)
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



56
57
58
# File 'lib/solace/programs/associated_token_account.rb', line 56

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

Parameters:

Returns:

  • (String)

    The address of the associated token account

Since:

  • 0.0.2



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)

   = @connection.(ata_address)

  return ata_address unless .nil?

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

Parameters:

Returns:

Since:

  • 0.0.2



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