@flashnet/sdk 0.3.3 → 0.3.6

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.
Files changed (66) hide show
  1. package/dist/cjs/index.d.ts +2 -1
  2. package/dist/cjs/index.d.ts.map +1 -1
  3. package/dist/cjs/index.js +17 -0
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/src/api/client.d.ts +11 -0
  6. package/dist/cjs/src/api/client.d.ts.map +1 -1
  7. package/dist/cjs/src/api/client.js.map +1 -1
  8. package/dist/cjs/src/api/typed-endpoints.d.ts +14 -0
  9. package/dist/cjs/src/api/typed-endpoints.d.ts.map +1 -1
  10. package/dist/cjs/src/api/typed-endpoints.js +18 -0
  11. package/dist/cjs/src/api/typed-endpoints.js.map +1 -1
  12. package/dist/cjs/src/client/FlashnetClient.d.ts +88 -7
  13. package/dist/cjs/src/client/FlashnetClient.d.ts.map +1 -1
  14. package/dist/cjs/src/client/FlashnetClient.js +240 -82
  15. package/dist/cjs/src/client/FlashnetClient.js.map +1 -1
  16. package/dist/cjs/src/config/index.d.ts +60 -2
  17. package/dist/cjs/src/config/index.d.ts.map +1 -1
  18. package/dist/cjs/src/config/index.js +184 -6
  19. package/dist/cjs/src/config/index.js.map +1 -1
  20. package/dist/cjs/src/types/index.d.ts +195 -61
  21. package/dist/cjs/src/types/index.d.ts.map +1 -1
  22. package/dist/cjs/src/types/index.js +19 -0
  23. package/dist/cjs/src/types/index.js.map +1 -1
  24. package/dist/cjs/src/utils/index.js +5 -0
  25. package/dist/cjs/src/utils/index.js.map +1 -1
  26. package/dist/cjs/src/utils/spark-address.d.ts +61 -13
  27. package/dist/cjs/src/utils/spark-address.d.ts.map +1 -1
  28. package/dist/cjs/src/utils/spark-address.js +192 -30
  29. package/dist/cjs/src/utils/spark-address.js.map +1 -1
  30. package/dist/cjs/src/utils/tokenAddress.d.ts +35 -0
  31. package/dist/cjs/src/utils/tokenAddress.d.ts.map +1 -0
  32. package/dist/cjs/src/utils/tokenAddress.js +111 -0
  33. package/dist/cjs/src/utils/tokenAddress.js.map +1 -0
  34. package/dist/esm/index.d.ts +2 -1
  35. package/dist/esm/index.d.ts.map +1 -1
  36. package/dist/esm/index.js +3 -2
  37. package/dist/esm/index.js.map +1 -1
  38. package/dist/esm/src/api/client.d.ts +11 -0
  39. package/dist/esm/src/api/client.d.ts.map +1 -1
  40. package/dist/esm/src/api/client.js.map +1 -1
  41. package/dist/esm/src/api/typed-endpoints.d.ts +14 -0
  42. package/dist/esm/src/api/typed-endpoints.d.ts.map +1 -1
  43. package/dist/esm/src/api/typed-endpoints.js +18 -0
  44. package/dist/esm/src/api/typed-endpoints.js.map +1 -1
  45. package/dist/esm/src/client/FlashnetClient.d.ts +88 -7
  46. package/dist/esm/src/client/FlashnetClient.d.ts.map +1 -1
  47. package/dist/esm/src/client/FlashnetClient.js +243 -85
  48. package/dist/esm/src/client/FlashnetClient.js.map +1 -1
  49. package/dist/esm/src/config/index.d.ts +60 -2
  50. package/dist/esm/src/config/index.d.ts.map +1 -1
  51. package/dist/esm/src/config/index.js +178 -7
  52. package/dist/esm/src/config/index.js.map +1 -1
  53. package/dist/esm/src/types/index.d.ts +195 -61
  54. package/dist/esm/src/types/index.d.ts.map +1 -1
  55. package/dist/esm/src/types/index.js +17 -1
  56. package/dist/esm/src/types/index.js.map +1 -1
  57. package/dist/esm/src/utils/index.js +1 -1
  58. package/dist/esm/src/utils/spark-address.d.ts +61 -13
  59. package/dist/esm/src/utils/spark-address.d.ts.map +1 -1
  60. package/dist/esm/src/utils/spark-address.js +188 -31
  61. package/dist/esm/src/utils/spark-address.js.map +1 -1
  62. package/dist/esm/src/utils/tokenAddress.d.ts +35 -0
  63. package/dist/esm/src/utils/tokenAddress.d.ts.map +1 -0
  64. package/dist/esm/src/utils/tokenAddress.js +106 -0
  65. package/dist/esm/src/utils/tokenAddress.js.map +1 -0
  66. package/package.json +3 -3
