Class: Solace::Programs::SplToken

Inherits:
Base
  • Object
show all
Defined in:
lib/solace/programs/spl_token.rb

Overview

Client for interacting with the SPL Token Program.

This client provides methods for interacting with the SPL Token Program. It is a wrapper around the SPL Token Program and provides a more convenient interface for creating and managing SPL Token mints and accounts.

Examples:

Create an SPL Token mint

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

# Create an SPL Token mint
result = program.create_mint(
  payer: payer,
  funder: funder,
  decimals: 6,
  mint_keypair: mint_keypair,
  mint_authority: mint_authority,
  freeze_authority: freeze_authority
)

# 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

Instance Method Summary collapse

Constructor Details

#initialize(connection:) ⇒ SplToken

Initializes a new SPL Token client.

Parameters:

Since:

  • 0.0.2



33
34
35
# File 'lib/solace/programs/spl_token.rb', line 33

def initialize(connection:)
  super(connection: connection, program_id: Solace::Constants::TOKEN_PROGRAM_ID)
end

Instance Method Details

#compose_create_mint(funder:, decimals:, mint_authority:, freeze_authority: nil, mint_account: Solace::Keypair.generate) ⇒ TransactionComposer

Prepares a new SPL Token mint transaction.

rubocop:disable Metrics/MethodLength

