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.

rubocop:disable Metrics/ClassLength

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



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

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

Instance Method Details

#create_mint(**options) ⇒ String

Creates a new SPL Token mint.

Parameters:

  • options (Hash)

    Options for calling the prepare_create_mint method.

Returns:

  • (String)

    The signature of the transaction.

Since:

  • 0.0.2



42
43
44
45
46
# File 'lib/solace/programs/spl_token.rb', line 42

def create_mint(**options)
  tx = prepare_create_mint(**options)

  @connection.send_transaction(tx.serialize)
end

#mint_to(**options) ⇒ String

Mint tokens to a token account

Parameters:

  • options (Hash)

    Options for calling the prepare_mint_to method.

Returns:

  • (String)

    The signature of the transaction.

Since:

  • 0.0.2



113
114
115
116
117
# File 'lib/solace/programs/spl_token.rb', line 113

def mint_to(**options)
  tx = prepare_mint_to(**options)

  @connection.send_transaction(tx.serialize)
end

#prepare_create_mint(payer:, decimals:, mint_authority:, freeze_authority:, mint_keypair: Solace::Keypair.generate) ⇒ Solace::Transaction

Prepares a new SPL Token mint and returns the signed transaction.

rubocop:disable Metrics/MethodLength

Parameters:

  • payer (Solace::Keypair)

    The keypair that will pay for fees and rent.

  • decimals (Integer)

    The number of decimal places for the token.

  • mint_authority (String)

    The base58 public key for the mint authority.

  • freeze_authority (String)

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

  • mint_keypair (Solace::Keypair) (defaults to: Solace::Keypair.generate)

    (Optional) The keypair for the new mint.

Returns:

Since:

  • 0.0.2



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
# File 'lib/solace/programs/spl_token.rb', line 58

def prepare_create_mint(
  payer:,
  decimals:,
  mint_authority:,
  freeze_authority:,
  mint_keypair: Solace::Keypair.generate
)
  accounts = [
    payer.address,
    mint_keypair.address,
    Solace::Constants::SYSVAR_RENT_PROGRAM_ID,
    Solace::Constants::TOKEN_PROGRAM_ID,
    Solace::Constants::SYSTEM_PROGRAM_ID
  ]

  rent_lamports = @connection.get_minimum_lamports_for_rent_exemption(82)

   = Solace::Instructions::SystemProgram::CreateAccountInstruction.build(
    from_index: 0,
    new_account_index: 1,
    system_program_index: 4,
    lamports: rent_lamports,
    space: 82,
    owner: program_id
  )

  freeze_authority_address = freeze_authority.respond_to?(:address) ? freeze_authority.address : nil

  initialize_mint_ix = Solace::Instructions::SplToken::InitializeMintInstruction.build(
    mint_account_index: 1,
    rent_sysvar_index: 2,
    program_index: 3,
    decimals: decimals,
    mint_authority: mint_authority.address,
    freeze_authority: freeze_authority_address
  )

  message = Message.new(
    header: [2, 0, 3],
    accounts: accounts,
    recent_blockhash: @connection.get_latest_blockhash,
    instructions: [, initialize_mint_ix]
  )

  tx = Transaction.new(message: message)
  tx.sign(payer, mint_keypair)

  tx
end

#prepare_mint_to(payer:, mint:, destination:, amount:, mint_authority:) ⇒ Solace::Transaction

Prepares a mint to instruction and returns the signed transaction.

rubocop:disable Metrics/MethodLength

Parameters:

  • amount (Integer)

    The amount of tokens to mint.

  • payer (PublicKey, Keypair, String)

    The payer of the transaction.

  • mint (PublicKey, Keypair, String)

    The mint of the token.

  • destination (PublicKey, Keypair, String)

    The destination of the token.

  • mint_authority (PublicKey, Keypair, String)

    The mint authority of the token.

Returns:

Since:

  • 0.0.2



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/solace/programs/spl_token.rb', line 129

def prepare_mint_to(
  payer:,
  mint:,
  destination:,
  amount:,
  mint_authority:
)
  accounts = [
    payer.address,
    mint_authority.address,
    mint.address,
    destination,
    Solace::Constants::TOKEN_PROGRAM_ID
  ]

  ix = Solace::Instructions::SplToken::MintToInstruction.build(
    amount: amount,
    mint_authority_index: 1,
    mint_index: 2,
    destination_index: 3,
    program_index: 4
  )

  message = Solace::Message.new(
    header: [2, 0, 1],
    accounts: accounts,
    instructions: [ix],
    recent_blockhash: connection.get_latest_blockhash
  )

  tx = Solace::Transaction.new(message: message)
  tx.sign(payer, mint_authority)

  tx
end

#prepare_transfer(amount:, payer:, source:, destination:, owner:) ⇒ Solace::Transaction

Prepares a transfer instruction and returns the signed transaction.

rubocop:disable Metrics/MethodLength

Parameters:

  • payer (Solace::Keypair)

    The keypair that will pay for fees and rent.

  • source (String)

    The source token account address.

  • destination (String)

    The destination token account address.

  • amount (Integer)

    The number of tokens to transfer.

  • owner (Solace::Keypair)

    The keypair of the owner of the source account.

Returns:

Since:

  • 0.0.2



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/solace/programs/spl_token.rb', line 186

def prepare_transfer(
  amount:,
  payer:,
  source:,
  destination:,
  owner:
)
  accounts = [
    payer.address,
    owner.address,
    source,
    destination,
    Solace::Constants::TOKEN_PROGRAM_ID
  ]

  ix = Solace::Instructions::SplToken::TransferInstruction.build(
    amount: amount,
    owner_index: 1,
    source_index: 2,
    destination_index: 3,
    program_index: 4
  )

  message = Solace::Message.new(
    header: [2, 0, 1],
    accounts: accounts,
    instructions: [ix],
    recent_blockhash: connection.get_latest_blockhash
  )

  tx = Solace::Transaction.new(message: message)
  tx.sign(payer, owner)

  tx
end

#transfer(**options) ⇒ String

Transfers tokens from one account to another

Parameters:

  • options (Hash)

    Options for calling the prepare_transfer method.

Returns:

  • (String)

    The signature of the transaction.

Since:

  • 0.0.2



170
171
172
173
174
# File 'lib/solace/programs/spl_token.rb', line 170

def transfer(**options)
  tx = prepare_transfer(**options)

  @connection.send_transaction(tx.serialize)
end