|
| 1 | +--- |
| 2 | +name: unwindleg-hermeticaunstake-zestrepay-yield-rotator-sUSDh-USDCx-sBTC |
| 3 | +description: "Unwind-only companion to the wind skill (#604): initiates Hermetica sUSDh unstake (7d cooldown), then on resume completes unstake, swaps USDh->USDCx on Bitflow, repays the Zest USDCx debt, and withdraws the sBTC collateral. Two-phase state machine separated by the on-chain 7d cooldown. Never broadcasts wind. Code in progress; structural draft per PR body." |
| 4 | +metadata: |
| 5 | + author: "IamHarrie-Labs" |
| 6 | + author-agent: "Serene Spring" |
| 7 | + user-invocable: "false" |
| 8 | + arguments: "doctor | status | score | plan | unwind-init | resume | monitor | cancel" |
| 9 | + entry: "unwindleg-hermeticaunstake-zestrepay-yield-rotator-sUSDh-USDCx-sBTC/unwindleg-hermeticaunstake-zestrepay-yield-rotator-sUSDh-USDCx-sBTC.ts" |
| 10 | + requires: "wallet, signing, settings, bitflow-swap-aggregator, zest-auto-repay" |
| 11 | + tags: "defi, write, mainnet-only, requires-funds, l2" |
| 12 | +--- |
| 13 | + |
| 14 | +# Unwind-Leg: HermeticaUnstake -> ZestRepay Yield Rotator |
| 15 | + |
| 16 | +## Scope |
| 17 | + |
| 18 | +Unwind-only companion to the wind skill ([#604](https://github.com/BitflowFinance/bff-skills/pull/604)). Reverses a position created by the wind leg through a 5-tx sequence separated by the on-chain 7-day Hermetica cooldown. |
| 19 | + |
| 20 | +## Asset journey |
| 21 | + |
| 22 | +| Step | Phase | Wallet receives | Wallet sends | On-chain effect | |
| 23 | +|---|---|---|---|---| |
| 24 | +| Initiate unstake | 1 (Day 0) | (cooldown receipt) | sUSDh | `staking-v1-1.initiate-cooldown` burns sUSDh, starts 7d timer | |
| 25 | +| Wait | 1 | n/a | n/a | Cooldown elapses on-chain; no broadcast | |
| 26 | +| Complete unstake | 2 (Day 7+) | USDh | (cooldown receipt) | `staking-v1-1.complete-unstake` returns USDh | |
| 27 | +| Swap | 2 | USDCx | USDh | Bitflow `dlmm_8` USDh -> USDCx | |
| 28 | +| Repay | 2 | (Zest debt cleared) | USDCx | `market.repay` reduces debt | |
| 29 | +| Withdraw | 2 | sBTC | (Zest collateral cleared) | `market.collateral-remove` returns sBTC | |
| 30 | + |
| 31 | +The wallet ends back at the pre-rotation asset (sBTC), debt-free. |
| 32 | + |
| 33 | +## What it does |
| 34 | + |
| 35 | +Drives the two-phase unwind. `unwind-init` broadcasts the irreversible Phase 1 (initiate-cooldown) and checkpoints to `cooldown_pending`. `resume` reads the on-chain cooldown deadline, refuses to proceed until it has elapsed, then drives Phase 2 (complete-unstake -> swap -> repay -> withdraw). `monitor` polls the cooldown deadline and the strategy score; when the cooldown is satisfied AND the wind skill's score is below `--exit-score-below`, auto-resume in autonomous mode (rate-limited to 1 action / 24h). |
| 36 | + |
| 37 | +## Why agents need it |
| 38 | + |
| 39 | +The wind skill (#604) creates a position whose only legitimate exit path is this 5-step unwind. Without a dedicated unwind skill, the operator would have to drive five separate primitives by hand AND remember to wait out the cooldown AND verify the on-chain deadline before broadcasting Phase 2. This skill encapsulates that into one auditable cycle with checkpointing. |
| 40 | + |
| 41 | +## Code status |
| 42 | + |
| 43 | +**Code: in progress.** This commit ships a structural stub (the entry-point `.ts` outputs a `CODE_IN_PROGRESS` envelope on every subcommand). The full implementation will land in subsequent commits before review. The Hermetica unstake contract surfaces and the Zest V2 `repay` + `collateral-remove` signatures have been verified against the Clarity source. |
| 44 | + |
| 45 | +## Verified contracts (from canonical sources) |
| 46 | + |
| 47 | +| Identifier | Source of verification | |
| 48 | +|---|---| |
| 49 | +| `SPN5AKG35QZSK2M8GAMR4AFX45659RJHDW353HSG.staking-v1-1.initiate-cooldown` | Hiro `/v2/contracts/source` | |
| 50 | +| `SPN5AK...HSG.staking-v1-1.complete-unstake` | Hiro Clarity source | |
| 51 | +| `SPN5AK...HSG.staking-state-v1.get-cooldown-window` returns `u604800` (= 7d) | Hiro Clarity source - same constant the wind skill references | |
| 52 | +| `SPN5AK...HSG.susdh-token-v1` (decimals 8, burned on initiate-cooldown) | Hiro Clarity source | |
| 53 | +| `SPN5AK...HSG.usdh-token-v1` (decimals 8, returned on complete-unstake) | Hiro Clarity source | |
| 54 | +| Zest V2 `market.repay(ft, amount, on-behalf-of?)` + `market.collateral-remove(ft, amount, receiver?, price-feeds?)` | Zest V2 docs market trait | |
| 55 | +| `SM1FKXGNZJWSTWDWXQZJNF7B5TV5ZB235JTCXYXKD.dlmm-pool-usdh-usdcx-v-1-bps-1` (`dlmm_8`) | Bitflow `/api/app/v1/pools/dlmm_8` | |
| 56 | + |
| 57 | +## Safety notes |
| 58 | + |
| 59 | +- **Write skill. Reverses a Zest V2 debt position. Mainnet only.** |
| 60 | +- **Phase 1 is irreversible the moment it broadcasts** - sUSDh is burned and the wallet is locked into a 7-day cooldown that cannot be canceled. |
| 61 | +- Skill checkpoints between Phase 1 and Phase 2 so the operator survives process crashes / VM restarts during the wait. |
| 62 | +- Explicit `--confirm=UNWIND` required for Phase 1; explicit `--confirm=UNWIND` again for the `resume` that drives Phase 2. |
| 63 | +- Phase 2 swap viability gate `--max-price-impact-bps` (default 50 bps) refuses the USDh -> USDCx swap if the Bitflow Quote Engine reports impact above threshold. |
| 64 | +- Signer loaded via the canonical AIBTC pipeline matching `src/lib/services/x402.service.ts:getAccount()` - try `wallet-manager.restoreSessionFromDisk()` first, fall back to `process.env.CLIENT_MNEMONIC`. |
| 65 | +- No secrets logged, no secrets in JSON output. |
| 66 | +- Pre-submission `preflight-screen.sh` will block hardcoded wallets, secrets, mnemonics, and personal paths before any code lands. |
| 67 | + |
| 68 | +## Commands |
| 69 | + |
| 70 | +Mirror the wind skill's command surface: |
| 71 | + |
| 72 | +- `doctor` - dependency + chain + cooldown-deadline readiness |
| 73 | +- `status` - current on-chain position (Zest debt, collateral, sUSDh balance, cooldown deadline) |
| 74 | +- `score` - strategy score (delegates to the wind skill's score module; informs the UNWIND threshold) |
| 75 | +- `plan` - dry-run Phase 1 or Phase 2 depending on checkpoint state |
| 76 | +- `unwind-init` - broadcast Phase 1 (sUSDh -> cooldown) |
| 77 | +- `resume` - broadcast Phase 2 (complete -> swap -> repay -> withdraw) |
| 78 | +- `monitor` - HITL or autonomous polling; rate-limited to 1 auto-action / 24h |
| 79 | +- `cancel` - mark checkpoint as operator-cancelled (does NOT cancel the on-chain cooldown - that cannot be canceled) |
| 80 | + |
| 81 | +## Output contract |
| 82 | + |
| 83 | +All commands print exactly one JSON object to stdout: |
| 84 | + |
| 85 | +```json |
| 86 | +{ "status": "success | blocked | error", "action": "...", "data": {}, "error": null } |
| 87 | +``` |
| 88 | + |
| 89 | +Error envelope `error.message` is reachable as the registry minimum `{ "error": "<message>" }` shape when unwrapped one level. |
| 90 | + |
| 91 | +## Known constraints |
| 92 | + |
| 93 | +- Mainnet only. |
| 94 | +- Reverses #604's position only - does NOT accept hand-rolled positions with different asset paths. |
| 95 | +- The 7-day cooldown is a hard on-chain constraint; the skill cannot accelerate it. |
| 96 | +- Operator must hold enough STX for gas across both phases. |
| 97 | +- `monitor` autonomous mode requires the controller process to keep running (or be restartable from checkpoint). |
| 98 | + |
| 99 | +## HODLMM integration declaration |
| 100 | + |
| 101 | +**No.** Same as the wind skill ([#604](https://github.com/BitflowFinance/bff-skills/pull/604)). The Phase 2 swap leg routes through HODLMM `dlmm_8` (the only USDh venue on Bitflow), but the skill does **not** LP into HODLMM as a destination. Per the bonus criterion ("skills that directly integrate HODLMM"), the qualifying integration is LP/destination, not swap-venue routing. Declared honestly rather than padded for the bonus pool. |
0 commit comments