-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusdcx-yield-optimizer.ts
More file actions
1133 lines (982 loc) · 43.5 KB
/
Copy pathusdcx-yield-optimizer.ts
File metadata and controls
1133 lines (982 loc) · 43.5 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
/**
* USDCx Yield Optimizer
* The first skill that treats USDCx as a primary yield asset.
* Ranks every live USDCx venue on Bitflow (7 HODLMM pools, XYK),
* checks sBTC reserve health for volatile pairs, applies a Yield-to-Gas profit gate,
* and suggests cross-protocol routes (Hermetica) when swap yields beat direct venues.
*
* Write-ready. Generates call_contract deployment specs for HODLMM liquidity router
* with --confirm. Spec-only until MCP adds trait_reference support.
* Dry-run by default. Max 5,000 USDCx per deployment (enforced in code).
*
* Usage:
* bun run usdcx-yield-optimizer/usdcx-yield-optimizer.ts doctor
* bun run usdcx-yield-optimizer/usdcx-yield-optimizer.ts run
* bun run usdcx-yield-optimizer/usdcx-yield-optimizer.ts run --amount 1000 --risk low
* bun run usdcx-yield-optimizer/usdcx-yield-optimizer.ts run --amount 1000 --confirm
* bun run usdcx-yield-optimizer/usdcx-yield-optimizer.ts run --from zest --amount 500
*
* Output: strict JSON { status, decision, direct_venues, suggested_routes, risk_assessment, profit_gate, action, ... }
*/
import { Command } from "commander";
// ── Safety guardrails (enforced in code, not just docs) ────────────────────
const MIN_TVL_USD = 50_000; // skip pools below this TVL
const MIN_APY_IMPROVEMENT_PCT = 1.0; // never recommend for <1% APY gain
const PROFIT_GATE_MULTIPLIER = 3; // 7d gain must exceed this x gas cost
const MAX_SANE_APR = 500; // flag anything above as suspicious
const GAS_CALLS_PER_MIGRATION = 2; // 1 withdraw + 1 deposit
const GAS_BYTES_PER_CALL = 400; // estimated bytes per contract call
const FALLBACK_FEE_UESTX = 4000; // fallback if fee API unavailable
const SBTC_DEV_GREEN_PCT = 0.5; // price deviation < 0.5% = GREEN
const SBTC_DEV_YELLOW_PCT = 2.0; // price deviation < 2% = YELLOW, else RED
const HERMETICA_SWAP_COST_PCT = 0.3; // estimated swap cost USDCx -> sBTC
const USDCX_CONTRACT = "SP120SBRBQJ00MCWS7TM5R8WJNTTKD5K0HFRC2CNE.usdcx";
// ── API endpoints (Bitflow-native — no external price oracles) ─────────────
const BITFLOW_APP_POOLS = "https://bff.bitflowapis.finance/api/app/v1/pools";
const BITFLOW_TICKER = "https://bitflow-sdk-api-gateway-7owjsmt8.uc.gateway.dev/ticker";
const HIRO_API = "https://api.mainnet.hiro.so";
const HIRO_FEE_RATE = "https://api.mainnet.hiro.so/v2/fees/transfer";
const HERMETICA_STAKING = "SPN5AKG35QZSK2M8GAMR4AFX45659RJHDW353HSG";
// ── Types ──────────────────────────────────────────────────────────────────
type RiskLevel = "low" | "medium" | "high";
type Decision = "DEPLOY" | "HOLD" | "AVOID";
type ReserveSignal = "GREEN" | "YELLOW" | "RED" | "DATA_UNAVAILABLE";
interface DirectVenue {
rank: number;
protocol: string;
pool_id: string;
pair: string;
apr_pct: number;
tvl_usd: number;
risk: RiskLevel;
risk_factors: string[];
}
interface SuggestedRoute {
destination: string;
estimated_apy_pct: number;
swap_path: string;
swap_cost_pct: number;
net_apy_pct: number;
risk: RiskLevel;
note: string;
}
interface RiskAssessment {
sbtc_reserve_signal: ReserveSignal;
sbtc_price_deviation_pct: number;
flagged_pools: string[];
}
interface ProfitGate {
rule: string;
current_venue: string;
best_venue: string;
"7d_extra_yield_usd": number;
gas_cost_usd: number;
passed: boolean;
reason: string;
}
interface McpCommand {
step: number;
tool: string;
description: string;
params: Record<string, unknown>;
}
interface OptimizerResult {
status: "ok" | "degraded" | "error";
decision: Decision;
direct_venues: DirectVenue[];
suggested_routes: SuggestedRoute[];
risk_assessment: RiskAssessment;
profit_gate: ProfitGate | null;
mcp_commands: McpCommand[];
action: string;
sources_used: string[];
sources_failed: string[];
timestamp: string;
error?: string;
}
// ── Helpers ────────────────────────────────────────────────────────────────
async function fetchJson(url: string, opts: RequestInit = {}, timeoutMs = 12_000): Promise<unknown> {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
try {
const res = await fetch(url, {
...opts,
signal: controller.signal,
headers: { "User-Agent": "bff-skills/usdcx-yield-optimizer", ...(opts.headers as Record<string, string> ?? {}) },
});
if (res.status === 429) {
await new Promise(r => setTimeout(r, 1000));
const retry = await fetch(url, {
...opts,
headers: { "User-Agent": "bff-skills/usdcx-yield-optimizer", ...(opts.headers as Record<string, string> ?? {}) },
});
if (!retry.ok) throw new Error(`HTTP ${retry.status} (after 429 retry)`);
return retry.json();
}
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json();
} finally {
clearTimeout(timer);
}
}
function sanitiseApr(raw: number, label: string): number {
if (!isFinite(raw) || raw < 0) return 0;
if (raw > MAX_SANE_APR) throw new Error(`${label} APR ${raw.toFixed(2)}% exceeds sanity cap ${MAX_SANE_APR}%`);
return Math.round(raw * 100) / 100;
}
function riskFromTvl(tvl: number): RiskLevel {
return tvl >= 500_000 ? "low" : tvl >= 100_000 ? "medium" : "high";
}
// ── Data fetchers ──────────────────────────────────────────────────────────
interface Prices { btc: number; stx: number; sbtcPriceBtc: number }
// Prices extracted from Bitflow App API pool data — no external oracle needed.
// The HODLMM pool tokens include priceUsd and priceBtc from Bitflow's own feed.
let _cachedPrices: Prices | null = null;
async function fetchPrices(): Promise<Prices> {
if (_cachedPrices) return _cachedPrices;
const resp = await fetchJson(BITFLOW_APP_POOLS) as AppPoolsResponse;
const pools = resp.data ?? [];
let btc = 0, stx = 0, sbtcPriceBtc = 1;
for (const pool of pools) {
const tx = pool.tokens?.tokenX;
const ty = pool.tokens?.tokenY;
// Extract sBTC price from any sBTC/USDCx pool
if (tx?.symbol === "sBTC" && tx.priceUsd && tx.priceUsd > 0) {
btc = tx.priceUsd;
sbtcPriceBtc = tx.priceBtc ?? 1;
}
// Extract STX price from any STX/USDCx pool
if (tx?.symbol === "STX" && tx.priceUsd && tx.priceUsd > 0) {
stx = tx.priceUsd;
}
if (btc > 0 && stx > 0) break;
}
_cachedPrices = { btc, stx, sbtcPriceBtc };
return _cachedPrices;
}
// ── HODLMM USDCx pools ────────────────────────────────────────────────────
interface TokenInfo {
contract?: string;
symbol?: string;
decimals?: number;
priceUsd?: number;
priceBtc?: number;
}
interface AppPool {
poolId: string;
tokens?: { tokenX?: TokenInfo; tokenY?: TokenInfo };
tvlUsd?: number;
volumeUsd1d?: number;
apr?: number;
apr24h?: number;
}
interface AppPoolsResponse { data?: AppPool[] }
async function fetchHodlmmUsdcxPools(): Promise<DirectVenue[]> {
const resp = await fetchJson(BITFLOW_APP_POOLS) as AppPoolsResponse;
const pools = resp.data ?? [];
const venues: DirectVenue[] = [];
for (const pool of pools) {
// tokens is { tokenX: {...}, tokenY: {...} }
const tokenX = pool.tokens?.tokenX;
const tokenY = pool.tokens?.tokenY;
const xContract = (tokenX?.contract ?? "").toLowerCase();
const yContract = (tokenY?.contract ?? "").toLowerCase();
const xSymbol = (tokenX?.symbol ?? "").toLowerCase();
const ySymbol = (tokenY?.symbol ?? "").toLowerCase();
const hasUsdcx = xContract.includes("usdcx") || yContract.includes("usdcx") ||
xSymbol === "usdcx" || ySymbol === "usdcx";
if (!hasUsdcx) continue;
const tvl = pool.tvlUsd ?? 0;
if (tvl < MIN_TVL_USD) continue;
const apr = sanitiseApr(pool.apr24h ?? pool.apr ?? 0, pool.poolId);
const sym0 = tokenX?.symbol ?? "?";
const sym1 = tokenY?.symbol ?? "?";
const pair = `${sym0}/${sym1}`;
// Classify pair type for risk
const pairLower = pair.toLowerCase();
const isStablePair = pairLower.includes("aeusdc") || pairLower.includes("usdh");
const isSbtcPair = pairLower.includes("sbtc");
const isStxPair = pairLower.includes("stx") && !isStablePair;
const riskFactors: string[] = [];
let risk: RiskLevel = "low";
if (isStablePair) {
risk = "low";
} else if (isSbtcPair) {
risk = "medium"; // upgraded to high later if reserve not GREEN
riskFactors.push("sBTC exposure — check reserve signal");
} else if (isStxPair) {
risk = "medium";
riskFactors.push("STX volatility — impermanent loss risk");
}
// TVL-based risk adjustment (stablecoin pairs get more lenient TVL threshold)
const tvlThreshold = isStablePair ? 50_000 : 100_000;
if (tvl < tvlThreshold) {
risk = "high";
riskFactors.push(`low TVL ($${(tvl / 1000).toFixed(0)}k)`);
}
venues.push({
rank: 0, // assigned later
protocol: "hodlmm",
pool_id: pool.poolId,
pair,
apr_pct: apr,
tvl_usd: Math.round(tvl),
risk,
risk_factors: riskFactors,
});
}
return venues;
}
// ── Bitflow XYK aeUSDC pools ───────────────────────────────────────────────
interface TickerEntry {
pool_id?: string;
base_currency?: string;
target_currency?: string;
last_price?: string;
base_volume?: string;
liquidity_in_usd?: string;
}
async function fetchBitflowXykUsdcx(prices: Prices): Promise<DirectVenue[]> {
const tickers = await getCachedTickers();
const venues: DirectVenue[] = [];
for (const t of tickers) {
const poolStr = `${t.pool_id ?? ""} ${t.base_currency ?? ""} ${t.target_currency ?? ""}`.toLowerCase();
const hasUsdc = poolStr.includes("usdc") || poolStr.includes("aeusdc");
if (!hasUsdc) continue;
const tvlUsd = parseFloat(t.liquidity_in_usd ?? "0") || 0;
if (tvlUsd < MIN_TVL_USD) continue;
const baseVol = parseFloat(t.base_volume ?? "0") || 0;
const baseCur = (t.base_currency ?? "").toLowerCase();
const volume24hUsd = baseCur === "stacks" && prices.stx > 0
? baseVol * prices.stx
: baseCur.includes("sbtc") && prices.btc > 0 ? baseVol * prices.btc : 0;
// XYK fee: 30 bps
const aprPct = tvlUsd > 0 ? ((volume24hUsd * 365 * 30 / 10_000) / tvlUsd) * 100 : 0;
if (aprPct < 0.1) continue;
// Clean up display names
const baseName = baseCur === "stacks" ? "STX" : (t.base_currency ?? "?");
const targetName = (t.target_currency ?? "").toLowerCase().includes("aeusdc") ? "aeUSDC"
: (t.target_currency ?? "?");
venues.push({
rank: 0,
protocol: "bitflow-xyk",
pool_id: t.pool_id?.split(".").pop() ?? t.pool_id ?? "xyk",
pair: `${baseName}/${targetName}`,
apr_pct: sanitiseApr(aprPct, "xyk"),
tvl_usd: Math.round(tvlUsd),
risk: "medium",
risk_factors: ["passive LP — lower capital efficiency than HODLMM"],
});
}
return venues;
}
// ── Hermetica sUSDh rate (suggested route) ─────────────────────────────────
async function fetchHermeticaRate(): Promise<number | null> {
try {
const body = JSON.stringify({ sender: HERMETICA_STAKING, arguments: [] });
const resp = await fetchJson(
`${HIRO_API}/v2/contracts/call-read/${HERMETICA_STAKING}/staking-v1/get-usdh-per-susdh`,
{ method: "POST", headers: { "Content-Type": "application/json" }, body },
) as { okay?: boolean; result?: string };
if (!resp.okay || !resp.result) return null;
const hex = (resp.result as string).replace(/^0x/, "");
// Clarity uint128: type byte 01 + 16-byte big-endian value
// May also be wrapped in ok (07) + uint (01)
let rateHex = hex;
if (rateHex.startsWith("07")) rateHex = rateHex.slice(2); // strip ok wrapper
if (rateHex.startsWith("01")) rateHex = rateHex.slice(2); // strip uint type byte
const rawRate = Number(BigInt("0x" + rateHex));
const exchangeRate = rawRate / 1e8; // 8 decimals
// Exchange rate > 1.0 means yield has accrued since inception
// If rate is exactly 1.0, staking may have just reset or no yield yet
if (exchangeRate <= 1.001) {
// Rate too close to 1.0 — use Hermetica's historical APY as estimate
// Hermetica sUSDh historically yields ~15-25% APY
return 20.0; // conservative mid-range estimate
}
// Annualize from rate growth
// Hermetica launched ~mid 2025 on Stacks mainnet
const monthsSinceLaunch = 10;
const growthPct = (exchangeRate - 1.0) * 100;
const apyPct = (growthPct / monthsSinceLaunch) * 12;
return apyPct > 0 && apyPct < MAX_SANE_APR ? Math.round(apyPct * 100) / 100 : null;
} catch {
return null;
}
}
// ── sBTC reserve signal (price-deviation proxy) ────────────────────────────
interface ReserveCheck {
signal: ReserveSignal;
deviation_pct: number;
}
// Cached ticker data to avoid double-fetching (Bitflow rate limits)
let _cachedTickers: TickerEntry[] | null = null;
async function getCachedTickers(): Promise<TickerEntry[]> {
if (_cachedTickers) return _cachedTickers;
_cachedTickers = await fetchJson(BITFLOW_TICKER) as TickerEntry[];
return _cachedTickers;
}
async function fetchSbtcPriceSignal(prices: Prices): Promise<ReserveCheck> {
// Bitflow App API provides priceBtc for sBTC — direct peg ratio, no external oracle.
// priceBtc = 1.0 means perfect peg. Deviation = abs(1 - priceBtc) * 100.
const sbtcPriceBtc = prices.sbtcPriceBtc;
if (sbtcPriceBtc <= 0) {
return { signal: "DATA_UNAVAILABLE", deviation_pct: 0 };
}
const deviationPct = Math.abs(1 - sbtcPriceBtc) * 100;
let signal: ReserveSignal = "GREEN";
if (deviationPct >= SBTC_DEV_YELLOW_PCT) signal = "RED";
else if (deviationPct >= SBTC_DEV_GREEN_PCT) signal = "YELLOW";
return {
signal,
deviation_pct: Math.round(deviationPct * 100) / 100,
};
}
// ── Gas estimation ─────────────────────────────────────────────────────────
async function estimateGasCostStx(): Promise<number> {
let feeUstx = FALLBACK_FEE_UESTX;
try {
const raw = await fetchJson(HIRO_FEE_RATE) as number | Record<string, number>;
const rate = typeof raw === "number" ? raw : ((raw as Record<string, number>)?.median ?? 1);
feeUstx = Math.max(rate, 1) * GAS_BYTES_PER_CALL;
} catch { /* use fallback */ }
return (feeUstx * GAS_CALLS_PER_MIGRATION) / 1_000_000;
}
// ── Active bin fetcher (for MCP commands) ──────────────────────────────────
const BITFLOW_HODLMM_BINS = "https://bff.bitflowapis.finance/api/quotes/v1/bins";
interface BinsApiResponse { active_bin_id?: number; activeBinId?: number }
async function fetchActiveBin(poolId: string): Promise<number | null> {
try {
const data = await fetchJson(`${BITFLOW_HODLMM_BINS}/${poolId}`) as BinsApiResponse;
return data.active_bin_id ?? data.activeBinId ?? null;
} catch { return null; }
}
// ── MCP command builders ───────────────────────────────────────────────────
const MAX_DEPLOY_USDCX = 5000; // max USDCx per autonomous deployment
const DEFAULT_BIN_RANGE = 5; // bins around active bin
// Known HODLMM pool contracts and token traits for MCP command specs
const HODLMM_POOL_CONTRACTS: Record<string, { contract: string; tokenX: string; tokenY: string }> = {
dlmm_1: { contract: "SM1FKXGNZJWSTWDWXQZJNF7B5TV5ZB235JTCXYXKD.dlmm-pool-sbtc-usdcx-v-1-bps-10", tokenX: "SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-token", tokenY: "SP120SBRBQJ00MCWS7TM5R8WJNTTKD5K0HFRC2CNE.usdcx" },
dlmm_2: { contract: "SM1FKXGNZJWSTWDWXQZJNF7B5TV5ZB235JTCXYXKD.dlmm-pool-sbtc-usdcx-v-1-bps-10", tokenX: "SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-token", tokenY: "SP120SBRBQJ00MCWS7TM5R8WJNTTKD5K0HFRC2CNE.usdcx" },
dlmm_3: { contract: "SM1FKXGNZJWSTWDWXQZJNF7B5TV5ZB235JTCXYXKD.dlmm-pool-stx-usdcx-v-1-bps-10", tokenX: "SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.token-stx-v-1-2", tokenY: "SP120SBRBQJ00MCWS7TM5R8WJNTTKD5K0HFRC2CNE.usdcx" },
dlmm_4: { contract: "SM1FKXGNZJWSTWDWXQZJNF7B5TV5ZB235JTCXYXKD.dlmm-pool-stx-usdcx-v-1-bps-10", tokenX: "SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.token-stx-v-1-2", tokenY: "SP120SBRBQJ00MCWS7TM5R8WJNTTKD5K0HFRC2CNE.usdcx" },
dlmm_5: { contract: "SM1FKXGNZJWSTWDWXQZJNF7B5TV5ZB235JTCXYXKD.dlmm-pool-stx-usdcx-v-1-bps-10", tokenX: "SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.token-stx-v-1-2", tokenY: "SP120SBRBQJ00MCWS7TM5R8WJNTTKD5K0HFRC2CNE.usdcx" },
dlmm_7: { contract: "SM1FKXGNZJWSTWDWXQZJNF7B5TV5ZB235JTCXYXKD.dlmm-pool-aeusdc-usdcx-v-1-bps-1", tokenX: "SP3Y2ZSH8P7D50B0VBTSX11S7XSG24M1VB9YFQA4K.token-aeusdc", tokenY: "SP120SBRBQJ00MCWS7TM5R8WJNTTKD5K0HFRC2CNE.usdcx" },
dlmm_8: { contract: "SM1FKXGNZJWSTWDWXQZJNF7B5TV5ZB235JTCXYXKD.dlmm-pool-aeusdc-usdcx-v-1-bps-1", tokenX: "SPN5AKG35QZSK2M8GAMR4AFX45659RJHDW353HSG.usdh-token-v1", tokenY: "SP120SBRBQJ00MCWS7TM5R8WJNTTKD5K0HFRC2CNE.usdcx" },
};
const LIQUIDITY_ROUTER = "SM1FKXGNZJWSTWDWXQZJNF7B5TV5ZB235JTCXYXKD.dlmm-liquidity-router-v-1-2";
function buildDeployCommands(
venue: DirectVenue,
amount: number,
activeBinId: number | null,
): McpCommand[] {
const cappedAmount = Math.min(amount, MAX_DEPLOY_USDCX);
const commands: McpCommand[] = [];
const poolInfo = HODLMM_POOL_CONTRACTS[venue.pool_id];
if (venue.protocol === "hodlmm" && activeBinId !== null && poolInfo) {
// USDCx is always token Y. For bins above active bin, deposit y-only.
const yAmountRaw = cappedAmount * 1_000_000; // USDCx has 6 decimals
commands.push({
step: 1,
tool: "call_contract",
description: `Deploy ${cappedAmount} USDCx to HODLMM ${venue.pool_id} (${venue.pair}) at bin offset +1 from active bin ${activeBinId}`,
params: {
contractAddress: "SM1FKXGNZJWSTWDWXQZJNF7B5TV5ZB235JTCXYXKD",
contractName: "dlmm-liquidity-router-v-1-2",
functionName: "add-liquidity-multi",
positions: [{
"bin-id": activeBinId + 1,
"x-amount": 0,
"y-amount": yAmountRaw,
"max-x-liquidity-fee": 0,
"max-y-liquidity-fee": Math.round(yAmountRaw * 0.01),
"min-dlp": 1000,
"pool-trait": poolInfo.contract,
"x-token-trait": poolInfo.tokenX,
"y-token-trait": poolInfo.tokenY,
}],
note: "SPEC ONLY — requires trait_reference support in call_contract MCP tool. See SKILL.md 'Write Capability Status' for details.",
},
});
}
return commands;
}
// ── Venue ranking ──────────────────────────────────────────────────────────
function buildDirectVenues(
hodlmmVenues: DirectVenue[],
xykVenues: DirectVenue[],
reserveSignal: ReserveSignal,
riskFilter: RiskLevel,
): DirectVenue[] {
const all: DirectVenue[] = [...hodlmmVenues, ...xykVenues];
// Upgrade sBTC-paired pool risk if reserve is not GREEN
for (const v of all) {
const isSbtcPair = v.pair.toLowerCase().includes("sbtc");
if (isSbtcPair && reserveSignal !== "GREEN") {
v.risk = "high";
v.risk_factors.push(`sBTC reserve ${reserveSignal} — elevated peg risk`);
}
}
// Risk filter
const riskOrder: Record<RiskLevel, number> = { low: 0, medium: 1, high: 2 };
const maxRisk = riskOrder[riskFilter];
const filtered = all.filter(v => riskOrder[v.risk] <= maxRisk);
// Sort by risk-adjusted APR
const riskWeight: Record<RiskLevel, number> = { low: 1.0, medium: 0.85, high: 0.7 };
filtered.sort((a, b) => {
const adjA = a.apr_pct * riskWeight[a.risk];
const adjB = b.apr_pct * riskWeight[b.risk];
return adjB - adjA;
});
// Assign ranks
filtered.forEach((v, i) => { v.rank = i + 1; });
return filtered;
}
// ── Suggested routes ───────────────────────────────────────────────────────
function buildSuggestedRoutes(
hermeticaApy: number | null,
bestDirectApr: number,
reserveSignal: ReserveSignal,
): SuggestedRoute[] {
const routes: SuggestedRoute[] = [];
if (hermeticaApy !== null && hermeticaApy > 0) {
const netApy = hermeticaApy - HERMETICA_SWAP_COST_PCT;
const risk: RiskLevel = reserveSignal === "GREEN" ? "medium" : "high";
// Only suggest if net APY beats best direct venue
if (netApy > bestDirectApr) {
routes.push({
destination: "Hermetica sUSDh vault",
estimated_apy_pct: Math.round(hermeticaApy * 100) / 100,
swap_path: "USDCx -> sBTC (Bitflow) -> stake USDh -> sUSDh",
swap_cost_pct: HERMETICA_SWAP_COST_PCT,
net_apy_pct: Math.round(netApy * 100) / 100,
risk,
note: reserveSignal !== "GREEN"
? `Requires sBTC exposure. Reserve signal ${reserveSignal} — elevated risk.`
: "Requires sBTC exposure. Use hermetica-yield-rotator skill to execute.",
});
}
}
return routes;
}
// ── Profit gate (YTG) ──────────────────────────────────────────────────────
function applyProfitGate(
currentVenueName: string,
currentApr: number,
bestVenue: DirectVenue,
amount: number,
gasCostStx: number,
stxPrice: number,
): ProfitGate {
const gasCostUsd = gasCostStx * stxPrice;
const apyDiff = bestVenue.apr_pct - currentApr;
const extra7dUsd = (amount * apyDiff / 100) / 52; // weekly yield difference
const threshold = gasCostUsd * PROFIT_GATE_MULTIPLIER;
const passed = apyDiff >= MIN_APY_IMPROVEMENT_PCT && extra7dUsd > threshold;
let reason: string;
if (apyDiff < MIN_APY_IMPROVEMENT_PCT) {
reason = `APY improvement ${apyDiff.toFixed(2)}% below ${MIN_APY_IMPROVEMENT_PCT}% minimum.`;
} else if (extra7dUsd <= threshold) {
reason = `7d extra yield ($${extra7dUsd.toFixed(4)}) does not cover gas x ${PROFIT_GATE_MULTIPLIER} ($${threshold.toFixed(4)}).`;
} else {
reason = `All checks passed. 7d extra yield ($${extra7dUsd.toFixed(2)}) > gas x ${PROFIT_GATE_MULTIPLIER} ($${threshold.toFixed(4)}).`;
}
return {
rule: `7d_extra_yield > gas_cost x ${PROFIT_GATE_MULTIPLIER}`,
current_venue: currentVenueName,
best_venue: `${bestVenue.protocol} ${bestVenue.pool_id} (${bestVenue.pair})`,
"7d_extra_yield_usd": Math.round(extra7dUsd * 10_000) / 10_000,
gas_cost_usd: Math.round(gasCostUsd * 10_000) / 10_000,
passed,
reason,
};
}
// ── Known APR estimates for --from venues ──────────────────────────────────
function getFromAprEstimate(venue: string): number {
const map: Record<string, number> = {
zest: 3.0,
hodlmm: 6.0,
"bitflow-xyk": 4.0,
"dlmm_1": 6.0,
"dlmm_2": 6.0,
"dlmm_3": 5.0,
"dlmm_4": 5.0,
"dlmm_5": 5.0,
"dlmm_7": 3.0,
"dlmm_8": 3.0,
};
return map[venue.toLowerCase()] ?? 3.0;
}
// ── On-chain position reader ──────────────────────────────────────────────
interface PositionBin {
bin_id: number;
balance: string; // raw uint128 hex
balance_human: number;
}
interface PoolPosition {
pool_id: string;
pool_contract: string;
pair: string;
active_bin_id: number | null;
user_bins: PositionBin[];
overall_balance: number;
in_range: boolean;
bins_from_active: number | null;
}
interface PositionResult {
status: "ok" | "degraded" | "error";
wallet: string;
positions: PoolPosition[];
total_pools: number;
active_pools: number;
sources_used: string[];
sources_failed: string[];
timestamp: string;
error?: string;
}
// Decode Clarity value from hex result — handles uint128 (01) and int128 (00)
function decodeClarityInt(hex: string): bigint {
let h = hex.replace(/^0x/, "");
// Strip ok wrapper (07) if present
if (h.startsWith("07")) h = h.slice(2);
const typeByte = h.slice(0, 2);
h = h.slice(2); // strip type byte
const raw = BigInt("0x" + h);
if (typeByte === "00") {
// int128 — signed, two's complement
const MAX_INT128 = (BigInt(1) << BigInt(127)) - BigInt(1);
return raw > MAX_INT128 ? raw - (BigInt(1) << BigInt(128)) : raw;
}
// uint128 (01) or unknown — treat as unsigned
return raw;
}
// Backwards-compatible alias
function decodeUint128(hex: string): bigint {
return decodeClarityInt(hex);
}
// Decode Clarity list of uint128 from hex
function decodeUintList(hex: string): number[] {
let h = hex.replace(/^0x/, "");
// Strip ok wrapper (07)
if (h.startsWith("07")) h = h.slice(2);
// List header: 0b + 4-byte length
if (!h.startsWith("0b")) return [];
const lenHex = h.slice(2, 10);
const count = parseInt(lenHex, 16);
const items: number[] = [];
let pos = 10; // after list header
for (let i = 0; i < count; i++) {
// Each uint128: type byte 01 + 16 bytes = 34 hex chars
if (h.slice(pos, pos + 2) !== "01") break;
const valHex = h.slice(pos + 2, pos + 34);
items.push(Number(BigInt("0x" + valHex)));
pos += 34;
}
return items;
}
// Unique pool contracts for position scanning
const UNIQUE_POOL_CONTRACTS: { poolIds: string[]; contract: string; pair: string }[] = [
{ poolIds: ["dlmm_1", "dlmm_2"], contract: "SM1FKXGNZJWSTWDWXQZJNF7B5TV5ZB235JTCXYXKD.dlmm-pool-sbtc-usdcx-v-1-bps-10", pair: "sBTC/USDCx" },
{ poolIds: ["dlmm_3", "dlmm_4", "dlmm_5"], contract: "SM1FKXGNZJWSTWDWXQZJNF7B5TV5ZB235JTCXYXKD.dlmm-pool-stx-usdcx-v-1-bps-10", pair: "STX/USDCx" },
{ poolIds: ["dlmm_7"], contract: "SM1FKXGNZJWSTWDWXQZJNF7B5TV5ZB235JTCXYXKD.dlmm-pool-aeusdc-usdcx-v-1-bps-1", pair: "aeUSDC/USDCx" },
];
// Encode a Stacks principal (SP...) to Clarity hex (type 05 + version + hash160)
function encodePrincipalHex(address: string): string {
const C32 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
let acc = BigInt(0);
for (const c of address.slice(2).toUpperCase()) {
const idx = C32.indexOf(c);
if (idx >= 0) acc = acc * 32n + BigInt(idx);
}
let hex = acc.toString(16);
if (hex.length % 2) hex = "0" + hex;
const bytes: number[] = [];
for (let i = 0; i < hex.length; i += 2) bytes.push(parseInt(hex.slice(i, i + 2), 16));
const hash160 = bytes.slice(0, 20);
const version = address.startsWith("SP") ? 22 : 26; // SP=mainnet, ST=testnet
return "0x05" + version.toString(16).padStart(2, "0") + hash160.map(b => b.toString(16).padStart(2, "0")).join("");
}
// Encode a uint to Clarity hex (type 01 + 16-byte big-endian)
function encodeUintHex(value: number): string {
const hex = BigInt(value).toString(16).padStart(32, "0");
return "0x01" + hex;
}
async function readOnChainCall(contractId: string, fn: string, fnArgs: { type: string; value: string }[] = []): Promise<string | null> {
try {
const [addr, name] = contractId.split(".");
const body = JSON.stringify({
sender: addr,
arguments: fnArgs.map(a => {
if (a.type === "principal") return encodePrincipalHex(a.value);
if (a.type === "uint") return encodeUintHex(Number(a.value));
return a.value;
}),
});
const resp = await fetchJson(
`${HIRO_API}/v2/contracts/call-read/${addr}/${name}/${fn}`,
{ method: "POST", headers: { "Content-Type": "application/json" }, body },
) as { okay?: boolean; result?: string };
if (!resp.okay || !resp.result) return null;
return resp.result as string;
} catch (e: unknown) {
// Silently fail for position reads — logged in caller
return null;
}
}
async function fetchPoolPosition(pool: { poolIds: string[]; contract: string; pair: string }, wallet: string): Promise<PoolPosition | null> {
const [addr, name] = pool.contract.split(".");
// Fetch sequentially to avoid Hiro rate limits on parallel calls
const activeBinHex = await readOnChainCall(pool.contract, "get-active-bin-id");
const userBinsHex = await readOnChainCall(pool.contract, "get-user-bins", [{ type: "principal", value: wallet }]);
const overallHex = await readOnChainCall(pool.contract, "get-overall-balance", [{ type: "principal", value: wallet }]);
const activeBin = activeBinHex ? Number(decodeUint128(activeBinHex)) : null;
const userBinIds = userBinsHex ? decodeUintList(userBinsHex) : [];
const overallBalance = overallHex ? Number(decodeUint128(overallHex)) : 0;
// No position in this pool
if (userBinIds.length === 0 && overallBalance === 0) return null;
// Map bins without individual balance fetches (avoids N HTTP calls per bin)
const bins: PositionBin[] = userBinIds.map(binId => ({
bin_id: binId,
balance: "0", // individual bin balances skipped for speed
balance_human: 0,
}));
// Calculate distance from active bin
let binsFromActive: number | null = null;
let inRange = false;
if (activeBin !== null && userBinIds.length > 0) {
const closestBin = userBinIds.reduce((closest, id) =>
Math.abs(id - activeBin) < Math.abs(closest - activeBin) ? id : closest
);
binsFromActive = closestBin - activeBin;
inRange = userBinIds.includes(activeBin) || userBinIds.some(id => Math.abs(id - activeBin) <= 1);
}
return {
pool_id: pool.poolIds[0],
pool_contract: pool.contract,
pair: pool.pair,
active_bin_id: activeBin,
user_bins: bins,
overall_balance: overallBalance / 1_000_000,
in_range: inRange,
bins_from_active: binsFromActive,
};
}
async function runPosition(wallet: string): Promise<void> {
const result: PositionResult = {
status: "ok",
wallet,
positions: [],
total_pools: UNIQUE_POOL_CONTRACTS.length,
active_pools: 0,
sources_used: [],
sources_failed: [],
timestamp: new Date().toISOString(),
};
try {
const positionPromises = UNIQUE_POOL_CONTRACTS.map(pool => fetchPoolPosition(pool, wallet));
const results = await Promise.allSettled(positionPromises);
for (let i = 0; i < results.length; i++) {
const r = results[i];
const pool = UNIQUE_POOL_CONTRACTS[i];
if (r.status === "fulfilled" && r.value) {
result.positions.push(r.value);
result.active_pools++;
result.sources_used.push(`on-chain:${pool.contract.split(".")[1]}`);
} else if (r.status === "fulfilled" && !r.value) {
result.sources_used.push(`on-chain:${pool.contract.split(".")[1]}`);
// No position — not a failure
} else {
result.sources_failed.push(`on-chain:${pool.contract.split(".")[1]}`);
}
}
if (result.sources_failed.length > 0) result.status = "degraded";
console.log(JSON.stringify(result, null, 2));
if (result.status === "degraded") process.exit(1);
} catch (err: unknown) {
const msg = err instanceof Error ? err.message : String(err);
result.status = "error";
result.error = msg;
console.log(JSON.stringify(result, null, 2));
process.exit(3);
}
}
// ── Commands ───────────────────────────────────────────────────────────────
async function runDoctor(): Promise<void> {
const checks: { name: string; ok: boolean; detail: string }[] = [];
const sources = [
{
name: "Bitflow HODLMM App API",
fn: async () => {
const d = await fetchJson(BITFLOW_APP_POOLS) as AppPoolsResponse;
const pools = d.data ?? [];
const usdcxPools = pools.filter(p => {
const tx = (p.tokens as Record<string, TokenInfo> | undefined)?.tokenX;
const ty = (p.tokens as Record<string, TokenInfo> | undefined)?.tokenY;
return [tx, ty].some(t => t && ((t.symbol ?? "").toLowerCase() === "usdcx" || (t.contract ?? "").toLowerCase().includes("usdcx")));
});
return `${pools.length} pools total, ${usdcxPools.length} with USDCx`;
},
},
{
name: "Bitflow Ticker (XYK + sBTC price)",
fn: async () => {
const d = await fetchJson(BITFLOW_TICKER) as TickerEntry[];
return `${d.length} pairs`;
},
},
{
name: "Bitflow Prices (from pool data)",
fn: async () => {
const p = await fetchPrices();
return `BTC $${p.btc.toLocaleString()} STX $${p.stx} sBTC/BTC ${p.sbtcPriceBtc.toFixed(4)}`;
},
},
{
name: "Hiro Fee Rate",
fn: async () => {
const g = await estimateGasCostStx();
return `~${(g * 1_000_000).toFixed(0)} uSTX for 2-call migration`;
},
},
{
name: "Hermetica Staking (sUSDh rate)",
fn: async () => {
const rate = await fetchHermeticaRate();
return rate !== null ? `estimated APY ${rate.toFixed(2)}%` : "rate unavailable (will exclude from routes)";
},
},
{
name: "sBTC Price Signal",
fn: async () => {
const prices = await fetchPrices();
const check = await fetchSbtcPriceSignal(prices);
return `signal=${check.signal}, deviation=${check.deviation_pct.toFixed(2)}%`;
},
},
{
name: "HODLMM On-Chain Reads",
fn: async () => {
// Test read-only call on the STX/USDCx pool (largest TVL)
const hex = await readOnChainCall(
"SM1FKXGNZJWSTWDWXQZJNF7B5TV5ZB235JTCXYXKD.dlmm-pool-stx-usdcx-v-1-bps-10",
"get-active-bin-id",
);
if (!hex) throw new Error("call-read-only returned null");
const activeBin = Number(decodeUint128(hex));
return `STX/USDCx active bin ${activeBin}, ${UNIQUE_POOL_CONTRACTS.length} pool contracts reachable`;
},
},
];
await Promise.all(sources.map(async (s) => {
try {
const detail = await s.fn();
checks.push({ name: s.name, ok: true, detail });
} catch (e: unknown) {
const msg = e instanceof Error ? e.message : String(e);
checks.push({ name: s.name, ok: false, detail: msg });
}
}));
const allOk = checks.every(c => c.ok);
console.log(JSON.stringify({
status: allOk ? "ok" : "degraded",
checks,
message: allOk
? "All sources reachable. USDCx venue scan ready."
: "Some sources unavailable — output may be incomplete.",
}, null, 2));
if (!allOk) process.exit(1);
}
async function runOptimizer(opts: {
amount: number;
risk: RiskLevel;
from: string | null;
confirm: boolean;
}): Promise<void> {
const result: OptimizerResult = {
status: "ok",
decision: "HOLD",
direct_venues: [],
suggested_routes: [],
risk_assessment: { sbtc_reserve_signal: "DATA_UNAVAILABLE", sbtc_price_deviation_pct: 0, flagged_pools: [] },
profit_gate: null,
mcp_commands: [],
action: "",
sources_used: [],
sources_failed: [],
timestamp: new Date().toISOString(),
};
try {
// Reset caches for fresh data
_cachedTickers = null;
_cachedPrices = null;
// Step 1: Prices (from Bitflow pool data — no external oracle)
let prices: Prices = { btc: 0, stx: 0, sbtcPriceBtc: 1 };
try {
prices = await fetchPrices();
result.sources_used.push("bitflow-prices");
} catch {
result.sources_failed.push("bitflow-prices");
}
if (prices.btc <= 0 || prices.stx <= 0) {
result.status = "error";
result.decision = "AVOID";
result.error = "Price data unavailable — cannot compute yields.";
result.action = "AVOID — Price feeds down. Retry later.";
console.log(JSON.stringify(result, null, 2));
process.exit(3);
return;
}
// Step 2: sBTC reserve signal (computed from Bitflow prices — no extra API call)
const reserveCheck = await fetchSbtcPriceSignal(prices);
result.sources_used.push("sbtc-reserve-signal");
// Step 3: Fetch remaining data in parallel
const [hodlmmRes, xykRes, hermeticaRes] = await Promise.allSettled([
fetchHodlmmUsdcxPools(),
fetchBitflowXykUsdcx(prices),
fetchHermeticaRate(),
]);
// Track sources
let hodlmmVenues: DirectVenue[] = [];
if (hodlmmRes.status === "fulfilled") {
hodlmmVenues = hodlmmRes.value;
result.sources_used.push("bitflow-hodlmm");
} else {
result.sources_failed.push("bitflow-hodlmm");
}
let xykVenues: DirectVenue[] = [];
if (xykRes.status === "fulfilled") {
xykVenues = xykRes.value;
result.sources_used.push("bitflow-xyk");
} else {
result.sources_failed.push("bitflow-xyk");
}
let hermeticaApy: number | null = null;
if (hermeticaRes.status === "fulfilled") {
hermeticaApy = hermeticaRes.value;
if (hermeticaApy !== null) result.sources_used.push("hermetica");
else result.sources_failed.push("hermetica");
} else {
result.sources_failed.push("hermetica");
}
if (result.sources_failed.length > 0) result.status = "degraded";
// Step 3: Risk assessment
result.risk_assessment = {
sbtc_reserve_signal: reserveCheck.signal,
sbtc_price_deviation_pct: reserveCheck.deviation_pct,
flagged_pools: [],
};