Class: Solace::Programs::SplToken

Inherits:
Base
  • Object
show all
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

#connection, #program_id

Instance Method Summary collapse

Constructor Details

#initialize(connection:) ⇒ SplToken

Initializes a new SPL Token client.

Parameters:



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.

Parameters:

  • options (Hash)

    Options for calling the prepare_create_mint method.

Returns:

  • (String)

    The signature of the transaction.



18
19
20
21
22
# File 'lib/solace/programs/spl_token.rb', line 18

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.



86
87
88
89
90
# File 'lib/solace/programs/spl_token.rb', line 86

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.

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:



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)

   = 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.

Parameters:

  • options (Hash)

    Options for calling the prepare_mint_to method.

Returns:



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,
    mint_authority.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
  )

  message = Solace::Message.new(
    header: [2, 0, 1],
    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.

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:



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
  )

  message = 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

Parameters:

  • options (Hash)

    Options for calling the prepare_transfer method.

Returns:

  • (String)

    The signature of the transaction.



136
137
138
139
140
# File 'lib/solace/programs/spl_token.rb', line 136

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

  @connection.send_transaction(tx.serialize)
end