1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
112
113
114
115
116
117
118
119
120
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
146
147
148
149
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
185
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright (c) Subzero Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
//! Utility functions for working with Rialo currency units, networks,
//! wallet configuration, and key conversions.
//!
//! This module provides helper functions for common operations including:
//! - Currency conversion between kelvin and RLO
//! - Formatting amounts with appropriate units
//! - Network URL resolution
//! - Wallet and account identifier parsing
//! - Network validation
//! - Cryptographic key conversions between different formats
use Signer;
use crate;
use crate::;
/// Converts an amount between different currency units.
///
/// # Arguments
///
/// * `amount` - The amount to convert
/// * `from_unit` - The source unit (either "kelvin" or "RLO")
/// * `to_unit` - The target unit (either "kelvin" or "RLO")
///
/// # Returns
///
/// The converted amount as a floating point number
///
/// # Examples
///
/// ```
/// use rialo_cdk::utils::convert_amount;
/// use rialo_cdk::constants::{UNIT_KELVIN, UNIT_RLO};
///
/// // Convert 1,000,000,000 kelvin to RLO
/// let rol_amount = convert_amount(1_000_000_000, UNIT_KELVIN, UNIT_RLO);
/// assert_eq!(rol_amount, 1.0);
///
/// // Convert 2 RLO to kelvin
/// let kelvin_amount = convert_amount(2, UNIT_RLO, UNIT_KELVIN);
/// assert_eq!(kelvin_amount, 2_000_000_000.0);
/// ```
/// Formats an amount with the specified unit for human-readable display.
///
/// # Arguments
///
/// * `amount` - The amount to format
///
/// # Returns
///
/// A formatted string with the amount and unit
///
/// # Examples
///
/// ```
/// use rialo_cdk::utils::format_amount;
///
/// // Format 1,000,000,000 kelvin as RLO
/// let formatted = format_amount(1_000_000_000);
/// assert_eq!(formatted, "1.000000000 RLO");
/// ```
/// Parses a string in the "wallet.account" format into separate components.
///
/// # Arguments
///
/// * `input` - A string in the format "wallet_name.account_index" or just "wallet_name"
///
/// # Returns
///
/// A tuple containing:
/// - The wallet name as a String
/// - An `Option<usize>` containing the account index if provided, None otherwise
///
/// # Examples
///
/// ```
/// use rialo_cdk::utils::parse_wallet_account;
///
/// // Parse a wallet with an account index
/// let (wallet, account) = parse_wallet_account("my_wallet.2");
/// assert_eq!(wallet, "my_wallet");
/// assert_eq!(account, Some(2));
///
/// // Parse just a wallet name
/// let (wallet, account) = parse_wallet_account("my_wallet");
/// assert_eq!(wallet, "my_wallet");
/// assert_eq!(account, None);
/// ```
/// Validates if a given string is a supported network name.
///
/// # Arguments
///
/// * `network` - The network name to validate
///
/// # Returns
///
/// `true` if the network name is valid, `false` otherwise
///
/// # Examples
///
/// ```
/// use rialo_cdk::utils::is_valid_network;
/// use rialo_cdk::constants::NETWORK_MAINNET;
///
/// assert!(is_valid_network(NETWORK_MAINNET));
/// assert!(!is_valid_network("invalid_network"));
/// ```
/// Returns a list of all supported network names.
///
/// # Returns
///
/// A vector of network name string literals
///
/// # Examples
///
/// ```
/// use rialo_cdk::utils::get_supported_networks;
///
/// let networks = get_supported_networks();
/// assert!(networks.contains(&"mainnet"));
/// assert!(networks.contains(&"devnet"));
/// ```
/// Converts a CDK keyring keypair to a Solana SDK keypair.
///
/// The CDK uses ed25519-dalek SigningKey which returns 32 bytes (private key only)
/// from `as_bytes()`, while Solana SDK expects 64 bytes (32 private + 32 public).
/// This function handles the conversion by properly combining the private and public key bytes.
///
/// **Note**: This function requires the `bincode` feature to be enabled.
///
/// # Arguments
///
/// * `keyring` - The CDK keyring containing the keypair to convert
///
/// # Returns
///
/// A Solana SDK keypair that can be used with Solana transactions
///
/// # Errors
///
/// Returns `RialoError::InvalidInput` if:
/// - The keypair bytes have an unexpected length
/// - The Solana keypair creation fails
///
/// # Examples
///
/// ```rust,no_run
/// use rialo_cdk::{utils::convert_keyring_to_solana_keypair, keyring::Keyring};
///
/// async fn example(keyring: &Keyring) -> Result<(), Box<dyn std::error::Error>> {
/// let rialo_s_keypair = convert_keyring_to_solana_keypair(keyring)?;
/// // Use rialo_s_keypair with Solana SDK functions
/// Ok(())
/// }
/// ```
/// Deprecated: Use [`convert_keyring_to_solana_keypair`] instead.