@@ -9,6 +9,7 @@ var auth = require('../utils/auth.js');
9
9
  var intents = require('../utils/intents.js');
10
10
  var signer = require('../utils/signer.js');
11
11
  var sparkAddress = require('../utils/spark-address.js');
12
+ var tokenAddress = require('../utils/tokenAddress.js');
12
13
 
13
14
  /**
14
15
  * FlashnetClient - A comprehensive client for interacting with Flashnet AMM
@@ -25,7 +26,8 @@ class FlashnetClient {
25
26
  apiClient;
26
27
  typedApi;
27
28
  authManager;
28
- network;
29
+ sparkNetwork;
30
+ clientEnvironment;
29
31
  publicKey = "";
30
32
  sparkAddress = "";
31
33
  isAuthenticated = false;
@@ -36,10 +38,27 @@ class FlashnetClient {
36
38
  return this._wallet;
37
39
  }
38
40
  /**
41
+ * Get the Spark network type (for blockchain operations)
42
+ */
43
+ get sparkNetworkType() {
44
+ return this.sparkNetwork;
45
+ }
46
+ /**
47
+ * Get the client environment (for API configuration)
48
+ */
49
+ get clientEnvironmentType() {
50
+ return this.clientEnvironment;
51
+ }
52
+ /**
53
+ * @deprecated Use sparkNetworkType instead
39
54
  * Get the network type
40
55
  */
41
56
  get networkType() {
42
- return this.network;
57
+ // Map Spark network back to legacy network type
58
+ // This is for backward compatibility
59
+ return this.sparkNetwork === "REGTEST" && this.clientEnvironment === "local"
60
+ ? "LOCAL"
61
+ : this.sparkNetwork;
43
62
  }
