@dcasegr/amm-client 0.39.0 → 0.42.2

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.
@@ -1,49 +1,28 @@
1
1
  /**
2
- * The active swap basket (skein-amm 0.12.0, David Case 2026-10-21/22; 0.29.0,
3
- * 2026-11-02): everything the swap legs spend, held outside the BRC-207 module
4
- * in the exchange's own basket `amm-swap` (src/wallet/color.ts): the token
5
- * outputs and each leg's sats.
2
+ * The active swap basket (skein-amm 0.12.0, David Case 2026-10-21/22; 0.40.0, agent-env
3
+ * plans/swap-balance-build-2026-11-05.md §0 7, Builder 2 item 2): what a swap stages for its legs,
4
+ * held outside the BRC-207 module in the exchange's own basket `amm-swap` (src/wallet/color.ts).
5
+ * No deposit, no withdraw, no balance the user manages.
6
6
  *
7
- * - **In** (`sendToSwapBasket`): before the legs of a tokens-in swap, ONE
8
- * wallet-built send through the module: one 1Color value output per leg
9
- * not covered by what `amm-swap` already holds, each exactly the leg's
10
- * amount in (the pool takes no token change, so a leg needs inputs that
11
- * sum to its amount exactly), locked to a key derived from the leg's pool outpoint (`swapKeyID`) under `[2, "amm
12
- * swap"]` (counterparty self), the plan naming them a transfer out; and
13
- * (0.29.0) one P2PKH sats output per leg not covered (the leg's exact
14
- * sats, `swapFunding`), to that outpoint's `:sats` key, filed in
15
- * `amm-swap` by the createAction itself (customInstructions `{sats: true,
16
- * assetId, protocolID, keyID, counterparty}`). The module passes the sats outputs
17
- * through, selects the token inputs, adds change, asks once, signs and
18
- * submits to `tm_color_<assetId>`; the wallet funds the sats; the page
19
- * submits it to this skein too (a repeat is harmless), then files each
20
- * token output in `amm-swap` (basket insertion, customInstructions
21
- * `{assetId, amount, protocolID, keyID, counterparty}`). A sats-in swap,
22
- * or a tokens-in swap whose tokens are all staged already, sends only the
23
- * sats (`sendSatsToSwapBasket`: a plain createAction, no `p color` labels).
24
- * - **Spent** by the legs (src/market/swapAction.ts): app-built,
25
- * `createSignature` under the key the customInstructions name; the
26
- * wallet marks them spent when it takes the accepted swap in (0.29.0: no
27
- * relinquishOutput). An output a failed leg did not spend stays for a
28
- * later swap.
29
- * - **Move back** (`moveBack`, the recovery path; 0.29.3: a module plan,
30
- * David Case 2026-11-02): ONE createAction through the wallet's BRC-207
31
- * module, a `p color plan` transfer: every `amm-swap` token output of the
32
- * token and (0.29.1) the sats rows staged for its legs (their `assetId`)
33
- * as explicit inputs (their source transactions in `inputBEEF`) — to the
34
- * module FOREIGN inputs (not in a `p color` basket): the token rows
35
- * checked admitted and unspent on the token's overlay, the sats rows
36
- * adding nothing; one output, the whole amount to a NEW module receiving
37
- * key of the user's own, the plan's owned output 0 (the module checks it
38
- * pays the remittance's key and files it in `p color <assetTag>` with its
39
- * custody in the same call). The page unlocks its inputs at signAction;
40
- * the wallet funds the fee and takes the sats rows into its change; the
41
- * module approves and submits, the wallet broadcasts. The page submits it
42
- * to this skein's token topic too. Nothing is taken in afterwards and
43
- * nothing is kept in the browser. * (0.29.6) With no token row and only sats rows staged for the token
44
- * (a sats-in leg that failed): one plain createAction (no `p color`
45
- * labels), the sats rows as explicit inputs and no outputs — the wallet
46
- * keeps them as its change and pays the fee (`moveBackSats`).
7
+ * - **Staging** (`stage`): at Swap, ONE wallet action. Sats only (a buy, or a sell whose tokens
8
+ * the basket holds): a plain createAction, ONE P2PKH output to a fresh key under `SWAP_PROTOCOL`
9
+ * (self, keyID random, `amm-swap-<hex>`), filed in `amm-swap` by the action with its
10
+ * customInstructions (`{sats: true, protocolID, keyID, counterparty}`: no token tag). With
11
+ * tokens: the module transfer plan — ONE token output to a fresh key and the ONE sats output in
12
+ * the same action, the token key's id ALSO in that output's description (`… · amm-swap <hex>`,
13
+ * as the LP key's `amm-lp <hex>`), the action labelled `SWAP_STAGE_LABEL`; the module selects
14
+ * the token inputs, adds change, signs and submits; the page submits it too, then files the
15
+ * token output in `amm-swap` (basket insertion, `{assetId, amount, protocolID, keyID,
16
+ * counterparty}`). A labelled staged token output the basket lacks (the page died before the
17
+ * filing) is filed on load from the action's description (`unfiledStagedTokens`).
18
+ * - **Spent** by the legs (src/market/swapAction.ts), their change kept here between legs.
19
+ * - **Move back** (`moveBack`, as 0.29.3): ONE createAction through the wallet's BRC-207 module,
20
+ * a `p color plan` transfer: every `amm-swap` token row of the token and every sats row as
21
+ * explicit inputs (their source transactions in `inputBEEF`), one output — the whole amount to
22
+ * a NEW module receiving key of the user's own, the plan's owned output 0. The page unlocks its
23
+ * inputs at signAction; the wallet funds the fee and takes the sats into its change. With no
24
+ * token row and sats rows staged: one plain createAction, the sats rows as explicit inputs and
25
+ * no outputs (`moveBackSats`).
47
26
  */
