@bitflowlabs/core-sdk 2.0.3 → 2.1.4

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 (38) hide show
  1. package/dist/src/BitflowSDK.d.ts +6 -4
  2. package/dist/src/BitflowSDK.js +439 -239
  3. package/dist/src/BitflowSDK.js.map +1 -1
  4. package/dist/src/helpers/callReadOnlyHelper.d.ts +7 -1
  5. package/dist/src/helpers/callReadOnlyHelper.js +201 -26
  6. package/dist/src/helpers/callReadOnlyHelper.js.map +1 -1
  7. package/dist/src/helpers/handleResultHelper.js.map +1 -1
  8. package/dist/src/test/testMethods.js +10 -0
  9. package/dist/src/test/testMethods.js.map +1 -1
  10. package/package.json +4 -3
  11. package/src/BitflowSDK.ts +532 -269
  12. package/src/helpers/callReadOnlyHelper.ts +258 -32
  13. package/src/helpers/handleResultHelper.ts +0 -2
  14. package/src/test/testMethods.ts +19 -0
  15. package/dist/src/test-get-user.d.ts +0 -1
  16. package/dist/src/test-get-user.js +0 -77
  17. package/dist/src/test-get-user.js.map +0 -1
  18. package/dist/src/test-keeper-routes.d.ts +0 -1
  19. package/dist/src/test-keeper-routes.js +0 -67
  20. package/dist/src/test-keeper-routes.js.map +0 -1
  21. package/dist/src/test-order.d.ts +0 -1
  22. package/dist/src/test-order.js +0 -71
  23. package/dist/src/test-order.js.map +0 -1
  24. package/dist/src/test-raw-token-response.d.ts +0 -1
  25. package/dist/src/test-raw-token-response.js +0 -79
  26. package/dist/src/test-raw-token-response.js.map +0 -1
  27. package/dist/src/test-sdk.d.ts +0 -1
  28. package/dist/src/test-sdk.js +0 -229
  29. package/dist/src/test-sdk.js.map +0 -1
  30. package/dist/src/test-token.fetch.d.ts +0 -1
  31. package/dist/src/test-token.fetch.js +0 -63
  32. package/dist/src/test-token.fetch.js.map +0 -1
  33. package/src/test-get-user.ts +0 -87
  34. package/src/test-keeper-routes.ts +0 -76
  35. package/src/test-order.ts +0 -83
  36. package/src/test-raw-token-response.ts +0 -124
  37. package/src/test-sdk.ts +0 -262
  38. package/src/test-token.fetch.ts +0 -72
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.BitflowSDK = void 0;
36
+ exports.BitflowSDK = exports.safeStringify = void 0;
37
37
  const network_1 = require("@stacks/network");
38
38
  const fetchDataHelper_1 = require("./helpers/fetchDataHelper");
39
39
  const callSwapHelper_1 = require("./helpers/callSwapHelper");
