SPB Git

Repository Public

WITHDRAWALS.md 5.2 KB 52 lines markdown

1# V8 — agent-vault withdrawals are tracked; where else success was reported too early23## Agent vault → owner (`POST /api/v1/agents/:id/withdraw`)45Before: the route signed, broadcast and answered `{ ok: true, txid }`; the receipt was never read, and with broadcasting6off the answer was still `ok: true`. Now (`services/api/src/agents/withdrawals.ts`, table `agent_withdrawals`):78```9requested ─► signed ─► broadcast ─► confirmed   receipt status 1 AND the payment to the owner's address seen in it10    │           │           └─────► failed      reverted, or the vault's nonce was used by another transaction11    │           └─────────────────► not_sent    broadcasting off on this server, or not relayed within 10 minutes12    └─────────────────────────────► failed      the signer refused, or the process died while signing (2 minutes)13```1415- **One id per requested withdrawal.** The row is inserted in state `requested` before the signer is called and its id is16  the intent's `proposalId` (the signer's replay rule is per proposal id). A partial unique index allows one open17  withdrawal per (agent, network, asset); the optional body field `idempotencyKey` (the web app sends one per sheet and18  asset) is unique per agent. A double tap, two concurrent requests or a retry return the same row with19  `duplicate: true`; the signer is called once (test: `signs === 1`).20- **Answer:** `{ id, ok, state, network, asset, symbol, amount, txid, explorer, error, arrived, createdAt, updatedAt,21  confirmedAt, duplicate? }`. `ok` is true only for `broadcast` and `confirmed`; `explorer` is null until broadcast;22  `arrived` is true only for `confirmed` with the payment verified (token: the `Transfer(vault → owner, amount)` log of23  the receipt; native coin: the transaction's `to` and `value`). A mined transaction in which the payment is not found is24  `confirmed` with `arrived: false` and an explanation — never shown green.25- **Worker:** `loop('withdrawals', advanceWithdrawals, 8 s)` — reads receipts, re-relays an already signed transaction26  every 45 s, never signs. A node that does not answer concludes nothing.27- **Read:** `GET /agents/:id/withdrawals` (last 20, plus `broadcast`), `GET /agents/:id/withdrawals/:wid`.28- **Audit:** `agent.withdraw` (signed, as before) then `agent.withdraw_confirmed | _failed | _not_sent`.29- **UI** (`apps/web/src/screens/withdrawTrack.tsx`; `AgentFund.tsx` withdraw sheet and "fonds sur d'autres réseaux"):30  the sentence follows the state, polled every 3 s until final — "signé — envoi en cours. Pas encore diffusé",31  "diffusé — en attente d'inclusion. Pas encore confirmé", "confirmé : arrivé dans votre portefeuille principal" (the only32  green one), "échoué", "signé, mais non envoyé". The button is disabled while one is in flight.33- The signer, the intent and its verification are unchanged. A withdrawal still open after a long time stays `broadcast`34  (it may still be mined); it is not yet covered by the `transfer.stuck` alert (that rule reads `proposals`).3536Tests: `services/api/test/agent-withdraw.test.ts` (6), `apps/web/test/hardening-b.test.ts` (3).3738## The same review of the other flows3940Method: grep of `config.broadcast`, `.broadcast(` and of what the routes answer after a signature. The collateral and plan41modules were read for their state names, not audited line by line.4243| Place | What it reports | Verdict |44|---|---|---|45| `agents/live.ts` `advanceOrder` (agent orders) | `signed → broadcast → filled` on the receipt and the router's `Swapped` event; broadcasting off → `failed: … nothing was sent` | fine |46| `derivatives/collateral.ts` venue withdrawal | `signed → submitted` on the venue's `ok`, final only when the venue's ledger / the wallet's chain shows it | fine as read |47| `plan-exec.ts` | never signs or broadcasts; a leg follows its proposal's state | fine as read |48| `bridge.ts` mint (`:252`, `:400`) | `broadcast(...).catch(() => {})` swallows a relay error; the phase still advances only on the mint's receipt | state is right; the swallowed error is not logged. Left (shared file) |49| **`transfers.ts:216` `broadcastNow` with broadcasting off** | moves the proposal to state **`broadcast`** with `error: 'broadcast disabled (test mode)'`. `swap-exec.ts:164`, `swap-exec-sol.ts:290`, `bridge.ts:264`, `xswap.ts:290` go through it; the row then stays `broadcast` for ever (the worker skips it) and the web app recognises the case by a regex on the error text (`TEST_NOT_SENT`) | **wrong state name — not fixed.** It only happens on a server started with `SPW_BROADCAST=off` (tests, QA), never in production. A fix is a new terminal state in the shared proposal state machine (`@spw/core`, the `transfer.stuck` alert, several screens) while other engineers are editing those files. Proposed: a terminal `not_sent`, as here |50| `derivatives/collateral.ts:501` venue deposit | maps the proposal's `broadcast` to the deposit's `sent` — so with broadcasting off a deposit reads `sent` | consequence of the row above; same fix |51| transfer / swap / bridge approve routes | answer the proposal row itself (`signed` or `broadcast`), which the UI follows to `finalized` | fine |52
Files main
Menu

Theme