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