Class: Solace::Programs::SplToken
- Defined in:
- lib/solace/programs/spl_token.rb
Overview
A client for interacting with the SPL Token Program.
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.
10 11 12 |
# File 'lib/solace/programs/spl_token.rb', line 10 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.
18 19 20 21 22 |
# File 'lib/solace/programs/spl_token.rb', line 18 def create_mint(**) tx = prepare_create_mint(**) @connection.send_transaction(tx.serialize) end |
#mint_to(**options) ⇒ String
Mint tokens to a token account
86 87 88 89 90 |
# File 'lib/solace/programs/spl_token.rb', line 86 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.
32 33 34 35 36 37 38 39 40 41 42 43 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 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/solace/programs/spl_token.rb', line 32 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.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/solace/programs/spl_token.rb', line 96 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:, mint_authority_index: 1, mint_index: 2, destination_index: 3, program_index: 4 ) = Solace::Message.new( header: [2, 0, 1], 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.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/solace/programs/spl_token.rb', line 150 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:, owner_index: 1, source_index: 2, destination_index: 3, program_index: 4 ) = Solace::Message.new( header: [2, 0, 1], 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
136 137 138 139 140 |
# File 'lib/solace/programs/spl_token.rb', line 136 def transfer(**) tx = prepare_transfer(**) @connection.send_transaction(tx.serialize) end |