Class: Solace::Programs::SplToken
- 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.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#compose_create_mint(funder:, decimals:, mint_authority:, freeze_authority: nil, mint_account: Solace::Keypair.generate) ⇒ TransactionComposer
Prepares a new SPL Token mint transaction.
-
#compose_mint_to(mint:, amount:, destination:, mint_authority:) ⇒ TransactionComposer
Prepares a mint to instruction and returns the signed transaction.
-
#compose_transfer(amount:, source:, destination:, owner:) ⇒ TransactionComposer
Prepares a transfer instruction and returns the signed transaction.
-
#compose_transfer_checked(to:, from:, mint:, authority:, amount:, decimals:) ⇒ TransactionComposer
Prepares a transfer checked instruction and returns the signed transaction.
-
#create_mint(payer:, sign: true, execute: true, **composer_opts) {|composer| ... } ⇒ String
Creates a new SPL Token mint.
-
#initialize(connection:) ⇒ SplToken
constructor
Initializes a new SPL Token client.
-
#mint_to(payer:, sign: true, execute: true, **composer_opts) {|composer| ... } ⇒ String
Mint tokens to a token account.
-
#transfer(payer:, sign: true, execute: true, **composer_opts) {|composer| ... } ⇒ String
Transfers tokens from one account to another.
-
#transfer_checked(payer:, sign: true, execute: true, **composer_opts) {|composer| ... } ⇒ String
Transfers tokens with decimal precision and validation checks.
Constructor Details
#initialize(connection:) ⇒ SplToken
Initializes a new SPL Token client.
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
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 create_account_ix = Composers::SystemProgramCreateAccountComposer.new( from: funder, new_account: mint_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_account, mint_authority: , freeze_authority: ) TransactionComposer .new(connection: @connection) .add_instruction(create_account_ix) .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.
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: ) TransactionComposer .new(connection: connection) .add_instruction(ix) end |
#compose_transfer(amount:, source:, destination:, owner:) ⇒ TransactionComposer
Prepares a transfer instruction and returns the signed transaction.
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.
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: , 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.
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
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
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
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 |