Parameters:

  • funder (#to_s, PublicKey)

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

  • decimals (Integer)

    The number of decimal places for the token.

  • mint_authority (#to_s, PublicKey)

    The base58 public key for the mint authority.

  • freeze_authority (#to_s, PublicKey) (defaults to: nil)

    (Optional) The base58 public key for the freeze authority.

  • mint_account (#to_s, PublicKey) (defaults to: Solace::Keypair.generate)

    (Optional) The keypair for the new mint.

Returns:

Since:

  • 0.0.2



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
# File 'lib/solace/programs/spl_token.rb', line 80

def compose_create_mint(
  funder:,
  decimals:,
  mint_authority:,
  freeze_authority: nil,
  mint_account: Solace::Keypair.generate
)
  # Mint accounts need 82 bytes of space, and we need to fund it with enough lamports to be rent-exempt
  rent_lamports = connection.get_minimum_lamports_for_rent_exemption(82)

  # Build the account for the mint
   = Composers::SystemProgramCreateAccountComposer.new(
    from: funder,
    new_account: ,
    owner: program_id,
    lamports: rent_lamports,
    space: 82
  )

  # Build the initialize mint composer
  initialize_mint_ix = Composers::SplTokenProgramInitializeMintComposer.new(
    decimals: decimals,
    mint_account: ,
    mint_authority: mint_authority,
    freeze_authority: freeze_authority
  )

  TransactionComposer
    .new(connection: @connection)
    .add_instruction()
    .add_instruction(initialize_mint_ix)
end

#compose_mint_to(mint:, amount:, destination:, mint_authority:) ⇒ TransactionComposer

Prepares a mint to instruction and returns the signed transaction.

Parameters:

  • amount (Integer)

    The amount of tokens to mint.

  • mint (#to_s, PublicKey)

    The mint of the token.

  • destination (#to_s, PublicKey)

    The destination of the token.

  • mint_authority (#to_s, PublicKey)

    The mint authority of the token.

  • ensure_account (Boolean)

Returns:

Since:

  • 0.0.2



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/solace/programs/spl_token.rb', line 156

def compose_mint_to(
  mint:,
  amount:,
  destination:,
  mint_authority:
)
  ix = Composers::SplTokenProgramMintToComposer.new(
    amount: amount,
    mint: mint,
    destination: destination,
    mint_authority: mint_authority
  )

  TransactionComposer
    .new(connection: connection)
    .add_instruction(ix)
end

#compose_transfer(amount:, source:, destination:, owner:) ⇒ TransactionComposer

Prepares a transfer instruction and returns the signed transaction.

Parameters:

  • source (#to_s, PublicKey)

    The source token account address.

  • destination (#to_s, PublicKey)

    The destination token account address.

  • amount (Integer)

    The number of tokens to transfer.

  • owner (#to_s, PublicKey)

    The keypair of the owner of the source account.

Returns:

Since:

  • 0.0.2



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/solace/programs/spl_token.rb', line 214

def compose_transfer(
  amount:,
  source:,
  destination:,
  owner:
)
  ix = Composers::SplTokenProgramTransferComposer.new(
    amount: amount,
    owner: owner,
    source: source,
    destination: destination
  )

  TransactionComposer
    .new(connection: connection)
    .add_instruction(ix)
end

#compose_transfer_checked(to:, from:, mint:, authority:, amount:, decimals:) ⇒ TransactionComposer

Prepares a transfer checked instruction and returns the signed transaction.

Parameters:

  • amount (Integer)

    The number of tokens to transfer.

  • decimals (Integer)

    The number of decimals for the token.

  • from (#to_s, PublicKey)

    The source token account address.

  • to (#to_s, PublicKey)

    The destination token account address.

  • mint (#to_s, PublicKey)

    The mint address

  • authority (#to_s, PublicKey)

    The keypair of the owner of the source account.

Returns:

Since:

  • 0.0.2



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/solace/programs/spl_token.rb', line 274

def compose_transfer_checked(
  to:,
  from:,
  mint:,
  authority:,
  amount:,
  decimals:
)
  ix = Composers::SplTokenProgramTransferCheckedComposer.new(
    to: to,
    from: from,
    mint: mint,
    authority: authority,
    amount: amount,
    decimals: decimals
  )

  TransactionComposer
    .new(connection: connection)
    .add_instruction(ix)
end

#create_mint(payer:, sign: true, execute: true, **composer_opts) {|composer| ... } ⇒ String

Creates a new SPL Token mint.

Parameters:

  • payer (#to_s, PublicKey)

    The keypair that will pay for fees and rent.

  • sign (Boolean) (defaults to: true)

    Whether to sign the transaction.

  • execute (Boolean) (defaults to: true)

    Whether to execute the transaction.

  • composer_opts (Hash)

    Options for calling the compose_create_mint method.

Yields:

  • (composer)

Returns:

  • (String)

    The signature of the transaction.

Since:

  • 0.0.2



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/solace/programs/spl_token.rb', line 44

def create_mint(
  payer:,
  sign: true,
  execute: true,
  **composer_opts
)
  composer = compose_create_mint(**composer_opts)

  yield composer if block_given?

  tx = composer
       .set_fee_payer(payer)
       .compose_transaction

  if sign
    tx.sign(
      payer,
      composer_opts[:funder],
      composer_opts[:mint_account]
    )

    connection.send_transaction(tx.serialize) if execute
  end

  tx
end

#mint_to(payer:, sign: true, execute: true, **composer_opts) {|composer| ... } ⇒ String

Mint tokens to a token account

Parameters:

  • payer (#to_s, PublicKey)

    The keypair that will pay for fees and rent.

  • sign (Boolean) (defaults to: true)

    Whether to sign the transaction.

  • execute (Boolean) (defaults to: true)

    Whether to execute the transaction.

  • composer_opts (Hash)

    Options for calling the compose_mint_to method.

Yields:

  • (composer)

Returns:

  • (String)

    The signature of the transaction.

Since:

  • 0.0.2



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/solace/programs/spl_token.rb', line 121

def mint_to(
  payer:,
  sign: true,
  execute: true,
  **composer_opts
)
  composer = compose_mint_to(**composer_opts)

  yield composer if block_given?

  tx = composer
       .set_fee_payer(payer)
       .compose_transaction

  if sign
    tx.sign(
      payer,
      composer_opts[:mint_authority]
    )

    connection.send_transaction(tx.serialize) if execute
  end

  tx
end

#transfer(payer:, sign: true, execute: true, **composer_opts) {|composer| ... } ⇒ String

Transfers tokens from one account to another

Parameters:

  • payer (#to_s, PublicKey)

    The keypair that will pay for fees and rent.

  • sign (Boolean) (defaults to: true)

    Whether to sign the transaction.

  • execute (Boolean) (defaults to: true)

    Whether to execute the transaction.

  • composer_opts (Hash)

    Options for calling the compose_transfer method.

Yields:

  • (composer)

Returns:

  • (String)

    The signature of the transaction.

Since:

  • 0.0.2



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/solace/programs/spl_token.rb', line 181

def transfer(
  payer:,
  sign: true,
  execute: true,
  **composer_opts
)
  composer = compose_transfer(**composer_opts)

  yield composer if block_given?

  tx = composer
       .set_fee_payer(payer)
       .compose_transaction

  if sign
    tx.sign(
      payer,
      composer_opts[:owner]
    )

    connection.send_transaction(tx.serialize) if execute
  end
  tx
end

#transfer_checked(payer:, sign: true, execute: true, **composer_opts) {|composer| ... } ⇒ String

Transfers tokens with decimal precision and validation checks

Parameters:

  • payer (#to_s, PublicKey)

    The keypair that will pay for fees and rent.

  • sign (Boolean) (defaults to: true)

    Whether to sign the transaction.

  • execute (Boolean) (defaults to: true)

    Whether to execute the transaction.

  • composer_opts (Hash)

    Options for calling the compose_transfer_checked method.

Yields:

  • (composer)

Returns:

  • (String)

    The signature of the transaction.

Since:

  • 0.0.2



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/solace/programs/spl_token.rb', line 239

def transfer_checked(
  payer:,
  sign: true,
  execute: true,
  **composer_opts
)
  composer = compose_transfer_checked(**composer_opts)

  yield composer if block_given?

  tx = composer
       .set_fee_payer(payer)
       .compose_transaction

  if sign
    tx.sign(
      payer,
      composer_opts[:authority]
    )

    connection.send_transaction(tx.serialize) if execute
  end

  tx
end