48
27
  import { Beef, Transaction, type WalletInterface } from "@bsv/sdk";
49
28
  import { type Steak } from "../lp/close.js";
@@ -51,83 +30,76 @@ import { type Spends } from "../wallet/action.js";
51
30
  import { type SatsInput, type TokenInput } from "./swapAction.js";
52
31
  /** The keyID prefix of an `amm-swap` key (`SWAP_PROTOCOL`, counterparty self). */
53
32
  export declare const SWAP_KEY_PREFIX = "amm-swap-";
54
- /**
55
- * The keyID of a staged `amm-swap` output (0.29.2, David Case 2026-11-02: derived, not random):
56
- * `amm-swap-<txid>.<vout>` for a leg's token row and `amm-swap-<txid>.<vout>:sats` for its sats
57
- * row, `<txid>.<vout>` the leg's pool outpoint as quoted (`leg.outpoint`, `_` written `.`).
58
- * Protocol `[2, "amm swap"]`, counterparty self, as before. A leg refused and retried against the
59
- * same pool outpoint stages to the same key again, which is fine.
60
- */
61
- export declare function swapKeyID(poolOutpoint: string, kind: "token" | "sats"): string;
33
+ /** The label of a staging action that carries tokens (0.40.0): the staged token output's key id is in its output description. */
34
+ export declare const SWAP_STAGE_LABEL = "amm-swap-stage";
35
+ /** The marker a staged token output's description ends with (` · amm-swap <hex>`, 0.40.0): it names the key id. */
36
+ export declare const SWAP_KEY_MARK = " \u00B7 amm-swap ";
37
+ /** A staged row's keyID (0.40.0): `amm-swap-<16 random bytes, hex>`. */
38
+ export declare function newSwapKeyID(): string;
39
+ /** A staged token output's description: the readable text (at most 50 characters), then `SWAP_KEY_MARK` and the keyID's hex. */
40
+ export declare function swapKeyDescription(readable: string, keyID: string): string;
41
+ /** The keyID a staged token output's description names (`swapKeyDescription`), or null. */
42
+ export declare function swapKeyIdOfDescription(description: string | undefined): string | null;
62
43
  /** The `amm-swap` outputs of `tokenId` the legs can spend. */
63
44
  export declare function swapHoldings(wallet: WalletInterface, tokenId: string): Promise<TokenInput[]>;
64
45
  /** The `amm-swap` sats outputs the legs can spend (0.29.0). */
65
46
  export declare function swapSatsHoldings(wallet: WalletInterface): Promise<SatsInput[]>;
66
- /**
67
- * What `amm-swap` holds, per token id: the token amount and the number of token outputs, and
68
- * (0.29.6) the sats staged for the token's legs (the sats rows whose customInstructions name its
69
- * `assetId`). A token with only sats staged has amount 0n and outputs 0.
70
- */
47
+ /** What `amm-swap` holds, per token id: the token amount and the number of token outputs (0.40.0: the sats rows are no token's, `swapSatsTotal`). */
71
48
  export declare function swapTotals(wallet: WalletInterface): Promise<Map<string, {
72
49
  amount: bigint;
73
50
  outputs: number;
74
- sats: number;
75
51
  }>>;
52
+ /** The sats `amm-swap` holds (0.40.0: shown once, not per token). */
53
+ export declare function swapSatsTotal(wallet: WalletInterface): Promise<number>;
76
54
  /** The source transactions of `amm-swap`'s outputs (a leg's BEEF), with `extra` merged in. */
77
55
  export declare function swapHoldingBeef(wallet: WalletInterface, extra?: number[]): Promise<Beef>;
