solace 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG +12 -0
- data/README.md +140 -285
- data/lib/solace/address_lookup_table.rb +34 -18
- data/lib/solace/composers/base.rb +16 -11
- data/lib/solace/composers/spl_token_program_transfer_checked_composer.rb +27 -1
- data/lib/solace/composers/system_program_transfer_composer.rb +22 -1
- data/lib/solace/concerns/binary_serializable.rb +39 -0
- data/lib/solace/connection.rb +69 -38
- data/lib/solace/constants.rb +7 -14
- data/lib/solace/instruction.rb +30 -19
- data/lib/solace/instructions/associated_token_account/create_associated_token_account_instruction.rb +18 -3
- data/lib/solace/instructions/spl_token/initialize_account_instruction.rb +24 -3
- data/lib/solace/instructions/spl_token/initialize_mint_instruction.rb +18 -1
- data/lib/solace/instructions/spl_token/mint_to_instruction.rb +16 -3
- data/lib/solace/instructions/spl_token/transfer_checked_instruction.rb +17 -1
- data/lib/solace/instructions/spl_token/transfer_instruction.rb +15 -2
- data/lib/solace/instructions/system_program/create_account_instruction.rb +18 -3
- data/lib/solace/instructions/system_program/transfer_instruction.rb +15 -7
- data/lib/solace/keypair.rb +64 -31
- data/lib/solace/message.rb +22 -10
- data/lib/solace/programs/associated_token_account.rb +45 -20
- data/lib/solace/programs/base.rb +6 -0
- data/lib/solace/programs/spl_token.rb +52 -14
- data/lib/solace/public_key.rb +45 -20
- data/lib/solace/serializers/address_lookup_table_deserializer.rb +3 -5
- data/lib/solace/serializers/address_lookup_table_serializer.rb +7 -7
- data/lib/solace/serializers/base_deserializer.rb +29 -19
- data/lib/solace/serializers/base_serializer.rb +18 -9
- data/lib/solace/serializers/instruction_deserializer.rb +5 -7
- data/lib/solace/serializers/instruction_serializer.rb +4 -6
- data/lib/solace/serializers/message_deserializer.rb +3 -5
- data/lib/solace/serializers/message_serializer.rb +3 -5
- data/lib/solace/serializers/transaction_deserializer.rb +5 -7
- data/lib/solace/serializers/transaction_serializer.rb +5 -7
- data/lib/solace/transaction.rb +38 -23
- data/lib/solace/transaction_composer.rb +47 -13
- data/lib/solace/utils/account_context.rb +64 -65
- data/lib/solace/utils/codecs.rb +56 -128
- data/lib/solace/utils/curve25519_dalek.rb +9 -4
- data/lib/solace/utils/pda.rb +22 -24
- data/lib/solace/version.rb +2 -1
- data/lib/solace.rb +4 -9
- metadata +7 -10
- data/lib/solace/instructions/base.rb +0 -21
- data/lib/solace/serializable_record.rb +0 -26
- data/lib/solace/serializers/base.rb +0 -31
@@ -3,7 +3,23 @@
|
|
3
3
|
module Solace
|
4
4
|
module Instructions
|
5
5
|
module SplToken
|
6
|
-
#
|
6
|
+
# Instruction for transferring SPL tokens.
|
7
|
+
#
|
8
|
+
# This instruction is used to transfer SPL tokens from one token account to another while checking the decimals
|
9
|
+
# of the token to ensure the transfer amount is correct.
|
10
|
+
#
|
11
|
+
# @example Build a TransferChecked instruction
|
12
|
+
# instruction = Solace::Instructions::SplToken::TransferCheckedInstruction.build(
|
13
|
+
# amount: 100,
|
14
|
+
# decimals: 6,
|
15
|
+
# to_index: 1,
|
16
|
+
# from_index: 2,
|
17
|
+
# mint_index: 3,
|
18
|
+
# authority_index: 4,
|
19
|
+
# program_index: 5
|
20
|
+
# )
|
21
|
+
#
|
22
|
+
# @since 0.0.2
|
7
23
|
class TransferCheckedInstruction
|
8
24
|
# SPL Token Program instruction index for Transfer Checked
|
9
25
|
INSTRUCTION_INDEX = [12].freeze
|
@@ -3,9 +3,22 @@
|
|
3
3
|
module Solace
|
4
4
|
module Instructions
|
5
5
|
module SplToken
|
6
|
-
#
|
6
|
+
# Instruction for transferring SPL tokens.
|
7
|
+
#
|
8
|
+
# This instruction is used to transfer SPL tokens from one token account to another.
|
9
|
+
#
|
10
|
+
# @example Build a Transfer instruction
|
11
|
+
# instruction = Solace::Instructions::SplToken::TransferInstruction.build(
|
12
|
+
# amount: 100,
|
13
|
+
# owner_index: 1,
|
14
|
+
# source_index: 2,
|
15
|
+
# destination_index: 3,
|
16
|
+
# program_index: 4
|
17
|
+
# )
|
18
|
+
#
|
19
|
+
# @since 0.0.2
|
7
20
|
class TransferInstruction
|
8
|
-
# @!
|
21
|
+
# @!attribute [Array<Integer>] INSTRUCTION_INDEX
|
9
22
|
# Instruction index for SPL Token Program's Transfer instruction.
|
10
23
|
INSTRUCTION_INDEX = [3].freeze
|
11
24
|
|
@@ -3,11 +3,25 @@
|
|
3
3
|
module Solace
|
4
4
|
module Instructions
|
5
5
|
module SystemProgram
|
6
|
+
# Instruction for creating a new account.
|
7
|
+
#
|
8
|
+
# This instruction is used to create a new account for a given program.
|
9
|
+
#
|
10
|
+
# @example Build a CreateAccount instruction
|
11
|
+
# instruction = Solace::Instructions::SystemProgram::CreateAccountInstruction.build(
|
12
|
+
# space: 1024,
|
13
|
+
# lamports: 1000,
|
14
|
+
# from_index: 0,
|
15
|
+
# new_account_index: 1,
|
16
|
+
# owner: owner.address,
|
17
|
+
# system_program_index: 2
|
18
|
+
# )
|
19
|
+
#
|
20
|
+
# @since 0.0.2
|
6
21
|
class CreateAccountInstruction
|
7
|
-
#
|
22
|
+
# @!attribute [Array<Integer>] INSTRUCTION_INDEX
|
8
23
|
# Instruction index for SystemProgram::CreateAccount
|
9
24
|
# This is the same across all Solana clusters
|
10
|
-
# @return [Array<Integer>]
|
11
25
|
INSTRUCTION_INDEX = [0, 0, 0, 0].freeze
|
12
26
|
|
13
27
|
# Builds a SystemProgram::CreateAccount instruction
|
@@ -22,9 +36,9 @@ module Solace
|
|
22
36
|
def self.build(
|
23
37
|
space:,
|
24
38
|
lamports:,
|
25
|
-
owner: Solace::Constants::SYSTEM_PROGRAM_ID,
|
26
39
|
from_index:,
|
27
40
|
new_account_index:,
|
41
|
+
owner: Solace::Constants::SYSTEM_PROGRAM_ID,
|
28
42
|
system_program_index: 2
|
29
43
|
)
|
30
44
|
Solace::Instruction.new.tap do |ix|
|
@@ -33,6 +47,7 @@ module Solace
|
|
33
47
|
ix.data = data(lamports, space, owner)
|
34
48
|
end
|
35
49
|
end
|
50
|
+
# rubocop:enable Metrics/ParameterLists
|
36
51
|
|
37
52
|
# Builds the data for a SystemProgram::CreateAccount instruction
|
38
53
|
#
|
@@ -3,17 +3,25 @@
|
|
3
3
|
module Solace
|
4
4
|
module Instructions
|
5
5
|
module SystemProgram
|
6
|
-
#
|
7
|
-
|
6
|
+
# Instruction for transferring SOL.
|
7
|
+
#
|
8
|
+
# This instruction is used to transfer SOL from one account to another.
|
9
|
+
#
|
10
|
+
# @example Build a Transfer instruction
|
11
|
+
# instruction = Solace::Instructions::SystemProgram::TransferInstruction.build(
|
12
|
+
# lamports: 100,
|
13
|
+
# to_index: 1,
|
14
|
+
# from_index: 2,
|
15
|
+
# program_index: 3
|
16
|
+
# )
|
17
|
+
#
|
18
|
+
# @since 0.0.2
|
19
|
+
class TransferInstruction
|
8
20
|
# Instruction ID for System Transfer
|
9
21
|
INSTRUCTION_ID = [2, 0, 0, 0].freeze
|
10
22
|
|
11
23
|
# Builds a Solace::Instruction for transferring SOL
|
12
24
|
#
|
13
|
-
# System Program transfer instruction layout:
|
14
|
-
# - 4 bytes: instruction index (0 for transfer)
|
15
|
-
# - 8 bytes: amount (u64, little-endian)
|
16
|
-
#
|
17
25
|
# @param lamports [Integer] Amount to transfer (in lamports)
|
18
26
|
# @param to_index [Integer] Index of the recipient in the transaction's accounts
|
19
27
|
# @param from_index [Integer] Index of the sender in the transaction's accounts
|
@@ -47,4 +55,4 @@ module Solace
|
|
47
55
|
end
|
48
56
|
end
|
49
57
|
end
|
50
|
-
end
|
58
|
+
end
|
data/lib/solace/keypair.rb
CHANGED
@@ -3,38 +3,40 @@
|
|
3
3
|
require 'rbnacl'
|
4
4
|
require 'base58'
|
5
5
|
|
6
|
-
# =============================
|
7
|
-
# Keypair
|
8
|
-
# =============================
|
9
|
-
#
|
10
|
-
# Represents a Solana Ed25519 Keypair
|
11
6
|
module Solace
|
7
|
+
# Class representing a Solana Ed25519 Keypair
|
8
|
+
#
|
9
|
+
# This class provides utility methods for encoding, decoding, signing, and validating keypairs.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# # Generate a new keypair
|
13
|
+
# keypair = Solace::Keypair.generate
|
14
|
+
#
|
15
|
+
# # Get the address of the pubkey
|
16
|
+
# keypair.address
|
17
|
+
#
|
18
|
+
# # Sign a message using the keypair
|
19
|
+
# keypair.sign("<any-message>")
|
20
|
+
#
|
21
|
+
# @since 0.0.1
|
12
22
|
class Keypair
|
13
|
-
#
|
14
|
-
# The length of a Solana secret key in bytes
|
15
|
-
#
|
16
|
-
# @return [Integer] The length of a secret key
|
23
|
+
# The length of a Solana secret key in bytes.
|
17
24
|
SECRET_LENGTH = 64
|
18
25
|
|
19
|
-
#
|
20
|
-
|
21
|
-
#
|
22
|
-
# @return [Integer] The length of a seed
|
23
|
-
SEED_LENGTH = RbNaCl::Signatures::Ed25519::SEEDBYTES
|
26
|
+
# The length of a Solana seed in bytes.
|
27
|
+
SEED_LENGTH = 32
|
24
28
|
|
25
|
-
#
|
26
|
-
# The RbNaCl signing key
|
29
|
+
# The full keypair bytes array
|
27
30
|
#
|
28
|
-
# @return [
|
29
|
-
SigningKey = RbNaCl::Signatures::Ed25519::SigningKey
|
30
|
-
|
31
|
-
# !@attribute [r] keypair_bytes
|
32
|
-
# @return [Array<Integer>] The keypair bytes
|
31
|
+
# @return [Array<u8>] The 64 bytes of the keypair
|
33
32
|
attr_reader :keypair_bytes
|
34
33
|
|
35
34
|
class << self
|
36
35
|
# Generate a new random keypair
|
37
36
|
#
|
37
|
+
# @example
|
38
|
+
# keypair = Solace::Keypair.generate
|
39
|
+
#
|
38
40
|
# @return [Keypair]
|
39
41
|
def generate
|
40
42
|
from_seed(RbNaCl::Random.random_bytes(SEED_LENGTH))
|
@@ -42,28 +44,40 @@ module Solace
|
|
42
44
|
|
43
45
|
# Create a keypair from a 32-byte seed
|
44
46
|
#
|
47
|
+
# @example
|
48
|
+
# keypair = Solace::Keypair.from_seed(seed)
|
49
|
+
#
|
45
50
|
# @param seed [String] 32-byte array
|
51
|
+
# @raise [ArgumentError] If the length of the seed isn't 32 bytes
|
46
52
|
# @return [Keypair]
|
47
53
|
def from_seed(seed)
|
48
54
|
raise ArgumentError, 'Seed must be 32 bytes' unless seed.length == SEED_LENGTH
|
49
55
|
|
50
|
-
new(SigningKey.new(seed).keypair_bytes.bytes)
|
56
|
+
new(RbNaCl::Signatures::Ed25519::SigningKey.new(seed).keypair_bytes.bytes)
|
51
57
|
end
|
52
58
|
|
53
59
|
# Create a keypair from a 64-byte secret key
|
54
60
|
#
|
61
|
+
# @example
|
62
|
+
# keypair = Solace::Keypair.from_secret_key(secret_key)
|
63
|
+
#
|
55
64
|
# @param secret_key [String] 64-byte array
|
65
|
+
# @raise [ArgumentError] If the length of the secret_key isn't 64 bytes
|
56
66
|
# @return [Keypair]
|
57
67
|
def from_secret_key(secret_key)
|
58
68
|
raise ArgumentError, 'Secret key must be 64 bytes' unless secret_key.length == SECRET_LENGTH
|
59
69
|
|
60
|
-
new(SigningKey.new(secret_key[0..31]).keypair_bytes.bytes)
|
70
|
+
new(RbNaCl::Signatures::Ed25519::SigningKey.new(secret_key[0..31]).keypair_bytes.bytes)
|
61
71
|
end
|
62
72
|
end
|
63
73
|
|
64
74
|
# Initialize a new keypair
|
65
75
|
#
|
76
|
+
# @example
|
77
|
+
# keypair = Solace::Keypair.new(bytes)
|
78
|
+
#
|
66
79
|
# @param keypair_bytes [Array<Integer>] The keypair bytes
|
80
|
+
# @raise [ArgumentError] If the length of the keypair_bytes isn't 64 bytes
|
67
81
|
# @return [Keypair] The new keypair object
|
68
82
|
def initialize(keypair_bytes)
|
69
83
|
raise ArgumentError, 'Keypair must be 64 bytes' unless keypair_bytes.length == SECRET_LENGTH
|
@@ -73,6 +87,9 @@ module Solace
|
|
73
87
|
|
74
88
|
# Returns the public key
|
75
89
|
#
|
90
|
+
# @example
|
91
|
+
# pubkey = keypair.public_key
|
92
|
+
#
|
76
93
|
# @return [PublicKey]
|
77
94
|
def public_key
|
78
95
|
@public_key ||= Solace::PublicKey.new(public_key_bytes)
|
@@ -80,16 +97,22 @@ module Solace
|
|
80
97
|
|
81
98
|
# Returns the signing key
|
82
99
|
#
|
100
|
+
# @example
|
101
|
+
# signing_key = instance.signing_key
|
102
|
+
#
|
83
103
|
# @return [RbNaCl::Signatures::Ed25519::SigningKey]
|
84
104
|
def signing_key
|
85
|
-
@signing_key ||= SigningKey.new(
|
105
|
+
@signing_key ||= RbNaCl::Signatures::Ed25519::SigningKey.new(private_key_bytes.pack('C*'))
|
86
106
|
end
|
87
107
|
|
88
108
|
# Returns the public key bytes
|
89
109
|
#
|
90
110
|
# The public key bytes are the last 32 bytes of the keypair
|
91
111
|
#
|
92
|
-
# @
|
112
|
+
# @example
|
113
|
+
# public_key_bytes = instance.public_key_bytes
|
114
|
+
#
|
115
|
+
# @return [Array<u8>] 32 bytes
|
93
116
|
def public_key_bytes
|
94
117
|
keypair_bytes[32..63]
|
95
118
|
end
|
@@ -98,12 +121,18 @@ module Solace
|
|
98
121
|
#
|
99
122
|
# The private key is the first 32 bytes of the keypair
|
100
123
|
#
|
101
|
-
# @
|
102
|
-
|
103
|
-
|
124
|
+
# @example
|
125
|
+
# private_key_bytes = instance.private_key_bytes
|
126
|
+
#
|
127
|
+
# @return [Array<u8>] 32 characters
|
128
|
+
def private_key_bytes
|
129
|
+
keypair_bytes[0..31]
|
104
130
|
end
|
105
131
|
|
106
|
-
# Returns the public key as a Base58 string
|
132
|
+
# Returns the public key address as a Base58 string
|
133
|
+
#
|
134
|
+
# @example
|
135
|
+
# pubkey_str = instance.address
|
107
136
|
#
|
108
137
|
# @return [String] Base58 encoded public key
|
109
138
|
def address
|
@@ -112,8 +141,12 @@ module Solace
|
|
112
141
|
|
113
142
|
# Signs a message (string or binary)
|
114
143
|
#
|
115
|
-
# @
|
116
|
-
#
|
144
|
+
# @example
|
145
|
+
# message = "An important message to be signed,"
|
146
|
+
# signature = instance.sign(message)
|
147
|
+
#
|
148
|
+
# @param message [String, Binary]
|
149
|
+
# @return [String] signature (binary string)
|
117
150
|
def sign(message)
|
118
151
|
signing_key.sign(message)
|
119
152
|
end
|
data/lib/solace/message.rb
CHANGED
@@ -1,18 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# =============================
|
4
|
-
# Message
|
5
|
-
# =============================
|
6
|
-
#
|
7
|
-
# Represents the message portion of a Solana transaction (legacy or versioned).
|
8
|
-
# Handles serialization and deserialization of message fields.
|
9
3
|
module Solace
|
10
|
-
|
11
|
-
|
4
|
+
# Solace::Message represents the message portion of a Solana transaction (legacy or versioned). It handles
|
5
|
+
# serialization and deserialization of message fields.
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# message = Solace::Message.new(
|
9
|
+
# version: 0,
|
10
|
+
# header: [0, 0, 0],
|
11
|
+
# accounts: ['11111111111111111111111111111111'],
|
12
|
+
# recent_blockhash: '11111111111111111111111111111111',
|
13
|
+
# instructions: [],
|
14
|
+
# address_lookup_tables: []
|
15
|
+
# )
|
16
|
+
#
|
17
|
+
# @since 0.0.1
|
18
|
+
class Message
|
19
|
+
include Solace::Concerns::BinarySerializable
|
20
|
+
|
21
|
+
# @!attribute SERIALIZER
|
12
22
|
# @return [Solace::Serializers::MessageSerializer] The serializer for the message
|
13
23
|
SERIALIZER = Solace::Serializers::MessageSerializer
|
14
24
|
|
15
|
-
# @!
|
25
|
+
# @!attribute DESERIALIZER
|
16
26
|
# @return [Solace::Serializers::MessageDeserializer] The deserializer for the message
|
17
27
|
DESERIALIZER = Solace::Serializers::MessageDeserializer
|
18
28
|
|
@@ -46,7 +56,7 @@ module Solace
|
|
46
56
|
# @param accounts [Array<String>] Account public keys (base58)
|
47
57
|
# @param instructions [Array<Solace::Instruction>] Instructions in the message
|
48
58
|
# @param recent_blockhash [String] Recent blockhash (base58)
|
49
|
-
# @param header [Array<Integer>] Message header
|
59
|
+
# @param header [Array<Integer>] Message header
|
50
60
|
# @param address_lookup_tables [Array<Solace::AddressLookupTable>]
|
51
61
|
def initialize(
|
52
62
|
version: nil,
|
@@ -56,6 +66,8 @@ module Solace
|
|
56
66
|
header: [0, 0, 0],
|
57
67
|
address_lookup_tables: []
|
58
68
|
)
|
69
|
+
super()
|
70
|
+
|
59
71
|
@version = version
|
60
72
|
@header = header
|
61
73
|
@accounts = accounts
|
@@ -2,13 +2,33 @@
|
|
2
2
|
|
3
3
|
module Solace
|
4
4
|
module Programs
|
5
|
-
#
|
5
|
+
# Client for interacting with the Associated Token Account Program.
|
6
|
+
#
|
7
|
+
# This client provides methods for interacting with the Associated Token Account Program. It is a
|
8
|
+
# wrapper around the SPL Token Program and provides a more convenient interface for creating and
|
9
|
+
# managing associated token accounts.
|
10
|
+
#
|
11
|
+
# @example Create an associated token account
|
12
|
+
# # Initialize the program with a connection
|
13
|
+
# program = Solace::Programs::AssociatedTokenAccount.new(connection: connection)
|
14
|
+
#
|
15
|
+
# # Create an associated token account
|
16
|
+
# result = program.create_associated_token_account(
|
17
|
+
# payer: payer,
|
18
|
+
# owner: owner,
|
19
|
+
# mint: mint
|
20
|
+
# )
|
21
|
+
#
|
22
|
+
# # Wait for the transaction to be finalized
|
23
|
+
# @connection.wait_for_confirmed_signature('finalized') { result['result'] }
|
24
|
+
#
|
25
|
+
# @since 0.0.2
|
6
26
|
class AssociatedTokenAccount < Base
|
7
27
|
class << self
|
8
28
|
# Gets the address of an associated token account.
|
9
29
|
#
|
10
|
-
# @param owner [Solace::Keypair
|
11
|
-
# @param mint [Solace::Keypair
|
30
|
+
# @param owner [Solace::Keypair, Solace::PublicKey] The keypair of the owner.
|
31
|
+
# @param mint [Solace::Keypair, Solace::PublicKey] The keypair of the mint.
|
12
32
|
# @return [String] The address of the associated token account.
|
13
33
|
def get_address(owner:, mint:)
|
14
34
|
Solace::Utils::PDA.find_program_address(
|
@@ -26,35 +46,37 @@ module Solace
|
|
26
46
|
#
|
27
47
|
# @param connection [Solace::Connection] The connection to the Solana cluster.
|
28
48
|
def initialize(connection:)
|
29
|
-
super(connection
|
49
|
+
super(connection: connection, program_id: Solace::Constants::ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID)
|
30
50
|
end
|
31
51
|
|
32
52
|
# Alias method for get_address
|
33
|
-
#
|
34
|
-
# @
|
53
|
+
#
|
54
|
+
# @option options [Hash] A hash of options for the get_address class method
|
55
|
+
# @return [Array<String, Integer>] The address of the associated token account and the bump seed
|
35
56
|
def get_address(**options)
|
36
57
|
self.class.get_address(**options)
|
37
58
|
end
|
38
59
|
|
39
60
|
# Gets the address of an associated token account, creating it if it doesn't exist.
|
40
61
|
#
|
41
|
-
# @param payer [Solace::Keypair] The keypair that will pay for fees and rent
|
42
|
-
# @param owner [Solace::Keypair
|
43
|
-
# @param mint [Solace::Keypair
|
62
|
+
# @param payer [Solace::Keypair] The keypair that will pay for fees and rent.
|
63
|
+
# @param owner [Solace::Keypair, Solace::PublicKey] The keypair of the owner.
|
64
|
+
# @param mint [Solace::Keypair, Solace::PublicKey] The keypair of the mint.
|
65
|
+
# @param commitment [String] The commitment level for the get_account_info call.
|
44
66
|
# @return [String] The address of the associated token account
|
45
67
|
def get_or_create_address(payer:, owner:, mint:, commitment: 'confirmed')
|
46
|
-
ata_address,
|
47
|
-
|
68
|
+
ata_address, _bump = get_address(owner: owner, mint: mint)
|
69
|
+
|
48
70
|
account_info = @connection.get_account_info(ata_address)
|
49
|
-
|
71
|
+
|
50
72
|
return ata_address if account_info
|
51
73
|
|
52
|
-
response = create_associated_token_account(payer
|
74
|
+
response = create_associated_token_account(payer: payer, owner: owner, mint: mint)
|
53
75
|
|
54
|
-
raise
|
76
|
+
raise 'Failed to create associated token account' unless response['result']
|
55
77
|
|
56
78
|
@connection.wait_for_confirmed_signature(commitment) { response['result'] }
|
57
|
-
|
79
|
+
|
58
80
|
ata_address
|
59
81
|
end
|
60
82
|
|
@@ -70,16 +92,18 @@ module Solace
|
|
70
92
|
|
71
93
|
# Prepares a new associated token account and returns the signed transaction.
|
72
94
|
#
|
73
|
-
# @param owner [Solace::Keypair
|
74
|
-
# @param mint [Solace::Keypair
|
95
|
+
# @param owner [Solace::Keypair, Solace::PublicKey] The keypair of the owner.
|
96
|
+
# @param mint [Solace::Keypair, Solace::PublicKey] The keypair of the mint.
|
75
97
|
# @param payer [Solace::Keypair] The keypair that will pay for fees and rent.
|
76
98
|
# @return [Solace::Transaction] The signed transaction.
|
99
|
+
#
|
100
|
+
# rubocop:disable Metrics/MethodLength
|
77
101
|
def prepare_create_associated_token_account(
|
78
|
-
payer:,
|
79
|
-
owner:,
|
102
|
+
payer:,
|
103
|
+
owner:,
|
80
104
|
mint:
|
81
105
|
)
|
82
|
-
ata_address,
|
106
|
+
ata_address, = get_address(owner: owner, mint: mint)
|
83
107
|
|
84
108
|
accounts = [
|
85
109
|
payer.address,
|
@@ -113,6 +137,7 @@ module Solace
|
|
113
137
|
|
114
138
|
tx
|
115
139
|
end
|
140
|
+
# rubocop:enable Metrics/MethodLength
|
116
141
|
end
|
117
142
|
end
|
118
143
|
end
|
data/lib/solace/programs/base.rb
CHANGED
@@ -5,7 +5,13 @@
|
|
5
5
|
module Solace
|
6
6
|
module Programs
|
7
7
|
# Base class for program-specific clients.
|
8
|
+
#
|
8
9
|
# Provides a consistent interface for interacting with on-chain programs.
|
10
|
+
#
|
11
|
+
# @abstract
|
12
|
+
# @see Solace::Programs::SplToken
|
13
|
+
# @see Solace::Programs::AssociatedTokenAccount
|
14
|
+
# @since 0.0.2
|
9
15
|
class Base
|
10
16
|
attr_reader :connection, :program_id
|
11
17
|
|
@@ -2,7 +2,31 @@
|
|
2
2
|
|
3
3
|
module Solace
|
4
4
|
module Programs
|
5
|
-
#
|
5
|
+
# Client for interacting with the SPL Token Program.
|
6
|
+
#
|
7
|
+
# This client provides methods for interacting with the SPL Token Program. It is a wrapper around
|
8
|
+
# the SPL Token Program and provides a more convenient interface for creating and managing SPL
|
9
|
+
# Token mints and accounts.
|
10
|
+
#
|
11
|
+
# @example Create an SPL Token mint
|
12
|
+
# # Initialize the program with a connection
|
13
|
+
# program = Solace::Programs::SplToken.new(connection: connection)
|
14
|
+
#
|
15
|
+
# # Create an SPL Token mint
|
16
|
+
# result = program.create_mint(
|
17
|
+
# payer: payer,
|
18
|
+
# decimals: 6,
|
19
|
+
# mint_keypair: mint_keypair,
|
20
|
+
# mint_authority: mint_authority,
|
21
|
+
# freeze_authority: freeze_authority
|
22
|
+
# )
|
23
|
+
#
|
24
|
+
# # Wait for the transaction to be finalized
|
25
|
+
# @connection.wait_for_confirmed_signature('finalized') { result['result'] }
|
26
|
+
#
|
27
|
+
# @since 0.0.2
|
28
|
+
#
|
29
|
+
# rubocop:disable Metrics/ClassLength
|
6
30
|
class SplToken < Base
|
7
31
|
# Initializes a new SPL Token client.
|
8
32
|
#
|
@@ -29,6 +53,8 @@ module Solace
|
|
29
53
|
# @param freeze_authority [String] (Optional) The base58 public key for the freeze authority.
|
30
54
|
# @param mint_keypair [Solace::Keypair] (Optional) The keypair for the new mint.
|
31
55
|
# @return [Solace::Transaction] The signed transaction.
|
56
|
+
#
|
57
|
+
# rubocop:disable Metrics/MethodLength
|
32
58
|
def prepare_create_mint(
|
33
59
|
payer:,
|
34
60
|
decimals:,
|
@@ -78,6 +104,7 @@ module Solace
|
|
78
104
|
|
79
105
|
tx
|
80
106
|
end
|
107
|
+
# rubocop:enable Metrics/MethodLength
|
81
108
|
|
82
109
|
# Mint tokens to a token account
|
83
110
|
#
|
@@ -91,10 +118,16 @@ module Solace
|
|
91
118
|
|
92
119
|
# Prepares a mint to instruction and returns the signed transaction.
|
93
120
|
#
|
94
|
-
# @param
|
121
|
+
# @param [Integer] amount The amount of tokens to mint.
|
122
|
+
# @param [PublicKey, Keypair, String] payer The payer of the transaction.
|
123
|
+
# @param [PublicKey, Keypair, String] mint The mint of the token.
|
124
|
+
# @param [PublicKey, Keypair, String] destination The destination of the token.
|
125
|
+
# @param [PublicKey, Keypair, String] mint_authority The mint authority of the token.
|
95
126
|
# @return [Solace::Transaction] The signed transaction.
|
127
|
+
#
|
128
|
+
# rubocop:disable Metrics/MethodLength
|
96
129
|
def prepare_mint_to(
|
97
|
-
payer:,
|
130
|
+
payer:,
|
98
131
|
mint:,
|
99
132
|
destination:,
|
100
133
|
amount:,
|
@@ -105,11 +138,11 @@ module Solace
|
|
105
138
|
mint_authority.address,
|
106
139
|
mint.address,
|
107
140
|
destination,
|
108
|
-
Solace::Constants::TOKEN_PROGRAM_ID
|
141
|
+
Solace::Constants::TOKEN_PROGRAM_ID
|
109
142
|
]
|
110
143
|
|
111
144
|
ix = Solace::Instructions::SplToken::MintToInstruction.build(
|
112
|
-
amount
|
145
|
+
amount: amount,
|
113
146
|
mint_authority_index: 1,
|
114
147
|
mint_index: 2,
|
115
148
|
destination_index: 3,
|
@@ -118,9 +151,9 @@ module Solace
|
|
118
151
|
|
119
152
|
message = Solace::Message.new(
|
120
153
|
header: [2, 0, 1],
|
121
|
-
accounts
|
154
|
+
accounts: accounts,
|
122
155
|
instructions: [ix],
|
123
|
-
recent_blockhash: connection.get_latest_blockhash
|
156
|
+
recent_blockhash: connection.get_latest_blockhash
|
124
157
|
)
|
125
158
|
|
126
159
|
tx = Solace::Transaction.new(message: message)
|
@@ -128,6 +161,7 @@ module Solace
|
|
128
161
|
|
129
162
|
tx
|
130
163
|
end
|
164
|
+
# rubocop:enable Metrics/MethodLength
|
131
165
|
|
132
166
|
# Transfers tokens from one account to another
|
133
167
|
#
|
@@ -147,11 +181,13 @@ module Solace
|
|
147
181
|
# @param amount [Integer] The number of tokens to transfer.
|
148
182
|
# @param owner [Solace::Keypair] The keypair of the owner of the source account.
|
149
183
|
# @return [Solace::Transaction] The signed transaction.
|
184
|
+
#
|
185
|
+
# rubocop:disable Metrics/MethodLength
|
150
186
|
def prepare_transfer(
|
151
|
-
amount:,
|
152
|
-
payer:,
|
153
|
-
source:,
|
154
|
-
destination:,
|
187
|
+
amount:,
|
188
|
+
payer:,
|
189
|
+
source:,
|
190
|
+
destination:,
|
155
191
|
owner:
|
156
192
|
)
|
157
193
|
accounts = [
|
@@ -163,7 +199,7 @@ module Solace
|
|
163
199
|
]
|
164
200
|
|
165
201
|
ix = Solace::Instructions::SplToken::TransferInstruction.build(
|
166
|
-
amount
|
202
|
+
amount: amount,
|
167
203
|
owner_index: 1,
|
168
204
|
source_index: 2,
|
169
205
|
destination_index: 3,
|
@@ -172,16 +208,18 @@ module Solace
|
|
172
208
|
|
173
209
|
message = Solace::Message.new(
|
174
210
|
header: [2, 0, 1],
|
175
|
-
accounts
|
211
|
+
accounts: accounts,
|
176
212
|
instructions: [ix],
|
177
213
|
recent_blockhash: connection.get_latest_blockhash
|
178
214
|
)
|
179
215
|
|
180
|
-
tx = Solace::Transaction.new(message:)
|
216
|
+
tx = Solace::Transaction.new(message: message)
|
181
217
|
tx.sign(payer, owner)
|
182
218
|
|
183
219
|
tx
|
184
220
|
end
|
221
|
+
# rubocop:enable Metrics/MethodLength
|
185
222
|
end
|
223
|
+
# rubocop:enable Metrics/ClassLength
|
186
224
|
end
|
187
225
|
end
|