forked from aibtcdev/skills
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitflow.ts
More file actions
1253 lines (1141 loc) · 40.2 KB
/
Copy pathbitflow.ts
File metadata and controls
1253 lines (1141 loc) · 40.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bun
/**
* Bitflow DEX skill CLI
* Token swaps, market data, routing, and Keeper automation on Bitflow (mainnet-only)
*
* Usage: bun run bitflow/bitflow.ts <subcommand> [options]
*/
import { Command } from "commander";
import { NETWORK, getExplorerTxUrl } from "../src/lib/config/networks.js";
import { getAccount, getWalletAddress } from "../src/lib/services/x402.service.js";
import { getWalletManager } from "../src/lib/services/wallet-manager.js";
import {
getBitflowService,
type BitflowToken,
type HodlmmActiveBinToleranceInput,
type HodlmmRelativeLiquidityBinInput,
type HodlmmRelativeWithdrawalInput,
type UnifiedBitflowRouteQuote,
} from "../src/lib/services/bitflow.service.js";
import { resolveFee } from "../src/lib/utils/fee.js";
import { printJson, handleError } from "../src/lib/utils/cli.js";
const HIGH_IMPACT_THRESHOLD = 0.05; // 5%
function parseJsonOption<T>(value: string, label: string): T {
try {
return JSON.parse(value) as T;
} catch {
throw new Error(`${label} must be valid JSON`);
}
}
function normalizeRelativeLiquidityBins(rawBins: unknown): HodlmmRelativeLiquidityBinInput[] {
if (!Array.isArray(rawBins) || rawBins.length === 0) {
throw new Error("--bins must be a non-empty JSON array");
}
return rawBins.map((bin, index) => {
if (!bin || typeof bin !== "object") {
throw new Error(`bins[${index}] must be an object`);
}
const value = bin as Record<string, unknown>;
const activeBinOffset = value.activeBinOffset ?? value.active_bin_offset;
const xAmount = value.xAmount ?? value.x_amount ?? 0;
const yAmount = value.yAmount ?? value.y_amount ?? 0;
if (typeof activeBinOffset !== "number") {
throw new Error(`bins[${index}].activeBinOffset must be a number`);
}
return {
activeBinOffset,
xAmount: String(xAmount),
yAmount: String(yAmount),
};
});
}
function normalizeRelativeWithdrawalPositions(rawPositions: unknown): HodlmmRelativeWithdrawalInput[] {
if (!Array.isArray(rawPositions) || rawPositions.length === 0) {
throw new Error("--positions must be a non-empty JSON array");
}
return rawPositions.map((position, index) => {
if (!position || typeof position !== "object") {
throw new Error(`positions[${index}] must be an object`);
}
const value = position as Record<string, unknown>;
const activeBinOffset = value.activeBinOffset ?? value.active_bin_offset;
const amount = value.amount;
const minXAmount = value.minXAmount ?? value.min_x_amount ?? 0;
const minYAmount = value.minYAmount ?? value.min_y_amount ?? 0;
if (typeof activeBinOffset !== "number") {
throw new Error(`positions[${index}].activeBinOffset must be a number`);
}
if (amount === undefined || amount === null) {
throw new Error(`positions[${index}].amount is required`);
}
return {
activeBinOffset,
amount: String(amount),
minXAmount: String(minXAmount),
minYAmount: String(minYAmount),
};
});
}
function normalizeActiveBinTolerance(raw: unknown): HodlmmActiveBinToleranceInput {
if (!raw || typeof raw !== "object") {
throw new Error("--active-bin-tolerance must be a JSON object");
}
const value = raw as Record<string, unknown>;
const expectedBinId = value.expectedBinId ?? value.expected_bin_id;
const maxDeviation = value.maxDeviation ?? value.max_deviation;
if (typeof expectedBinId !== "number") {
throw new Error("activeBinTolerance.expectedBinId must be a number");
}
if (maxDeviation === undefined || maxDeviation === null) {
throw new Error("activeBinTolerance.maxDeviation is required");
}
return {
expectedBinId,
maxDeviation: String(maxDeviation),
};
}
function summarizeUnifiedRoute(route: UnifiedBitflowRouteQuote) {
return {
source: route.source,
label: route.label,
executable: route.executable,
tokenPath: route.tokenPath,
dexPath: route.dexPath,
poolIds: route.poolIds,
poolContracts: route.poolContracts,
expectedAmountOut: route.amountOutHuman,
amountOutAtomic: route.amountOutAtomic,
priceImpact: route.priceImpact,
};
}
function summarizeQuoteOutput(quote: {
tokenIn: string;
tokenOut: string;
amountIn: string;
expectedAmountOut: string;
route: string[];
}) {
return {
tokenIn: quote.tokenIn,
tokenOut: quote.tokenOut,
amountIn: quote.amountIn,
expectedAmountOut: quote.expectedAmountOut,
route: quote.route,
};
}
async function getWriteAccount(walletPassword?: string) {
if (walletPassword) {
const walletManager = getWalletManager();
const walletId = await walletManager.getActiveWalletId();
if (!walletId) {
throw new Error("No active wallet configured. Create or switch to a wallet first.");
}
return walletManager.unlock(walletId, walletPassword);
}
return getAccount();
}
function formatScaledInteger(value: string | number, scale: number): string {
const negative = String(value).startsWith("-");
const digits = String(value).replace("-", "").replace(/^0+/, "") || "0";
const padded = digits.padStart(scale + 1, "0");
const whole = padded.slice(0, padded.length - scale);
const fraction = padded.slice(padded.length - scale).replace(/0+$/, "");
const result = fraction ? `${whole}.${fraction}` : whole;
return negative ? `-${result}` : result;
}
function invertDecimalString(value: string, decimals: number = 8): string | null {
const num = Number(value);
if (!Number.isFinite(num) || num === 0) return null;
return (1 / num).toFixed(decimals).replace(/0+$/, "").replace(/\.$/, "");
}
function createTokenSymbolLookup(tokens: BitflowToken[]) {
const lookup = new Map<string, string>();
for (const token of tokens) {
lookup.set(token.id, token.symbol);
lookup.set(token.contractId, token.symbol);
}
lookup.set("Stacks", "STX");
return lookup;
}
function resolveTokenLabel(tokenId: string, lookup: Map<string, string>): string {
return lookup.get(tokenId) || tokenId;
}
function matchesTickerSelector(
selector: string | undefined,
rawValue: string,
symbol: string
): boolean {
if (!selector) return true;
const normalizedSelector = selector.toLowerCase();
const selectorAliases = new Set([normalizedSelector]);
if (normalizedSelector.startsWith("token-")) {
selectorAliases.add(normalizedSelector.replace(/^token-/, ""));
}
if (normalizedSelector === "token-stx") {
selectorAliases.add("stacks");
selectorAliases.add("stx");
}
return (
Array.from(selectorAliases).some(
(candidate) =>
rawValue.toLowerCase() === candidate ||
symbol.toLowerCase() === candidate ||
rawValue.toLowerCase().includes(candidate)
)
);
}
function summarizeTickerEntry(
ticker: Record<string, string>,
symbolLookup: Map<string, string>
) {
const baseSymbol = resolveTokenLabel(ticker.base_currency, symbolLookup);
const targetSymbol = resolveTokenLabel(ticker.target_currency, symbolLookup);
return {
pair: `${baseSymbol}/${targetSymbol}`,
baseSymbol,
targetSymbol,
lastPrice: ticker.last_price,
bid: ticker.bid,
ask: ticker.ask,
high: ticker.high,
low: ticker.low,
baseVolume: ticker.base_volume,
targetVolume: ticker.target_volume,
liquidityUsd: ticker.liquidity_in_usd,
};
}
function summarizeHodlmmPool(pool: {
pool_id: string;
token_x: string;
token_y: string;
token_x_symbol?: string | null;
token_y_symbol?: string | null;
pool_name?: string | null;
bin_step: number;
active_bin: number;
pool_token?: string | null;
suggested?: boolean | null;
sbtc_incentives?: boolean | null;
}) {
const tokenXSymbol = pool.token_x_symbol || pool.token_x;
const tokenYSymbol = pool.token_y_symbol || pool.token_y;
return {
poolId: pool.pool_id,
pair: `${tokenXSymbol}/${tokenYSymbol}`,
tokenXSymbol,
tokenYSymbol,
binStepBps: pool.bin_step,
activeBinId: pool.active_bin,
poolContract: pool.pool_token,
suggested: pool.suggested,
sbtcIncentives: pool.sbtc_incentives,
};
}
function summarizeHodlmmBin(
bin: {
bin_id: number;
reserve_x: string;
reserve_y: string;
price?: string | null;
liquidity?: string | null;
},
pool: {
token_x_symbol?: string | null;
token_y_symbol?: string | null;
token_x_decimals?: number | null;
token_y_decimals?: number | null;
}
) {
const tokenXSymbol = pool.token_x_symbol || "tokenX";
const tokenYSymbol = pool.token_y_symbol || "tokenY";
const tokenXDecimals = pool.token_x_decimals ?? 6;
const tokenYDecimals = pool.token_y_decimals ?? 6;
const rawPrice = bin.price || "0";
const quotePerBase = formatScaledInteger(rawPrice, tokenYDecimals + 2);
const basePerQuote = invertDecimalString(quotePerBase);
return {
binId: bin.bin_id,
reserveX: formatScaledInteger(bin.reserve_x, tokenXDecimals),
reserveY: formatScaledInteger(bin.reserve_y, tokenYDecimals),
reserveXSymbol: tokenXSymbol,
reserveYSymbol: tokenYSymbol,
rawPrice,
approxPrice: {
quotePerBase,
quotePerBasePair: `${tokenYSymbol} per ${tokenXSymbol}`,
basePerQuote,
basePerQuotePair: `${tokenXSymbol} per ${tokenYSymbol}`,
note: `bin.price is raw atomic price. Approx ${tokenYSymbol}/${tokenXSymbol} = rawPrice / 10^(${tokenYDecimals} + 2)`,
},
rawLiquidity: bin.liquidity || "0",
};
}
// ---------------------------------------------------------------------------
// Program
// ---------------------------------------------------------------------------
const program = new Command();
program
.name("bitflow")
.description(
"Bitflow DEX: token swaps, market data, routing, and Keeper automation on Stacks. Mainnet-only. " +
"No API key required — uses public endpoints (500 req/min)."
)
.version("0.1.0");
// ---------------------------------------------------------------------------
// get-ticker
// ---------------------------------------------------------------------------
program
.command("get-ticker")
.description(
"Get market ticker data from Bitflow DEX. Returns price, volume, and liquidity data for all trading pairs. " +
"No API key required. Mainnet-only."
)
.option(
"--base-currency <contractId>",
"Optional: filter by base currency contract ID"
)
.option(
"--target-currency <contractId>",
"Optional: filter by target currency contract ID"
)
.action(
async (opts: { baseCurrency?: string; targetCurrency?: string }) => {
try {
if (NETWORK !== "mainnet") {
printJson({ error: "Bitflow is only available on mainnet", network: NETWORK });
return;
}
const bitflowService = getBitflowService(NETWORK);
const symbolLookup = createTokenSymbolLookup(await bitflowService.getAvailableTokens());
const tickers = await bitflowService.getTicker();
if (opts.baseCurrency || opts.targetCurrency) {
const filtered = tickers
.filter((ticker) => {
const baseSymbol = resolveTokenLabel(String(ticker.base_currency), symbolLookup);
const targetSymbol = resolveTokenLabel(String(ticker.target_currency), symbolLookup);
return (
matchesTickerSelector(opts.baseCurrency, String(ticker.base_currency), baseSymbol) &&
matchesTickerSelector(opts.targetCurrency, String(ticker.target_currency), targetSymbol)
);
})
.map((ticker) => summarizeTickerEntry(ticker as Record<string, string>, symbolLookup));
if (filtered.length === 0) {
printJson({
error: "Trading pair not found",
baseCurrency: opts.baseCurrency,
targetCurrency: opts.targetCurrency,
});
return;
}
printJson({
network: NETWORK,
pairCount: filtered.length,
tickers: filtered,
});
return;
}
const summarizedTickers = tickers.map((ticker) =>
summarizeTickerEntry(ticker as Record<string, string>, symbolLookup)
);
printJson({
network: NETWORK,
pairCount: tickers.length,
tickers: summarizedTickers.slice(0, 50),
note:
tickers.length > 50
? `Showing 50 of ${tickers.length} pairs`
: undefined,
});
} catch (error) {
handleError(error);
}
}
);
// ---------------------------------------------------------------------------
// get-tokens
// ---------------------------------------------------------------------------
program
.command("get-tokens")
.description(
"Get all available tokens for swapping on Bitflow. " +
"No API key required — uses public endpoints (500 req/min). Mainnet-only."
)
.action(async () => {
try {
if (NETWORK !== "mainnet") {
printJson({ error: "Bitflow is only available on mainnet", network: NETWORK });
return;
}
const bitflowService = getBitflowService(NETWORK);
const tokens = await bitflowService.getAvailableTokens();
printJson({
network: NETWORK,
tokenCount: tokens.length,
tokens,
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// get-hodlmm-pools
// ---------------------------------------------------------------------------
program
.command("get-hodlmm-pools")
.description(
"Get HODLMM pools from the Bitflow BFF API. Returns DLMM pool metadata for picking pool IDs before bin operations. Mainnet-only."
)
.option("--suggested", "Filter to suggested HODLMM pools")
.option("--sbtc-incentives", "Filter to pools with sBTC incentives")
.option("--limit <number>", "Maximum pools to return", "20")
.action(
async (opts: { suggested?: boolean; sbtcIncentives?: boolean; limit: string }) => {
try {
if (NETWORK !== "mainnet") {
printJson({ error: "Bitflow is only available on mainnet", network: NETWORK });
return;
}
const bitflowService = getBitflowService(NETWORK);
const pools = await bitflowService.getHodlmmPools({
suggested: opts.suggested,
sbtcIncentives: opts.sbtcIncentives,
limit: Number(opts.limit),
});
printJson({
network: NETWORK,
poolCount: pools.length,
pools: pools.map(summarizeHodlmmPool),
});
} catch (error) {
handleError(error);
}
}
);
// ---------------------------------------------------------------------------
// get-hodlmm-bins
// ---------------------------------------------------------------------------
program
.command("get-hodlmm-bins")
.description(
"Get all HODLMM bins for a pool from the Bitflow BFF API. Useful for selecting relative bin offsets before adding liquidity. Mainnet-only."
)
.requiredOption("--pool-id <poolId>", "HODLMM pool ID (e.g. dlmm_1)")
.option("--allow-fallback", "Enable on-chain fallback if indexed data is stale")
.action(async (opts: { poolId: string; allowFallback?: boolean }) => {
try {
if (NETWORK !== "mainnet") {
printJson({ error: "Bitflow is only available on mainnet", network: NETWORK });
return;
}
const bitflowService = getBitflowService(NETWORK);
const [bins, pool] = await Promise.all([
bitflowService.getHodlmmPoolBins(opts.poolId, opts.allowFallback),
bitflowService.getHodlmmPool(opts.poolId, opts.allowFallback),
]);
printJson({
network: NETWORK,
pool: summarizeHodlmmPool(pool),
units: {
reserveX: `${pool.token_x_symbol || "tokenX"} in atomic base units on-chain, shown here in human units`,
reserveY: `${pool.token_y_symbol || "tokenY"} in atomic base units on-chain, shown here in human units`,
rawPrice: `Raw HODLMM bin price from API`,
approxPrice: `${pool.token_y_symbol || "tokenY"} per ${pool.token_x_symbol || "tokenX"} derived from rawPrice / 10^(${pool.token_y_decimals ?? 6} + 2)`,
},
activeBinId: bins.active_bin_id,
activeBin: bins.bins
.filter((bin) => bin.bin_id === bins.active_bin_id)
.map((bin) => summarizeHodlmmBin(bin, pool))[0],
nearbyBins: bins.bins
.filter((bin) =>
bins.active_bin_id === undefined || bins.active_bin_id === null
? bin.bin_id < 7
: Math.abs(bin.bin_id - bins.active_bin_id) <= 3
)
.map((bin) => summarizeHodlmmBin(bin, pool)),
totalBinCount: bins.total_bins,
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// get-hodlmm-position-bins
// ---------------------------------------------------------------------------
program
.command("get-hodlmm-position-bins")
.description(
"Get your HODLMM position bins for a pool from the Bitflow BFF API. Mainnet-only."
)
.requiredOption("--pool-id <poolId>", "HODLMM pool ID (e.g. dlmm_1)")
.option("--address <stacksAddress>", "Stacks address (uses active wallet if not specified)")
.option("--fresh", "Bypass cache and fetch fresh position data")
.option("--allow-fallback", "Enable on-chain fallback if indexed data is stale")
.action(
async (opts: {
poolId: string;
address?: string;
fresh?: boolean;
allowFallback?: boolean;
}) => {
try {
if (NETWORK !== "mainnet") {
printJson({ error: "Bitflow is only available on mainnet", network: NETWORK });
return;
}
const bitflowService = getBitflowService(NETWORK);
const address = opts.address || (await getWalletAddress());
const [positions, pool] = await Promise.all([
bitflowService.getHodlmmUserPositionBins(address, opts.poolId, {
fresh: opts.fresh,
allowFallback: opts.allowFallback,
}),
bitflowService.getHodlmmPool(opts.poolId, opts.allowFallback),
]);
printJson({
network: NETWORK,
address,
poolId: opts.poolId,
pool: summarizeHodlmmPool(pool),
positions: positions.bins.map((bin) => {
const summary = summarizeHodlmmBin(
{
bin_id: bin.bin_id,
reserve_x: String(bin.reserve_x ?? "0"),
reserve_y: String(bin.reserve_y ?? "0"),
price: String(bin.price ?? "0"),
liquidity: String(bin.liquidity ?? "0"),
},
pool
);
return {
binId: bin.bin_id,
userLiquidity: String(bin.user_liquidity ?? "0"),
price: summary.approxPrice,
reserveX: summary.reserveX,
reserveY: summary.reserveY,
raw: bin,
};
}),
});
} catch (error) {
handleError(error);
}
}
);
// ---------------------------------------------------------------------------
// get-swap-targets
// ---------------------------------------------------------------------------
program
.command("get-swap-targets")
.description(
"Get possible swap target tokens for a given input token on Bitflow. " +
"Returns all tokens that can be received when swapping from the specified token. Mainnet-only."
)
.requiredOption(
"--token-id <contractId>",
"The input token ID (contract address)"
)
.action(async (opts: { tokenId: string }) => {
try {
if (NETWORK !== "mainnet") {
printJson({ error: "Bitflow is only available on mainnet", network: NETWORK });
return;
}
const bitflowService = getBitflowService(NETWORK);
const targets = await bitflowService.getPossibleSwapTargets(opts.tokenId);
printJson({
network: NETWORK,
inputToken: opts.tokenId,
targetCount: targets.length,
targets,
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// get-quote
// ---------------------------------------------------------------------------
program
.command("get-quote")
.description(
"Get a swap quote from Bitflow DEX. Returns expected output amount, best route, and price impact. Mainnet-only."
)
.requiredOption(
"--token-x <tokenId>",
"Input token ID (e.g. 'token-stx', 'token-sbtc')"
)
.requiredOption(
"--token-y <tokenId>",
"Output token ID (e.g. 'token-sbtc', 'token-USDCx-auto'; use 'token-aeusdc' only for explicit aeUSDC requests)"
)
.requiredOption(
"--amount-in <decimal>",
"Amount in human-readable decimal (e.g. 0.00015 for 15k sats sBTC, 21.0 for 21 STX)"
)
.action(
async (opts: { tokenX: string; tokenY: string; amountIn: string }) => {
try {
if (NETWORK !== "mainnet") {
printJson({ error: "Bitflow is only available on mainnet", network: NETWORK });
return;
}
const bitflowService = getBitflowService(NETWORK);
const quote = await bitflowService.getSwapQuote(
opts.tokenX,
opts.tokenY,
Number(opts.amountIn)
);
const priceImpact = quote.priceImpact;
const highImpactWarning =
priceImpact && priceImpact.combinedImpact > HIGH_IMPACT_THRESHOLD
? `High price impact detected (${priceImpact.combinedImpactPct}). Consider reducing trade size.`
: undefined;
printJson({
network: NETWORK,
quote: summarizeQuoteOutput(quote),
selectedRoute: quote.selectedRoute ? summarizeUnifiedRoute(quote.selectedRoute) : undefined,
bestExecutableRoute: quote.bestExecutableRoute
? summarizeUnifiedRoute(quote.bestExecutableRoute)
: undefined,
rankedRoutes: quote.rankedRoutes?.map(summarizeUnifiedRoute),
priceImpact,
executionWarning: quote.executionWarning,
highImpactWarning,
});
} catch (error) {
handleError(error);
}
}
);
// ---------------------------------------------------------------------------
// get-routes
// ---------------------------------------------------------------------------
program
.command("get-routes")
.description(
"Get all possible swap routes between two tokens on Bitflow. " +
"Includes multi-hop routes through intermediate tokens. Mainnet-only."
)
.requiredOption(
"--token-x <tokenId>",
"Input token ID (e.g. 'token-stx', 'token-sbtc')"
)
.requiredOption(
"--token-y <tokenId>",
"Output token ID (e.g. 'token-sbtc', 'token-USDCx-auto'; use 'token-aeusdc' only for explicit aeUSDC requests)"
)
.option(
"--amount-in <decimal>",
"Optional amount in human-readable decimal to rank routes by expected output"
)
.action(async (opts: { tokenX: string; tokenY: string; amountIn?: string }) => {
try {
if (NETWORK !== "mainnet") {
printJson({ error: "Bitflow is only available on mainnet", network: NETWORK });
return;
}
const bitflowService = getBitflowService(NETWORK);
const routes = await bitflowService.getAllRoutes(opts.tokenX, opts.tokenY, opts.amountIn ? Number(opts.amountIn) : undefined);
const unifiedRoutes = routes as UnifiedBitflowRouteQuote[];
printJson({
network: NETWORK,
tokenX: opts.tokenX,
tokenY: opts.tokenY,
amountIn: opts.amountIn,
routeCount: unifiedRoutes.length,
routes: unifiedRoutes.map((r) => {
const summary = summarizeUnifiedRoute(r);
if (opts.amountIn) return summary;
const { expectedAmountOut, amountOutAtomic, priceImpact, ...rest } = summary;
return rest;
}),
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// swap
// ---------------------------------------------------------------------------
program
.command("swap")
.description(
"Execute a token swap on Bitflow DEX. Automatically finds the best route across all Bitflow pools. " +
"Requires an unlocked wallet with sufficient token balance. Mainnet-only."
)
.requiredOption(
"--token-x <tokenId>",
"Input token ID (contract address)"
)
.requiredOption(
"--token-y <tokenId>",
"Output token ID (contract address)"
)
.requiredOption(
"--amount-in <decimal>",
"Amount in human-readable decimal (e.g. 0.00015 for 15k sats sBTC, 21.0 for 21 STX)"
)
.option(
"--slippage-tolerance <decimal>",
"Slippage tolerance as decimal (default 0.01 = 1%)",
"0.01"
)
.option(
"--fee <value>",
"Optional fee: 'low' | 'medium' | 'high' preset or micro-STX amount. If omitted, auto-estimated."
)
.option(
"--wallet-password <password>",
"Optional wallet password to unlock the active managed wallet for this command"
)
.option(
"--confirm-high-impact",
"Set to execute swaps with price impact above 5%"
)
.action(
async (opts: {
tokenX: string;
tokenY: string;
amountIn: string;
slippageTolerance: string;
fee?: string;
walletPassword?: string;
confirmHighImpact?: boolean;
}) => {
try {
if (NETWORK !== "mainnet") {
printJson({ error: "Bitflow is only available on mainnet", network: NETWORK });
return;
}
const bitflowService = getBitflowService(NETWORK);
const slippage = parseFloat(opts.slippageTolerance);
// Safety check: require explicit confirmation for high-impact swaps
const quote = await bitflowService.getSwapQuote(
opts.tokenX,
opts.tokenY,
Number(opts.amountIn)
);
const impact = quote.priceImpact;
if (
impact &&
impact.combinedImpact > HIGH_IMPACT_THRESHOLD &&
!opts.confirmHighImpact
) {
printJson({
error: "High price impact swap requires explicit confirmation",
message: `This swap has ${impact.combinedImpactPct} price impact (${impact.severity}). Use --confirm-high-impact to proceed.`,
quote,
threshold: `${(HIGH_IMPACT_THRESHOLD * 100).toFixed(0)}%`,
requiredFlag: "--confirm-high-impact",
});
return;
}
const account = await getWriteAccount(opts.walletPassword);
const resolvedFee = await resolveFee(opts.fee, NETWORK, "contract_call");
const result = await bitflowService.swap(
account,
opts.tokenX,
opts.tokenY,
Number(opts.amountIn),
slippage,
resolvedFee
);
printJson({
success: true,
txid: result.txid,
swap: {
tokenIn: opts.tokenX,
tokenOut: opts.tokenY,
amountIn: opts.amountIn,
slippageTolerance: slippage,
priceImpact: impact,
quotedBestRoute: quote.selectedRoute ? summarizeUnifiedRoute(quote.selectedRoute) : undefined,
executedRoute: quote.bestExecutableRoute
? summarizeUnifiedRoute(quote.bestExecutableRoute)
: undefined,
},
executionWarning: quote.executionWarning,
network: NETWORK,
explorerUrl: getExplorerTxUrl(result.txid, NETWORK),
});
} catch (error) {
handleError(error);
}
}
);
// ---------------------------------------------------------------------------
// add-liquidity-simple
// ---------------------------------------------------------------------------
program
.command("add-liquidity-simple")
.description(
"Add liquidity to HODLMM bins using relative offsets from the active bin. Uses Bitflow's simple mode so the transaction is more tolerant of active-bin movement. Requires an unlocked wallet. Mainnet-only."
)
.requiredOption("--pool-id <poolId>", "HODLMM pool ID (e.g. dlmm_1)")
.requiredOption(
"--bins <json>",
"JSON array of bins to add, e.g. '[{\"activeBinOffset\":0,\"xAmount\":\"0\",\"yAmount\":\"100000\"},{\"activeBinOffset\":1,\"xAmount\":\"100000\",\"yAmount\":\"0\"}]'"
)
.option(
"--active-bin-tolerance <json>",
"Optional JSON object like '{\"expectedBinId\":500,\"maxDeviation\":\"2\"}'"
)
.option(
"--slippage-tolerance <percent>",
"Slippage tolerance percentage used for min DLP and liquidity fee bounds (default 1)",
"1"
)
.option(
"--pool-contract <contractId>",
"Override pool core contract identifier if the API response is missing it"
)
.option(
"--x-token-contract <contractId>",
"Override token X contract identifier if needed"
)
.option(
"--y-token-contract <contractId>",
"Override token Y contract identifier if needed"
)
.option("--allow-fallback", "Enable on-chain fallback when reading pool/bin metadata")
.option(
"--fee <value>",
"Optional STX fee: 'low' | 'medium' | 'high' preset or micro-STX amount"
)
.option(
"--wallet-password <password>",
"Optional wallet password to unlock the active managed wallet for this command"
)
.action(
async (opts: {
poolId: string;
bins: string;
activeBinTolerance?: string;
slippageTolerance: string;
poolContract?: string;
xTokenContract?: string;
yTokenContract?: string;
allowFallback?: boolean;
fee?: string;
walletPassword?: string;
}) => {
try {
if (NETWORK !== "mainnet") {
printJson({ error: "Bitflow is only available on mainnet", network: NETWORK });
return;
}
const bitflowService = getBitflowService(NETWORK);
const account = await getWriteAccount(opts.walletPassword);
const bins = normalizeRelativeLiquidityBins(
parseJsonOption<unknown>(opts.bins, "--bins")
);
const activeBinTolerance = opts.activeBinTolerance
? normalizeActiveBinTolerance(
parseJsonOption<unknown>(opts.activeBinTolerance, "--active-bin-tolerance")
)
: undefined;
const resolvedFee = await resolveFee(opts.fee, NETWORK, "contract_call");
const result = await bitflowService.addHodlmmLiquiditySimple({
account,
poolId: opts.poolId,
bins,
activeBinTolerance,
slippageTolerance: Number(opts.slippageTolerance),
allowFallback: opts.allowFallback,
fee: resolvedFee,
poolContract: opts.poolContract,
xTokenContract: opts.xTokenContract,
yTokenContract: opts.yTokenContract,
});
printJson({
success: true,
network: NETWORK,
txid: result.txid,
poolId: result.poolId,
preparedBins: result.preparedBins,
explorerUrl: getExplorerTxUrl(result.txid, NETWORK),
});
} catch (error) {
handleError(error);
}
}
);
// ---------------------------------------------------------------------------
// withdraw-liquidity-simple
// ---------------------------------------------------------------------------
program
.command("withdraw-liquidity-simple")
.description(
"Withdraw HODLMM liquidity using relative offsets from the current active bin. Use get-hodlmm-position-bins and get-hodlmm-bins first to calculate the current offset for your position. Requires an unlocked wallet. Mainnet-only."
)
.requiredOption("--pool-id <poolId>", "HODLMM pool ID (e.g. dlmm_6)")
.requiredOption(
"--positions <json>",
"JSON array of positions to withdraw, e.g. '[{\"activeBinOffset\":5,\"amount\":\"392854\",\"minXAmount\":\"1999000\",\"minYAmount\":\"0\"}]'"
)
.option(
"--pool-contract <contractId>",
"Override pool contract identifier if needed"
)
.option(
"--x-token-contract <contractId>",
"Override token X contract identifier if needed"
)
.option(
"--y-token-contract <contractId>",
"Override token Y contract identifier if needed"
)
.option("--allow-fallback", "Enable on-chain fallback when reading pool/bin metadata")
.option(
"--fee <value>",
"Optional STX fee: 'low' | 'medium' | 'high' preset or micro-STX amount"
)
.option(
"--wallet-password <password>",
"Optional wallet password to unlock the active managed wallet for this command"
)
.action(
async (opts: {
poolId: string;
positions: string;
poolContract?: string;
xTokenContract?: string;
yTokenContract?: string;
allowFallback?: boolean;
fee?: string;
walletPassword?: string;
}) => {
try {
if (NETWORK !== "mainnet") {
printJson({ error: "Bitflow is only available on mainnet", network: NETWORK });
return;
}