78
- export interface SentToSwapBasket {
56
+ /** What a staging action made (0.40.0): the sats row and, with tokens, the token row. */
57
+ export interface Staged {
79
58
  txid: string;
80
- /** The send as the wallet's AtomicBEEF. */
59
+ /** The action as the wallet's AtomicBEEF. */
81
60
  tx: number[];
82
- /** The new `amm-swap` token outputs, in the order of `stage.tokens`. */
83
- holdings: TokenInput[];
84
- /** The new `amm-swap` sats outputs, in the order of `stage.sats` (0.29.0). */
85
- sats: SatsInput[];
61
+ /** The sats row (absent: tokens staged with no sats lacking). */
62
+ sats?: SatsInput;
63
+ tokens?: TokenInput;
64
+ /** The token row filed in `amm-swap` (with tokens). */
86
65
  filed: boolean;
87
66
  submitted: string;
88
67
  errors: string[];
89
68
  }
90
- /** A staged sats output: the leg's exact sats, keyed by the leg's pool outpoint (`swapKeyID`). */
91
- export interface StagedSats {
92
- satoshis: number;
93
- /** The leg's pool outpoint, `<txid>_<vout>` or `<txid>.<vout>`. */
94
- pool: string;
95
- }
96
- /** What one send stages: a token output per leg (its amount in), then a sats output per leg. */
97
- export interface SwapStage {
98
- tokens: {
69
+ /** What one staging action puts into `amm-swap` (0.40.0): sats, and (a sell the basket does not cover) tokens of one token. */
70
+ export interface StageRequest {
71
+ sats: number;
72
+ tokens?: {
73
+ tokenId: string;
99
74
  amount: bigint;
100
- pool: string;
101
- }[];
102
- sats: StagedSats[];
75
+ };
103
76
  }
104
77
  /**
105
- * "In" (see the module comment): one module send of `stage.tokens` (each one
106
- * output) to `amm-swap` keys derived from each leg's pool (`swapKeyID`), filed in `amm-swap`, and of `stage.sats`
107
- * (each one output) after them. Throws when the wallet refuses or fails the
108
- * send; filing and the page's own submit are reported in `errors`.
78
+ * Staging (see the module comment): ONE wallet action. Throws when the wallet refuses or fails it;
79
+ * the token row's filing and the page's own submit are reported in `errors`.
109
80
  */
110
- export declare function sendToSwapBasket(wallet: WalletInterface, base: string, tokenId: string, stage: SwapStage, meta?: {
81
+ export declare function stage(wallet: WalletInterface, req: StageRequest, o?: {
111
82
  sym?: string;
112
- }, submit?: (topic: string, beef: number[]) => Promise<{
113
- steak?: Steak;
114
- }>): Promise<SentToSwapBasket>;
115
- export interface SentSats {
83
+ submit?: (topic: string, beef: number[]) => Promise<{
84
+ steak?: Steak;
85
+ }>;
86
+ }): Promise<Staged>;
87
+ /** A staged token output the basket lacks (0.40.0): its outpoint, token, amount and the key its description names. */
88
+ export interface UnfiledStaged {
116
89
  txid: string;
117
- /** The send as the wallet's AtomicBEEF. */
118
- tx: number[];
119
- /** The new `amm-swap` sats outputs, in the order of `sats`. */
120
- sats: SatsInput[];
121
- errors: string[];
90
+ vout: number;
91
+ tokenId: string;
92
+ amount: bigint;
93
+ keyID: string;
122
94
  }
123
95
  /**
124
- * The sats-only send (0.29.0): one plain createAction (no `p color` labels: the module is not in
125
- * the path) with one P2PKH output per amount to the leg's "amm swap" `:sats` key (`swapKeyID`), filed in `amm-swap`; the
126
- * wallet funds it, adds change and broadcasts. Throws when the wallet refuses or fails it.
96
+ * The labelled staging actions' token outputs not in `amm-swap` (0.40.0): every output of an action
97
+ * labelled `SWAP_STAGE_LABEL` whose description names a staged key and whose script is a 1Color
98
+ * value output, its basket not `amm-swap`. A wallet that refuses the read: none.
127
99
  */
128
- export declare function sendSatsToSwapBasket(wallet: WalletInterface, tokenId: string, sats: StagedSats[], meta?: {
129
- sym?: string;
130
- }): Promise<SentSats>;
100
+ export declare function unfiledStagedTokens(wallet: WalletInterface): Promise<UnfiledStaged[]>;
101
+ /** One unfiled staged token output filed in `amm-swap` from its transaction's BEEF (basket insertion, as `stage` files it). */
102
+ export declare function fileStagedToken(wallet: WalletInterface, u: UnfiledStaged, beef: number[]): Promise<boolean>;
131
103
  export interface MovedBack {
132
104
  txid: string;
133
105
  amount: bigint;
@@ -156,3 +128,12 @@ export declare function moveBack(i: {
156
128
  steak?: Steak;
157
129
  }>;
158
130
  }): Promise<MovedBack>;
131
+ /**
132
+ * Move back with only sats in the swap basket (0.29.6, David Case 2026-11-03; 0.40.0: every sats
133
+ * row, `satsRows` default the basket's): ONE plain wallet createAction (no `p color` labels, the module
134
+ * is not in the path) with the token's `amm-swap` sats rows as explicit inputs (their sources in
135
+ * `inputBEEF`) and NO outputs: the wallet keeps everything as its change and pays the fee from it.
136
+ * The page unlocks the rows at signAction; the wallet broadcasts and marks the rows spent itself
137
+ * (no relinquish, no take-in, nothing kept). Nothing is submitted to an overlay (no token moves).
138
+ */
139
+ export declare function moveBackSats(wallet: WalletInterface, sym?: string, satsRows?: SatsInput[]): Promise<MovedBack>;