@@ -43,6 +43,10 @@ const getContractInterfaceAndFunction_1 = require("./helpers/getContractInterfac
43
43
  const config_1 = require("./config");
44
44
  const callGetSwapParams_1 = require("./helpers/callGetSwapParams");
45
45
  const keeperAPI_1 = require("./keeper/keeperAPI");
46
+ const safeStringify = (obj, indent = 2) => {
47
+ return JSON.stringify(obj, (_, value) => (typeof value === 'bigint' ? value.toString() + 'n' : value), indent);
48
+ };
49
+ exports.safeStringify = safeStringify;
46
50
  class BitflowSDK {
47
51
  constructor(config) {
48
52
  if (config)
@@ -58,19 +62,19 @@ class BitflowSDK {
58
62
  this.initializeContext();
59
63
  }
60
64
  async loadConnectDependencies() {
61
- if (typeof window === "undefined") {
62
- throw new Error("Connect features are only available in browser environments");
65
+ if (typeof window === 'undefined') {
66
+ throw new Error('Connect features are only available in browser environments');
63
67
  }
64
68
  try {
65
- const { getStacksProvider } = await Promise.resolve().then(() => __importStar(require("@stacks/connect")));
69
+ const { getStacksProvider } = await Promise.resolve().then(() => __importStar(require('@stacks/connect')));
66
70
  if (!this.stacksProvider) {
67
71
  this.stacksProvider = await getStacksProvider();
68
72
  }
69
73
  return this.stacksProvider;
70
74
  }
71
75
  catch (error) {
72
- console.error("Error loading Stacks Connect:", error);
73
- throw new Error("Failed to load Stacks Connect dependencies");
76
+ console.error('Error loading Stacks Connect:', error);
77
+ throw new Error('Failed to load Stacks Connect dependencies');
74
78
  }
75
79
  }
76
80
  async initializeContext() {
@@ -98,7 +102,7 @@ class BitflowSDK {
98
102
  // Use a separate cache key for keeper routes
99
103
  const cacheKey = `keeper_${tokenX}`;
100
104
  if (!this.context.swapOptions[cacheKey]) {
101
- this.context.swapOptions[cacheKey] = await (0, fetchPossibleSwap_1.fetchPossibleSwapsFromAPI)(tokenX, "KEEPER");
105
+ this.context.swapOptions[cacheKey] = await (0, fetchPossibleSwap_1.fetchPossibleSwapsFromAPI)(tokenX, 'KEEPER');
102
106
  }
103
107
  return this.context.swapOptions[cacheKey];
104
108
  }
@@ -134,40 +138,40 @@ class BitflowSDK {
134
138
  console.warn(`Skipping route ${routeIndex + 1} due to missing required properties:`, route.quoteData);
135
139
  continue;
136
140
  }
137
- const [contractDeployer, contractName] = contract.split(".");
141
+ const [contractDeployer, contractName] = contract.split('.');
138
142
  if (!this.context.contractInterfaces[contract]) {
139
143
  this.context.contractInterfaces[contract] =
140
144
  await (0, getContractInterfaceAndFunction_1.getContractInterfaceAndFunction)(contractDeployer, contractName, functionName);
141
145
  }
142
146
  const { interface: contractInterface, functionArgs } = this.context.contractInterfaces[contract];
143
147
  const params = { ...parameters };
144
- if ("dx" in params && params.dx === null) {
148
+ if ('dx' in params && params.dx === null) {
145
149
  params.dx = amountInput;
146
150
  }
147
- else if ("dy" in params && params.dy === null) {
151
+ else if ('dy' in params && params.dy === null) {
148
152
  params.dy = amountInput;
149
153
  }
150
- else if ("amount" in params && params.amount === null) {
154
+ else if ('amount' in params && params.amount === null) {
151
155
  params.amount = amountInput;
152
156
  }
153
- else if ("amt-in" in params && params["amt-in"] === null) {
154
- params["amt-in"] = amountInput;
157
+ else if ('amt-in' in params && params['amt-in'] === null) {
158
+ params['amt-in'] = amountInput;
155
159
  }
156
- else if ("amt-in-max" in params && params["amt-in-max"] === null) {
157
- params["amt-in-max"] = amountInput;
160
+ else if ('amt-in-max' in params && params['amt-in-max'] === null) {
161
+ params['amt-in-max'] = amountInput;
158
162
  }
159
- else if ("y-amount" in params && params["y-amount"] === null) {
160
- params["y-amount"] = amountInput;
161
- params["x-amount"] = amountInput;
163
+ else if ('y-amount' in params && params['y-amount'] === null) {
164
+ params['y-amount'] = amountInput;
165
+ params['x-amount'] = amountInput;
162
166
  }
163
- else if ("x-amount" in params && params["x-amount"] === null) {
164
- params["x-amount"] = amountInput;
167
+ else if ('x-amount' in params && params['x-amount'] === null) {
168
+ params['x-amount'] = amountInput;
165
169
  }
166
170
  else {
167
171
  params.dx = amountInput;
168
172
  }
169
173
  const { convertedResult, rawResult, tokenXDecimals, tokenYDecimals } = await (0, callReadOnlyHelper_1.callReadOnlyFunctionHelper)(contractDeployer, contractName, functionName, params, contractDeployer, tokenX, tokenY, route.swapData, this.context);
170
- if (typeof convertedResult === "number" && convertedResult > 0) {
174
+ if (typeof convertedResult === 'number' && convertedResult > 0) {
171
175
  const updatedQuoteData = {
172
176
  ...route.quoteData,
173
177
  parameters: { ...params },
@@ -178,63 +182,63 @@ class BitflowSDK {
178
182
  ...route.swapData.parameters,
179
183
  amount: params.amount ||
180
184
  params.dx ||
181
- params["amt-in"] ||
182
- params["amt-in-max"] ||
183
- params["y-amount"] ||
184
- params["x-amount"] ||
185
+ params['amt-in'] ||
186
+ params['amt-in-max'] ||
187
+ params['y-amount'] ||
188
+ params['x-amount'] ||
185
189
  params.dy,
186
190
  dx: params.amount ||
187
191
  params.dx ||
188
- params["amt-in"] ||
189
- params["amt-in-max"] ||
190
- params["y-amount"] ||
191
- params["x-amount"] ||
192
+ params['amt-in'] ||
193
+ params['amt-in-max'] ||
194
+ params['y-amount'] ||
195
+ params['x-amount'] ||
192
196
  params.dy,
193
197
  dy: params.amount ||
194
198
  params.dx ||
195
- params["amt-in"] ||
196
- params["amt-in-max"] ||
197
- params["y-amount"] ||
198
- params["x-amount"] ||
199
+ params['amt-in'] ||
200
+ params['amt-in-max'] ||
201
+ params['y-amount'] ||
202
+ params['x-amount'] ||
199
203
  params.dy,
200
- "amt-in": params.amount ||
204
+ 'amt-in': params.amount ||
201
205
  params.dx ||
202
- params["amt-in"] ||
203
- params["amt-in-max"] ||
204
- params["y-amount"] ||
205
- params["x-amount"] ||
206
+ params['amt-in'] ||
207
+ params['amt-in-max'] ||
208
+ params['y-amount'] ||
209
+ params['x-amount'] ||
206
210
  params.dy,
207
- "amt-in-max": params.amount ||
211
+ 'amt-in-max': params.amount ||
208
212
  params.dx ||
209
- params["amt-in"] ||
210
- params["amt-in-max"] ||
211
- params["y-amount"] ||
212
- params["x-amount"] ||
213
+ params['amt-in'] ||
214
+ params['amt-in-max'] ||
215
+ params['y-amount'] ||
216
+ params['x-amount'] ||
213
217
  params.dy,
214
- "y-amount": params.amount ||
218
+ 'y-amount': params.amount ||
215
219
  params.dx ||
216
- params["amt-in"] ||
217
- params["amt-in-max"] ||
218
- params["y-amount"] ||
219
- params["x-amount"] ||
220
+ params['amt-in'] ||
221
+ params['amt-in-max'] ||
222
+ params['y-amount'] ||
223
+ params['x-amount'] ||
220
224
  params.dy,
221
- "x-amount": params.amount ||
225
+ 'x-amount': params.amount ||
222
226
  params.dx ||
223
- params["amt-in"] ||
224
- params["amt-in-max"] ||
225
- params["y-amount"] ||
226
- params["x-amount"] ||
227
+ params['amt-in'] ||
228
+ params['amt-in-max'] ||
229
+ params['y-amount'] ||
230
+ params['x-amount'] ||
227
231
  params.dy,
228
- "min-received": rawResult,
229
- "min-dy": rawResult,
230
- "min-dz": rawResult,
231
- "min-dw": rawResult,
232
- "amt-out": rawResult,
233
- "amt-out-min": rawResult,
234
- "min-x-amount": rawResult,
235
- "min-dv": rawResult,
236
- "min-y-amount": rawResult,
237
- "min-dx": rawResult,
232
+ 'min-received': rawResult,
233
+ 'min-dy': rawResult,
234
+ 'min-dz': rawResult,
235
+ 'min-dw': rawResult,
236
+ 'amt-out': rawResult,
237
+ 'amt-out-min': rawResult,
238
+ 'min-x-amount': rawResult,
239
+ 'min-dv': rawResult,
240
+ 'min-y-amount': rawResult,
241
+ 'min-dx': rawResult,
238
242
  },
239
243
  };
240
244
  const quoteResult = {
@@ -255,7 +259,7 @@ class BitflowSDK {
255
259
  allRoutes.push(quoteResult);
256
260
  }
257
261
  else {
258
- throw new Error("Invalid quote result");
262
+ throw new Error('Invalid quote result');
259
263
  }
260
264
  }
261
265
  catch (error) {
@@ -287,8 +291,8 @@ class BitflowSDK {
287
291
  async getKeeperQuoteForRoute(tokenX, tokenY, amountInput) {
288
292
  var _a;
289
293
  const COMPATIBLE_DEX_PATHS = new Set([
290
- "BITFLOW_STABLE_XY_2",
291
- "BITFLOW_XYK_XY_2",
294
+ 'BITFLOW_STABLE_XY_2',
295
+ 'BITFLOW_XYK_XY_2',
292
296
  ]);
293
297
  const isCompatibleRoute = (dexPath) => {
294
298
  return dexPath.every((path) => COMPATIBLE_DEX_PATHS.has(path));
@@ -308,40 +312,40 @@ class BitflowSDK {
308
312
  console.warn(`Skipping route ${routeIndex + 1} due to missing required properties:`, route.quoteData);
309
313
  continue;
310
314
  }
311
- const [contractDeployer, contractName] = contract.split(".");
315
+ const [contractDeployer, contractName] = contract.split('.');
312
316
  if (!this.context.contractInterfaces[contract]) {
313
317
  this.context.contractInterfaces[contract] =
314
318
  await (0, getContractInterfaceAndFunction_1.getContractInterfaceAndFunction)(contractDeployer, contractName, functionName);
315
319
  }
316
320
  const { interface: contractInterface, functionArgs } = this.context.contractInterfaces[contract];
317
321
  const params = { ...parameters };
318
- if ("dx" in params && params.dx === null) {
322
+ if ('dx' in params && params.dx === null) {
319
323
  params.dx = amountInput;
320
324
  }
321
- else if ("dy" in params && params.dy === null) {
325
+ else if ('dy' in params && params.dy === null) {
322
326
  params.dy = amountInput;
323
327
  }
324
- else if ("amount" in params && params.amount === null) {
328
+ else if ('amount' in params && params.amount === null) {
325
329
  params.amount = amountInput;
326
330
  }
327
- else if ("amt-in" in params && params["amt-in"] === null) {
328
- params["amt-in"] = amountInput;
331
+ else if ('amt-in' in params && params['amt-in'] === null) {
332
+ params['amt-in'] = amountInput;
329
333
  }
330
- else if ("amt-in-max" in params && params["amt-in-max"] === null) {
331
- params["amt-in-max"] = amountInput;
334
+ else if ('amt-in-max' in params && params['amt-in-max'] === null) {
335
+ params['amt-in-max'] = amountInput;
332
336
  }
333
- else if ("y-amount" in params && params["y-amount"] === null) {
334
- params["y-amount"] = amountInput;
335
- params["x-amount"] = amountInput;
337
+ else if ('y-amount' in params && params['y-amount'] === null) {
338
+ params['y-amount'] = amountInput;
339
+ params['x-amount'] = amountInput;
336
340
  }
337
- else if ("x-amount" in params && params["x-amount"] === null) {
338
- params["x-amount"] = amountInput;
341
+ else if ('x-amount' in params && params['x-amount'] === null) {
342
+ params['x-amount'] = amountInput;
339
343
  }
340
344
  else {
341
345
  params.dx = amountInput;
342
346
  }
343
347
  const { convertedResult, rawResult, tokenXDecimals, tokenYDecimals } = await (0, callReadOnlyHelper_1.callReadOnlyFunctionHelper)(contractDeployer, contractName, functionName, params, contractDeployer, tokenX, tokenY, route.swapData, this.context);
344
- if (typeof convertedResult === "number" && convertedResult > 0) {
348
+ if (typeof convertedResult === 'number' && convertedResult > 0) {
345
349
  const updatedQuoteData = {
346
350
  ...route.quoteData,
347
351
  parameters: { ...params },
@@ -352,56 +356,56 @@ class BitflowSDK {
352
356
  ...route.swapData.parameters,
353
357
  amount: params.amount ||
354
358
  params.dx ||
355
- params["amt-in"] ||
356
- params["amt-in-max"] ||
357
- params["y-amount"] ||
358
- params["x-amount"] ||
359
+ params['amt-in'] ||
360
+ params['amt-in-max'] ||
361
+ params['y-amount'] ||
362
+ params['x-amount'] ||
359
363
  params.dy,
360
364
  dx: params.amount ||
361
365
  params.dx ||
362
- params["amt-in"] ||
363
- params["amt-in-max"] ||
364
- params["y-amount"] ||
365
- params["x-amount"] ||
366
+ params['amt-in'] ||
367
+ params['amt-in-max'] ||
368
+ params['y-amount'] ||
369
+ params['x-amount'] ||
366
370
  params.dy,
367
- "amt-in": params.amount ||
371
+ 'amt-in': params.amount ||
368
372
  params.dx ||
369
- params["amt-in"] ||
370
- params["amt-in-max"] ||
371
- params["y-amount"] ||
372
- params["x-amount"] ||
373
+ params['amt-in'] ||
374
+ params['amt-in-max'] ||
375
+ params['y-amount'] ||
376
+ params['x-amount'] ||
373
377
  params.dy,
374
- "amt-in-max": params.amount ||
378
+ 'amt-in-max': params.amount ||
375
379
  params.dx ||
376
- params["amt-in"] ||
377
- params["amt-in-max"] ||
378
- params["y-amount"] ||
379
- params["x-amount"] ||
380
+ params['amt-in'] ||
381
+ params['amt-in-max'] ||
382
+ params['y-amount'] ||
383
+ params['x-amount'] ||
380
384
  params.dy,
381
- "y-amount": params.amount ||
385
+ 'y-amount': params.amount ||
382
386
  params.dx ||
383
- params["amt-in"] ||
384
- params["amt-in-max"] ||
385
- params["y-amount"] ||
386
- params["x-amount"] ||
387
+ params['amt-in'] ||
388
+ params['amt-in-max'] ||
389
+ params['y-amount'] ||
390
+ params['x-amount'] ||
387
391
  params.dy,
388
- "x-amount": params.amount ||
392
+ 'x-amount': params.amount ||
389
393
  params.dx ||
390
- params["amt-in"] ||
391
- params["amt-in-max"] ||
392
- params["y-amount"] ||
393
- params["x-amount"] ||
394
+ params['amt-in'] ||
395
+ params['amt-in-max'] ||
396
+ params['y-amount'] ||
397
+ params['x-amount'] ||
394
398
  params.dy,
395
- "min-received": rawResult,
396
- "min-dy": rawResult,
397
- "min-dz": rawResult,
398
- "min-dw": rawResult,
399
- "amt-out": rawResult,
400
- "amt-out-min": rawResult,
401
- "min-x-amount": rawResult,
402
- "min-dv": rawResult,
403
- "min-y-amount": rawResult,
404
- "min-dx": rawResult,
399
+ 'min-received': rawResult,
400
+ 'min-dy': rawResult,
401
+ 'min-dz': rawResult,
402
+ 'min-dw': rawResult,
403
+ 'amt-out': rawResult,
404
+ 'amt-out-min': rawResult,
405
+ 'min-x-amount': rawResult,
406
+ 'min-dv': rawResult,
407
+ 'min-y-amount': rawResult,
408
+ 'min-dx': rawResult,
405
409
  },
406
410
  };
407
411
  const quoteResult = {
@@ -422,7 +426,174 @@ class BitflowSDK {
422
426
  allRoutes.push(quoteResult);
423
427
  }
424
428
  else {
425
- throw new Error("Invalid quote result");
429
+ throw new Error('Invalid quote result');
430
+ }
431
+ }
432
+ catch (error) {
433
+ console.warn(`Failed to get quote for route ${routeIndex + 1}:`, route, error);
434
+ allRoutes.push({
435
+ route,
436
+ quote: null,
437
+ params: route.quoteData
438
+ ? { ...route.quoteData.parameters, amountInput }
439
+ : { amountInput },
440
+ quoteData: route.quoteData,
441
+ swapData: route.swapData,
442
+ dexPath: route.dex_path,
443
+ tokenPath: route.token_path,
444
+ tokenXDecimals: route.tokenXDecimals,
445
+ tokenYDecimals: route.tokenYDecimals,
446
+ error: error.message,
447
+ });
448
+ }
449
+ }
450
+ allRoutes.sort((a, b) => (b.quote || 0) - (a.quote || 0));
451
+ const result = {
452
+ bestRoute: ((_a = allRoutes[0]) === null || _a === void 0 ? void 0 : _a.quote) !== null ? allRoutes[0] : null,
453
+ allRoutes,
454
+ inputData: { tokenX, tokenY, amountInput },
455
+ };
456
+ return result;
457
+ }
458
+ async getKeeperQuoteForRouteWithoutScaling(tokenX, tokenY, amountInput) {
459
+ var _a;
460
+ const COMPATIBLE_DEX_PATHS = new Set([
461
+ 'BITFLOW_STABLE_XY_2',
462
+ 'BITFLOW_XYK_XY_2',
463
+ ]);
464
+ const isCompatibleRoute = (dexPath) => {
465
+ return dexPath.every((path) => COMPATIBLE_DEX_PATHS.has(path));
466
+ };
467
+ let routes = await this.getAllPossibleTokenYRoutes(tokenX, tokenY);
468
+ routes = routes.filter((route) => isCompatibleRoute(route.dex_path));
469
+ const allRoutes = [];
470
+ for (let routeIndex = 0; routeIndex < routes.length; routeIndex++) {
471
+ const route = routes[routeIndex];
472
+ try {
473
+ if (!route.quoteData) {
474
+ console.warn(`Skipping route ${routeIndex + 1} due to null quoteData:`, route);
475
+ continue;
476
+ }
477
+ const { contract, function: functionName, parameters, } = route.quoteData;
478
+ if (!contract || !functionName || !parameters) {
479
+ console.warn(`Skipping route ${routeIndex + 1} due to missing required properties:`, route.quoteData);
480
+ continue;
481
+ }
482
+ const [contractDeployer, contractName] = contract.split('.');
483
+ if (!this.context.contractInterfaces[contract]) {
484
+ this.context.contractInterfaces[contract] =
485
+ await (0, getContractInterfaceAndFunction_1.getContractInterfaceAndFunction)(contractDeployer, contractName, functionName);
486
+ }
487
+ const { interface: contractInterface, functionArgs } = this.context.contractInterfaces[contract];
488
+ const params = { ...parameters };
489
+ if ('dx' in params && params.dx === null) {
490
+ params.dx = amountInput;
491
+ }
492
+ else if ('dy' in params && params.dy === null) {
493
+ params.dy = amountInput;
494
+ }
495
+ else if ('amount' in params && params.amount === null) {
496
+ params.amount = amountInput;
497
+ }
498
+ else if ('amt-in' in params && params['amt-in'] === null) {
499
+ params['amt-in'] = amountInput;
500
+ }
501
+ else if ('amt-in-max' in params && params['amt-in-max'] === null) {
502
+ params['amt-in-max'] = amountInput;
503
+ }
504
+ else if ('y-amount' in params && params['y-amount'] === null) {
505
+ params['y-amount'] = amountInput;
506
+ params['x-amount'] = amountInput;
507
+ }
508
+ else if ('x-amount' in params && params['x-amount'] === null) {
509
+ params['x-amount'] = amountInput;
510
+ }
511
+ else {
512
+ params.dx = amountInput;
513
+ }
514
+ const { convertedResult, rawResult, tokenXDecimals, tokenYDecimals } = await (0, callReadOnlyHelper_1.callReadOnlyFunctionHelperWithoutScaling)(contractDeployer, contractName, functionName, params, contractDeployer, tokenX, tokenY, route.swapData, this.context);
515
+ if (typeof convertedResult === 'number' && convertedResult > 0) {
516
+ const updatedQuoteData = {
517
+ ...route.quoteData,
518
+ parameters: { ...params },
519
+ };
520
+ const updatedSwapData = {
521
+ ...route.swapData,
522
+ parameters: {
523
+ ...route.swapData.parameters,
524
+ amount: params.amount ||
525
+ params.dx ||
526
+ params['amt-in'] ||
527
+ params['amt-in-max'] ||
528
+ params['y-amount'] ||
529
+ params['x-amount'] ||
530
+ params.dy,
531
+ dx: params.amount ||
532
+ params.dx ||
533
+ params['amt-in'] ||
534
+ params['amt-in-max'] ||
535
+ params['y-amount'] ||
536
+ params['x-amount'] ||
537
+ params.dy,
538
+ 'amt-in': params.amount ||
539
+ params.dx ||
540
+ params['amt-in'] ||
541
+ params['amt-in-max'] ||
542
+ params['y-amount'] ||
543
+ params['x-amount'] ||
544
+ params.dy,
545
+ 'amt-in-max': params.amount ||
546
+ params.dx ||
547
+ params['amt-in'] ||
548
+ params['amt-in-max'] ||
549
+ params['y-amount'] ||
550
+ params['x-amount'] ||
551
+ params.dy,
552
+ 'y-amount': params.amount ||
553
+ params.dx ||
554
+ params['amt-in'] ||
555
+ params['amt-in-max'] ||
556
+ params['y-amount'] ||
557
+ params['x-amount'] ||
558
+ params.dy,
559
+ 'x-amount': params.amount ||
560
+ params.dx ||
561
+ params['amt-in'] ||
562
+ params['amt-in-max'] ||
563
+ params['y-amount'] ||
564
+ params['x-amount'] ||
565
+ params.dy,
566
+ 'min-received': rawResult,
567
+ 'min-dy': rawResult,
568
+ 'min-dz': rawResult,
569
+ 'min-dw': rawResult,
570
+ 'amt-out': rawResult,
571
+ 'amt-out-min': rawResult,
572
+ 'min-x-amount': rawResult,
573
+ 'min-dv': rawResult,
574
+ 'min-y-amount': rawResult,
575
+ 'min-dx': rawResult,
576
+ },
577
+ };
578
+ const quoteResult = {
579
+ route: {
580
+ ...route,
581
+ quoteData: updatedQuoteData,
582
+ swapData: updatedSwapData,
583
+ },
584
+ quote: convertedResult,
585
+ params: params,
586
+ quoteData: updatedQuoteData,
587
+ swapData: updatedSwapData,
588
+ dexPath: route.dex_path,
589
+ tokenPath: route.token_path,
590
+ tokenXDecimals: tokenXDecimals,
591
+ tokenYDecimals: tokenYDecimals,
592
+ };
593
+ allRoutes.push(quoteResult);
594
+ }
595
+ else {
596
+ throw new Error('Invalid quote result');
426
597
  }
427
598
  }
428
599
  catch (error) {
@@ -462,37 +633,37 @@ class BitflowSDK {
462
633
  ...route.swapData.parameters,
463
634
  amount: route.swapData.parameters.amount ||
464
635
  amount ||
465
- route.swapData.parameters["amt-in"] ||
466
- route.swapData.parameters["amt-in-max"] ||
467
- route.swapData.parameters["y-amount"],
636
+ route.swapData.parameters['amt-in'] ||
637
+ route.swapData.parameters['amt-in-max'] ||
638
+ route.swapData.parameters['y-amount'],
468
639
  dx: route.swapData.parameters.dx ||
469
640
  amount ||
470
- route.swapData.parameters["amt-in"] ||
471
- route.swapData.parameters["amt-in-max"] ||
472
- route.swapData.parameters["y-amount"],
473
- "amt-in": route.swapData.parameters.dx ||
641
+ route.swapData.parameters['amt-in'] ||
642
+ route.swapData.parameters['amt-in-max'] ||
643
+ route.swapData.parameters['y-amount'],
644
+ 'amt-in': route.swapData.parameters.dx ||
474
645
  amount ||
475
- route.swapData.parameters["amt-in"] ||
476
- route.swapData.parameters["amt-in-max"] ||
477
- route.swapData.parameters["y-amount"] ||
646
+ route.swapData.parameters['amt-in'] ||
647
+ route.swapData.parameters['amt-in-max'] ||
648
+ route.swapData.parameters['y-amount'] ||
478
649
  route.swapData.parameters.dy,
479
- "amt-in-max": route.swapData.parameters.dx ||
650
+ 'amt-in-max': route.swapData.parameters.dx ||
480
651
  amount ||
481
- route.swapData.parameters["amt-in"] ||
482
- route.swapData.parameters["amt-in-max"] ||
483
- route.swapData.parameters["y-amount"] ||
652
+ route.swapData.parameters['amt-in'] ||
653
+ route.swapData.parameters['amt-in-max'] ||
654
+ route.swapData.parameters['y-amount'] ||
484
655
  route.swapData.parameters.dy,
485
- "y-amount": route.swapData.parameters.dx ||
656
+ 'y-amount': route.swapData.parameters.dx ||
486
657
  amount ||
487
- route.swapData.parameters["amt-in"] ||
488
- route.swapData.parameters["amt-in-max"] ||
489
- route.swapData.parameters["y-amount"] ||
658
+ route.swapData.parameters['amt-in'] ||
659
+ route.swapData.parameters['amt-in-max'] ||
660
+ route.swapData.parameters['y-amount'] ||
490
661
  route.swapData.parameters.dy,
491
662
  dy: route.swapData.parameters.dy ||
492
663
  amount ||
493
- route.swapData.parameters["amt-in"] ||
494
- route.swapData.parameters["amt-in-max"] ||
495
- route.swapData.parameters["y-amount"] ||
664
+ route.swapData.parameters['amt-in'] ||
665
+ route.swapData.parameters['amt-in-max'] ||
666
+ route.swapData.parameters['y-amount'] ||
496
667
  route.swapData.parameters.dy,
497
668
  },
498
669
  },
@@ -505,10 +676,10 @@ class BitflowSDK {
505
676
  }
506
677
  async executeSwap(swapExecutionData, senderAddress, slippageTolerance = 0.015, stacksProvider, onFinish, onCancel) {
507
678
  const swapParams = await this.getSwapParams(swapExecutionData, senderAddress, slippageTolerance);
508
- if (typeof window === "undefined") {
509
- throw new Error("executeSwap is only available in browser environments. " +
510
- "For Node.js environments, use getSwapParams to get the transaction parameters " +
511
- "and handle the transaction execution separately.");
679
+ if (typeof window === 'undefined') {
680
+ throw new Error('executeSwap is only available in browser environments. ' +
681
+ 'For Node.js environments, use getSwapParams to get the transaction parameters ' +
682
+ 'and handle the transaction execution separately.');
512
683
  }
513
684
  try {
514
685
  let provider;
@@ -518,7 +689,7 @@ class BitflowSDK {
518
689
  else {
519
690
  const loadedProvider = await this.loadConnectDependencies();
520
691
  if (!loadedProvider) {
521
- throw new Error("Failed to initialize Stacks provider");
692
+ throw new Error('Failed to initialize Stacks provider');
522
693
  }
523
694
  provider = loadedProvider;
524
695
  }
@@ -526,11 +697,11 @@ class BitflowSDK {
526
697
  }
527
698
  catch (error) {
528
699
  if (error instanceof Error &&
529
- error.message.includes("only available in browser environments")) {
700
+ error.message.includes('only available in browser environments')) {
530
701
  throw error;
531
702
  }
532
- console.error("Error executing swap:", error);
533
- throw new Error(`Failed to execute swap: ${error instanceof Error ? error.message : "Unknown error"}`);
703
+ console.error('Error executing swap:', error);
704
+ throw new Error(`Failed to execute swap: ${error instanceof Error ? error.message : 'Unknown error'}`);
534
705
  }
535
706
  }
536
707
  async prepareSwap(swapExecutionData, senderAddress, slippageTolerance = 0.015) {
@@ -541,7 +712,7 @@ class BitflowSDK {
541
712
  return await (0, keeperAPI_1.getOrCreateKeeperContractAPI)(params);
542
713
  }
543
714
  catch (error) {
544
- console.error("Error in BitflowSDK.getOrCreateKeeperContract:", error);
715
+ console.error('Error in BitflowSDK.getOrCreateKeeperContract:', error);
545
716
  throw error;
546
717
  }
547
718
  }
@@ -550,7 +721,7 @@ class BitflowSDK {
550
721
  return await (0, keeperAPI_1.getOrderAPI)(orderId);
551
722
  }
552
723
  catch (error) {
553
- console.error("Error in BitflowSDK.getOrder:", error);
724
+ console.error('Error in BitflowSDK.getOrder:', error);
554
725
  throw error;
555
726
  }
556
727
  }
@@ -559,7 +730,7 @@ class BitflowSDK {
559
730
  return await (0, keeperAPI_1.getUserAPI)(stacksAddress);
560
731
  }
561
732
  catch (error) {
562
- console.error("Error in BitflowSDK.getUser:", error);
733
+ console.error('Error in BitflowSDK.getUser:', error);
563
734
  throw error;
564
735
  }
565
736
  }
@@ -568,7 +739,7 @@ class BitflowSDK {
568
739
  return await (0, keeperAPI_1.createOrderAPI)(params);
569
740
  }
570
741
  catch (error) {
571
- console.error("Error in BitflowSDK.createOrder:", error);
742
+ console.error('Error in BitflowSDK.createOrder:', error);
572
743
  throw error;
573
744
  }
574
745
  }
@@ -577,24 +748,10 @@ class BitflowSDK {
577
748
  return await (0, keeperAPI_1.getQuoteAPI)(params);
578
749
  }
579
750
  catch (error) {
580
- console.error("Error in BitflowSDK.getQuote:", error);
751
+ console.error('Error in BitflowSDK.getQuote:', error);
581
752
  throw error;
582
753
  }
583
754
  }
584
- mapDexPathToActionTrait(dexPath) {
585
- const isXYK = dexPath.some((d) => d.toLowerCase().includes("xyk"));
586
- const isStable = dexPath.some((d) => d.toLowerCase().includes("stable"));
587
- if (isXYK && !isStable) {
588
- return "SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.keeper-action-1-v-1-1";
589
- }
590
- if (isXYK && isStable) {
591
- return "SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.keeper-action-2-v-1-1";
592
- }
593
- if (!isXYK && isStable) {
594
- return "SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.keeper-action-3-v-1-1";
595
- }
596
- throw new Error(`Unsupported DEX path: ${dexPath.join(", ")}`);
597
- }
598
755
  async transformRouteToActionArgs(route) {
599
756
  const actionFunctionArgs = {
600
757
  tokenList: {},
@@ -604,96 +761,139 @@ class BitflowSDK {
604
761
  };
605
762
  const swapData = route.swapData;
606
763
  if (!swapData || !swapData.parameters) {
607
- throw new Error("Invalid route data - missing swapData or swapData.parameters");
608
- }
609
- const availableTokens = await this.getAvailableTokens();
610
- const tokenPath = route.tokenPath;
611
- const expandedTokenPath = [];
612
- for (let i = 0; i < tokenPath.length - 1; i++) {
613
- expandedTokenPath.push(tokenPath[i]);
614
- if (i > 0 && i < tokenPath.length - 1) {
764
+ throw new Error('Invalid route data - missing swapData or swapData.parameters');
765
+ }
766
+ try {
767
+ const availableTokens = await this.getAvailableTokens();
768
+ const tokenPath = route.tokenPath;
769
+ const expandedTokenPath = [];
770
+ // Expand token path
771
+ for (let i = 0; i < tokenPath.length - 1; i++) {
615
772
  expandedTokenPath.push(tokenPath[i]);
773
+ if (i > 0 && i < tokenPath.length - 1) {
774
+ expandedTokenPath.push(tokenPath[i]);
775
+ }
616
776
  }
617
- }
618
- expandedTokenPath.push(tokenPath[tokenPath.length - 1]);
619
- const tokenList = {};
620
- expandedTokenPath.forEach((tokenId, index) => {
621
- let contractIdentifier;
622
- if (tokenId === "token-stx") {
623
- contractIdentifier =
624
- "SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.token-stx-v-1-2";
777
+ expandedTokenPath.push(tokenPath[tokenPath.length - 1]);
778
+ // Map tokens to contract identifiers
779
+ const tokenList = {};
780
+ for (let index = 0; index < expandedTokenPath.length; index++) {
781
+ const tokenId = expandedTokenPath[index];
782
+ let contractIdentifier;
783
+ if (tokenId === 'token-stx') {
784
+ contractIdentifier =
785
+ 'SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.token-stx-v-1-2';
786
+ }
787
+ else {
788
+ const token = availableTokens.find((t) => t.tokenId === tokenId);
789
+ if (!token || !token.tokenContract) {
790
+ throw new Error(`Could not find contract identifier for token ${tokenId}`);
791
+ }
792
+ contractIdentifier = token.tokenContract;
793
+ }
794
+ const key = String.fromCharCode(97 + index); // 97 = 'a'
795
+ tokenList[key] = contractIdentifier;
625
796
  }
626
- else {
627
- const token = availableTokens.find((t) => t.tokenId === tokenId);
628
- if (!token || !token.tokenContract) {
629
- throw new Error(`Could not find contract identifier for token ${tokenId}`);
797
+ actionFunctionArgs.tokenList = tokenList;
798
+ // Determine if this is a stableswap or XYK route
799
+ const dexPath = route.dexPath;
800
+ const isStableswapRoute = dexPath.some((path) => path.toLowerCase().includes('stable'));
801
+ // Transform pool lists
802
+ const transformPoolList = (pools) => {
803
+ if (typeof pools === 'string') {
804
+ return { a: pools };
805
+ }
806
+ if (Array.isArray(pools)) {
807
+ const poolObject = {};
808
+ const letters = 'abcdefghijklmnopqrstuvwxyz';
809
+ pools.forEach((pool, index) => {
810
+ poolObject[letters[index]] = pool;
811
+ });
812
+ return poolObject;
813
+ }
814
+ if (typeof pools === 'object' && pools !== null) {
815
+ return pools;
816
+ }
817
+ return {};
818
+ };
819
+ // Handle pools based on whether they're stableswap or XYK
820
+ if (swapData.parameters['pool-trait']) {
821
+ const poolIdentifier = swapData.parameters['pool-trait'];
822
+ const transformedPool = transformPoolList(poolIdentifier);
823
+ // Check if the pool name contains 'stableswap'
824
+ if (isStableswapRoute ||
825
+ (typeof poolIdentifier === 'string' &&
826
+ poolIdentifier.toLowerCase().includes('stableswap'))) {
827
+ actionFunctionArgs.stableswapPoolList = transformedPool;
828
+ }
829
+ else {
830
+ actionFunctionArgs.xykPoolList = transformedPool;
630
831
  }
631
- contractIdentifier = token.tokenContract;
632
832
  }
633
- const key = String.fromCharCode(97 + index); // 97 = 'a'
634
- tokenList[key] = contractIdentifier;
635
- });
636
- actionFunctionArgs.tokenList = tokenList;
637
- const transformPoolList = (pools) => {
638
- if (typeof pools === "string") {
639
- return { a: pools };
833
+ else {
834
+ // Handle specific pool types
835
+ if (swapData.parameters['xyk-pools']) {
836
+ actionFunctionArgs.xykPoolList = transformPoolList(swapData.parameters['xyk-pools']);
837
+ }
838
+ if (swapData.parameters['stableswap-pools']) {
839
+ actionFunctionArgs.stableswapPoolList = transformPoolList(swapData.parameters['stableswap-pools']);
840
+ }
640
841
  }
641
- if (Array.isArray(pools)) {
642
- const poolObject = {};
643
- const letters = "abcdefghijklmnopqrstuvwxyz";
644
- pools.forEach((pool, index) => {
645
- poolObject[letters[index]] = pool;
646
- });
647
- return poolObject;
842
+ if ('swaps-reversed' in swapData.parameters) {
843
+ actionFunctionArgs.boolList = {
844
+ a: swapData.parameters['swaps-reversed'].toString(),
845
+ };
648
846
  }
649
- if (typeof pools === "object" && pools !== null) {
650
- return pools;
847
+ else {
848
+ delete actionFunctionArgs.boolList;
651
849
  }
652
- return {};
653
- };
654
- if (swapData.parameters["xyk-pools"]) {
655
- actionFunctionArgs.xykPoolList = transformPoolList(swapData.parameters["xyk-pools"]);
850
+ const trait = this.mapDexPathToActionTrait(dexPath);
851
+ actionFunctionArgs.actionTrait = trait;
852
+ return actionFunctionArgs;
656
853
  }
657
- else if (swapData.parameters["pool-trait"]) {
658
- actionFunctionArgs.xykPoolList = transformPoolList(swapData.parameters["pool-trait"]);
854
+ catch (error) {
855
+ throw error;
659
856
  }
660
- if (swapData.parameters["stableswap-pools"]) {
661
- actionFunctionArgs.stableswapPoolList =
662
- swapData.parameters["stableswap-pools"];
857
+ }
858
+ mapDexPathToActionTrait(dexPath) {
859
+ const isXYK = dexPath.some((d) => d.toLowerCase().includes('xyk'));
860
+ const isStable = dexPath.some((d) => d.toLowerCase().includes('stable'));
861
+ if (isXYK && !isStable) {
862
+ return 'SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.keeper-action-1-v-1-1';
663
863
  }
664
- if ("swaps-reversed" in swapData.parameters) {
665
- actionFunctionArgs.boolList = {
666
- a: swapData.parameters["swaps-reversed"].toString(),
667
- };
864
+ if (isXYK && isStable) {
865
+ return 'SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.keeper-action-2-v-1-1';
668
866
  }
669
- else {
670
- delete actionFunctionArgs.boolList;
867
+ if (!isXYK && isStable) {
868
+ return 'SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.keeper-action-3-v-1-1';
671
869
  }
672
- const dexPath = route.dexPath;
673
- const trait = this.mapDexPathToActionTrait(dexPath);
674
- actionFunctionArgs.actionTrait = trait;
675
- return actionFunctionArgs;
870
+ throw new Error(`Unsupported DEX path: ${dexPath.join(', ')}`);
676
871
  }
677
872
  async getKeeperAggregatorRouteData(tokenX, tokenY, amountX) {
678
- const quoteResult = await this.getKeeperQuoteForRoute(tokenX, tokenY, amountX);
679
- if (!quoteResult ||
680
- !quoteResult.allRoutes ||
681
- quoteResult.allRoutes.length === 0) {
682
- throw new Error("No routes found");
683
- }
684
- const { bestRoute } = quoteResult;
685
- if (!bestRoute) {
686
- throw new Error("No best route found for keeper-compatible DEX paths");
687
- }
688
- const actionFunctionArgs = this.transformRouteToActionArgs(bestRoute);
689
- return actionFunctionArgs;
873
+ try {
874
+ const quoteResult = await this.getKeeperQuoteForRouteWithoutScaling(tokenX, tokenY, amountX);
875
+ if (!quoteResult ||
876
+ !quoteResult.allRoutes ||
877
+ quoteResult.allRoutes.length === 0) {
878
+ throw new Error('No routes found');
879
+ }
880
+ const { bestRoute } = quoteResult;
881
+ if (!bestRoute) {
882
+ throw new Error('No best route found for keeper-compatible DEX paths');
883
+ }
884
+ const actionFunctionArgs = await this.transformRouteToActionArgs(bestRoute);
885
+ return actionFunctionArgs;
886
+ }
887
+ catch (error) {
888
+ throw error;
889
+ }
690
890
  }
691
891
  async createGroupOrder(params) {
692
892
  try {
693
893
  return await (0, keeperAPI_1.createGroupOrderAPI)(params);
694
894
  }
695
895
  catch (error) {
696
- console.error("Error in BitflowSDK.createGroupOrder:", error);
896
+ console.error('Error in BitflowSDK.createGroupOrder:', error);
697
897
  throw error;
698
898
  }
699
899
  }
@@ -702,7 +902,7 @@ class BitflowSDK {
702
902
  return await (0, keeperAPI_1.getGroupOrderAPI)(groupId, includeOrders);
703
903
  }
704
904
  catch (error) {
705
- console.error("Error in BitflowSDK.getGroupOrder:", error);
905
+ console.error('Error in BitflowSDK.getGroupOrder:', error);
706
906
  throw error;
707
907
  }
708
908
  }
@@ -711,7 +911,7 @@ class BitflowSDK {
711
911
  return await (0, keeperAPI_1.cancelOrderAPI)(orderId);
712
912
  }
713
913
  catch (error) {
714
- console.error("Error in BitflowSDK.cancelOrder:", error);
914
+ console.error('Error in BitflowSDK.cancelOrder:', error);
715
915
  throw error;
716
916
  }
717
917
  }
@@ -720,7 +920,7 @@ class BitflowSDK {
720
920
  return await (0, keeperAPI_1.cancelGroupOrderAPI)(groupId);
721
921
  }
722
922
  catch (error) {
723
- console.error("Error in BitflowSDK.cancelGroupOrder:", error);
923
+ console.error('Error in BitflowSDK.cancelGroupOrder:', error);
724
924
  throw error;
725
925
  }
726
926
  }