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.
rubocop:disable Metrics/ClassLength
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#create_mint(**options) ⇒ String
Creates a new SPL Token mint.
-
#initialize(connection:) ⇒ SplToken
constructor
Initializes a new SPL Token client.
-
#mint_to(**options) ⇒ String
Mint tokens to a token account.
-
#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.
-
#prepare_mint_to(payer:, mint:, destination:, amount:, mint_authority:) ⇒ Solace::Transaction
Prepares a mint to instruction and returns the signed transaction.
-
#prepare_transfer(amount:, payer:, source:, destination:, owner:) ⇒ Solace::Transaction
Prepares a transfer instruction and returns the signed transaction.
-
#transfer(**options) ⇒ String
Transfers tokens from one account to another.
Constructor Details
#initialize(connection:) ⇒ SplToken
Initializes a new SPL Token client.
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.
42 43 44 45 46 |
# File 'lib/solace/programs/spl_token.rb', line 42 def create_mint(**) tx = prepare_create_mint(**) @connection.send_transaction(tx.serialize) end |
#mint_to(**options) ⇒ String
Mint tokens to a token account
113 114 115 116 117 |
# File 'lib/solace/programs/spl_token.rb', line 113 def mint_to(**) tx = prepare_mint_to(**) @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
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) create_account_ix = Solace::Instructions::SystemProgram::CreateAccountInstruction.build( from_index: 0, new_account_index: 1, system_program_index: 4, lamports: rent_lamports, space: 82, owner: program_id ) = .respond_to?(:address) ? .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: .address, freeze_authority: ) = Message.new( header: [2, 0, 3], accounts: accounts, recent_blockhash: @connection.get_latest_blockhash, instructions: [create_account_ix, initialize_mint_ix] ) tx = Transaction.new(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
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, .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 ) = Solace::Message.new( header: [2, 0, 1], accounts: accounts, instructions: [ix], recent_blockhash: connection.get_latest_blockhash ) tx = Solace::Transaction.new(message: ) tx.sign(payer, ) tx end |
#prepare_transfer(amount:, payer:, source:, destination:, owner:) ⇒ Solace::Transaction
Prepares a transfer instruction and returns the signed transaction.
rubocop:disable Metrics/MethodLength
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 ) = Solace::Message.new( header: [2, 0, 1], accounts: accounts, instructions: [ix], recent_blockhash: connection.get_latest_blockhash ) tx = Solace::Transaction.new(message: ) tx.sign(payer, owner) tx end |
#transfer(**options) ⇒ String
Transfers tokens from one account to another
170 171 172 173 174 |
# File 'lib/solace/programs/spl_token.rb', line 170 def transfer(**) tx = prepare_transfer(**) @connection.send_transaction(tx.serialize) end |