44
63
  /**
45
64
  * Get the wallet's public key
@@ -53,24 +72,87 @@ class FlashnetClient {
53
72
  get address() {
54
73
  return this.sparkAddress;
55
74
  }
56
- /**
57
- * Create a new FlashnetClient instance
58
- * @param wallet - The SparkWallet to use
59
- * @param options - Client options
60
- */
61
- constructor(wallet, _options = {}) {
75
+ constructor(wallet, configOrOptions) {
62
76
  this._wallet = wallet;
63
- // We'll initialize these in the init method
64
- // @ts-expect-error - wallet.config is protected
65
- const networkEnum = wallet.config.getNetwork();
66
- const networkName = index.Network[networkEnum];
67
- this.network = networkName === "MAINNET" ? "MAINNET" : "REGTEST";
68
- // panic if mainnet for now
69
- if (networkName === "MAINNET") {
77
+ // Determine configuration type and extract values
78
+ const isLegacyConfig = !configOrOptions || 'network' in configOrOptions ||
79
+ (!('sparkNetworkType' in configOrOptions));
80
+ if (isLegacyConfig) {
81
+ // Legacy configuration system - derive from wallet or options
82
+ const legacyConfig = configOrOptions;
83
+ if (legacyConfig?.network) {
84
+ // Use provided legacy network
85
+ this.sparkNetwork = index.getSparkNetworkFromLegacy(legacyConfig.network);
86
+ this.clientEnvironment = index.getClientEnvironmentFromLegacy(legacyConfig.network);
87
+ }
88
+ else {
89
+ // Auto-detect from wallet (existing behavior)
90
+ // @ts-expect-error - wallet.config is protected
91
+ const networkEnum = wallet.config.getNetwork();
92
+ const networkName = index.Network[networkEnum];
93
+ const detectedNetwork = networkName === "MAINNET" ? "MAINNET" : "REGTEST";
94
+ this.sparkNetwork = index.getSparkNetworkFromLegacy(detectedNetwork);
95
+ this.clientEnvironment = index.getClientEnvironmentFromLegacy(detectedNetwork);
96
+ }
97
+ }
98
+ else {
99
+ // New configuration system
100
+ const config = configOrOptions;
101
+ this.sparkNetwork = config.sparkNetworkType;
102
+ // Determine client configuration based on the specific config type
103
+ let clientConfig;
104
+ if ('clientConfig' in config) {
105
+ // FlashnetClientConfig - can be either environment or custom config
106
+ clientConfig = config.clientConfig;
107
+ }
108
+ else if ('clientNetworkConfig' in config) {
109
+ // FlashnetClientCustomConfig - custom configuration
110
+ clientConfig = config.clientNetworkConfig;
111
+ }
112
+ else if ('clientEnvironment' in config) {
113
+ // FlashnetClientEnvironmentConfig - predefined environment
114
+ clientConfig = config.clientEnvironment;
115
+ }
116
+ else {
117
+ throw new Error('Invalid configuration: must specify clientConfig, clientNetworkConfig, or clientEnvironment');
118
+ }
119
+ // Resolve the client environment name for internal tracking
120
+ const environmentName = index$1.getClientEnvironmentName(clientConfig);
121
+ this.clientEnvironment = environmentName === 'custom' ? 'local' : environmentName;
122
+ // Validate Spark network and client environment combination
123
+ const validation = index$1.validateNetworkCombination(this.sparkNetwork, this.clientEnvironment);
124
+ if (!validation.valid) {
125
+ throw new Error(`Invalid network combination: ${validation.error}`);
126
+ }
127
+ }
128
+ // Panic if mainnet for now
129
+ if (this.sparkNetwork === "MAINNET") {
70
130
  throw new Error("Mainnet is not supported yet");
71
131
  }
72
- const config = index$1.getNetworkConfig(this.network);
73
- this.apiClient = new client.ApiClient(config);
132
+ // Initialize API client with resolved client configuration
133
+ let resolvedClientConfig;
134
+ if (!isLegacyConfig) {
135
+ const config = configOrOptions;
136
+ let clientConfigParam;
137
+ if ('clientConfig' in config) {
138
+ clientConfigParam = config.clientConfig;
139
+ }
140
+ else if ('clientNetworkConfig' in config) {
141
+ clientConfigParam = config.clientNetworkConfig;
142
+ }
143
+ else if ('clientEnvironment' in config) {
144
+ clientConfigParam = config.clientEnvironment;
145
+ }
146
+ else {
147
+ throw new Error('Invalid configuration');
148
+ }
149
+ resolvedClientConfig = index$1.resolveClientNetworkConfig(clientConfigParam);
150
+ }
151
+ else {
152
+ // Use legacy resolution
153
+ resolvedClientConfig = index$1.getClientNetworkConfig(this.clientEnvironment);
154
+ }
155
+ this.apiClient = new client.ApiClient(resolvedClientConfig);
74
156
  this.typedApi = new typedEndpoints.TypedAmmApi(this.apiClient);
75
157
  this.authManager = new auth.AuthManager(this.apiClient, "", wallet);
76
158
  }
@@ -85,20 +167,21 @@ class FlashnetClient {
85
167
  // Get wallet details
86
168
  this.publicKey = await this._wallet.getIdentityPublicKey();
87
169
  this.sparkAddress = await this._wallet.getSparkAddress();
88
- // Deduce network from spark address
89
- const detectedNetwork = sparkAddress.getNetworkFromAddress(this.sparkAddress);
90
- if (!detectedNetwork) {
91
- throw new Error(`Unable to determine network from spark address: ${this.sparkAddress}`);
92
- }
93
- this.network = detectedNetwork;
94
- // panic if mainnet for now
95
- if (detectedNetwork === "MAINNET") {
170
+ // Deduce Spark network from spark address and validate consistency
171
+ const detectedSparkNetwork = sparkAddress.getSparkNetworkFromAddress(this.sparkAddress);
172
+ if (!detectedSparkNetwork) {
173
+ throw new Error(`Unable to determine Spark network from spark address: ${this.sparkAddress}`);
174
+ }
175
+ // Warn if configured Spark network doesn't match detected network
176
+ if (this.sparkNetwork !== detectedSparkNetwork) {
177
+ console.warn(`Warning: Configured Spark network (${this.sparkNetwork}) doesn't match detected network from address (${detectedSparkNetwork}). Using detected network.`);
178
+ this.sparkNetwork = detectedSparkNetwork;
179
+ }
180
+ // Panic if mainnet for now
181
+ if (this.sparkNetwork === "MAINNET") {
96
182
  throw new Error("Mainnet is not supported yet");
97
183
  }
98
- // Reinitialize API client with correct network
99
- const config = index$1.getNetworkConfig(this.network);
100
- this.apiClient = new client.ApiClient(config);
101
- this.typedApi = new typedEndpoints.TypedAmmApi(this.apiClient);
184
+ // Re-initialize auth manager with correct public key
102
185
  this.authManager = new auth.AuthManager(this.apiClient, this.publicKey, signer.createWalletSigner(this._wallet));
103
186
  // Authenticate
104
187
  const token = await this.authManager.authenticate();
@@ -113,6 +196,34 @@ class FlashnetClient {
113
196
  await this.initialize();
114
197
  }
115
198
  }
199
+ /**
200
+ * Ensure a token identifier is in human-readable (Bech32m) form expected by the Spark SDK.
201
+ * If the identifier is already human-readable or it is the BTC constant, it is returned unchanged.
202
+ * Otherwise, it is encoded from the raw hex form using the client's Spark network.
203
+ */
204
+ toHumanReadableTokenIdentifier(tokenIdentifier) {
205
+ if (tokenIdentifier === index$1.BTC_ASSET_PUBKEY) {
206
+ return tokenIdentifier;
207
+ }
208
+ if (tokenIdentifier.startsWith("btkn")) {
209
+ return tokenIdentifier;
210
+ }
211
+ return tokenAddress.encodeSparkHumanReadableTokenIdentifier(tokenIdentifier, this.sparkNetwork);
212
+ }
213
+ /**
214
+ * Convert a token identifier into the raw hex string form expected by the Flashnet backend.
215
+ * If the identifier is the BTC constant or already a hex string, it is returned unchanged.
216
+ * If it is in Bech32m human-readable form, it is decoded to hex.
217
+ */
218
+ toHexTokenIdentifier(tokenIdentifier) {
219
+ if (tokenIdentifier === index$1.BTC_ASSET_PUBKEY) {
220
+ return tokenIdentifier;
221
+ }
222
+ if (tokenIdentifier.startsWith("btkn")) {
223
+ return tokenAddress.decodeSparkHumanReadableTokenIdentifier(tokenIdentifier, this.sparkNetwork).tokenIdentifier;
224
+ }
225
+ return tokenIdentifier;
226
+ }
116
227
  /**
117
228
  * Get wallet balance including BTC and token balances
118
229
  */
@@ -122,14 +233,17 @@ class FlashnetClient {
122
233
  const tokenBalances = new Map();
123
234
  if (balance.tokenBalances) {
124
235
  for (const [tokenPubkey, tokenData] of balance.tokenBalances.entries()) {
236
+ const info = "tokenInfo" in tokenData
237
+ ? tokenData.tokenInfo
238
+ : tokenData.tokenMetadata;
125
239
  tokenBalances.set(tokenPubkey, {
126
240
  balance: BigInt(tokenData.balance),
127
241
  tokenInfo: {
128
- tokenPublicKey: tokenData.tokenInfo.tokenPublicKey,
129
- tokenName: tokenData.tokenInfo.tokenName,
130
- tokenSymbol: tokenData.tokenInfo.tokenSymbol,
131
- tokenDecimals: tokenData.tokenInfo.tokenDecimals,
132
- maxSupply: tokenData.tokenInfo.maxSupply,
242
+ tokenPublicKey: info.tokenPublicKey,
243
+ tokenName: info.tokenName,
244
+ tokenSymbol: (info.tokenSymbol || info.tokenTicker),
245
+ tokenDecimals: (info.tokenDecimals ?? info.decimals),
246
+ maxSupply: info.maxSupply,
133
247
  },
134
248
  });
135
249
  }
@@ -154,8 +268,10 @@ class FlashnetClient {
154
268
  // Check token balances
155
269
  if (requirements.tokens) {
156
270
  for (const [tokenPubkey, requiredAmount,] of requirements.tokens.entries()) {
157
- const tokenBalance = balance.tokenBalances.get(tokenPubkey);
158
- const available = tokenBalance?.balance ?? 0n;
271
+ // If direct lookup fails (possible representation mismatch), try the human-readable form
272
+ const hrKey = this.toHumanReadableTokenIdentifier(tokenPubkey);
273
+ const effectiveTokenBalance = balance.tokenBalances.get(tokenPubkey) ?? balance.tokenBalances.get(hrKey);
274
+ const available = effectiveTokenBalance?.balance ?? 0n;
159
275
  if (available < requiredAmount) {
160
276
  return {
161
277
  sufficient: false,
@@ -189,6 +305,13 @@ class FlashnetClient {
189
305
  const provider = providerPublicKey || this.publicKey;
190
306
  return this.typedApi.getLpPosition(poolId, provider);
191
307
  }
308
+ /**
309
+ * Get LP position details for a provider in a pool
310
+ */
311
+ async getAllLpPositions() {
312
+ await this.ensureInitialized();
313
+ return this.typedApi.getAllLpPositions();
314
+ }
192
315
  /**
193
316
  * Create a constant product pool
194
317
  */
@@ -221,8 +344,8 @@ class FlashnetClient {
221
344
  const nonce = index$2.generateNonce();
222
345
  const intentMessage = intents.generateConstantProductPoolInitializationIntentMessage({
223
346
  poolOwnerPublicKey: this.publicKey,
224
- assetAAddress: params.assetAAddress,
225
- assetBAddress: params.assetBAddress,
347
+ assetAAddress: this.toHexTokenIdentifier(params.assetAAddress),
348
+ assetBAddress: this.toHexTokenIdentifier(params.assetBAddress),
226
349
  lpFeeRateBps: params.lpFeeRateBps.toString(),
227
350
  totalHostFeeRateBps: params.totalHostFeeRateBps.toString(),
228
351
  nonce,
@@ -233,8 +356,8 @@ class FlashnetClient {
233
356
  // Create pool
234
357
  const request = {
235
358
  poolOwnerPublicKey: this.publicKey,
236
- assetAAddress: params.assetAAddress,
237
- assetBAddress: params.assetBAddress,
359
+ assetAAddress: this.toHexTokenIdentifier(params.assetAAddress),
360
+ assetBAddress: this.toHexTokenIdentifier(params.assetBAddress),
238
361
  lpFeeRateBps: params.lpFeeRateBps.toString(),
239
362
  totalHostFeeRateBps: params.totalHostFeeRateBps.toString(),
240
363
  hostNamespace: params.hostNamespace || "",
@@ -244,7 +367,7 @@ class FlashnetClient {
244
367
  const response = await this.typedApi.createConstantProductPool(request);
245
368
  // Add initial liquidity if specified
246
369
  if (params.initialLiquidity && response.poolId) {
247
- await this.addInitialLiquidity(response.poolId, params.assetAAddress, params.assetBAddress, params.initialLiquidity.assetAAmount, params.initialLiquidity.assetBAmount);
370
+ await this.addInitialLiquidity(response.poolId, params.assetAAddress, params.assetBAddress, params.initialLiquidity.assetAAmount.toString(), params.initialLiquidity.assetBAmount.toString());
248
371
  }
249
372
  return response;
250
373
  }
@@ -282,8 +405,8 @@ class FlashnetClient {
282
405
  const nonce = index$2.generateNonce();
283
406
  const intentMessage = intents.generatePoolInitializationIntentMessage({
284
407
  poolOwnerPublicKey: this.publicKey,
285
- assetAAddress: params.assetAAddress,
286
- assetBAddress: params.assetBAddress,
408
+ assetAAddress: this.toHexTokenIdentifier(params.assetAAddress),
409
+ assetBAddress: this.toHexTokenIdentifier(params.assetBAddress),
287
410
  assetAInitialReserve: params.assetAInitialReserve,
288
411
  graduationThresholdPct: params.assetAPctSoldAtGraduation.toString(),
289
412
  targetBRaisedAtGraduation: params.targetBRaisedAtGraduation,
@@ -296,8 +419,8 @@ class FlashnetClient {
296
419
  // Create pool
297
420
  const request = {
298
421
  poolOwnerPublicKey: this.publicKey,
299
- assetAAddress: params.assetAAddress,
300
- assetBAddress: params.assetBAddress,
422
+ assetAAddress: this.toHexTokenIdentifier(params.assetAAddress),
423
+ assetBAddress: this.toHexTokenIdentifier(params.assetBAddress),
301
424
  assetAInitialReserve: params.assetAInitialReserve,
302
425
  graduationThresholdPct: params.assetAPctSoldAtGraduation,
303
426
  targetBRaisedAtGraduation: params.targetBRaisedAtGraduation,
@@ -311,10 +434,10 @@ class FlashnetClient {
311
434
  // If pool creation was successful, handle the initial deposit
312
435
  if (createResponse.poolId) {
313
436
  try {
314
- // Transfer initial reserve to the pool
315
- const lpSparkAddress = sparkAddress.encodeSparkAddress({
437
+ // Transfer initial reserve to the pool using new address encoding
438
+ const lpSparkAddress = sparkAddress.encodeSparkAddressNew({
316
439
  identityPublicKey: createResponse.poolId,
317
- network: this.network,
440
+ network: this.sparkNetwork,
318
441
  });
319
442
  let assetATransferId;
320
443
  if (params.assetAAddress === index$1.BTC_ASSET_PUBKEY) {
@@ -326,7 +449,7 @@ class FlashnetClient {
326
449
  }
327
450
  else {
328
451
  assetATransferId = await this._wallet.transferTokens({
329
- tokenPublicKey: params.assetAAddress,
452
+ tokenIdentifier: this.toHumanReadableTokenIdentifier(params.assetAAddress),
330
453
  tokenAmount: BigInt(params.assetAInitialReserve),
331
454
  receiverSparkAddress: lpSparkAddress,
332
455
  });
@@ -413,10 +536,10 @@ class FlashnetClient {
413
536
  if (!balanceCheck.sufficient) {
414
537
  throw new Error(`Insufficient balance for swap: ${balanceCheck.message}`);
415
538
  }
416
- // Transfer assets to pool
417
- const lpSparkAddress = sparkAddress.encodeSparkAddress({
539
+ // Transfer assets to pool using new address encoding
540
+ const lpSparkAddress = sparkAddress.encodeSparkAddressNew({
418
541
  identityPublicKey: params.poolId,
419
- network: this.network,
542
+ network: this.sparkNetwork,
420
543
  });
421
544
  let transferId;
422
545
  if (params.assetInAddress === index$1.BTC_ASSET_PUBKEY) {
@@ -428,7 +551,7 @@ class FlashnetClient {
428
551
  }
429
552
  else {
430
553
  transferId = await this._wallet.transferTokens({
431
- tokenPublicKey: params.assetInAddress,
554
+ tokenIdentifier: this.toHumanReadableTokenIdentifier(params.assetInAddress),
432
555
  tokenAmount: BigInt(params.amountIn),
433
556
  receiverSparkAddress: lpSparkAddress,
434
557
  });
@@ -439,8 +562,8 @@ class FlashnetClient {
439
562
  userPublicKey: this.publicKey,
440
563
  lpIdentityPublicKey: params.poolId,
441
564
  assetInSparkTransferId: transferId,
442
- assetInTokenPublicKey: params.assetInAddress,
443
- assetOutTokenPublicKey: params.assetOutAddress,
565
+ assetInTokenPublicKey: this.toHexTokenIdentifier(params.assetInAddress),
566
+ assetOutTokenPublicKey: this.toHexTokenIdentifier(params.assetOutAddress),
444
567
  amountIn: params.amountIn.toString(),
445
568
  maxSlippageBps: params.maxSlippageBps.toString(),
446
569
  minAmountOut: params.minAmountOut,
@@ -453,8 +576,8 @@ class FlashnetClient {
453
576
  const request = {
454
577
  userPublicKey: this.publicKey,
455
578
  poolId: params.poolId,
456
- assetInAddress: params.assetInAddress,
457
- assetOutAddress: params.assetOutAddress,
579
+ assetInAddress: this.toHexTokenIdentifier(params.assetInAddress),
580
+ assetOutAddress: this.toHexTokenIdentifier(params.assetOutAddress),
458
581
  amountIn: params.amountIn.toString(),
459
582
  maxSlippageBps: params.maxSlippageBps?.toString(),
460
583
  minAmountOut: params.minAmountOut,
@@ -505,14 +628,14 @@ class FlashnetClient {
505
628
  if (!params.hops.length) {
506
629
  throw new Error("Route swap requires at least one hop");
507
630
  }
508
- // Transfer initial asset to first pool
631
+ // Transfer initial asset to first pool using new address encoding
509
632
  const firstPoolId = params.hops[0]?.poolId;
510
633
  if (!firstPoolId) {
511
634
  throw new Error("First pool ID is required");
512
635
  }
513
- const lpSparkAddress = sparkAddress.encodeSparkAddress({
636
+ const lpSparkAddress = sparkAddress.encodeSparkAddressNew({
514
637
  identityPublicKey: firstPoolId,
515
- network: this.network,
638
+ network: this.sparkNetwork,
516
639
  });
517
640
  let initialTransferId;
518
641
  if (params.initialAssetAddress === index$1.BTC_ASSET_PUBKEY) {
@@ -524,7 +647,7 @@ class FlashnetClient {
524
647
  }
525
648
  else {
526
649
  initialTransferId = await this._wallet.transferTokens({
527
- tokenPublicKey: params.initialAssetAddress,
650
+ tokenIdentifier: this.toHumanReadableTokenIdentifier(params.initialAssetAddress),
528
651
  tokenAmount: BigInt(params.inputAmount),
529
652
  receiverSparkAddress: lpSparkAddress,
530
653
  });
@@ -532,8 +655,8 @@ class FlashnetClient {
532
655
  // Prepare hops for validation
533
656
  const hops = params.hops.map((hop) => ({
534
657
  lpIdentityPublicKey: hop.poolId,
535
- inputAssetPublicKey: hop.assetInAddress,
536
- outputAssetPublicKey: hop.assetOutAddress,
658
+ inputAssetPublicKey: this.toHexTokenIdentifier(hop.assetInAddress),
659
+ outputAssetPublicKey: this.toHexTokenIdentifier(hop.assetOutAddress),
537
660
  hopIntegratorFeeRateBps: hop.hopIntegratorFeeRateBps !== undefined &&
538
661
  hop.hopIntegratorFeeRateBps !== null
539
662
  ? hop.hopIntegratorFeeRateBps.toString()
@@ -542,8 +665,8 @@ class FlashnetClient {
542
665
  // Convert hops and ensure integrator fee is always present
543
666
  const requestHops = params.hops.map((hop) => ({
544
667
  poolId: hop.poolId,
545
- assetInAddress: hop.assetInAddress,
546
- assetOutAddress: hop.assetOutAddress,
668
+ assetInAddress: this.toHexTokenIdentifier(hop.assetInAddress),
669
+ assetOutAddress: this.toHexTokenIdentifier(hop.assetOutAddress),
547
670
  hopIntegratorFeeRateBps: hop.hopIntegratorFeeRateBps !== undefined &&
548
671
  hop.hopIntegratorFeeRateBps !== null
549
672
  ? hop.hopIntegratorFeeRateBps.toString()
@@ -612,25 +735,25 @@ class FlashnetClient {
612
735
  tokens: new Map(),
613
736
  };
614
737
  if (pool.assetAAddress === index$1.BTC_ASSET_PUBKEY) {
615
- requirements.btc = params.assetAAmount;
738
+ requirements.btc = BigInt(params.assetAAmount);
616
739
  }
617
740
  else {
618
- requirements.tokens?.set(pool.assetAAddress, params.assetAAmount);
741
+ requirements.tokens?.set(pool.assetAAddress, BigInt(params.assetAAmount));
619
742
  }
620
743
  if (pool.assetBAddress === index$1.BTC_ASSET_PUBKEY) {
621
- requirements.btc = (requirements.btc || 0n) + params.assetBAmount;
744
+ requirements.btc = (requirements.btc || 0n) + BigInt(params.assetBAmount);
622
745
  }
623
746
  else {
624
- requirements.tokens?.set(pool.assetBAddress, params.assetBAmount);
747
+ requirements.tokens?.set(pool.assetBAddress, BigInt(params.assetBAmount));
625
748
  }
626
749
  const balanceCheck = await this.checkBalance(requirements);
627
750
  if (!balanceCheck.sufficient) {
628
751
  throw new Error(`Insufficient balance for adding liquidity: ${balanceCheck.message}`);
629
752
  }
630
- // Transfer assets to pool
631
- const lpSparkAddress = sparkAddress.encodeSparkAddress({
753
+ // Transfer assets to pool using new address encoding
754
+ const lpSparkAddress = sparkAddress.encodeSparkAddressNew({
632
755
  identityPublicKey: params.poolId,
633
- network: this.network,
756
+ network: this.sparkNetwork,
634
757
  });
635
758
  // Transfer asset A
636
759
  let assetATransferId;
@@ -643,8 +766,8 @@ class FlashnetClient {
643
766
  }
644
767
  else {
645
768
  assetATransferId = await this._wallet.transferTokens({
646
- tokenPublicKey: pool.assetAAddress,
647
- tokenAmount: params.assetAAmount,
769
+ tokenIdentifier: this.toHumanReadableTokenIdentifier(pool.assetAAddress),
770
+ tokenAmount: BigInt(params.assetAAmount),
648
771
  receiverSparkAddress: lpSparkAddress,
649
772
  });
650
773
  }
@@ -659,8 +782,8 @@ class FlashnetClient {
659
782
  }
660
783
  else {
661
784
  assetBTransferId = await this._wallet.transferTokens({
662
- tokenPublicKey: pool.assetBAddress,
663
- tokenAmount: params.assetBAmount,
785
+ tokenIdentifier: this.toHumanReadableTokenIdentifier(pool.assetBAddress),
786
+ tokenAmount: BigInt(params.assetBAmount),
664
787
  receiverSparkAddress: lpSparkAddress,
665
788
  });
666
789
  }
@@ -888,6 +1011,41 @@ class FlashnetClient {
888
1011
  const user = userPublicKey || this.publicKey;
889
1012
  return this.typedApi.getUserSwaps(user, query);
890
1013
  }
1014
+ // ===== Token Address Operations =====
1015
+ /**
1016
+ * Encode a token identifier into a human-readable token address using the client's Spark network
1017
+ * @param tokenIdentifier - Token identifier as hex string or Uint8Array
1018
+ * @returns Human-readable token address
1019
+ */
1020
+ encodeTokenAddress(tokenIdentifier) {
1021
+ return tokenAddress.encodeSparkHumanReadableTokenIdentifier(tokenIdentifier, this.sparkNetwork);
1022
+ }
1023
+ /**
1024
+ * Decode a human-readable token address back to its identifier
1025
+ * @param address - Human-readable token address
1026
+ * @returns Object containing the token identifier (as hex string) and Spark network
1027
+ */
1028
+ decodeTokenAddress(address) {
1029
+ return tokenAddress.decodeSparkHumanReadableTokenIdentifier(address, this.sparkNetwork);
1030
+ }
1031
+ /**
1032
+ * @deprecated Use encodeTokenAddress instead - this method uses legacy types
1033
+ * Encode a token identifier into a human-readable token address using legacy types
1034
+ * @param tokenIdentifier - Token identifier as hex string or Uint8Array
1035
+ * @returns Human-readable token address
1036
+ */
1037
+ encodeLegacyTokenAddress(tokenIdentifier) {
1038
+ return tokenAddress.encodeHumanReadableTokenIdentifier(tokenIdentifier, this.sparkNetwork);
1039
+ }
1040
+ /**
1041
+ * @deprecated Use decodeTokenAddress instead - this method uses legacy types
1042
+ * Decode a human-readable token address back to its identifier using legacy types
1043
+ * @param address - Human-readable token address
1044
+ * @returns Object containing the token identifier (as hex string) and network
1045
+ */
1046
+ decodeLegacyTokenAddress(address) {
1047
+ return tokenAddress.decodeHumanReadableTokenIdentifier(address, this.sparkNetwork);
1048
+ }
891
1049
  // ===== Status =====
892
1050
  /**
893
1051
  * Ping the settlement service
@@ -901,9 +1059,9 @@ class FlashnetClient {
901
1059
  * Helper method to add initial liquidity after pool creation
902
1060
  */
903
1061
  async addInitialLiquidity(poolId, assetATokenPublicKey, assetBTokenPublicKey, assetAAmount, assetBAmount) {
904
- const lpSparkAddress = sparkAddress.encodeSparkAddress({
1062
+ const lpSparkAddress = sparkAddress.encodeSparkAddressNew({
905
1063
  identityPublicKey: poolId,
906
- network: this.network,
1064
+ network: this.sparkNetwork,
907
1065
  });
908
1066
  // Transfer asset A
909
1067
  let assetATransferId;
@@ -916,8 +1074,8 @@ class FlashnetClient {
916
1074
  }
917
1075
  else {
918
1076
  assetATransferId = await this._wallet.transferTokens({
919
- tokenPublicKey: assetATokenPublicKey,
920
- tokenAmount: assetAAmount,
1077
+ tokenIdentifier: this.toHumanReadableTokenIdentifier(assetATokenPublicKey),
1078
+ tokenAmount: BigInt(assetAAmount),
921
1079
  receiverSparkAddress: lpSparkAddress,
922
1080
  });
923
1081
  }
@@ -932,8 +1090,8 @@ class FlashnetClient {
932
1090
  }
933
1091
  else {
934
1092
  assetBTransferId = await this._wallet.transferTokens({
935
- tokenPublicKey: assetBTokenPublicKey,
936
- tokenAmount: assetBAmount,
1093
+ tokenIdentifier: this.toHumanReadableTokenIdentifier(assetBTokenPublicKey),
1094
+ tokenAmount: BigInt(assetBAmount),
937
1095
  receiverSparkAddress: lpSparkAddress,
938
1096
  });
939
1097
  }