@dcasegr/amm-client 0.0.0-stage → 0.38.1

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.
Files changed (121) hide show
  1. package/README.md +107 -2
  2. package/dist/client.d.ts +534 -0
  3. package/dist/client.js +1319 -0
  4. package/dist/errors.d.ts +17 -0
  5. package/dist/errors.js +75 -0
  6. package/dist/index.d.ts +51 -0
  7. package/dist/index.js +53 -0
  8. package/dist/lib/config.d.ts +42 -0
  9. package/dist/lib/config.js +64 -0
  10. package/dist/lib/keys.d.ts +42 -0
  11. package/dist/lib/keys.js +48 -0
  12. package/dist/lib/overlay.d.ts +190 -0
  13. package/dist/lib/overlay.js +302 -0
  14. package/dist/lib/skein.d.ts +82 -0
  15. package/dist/lib/skein.js +142 -0
  16. package/dist/lib/tokenId.d.ts +72 -0
  17. package/dist/lib/tokenId.js +121 -0
  18. package/dist/lp/amounts.d.ts +12 -0
  19. package/dist/lp/amounts.js +38 -0
  20. package/dist/lp/close.d.ts +90 -0
  21. package/dist/lp/close.js +140 -0
  22. package/dist/lp/images.d.ts +11 -0
  23. package/dist/lp/images.js +45 -0
  24. package/dist/lp/inventory.d.ts +68 -0
  25. package/dist/lp/inventory.js +84 -0
  26. package/dist/lp/poolDeploy.d.ts +220 -0
  27. package/dist/lp/poolDeploy.js +291 -0
  28. package/dist/lp/poolRows.d.ts +13 -0
  29. package/dist/lp/poolRows.js +19 -0
  30. package/dist/lp/positions.d.ts +81 -0
  31. package/dist/lp/positions.js +144 -0
  32. package/dist/lp/rebalance.d.ts +101 -0
  33. package/dist/lp/rebalance.js +168 -0
  34. package/dist/lp/wallet.d.ts +12 -0
  35. package/dist/lp/wallet.js +19 -0
  36. package/dist/market/activeSwap.d.ts +158 -0
  37. package/dist/market/activeSwap.js +320 -0
  38. package/dist/market/plan.d.ts +85 -0
  39. package/dist/market/plan.js +149 -0
  40. package/dist/market/prices.d.ts +13 -0
  41. package/dist/market/prices.js +14 -0
  42. package/dist/market/relay.d.ts +84 -0
  43. package/dist/market/relay.js +201 -0
  44. package/dist/market/swapAction.d.ts +306 -0
  45. package/dist/market/swapAction.js +508 -0
  46. package/dist/market/swapFlow.d.ts +58 -0
  47. package/dist/market/swapFlow.js +86 -0
  48. package/dist/market/view.d.ts +58 -0
  49. package/dist/market/view.js +63 -0
  50. package/dist/pool/artifact.d.ts +23 -0
  51. package/dist/pool/artifact.js +38 -0
  52. package/dist/pool/index.d.ts +8 -0
  53. package/dist/pool/index.js +8 -0
  54. package/dist/pool/oneColor.d.ts +19 -0
  55. package/dist/pool/oneColor.js +39 -0
  56. package/dist/pool/pool.artifact.json +5069 -0
  57. package/dist/pool/runar-ir-schema.d.ts +21 -0
  58. package/dist/pool/runar-ir-schema.js +18 -0
  59. package/dist/pool/template.d.ts +295 -0
  60. package/dist/pool/template.js +715 -0
  61. package/dist/vendor/runar/LICENSE +21 -0
  62. package/dist/vendor/runar/NOTICE +15 -0
  63. package/dist/vendor/runar/runar-ir-schema/src/abi-type-encoding.d.ts +42 -0
  64. package/dist/vendor/runar/runar-ir-schema/src/abi-type-encoding.js +53 -0
  65. package/dist/vendor/runar/runar-ir-schema/src/anf-ir.d.ts +221 -0
  66. package/dist/vendor/runar/runar-ir-schema/src/anf-ir.js +33 -0
  67. package/dist/vendor/runar/runar-ir-schema/src/artifact.d.ts +288 -0
  68. package/dist/vendor/runar/runar-ir-schema/src/artifact.js +8 -0
  69. package/dist/vendor/runar/runar-ir-schema/src/canonical-json.d.ts +43 -0
  70. package/dist/vendor/runar/runar-ir-schema/src/canonical-json.js +204 -0
  71. package/dist/vendor/runar/runar-ir-schema/src/input-limits.d.ts +89 -0
  72. package/dist/vendor/runar/runar-ir-schema/src/input-limits.js +100 -0
  73. package/dist/vendor/runar/runar-ir-schema/src/stack-ir.d.ts +118 -0
  74. package/dist/vendor/runar/runar-ir-schema/src/stack-ir.js +8 -0
  75. package/dist/vendor/runar/runar-ir-schema/src/state-layout.d.ts +56 -0
  76. package/dist/vendor/runar/runar-ir-schema/src/state-layout.js +145 -0
  77. package/dist/vendor/runar/runar-ir-schema/src/unknown-anf-kind-error.d.ts +18 -0
  78. package/dist/vendor/runar/runar-ir-schema/src/unknown-anf-kind-error.js +25 -0
  79. package/dist/vendor/runar/runar-sdk/src/anf-interpreter.d.ts +160 -0
  80. package/dist/vendor/runar/runar-sdk/src/anf-interpreter.js +1149 -0
  81. package/dist/vendor/runar/runar-sdk/src/calling.d.ts +98 -0
  82. package/dist/vendor/runar/runar-sdk/src/calling.js +209 -0
  83. package/dist/vendor/runar/runar-sdk/src/contract.d.ts +443 -0
  84. package/dist/vendor/runar/runar-sdk/src/contract.js +2516 -0
  85. package/dist/vendor/runar/runar-sdk/src/deployment.d.ts +36 -0
  86. package/dist/vendor/runar/runar-sdk/src/deployment.js +119 -0
  87. package/dist/vendor/runar/runar-sdk/src/errors.d.ts +46 -0
  88. package/dist/vendor/runar/runar-sdk/src/errors.js +59 -0
  89. package/dist/vendor/runar/runar-sdk/src/oppushtx.d.ts +23 -0
  90. package/dist/vendor/runar/runar-sdk/src/oppushtx.js +95 -0
  91. package/dist/vendor/runar/runar-sdk/src/ordinals/envelope.d.ts +31 -0
  92. package/dist/vendor/runar/runar-sdk/src/ordinals/envelope.js +272 -0
  93. package/dist/vendor/runar/runar-sdk/src/ordinals/types.d.ts +21 -0
  94. package/dist/vendor/runar/runar-sdk/src/ordinals/types.js +4 -0
  95. package/dist/vendor/runar/runar-sdk/src/providers/provider.d.ts +65 -0
  96. package/dist/vendor/runar/runar-sdk/src/providers/provider.js +59 -0
  97. package/dist/vendor/runar/runar-sdk/src/providers/wallet-provider.d.ts +94 -0
  98. package/dist/vendor/runar/runar-sdk/src/providers/wallet-provider.js +297 -0
  99. package/dist/vendor/runar/runar-sdk/src/script-utils.d.ts +54 -0
  100. package/dist/vendor/runar/runar-sdk/src/script-utils.js +252 -0
  101. package/dist/vendor/runar/runar-sdk/src/signers/signer.d.ts +17 -0
  102. package/dist/vendor/runar/runar-sdk/src/signers/signer.js +4 -0
  103. package/dist/vendor/runar/runar-sdk/src/spend-safety.d.ts +31 -0
  104. package/dist/vendor/runar/runar-sdk/src/spend-safety.js +35 -0
  105. package/dist/vendor/runar/runar-sdk/src/state.d.ts +54 -0
  106. package/dist/vendor/runar/runar-sdk/src/state.js +584 -0
  107. package/dist/vendor/runar/runar-sdk/src/types.d.ts +227 -0
  108. package/dist/vendor/runar/runar-sdk/src/types.js +4 -0
  109. package/dist/vendor/runar/runar-sdk/src/unsound-primitives.d.ts +30 -0
  110. package/dist/vendor/runar/runar-sdk/src/unsound-primitives.js +42 -0
  111. package/dist/vendor/runar/runar-sdk.d.ts +9 -0
  112. package/dist/vendor/runar/runar-sdk.js +8 -0
  113. package/dist/wallet/action.d.ts +28 -0
  114. package/dist/wallet/action.js +42 -0
  115. package/dist/wallet/brc29.d.ts +83 -0
  116. package/dist/wallet/brc29.js +80 -0
  117. package/dist/wallet/color.d.ts +110 -0
  118. package/dist/wallet/color.js +134 -0
  119. package/dist/wallet/swapPayouts.d.ts +35 -0
  120. package/dist/wallet/swapPayouts.js +146 -0
  121. package/package.json +39 -4
@@ -0,0 +1,58 @@
1
+ /**
2
+ * The Swap page's market view, shaped from the instance's answers (pure):
3
+ * the token topics it serves, each token's pools from its lookup, each pool's
4
+ * marginal price, and its validator's liveness from the runtime's liveness
5
+ * read of the token's beacon topic (`GET <base>/.live/tm_color_<txid>_0-live`). Token
6
+ * metadata (symbol, decimals, icon) comes from the wallet when it holds the
7
+ * token's deploy output (the LP page's inventory); otherwise only the id.
8
+ */
9
+ import type { PoolState } from "@dcasegr/amm-engine";
10
+ import type { LiveAnswer, TokenTopic } from "../lib/overlay.js";
11
+ import type { IconRef } from "../lp/inventory.js";
12
+ export interface TokenMeta {
13
+ sym?: string;
14
+ dec?: number;
15
+ icon?: IconRef;
16
+ }
17
+ export interface ValidatorStatus {
18
+ identityKey: string;
19
+ /** The token's liveness read returned a beat from it (the instance's window; the pages keep none). */
20
+ live: boolean;
21
+ /** Present in the token's liveness read at all. */
22
+ seen: boolean;
23
+ peerId?: string;
24
+ at?: number;
25
+ ageMs?: number;
26
+ }
27
+ export interface PoolRow {
28
+ pool: PoolState;
29
+ /** Marginal price, sats per token (display units when `dec` is known, else per base unit). */
30
+ price: string;
31
+ priceUnit: "token" | "base unit";
32
+ validator: ValidatorStatus;
33
+ }
34
+ export interface MarketToken {
35
+ topic: TokenTopic;
36
+ meta?: TokenMeta;
37
+ pools: PoolRow[];
38
+ /** The validators live on the token (`GET <base>/.live/tm_color_<txid>_0-live`), or null when unread. */
39
+ live: LiveAnswer | null;
40
+ /** The lookup failed for this token. */
41
+ error?: string;
42
+ /** The liveness read failed for this token. */
43
+ liveError?: string;
44
+ }
45
+ export declare const PRICE_DECIMALS = 8;
46
+ /**
47
+ * bsvReserve / tokenReserve, in sats per display token when `dec` is known
48
+ * (× 10^dec), as a decimal string with up to 8 places. Pre-fee: what an
49
+ * infinitesimal swap would pay.
50
+ */
51
+ export declare function marginalPrice(pool: Pick<PoolState, "bsvReserve" | "tokenReserve">, dec?: number): string;
52
+ /** sats ÷ tokens (base units) as sats per display token, 8 places; "—" when either is 0. */
53
+ export declare function priceOf(sats: bigint, tokens: bigint, dec?: number): string;
54
+ export declare function validatorStatus(identityKey: string, live: LiveAnswer | null): ValidatorStatus;
55
+ /** The engine's `lastSeen` from the heartbeat map (pools whose validator was never seen keep none). */
56
+ export declare function withLastSeen(pools: PoolState[], live: LiveAnswer | null): PoolState[];
57
+ export declare function buildMarketView(topics: TokenTopic[], pools: Map<string, PoolState[] | Error>, liveByToken: Map<string, LiveAnswer | Error>, meta: Map<string, TokenMeta>): MarketToken[];
58
+ export declare function shortKey(k: string): string;
@@ -0,0 +1,63 @@
1
+ import { formatAmount } from "../lp/amounts.js";
2
+ export const PRICE_DECIMALS = 8;
3
+ /**
4
+ * bsvReserve / tokenReserve, in sats per display token when `dec` is known
5
+ * (× 10^dec), as a decimal string with up to 8 places. Pre-fee: what an
6
+ * infinitesimal swap would pay.
7
+ */
8
+ export function marginalPrice(pool, dec) {
9
+ if (pool.tokenReserve <= 0n)
10
+ return "—";
11
+ const scale = 10n ** BigInt(PRICE_DECIMALS);
12
+ const num = pool.bsvReserve * 10n ** BigInt(dec ?? 0) * scale;
13
+ return formatAmount(num / pool.tokenReserve, PRICE_DECIMALS);
14
+ }
15
+ /** sats ÷ tokens (base units) as sats per display token, 8 places; "—" when either is 0. */
16
+ export function priceOf(sats, tokens, dec) {
17
+ if (sats <= 0n || tokens <= 0n)
18
+ return "—";
19
+ return marginalPrice({ bsvReserve: sats, tokenReserve: tokens }, dec);
20
+ }
21
+ export function validatorStatus(identityKey, live) {
22
+ const v = live?.validators.find((x) => x.identityKey === identityKey.toLowerCase());
23
+ if (!v)
24
+ return { identityKey, live: false, seen: false };
25
+ return { identityKey, live: v.live, seen: true, peerId: v.peerId, at: v.at, ageMs: v.ageMs };
26
+ }
27
+ /** The engine's `lastSeen` from the heartbeat map (pools whose validator was never seen keep none). */
28
+ export function withLastSeen(pools, live) {
29
+ return pools.map((p) => {
30
+ const v = live?.validators.find((x) => x.identityKey === p.validatorIdentityKey.toLowerCase());
31
+ return v ? { ...p, lastSeen: v.at } : p;
32
+ });
33
+ }
34
+ export function buildMarketView(topics, pools, liveByToken, meta) {
35
+ return topics.map((topic) => {
36
+ const m = meta.get(topic.tokenId);
37
+ const answer = pools.get(topic.tokenId);
38
+ const read = liveByToken.get(topic.tokenId);
39
+ const live = read && !(read instanceof Error) ? read : null;
40
+ const token = { topic, pools: [], live, ...(m ? { meta: m } : {}), ...(read instanceof Error ? { liveError: read.message } : {}) };
41
+ if (answer instanceof Error)
42
+ return { ...token, error: answer.message };
43
+ const dec = m?.dec;
44
+ token.pools = (answer ?? [])
45
+ .map((pool) => ({
46
+ pool,
47
+ price: marginalPrice(pool, dec),
48
+ priceUnit: dec === undefined ? "base unit" : "token",
49
+ validator: validatorStatus(pool.validatorIdentityKey, live),
50
+ }))
51
+ // Cheapest tokens first (most tokens per sat).
52
+ .sort((a, b) => cmpPrice(a.pool, b.pool) || a.pool.outpoint.localeCompare(b.pool.outpoint));
53
+ return token;
54
+ });
55
+ }
56
+ function cmpPrice(a, b) {
57
+ const l = a.bsvReserve * b.tokenReserve;
58
+ const r = b.bsvReserve * a.tokenReserve;
59
+ return l < r ? -1 : l > r ? 1 : 0;
60
+ }
61
+ export function shortKey(k) {
62
+ return k.length > 20 ? `${k.slice(0, 10)}…${k.slice(-6)}` : k;
63
+ }
@@ -0,0 +1,23 @@
1
+ import type { RunarArtifact } from "../vendor/runar/runar-sdk.js";
2
+ /**
3
+ * The compiled Pool contract (pool/Pool.runar.go), as the Go compiler writes
4
+ * it (`compiler.ArtifactToJSON`; see README.md "pool.artifact.json"). Its
5
+ * `script` is byte-identical to the template in src/fixtures/pool_artifact.zig
6
+ * (there with its codeSepIndex slots resolved); both are written by
7
+ * `go run ./gen/vectors` (0.9.0).
8
+ *
9
+ * 0.13.0 (layout 2): state TokenReserve, LpPubKey, ValidatorPubKey,
10
+ * ValidatorIdentity, Nonce, Affirmation; methods Swap, Close (the LP's, outputs
11
+ * free), Rescind.
12
+ */
13
+ export declare const poolArtifact: RunarArtifact;
14
+ /** A pool's layout: 2, the only one (0.29.1: layout 1, the 0.9.0-0.12.x contract, is dropped). */
15
+ export type PoolVersion = 2;
16
+ export declare function artifactOf(_version: PoolVersion): RunarArtifact;
17
+ /** Public method indices (the method selector pushed last in a call). */
18
+ export declare const METHOD_INDEX: {
19
+ readonly swap: 0;
20
+ readonly close: 1;
21
+ readonly rescind: 2;
22
+ };
23
+ export type PoolMethod = keyof typeof METHOD_INDEX;
@@ -0,0 +1,38 @@
1
+ import poolArtifactJson from "./pool.artifact.json" with { type: "json" };
2
+ /**
3
+ * The compiled Pool contract (pool/Pool.runar.go), as the Go compiler writes
4
+ * it (`compiler.ArtifactToJSON`; see README.md "pool.artifact.json"). Its
5
+ * `script` is byte-identical to the template in src/fixtures/pool_artifact.zig
6
+ * (there with its codeSepIndex slots resolved); both are written by
7
+ * `go run ./gen/vectors` (0.9.0).
8
+ *
9
+ * 0.13.0 (layout 2): state TokenReserve, LpPubKey, ValidatorPubKey,
10
+ * ValidatorIdentity, Nonce, Affirmation; methods Swap, Close (the LP's, outputs
11
+ * free), Rescind.
12
+ */
13
+ export const poolArtifact = resolveCodeSepSlots(poolArtifactJson);
14
+ /**
15
+ * The artifact with its codeSepIndex slots resolved in `script` (layout 2: its variable-length
16
+ * state makes each method push its OP_CODESEPARATOR's index). The separator is the code's
17
+ * byte 1, before every constructor slot, so each slot is OP_1 in every pool — as gen/vectors
18
+ * resolves them for src/pool.zig. runar-sdk's matcher (`matchesArtifact`,
19
+ * `extractConstructorArgs`) knows only constructor slots; its builder overwrites the resolved
20
+ * byte with the same OP_1.
21
+ */
22
+ function resolveCodeSepSlots(a) {
23
+ const slots = a.codeSepIndexSlots ?? [];
24
+ if (slots.length === 0)
25
+ return a;
26
+ let script = a.script;
27
+ for (const s of slots) {
28
+ if (s.codeSepIndex !== 1 || script.slice(s.byteOffset * 2, s.byteOffset * 2 + 2) !== "00")
29
+ throw new Error("pool artifact: a codeSepIndex slot not at byte 1");
30
+ script = script.slice(0, s.byteOffset * 2) + "51" + script.slice(s.byteOffset * 2 + 2);
31
+ }
32
+ return { ...a, script };
33
+ }
34
+ export function artifactOf(_version) {
35
+ return poolArtifact;
36
+ }
37
+ /** Public method indices (the method selector pushed last in a call). */
38
+ export const METHOD_INDEX = { swap: 0, close: 1, rescind: 2 };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * The AMM Pool script template (pool/Pool.runar.go) over runar-sdk, vendored in
3
+ * src/vendor/runar (README.md, "runar-sdk, vendored").
4
+ * Independent of React.
5
+ */
6
+ export { PoolTemplate, PoolBuildError, RESCINDED_KEY, p2pkhUnlock, type Pool, type PoolArgs, type PoolFields, type PoolUtxo, type BuilderInput, type FeeSpec, type SwapParams, type SwapExpectation, type SwapAmounts, type SwapPlanParams, type CallPlan, type CloseParams, type PoolCall, type PoolSigner, type SigSlot, type PoolBuildErrorCode, } from "./template.js";
7
+ export { poolArtifact, artifactOf, METHOD_INDEX, type PoolMethod, type PoolVersion } from "./artifact.js";
8
+ export { decodeOneColor, oneColorValuePrefix, type OneColorPrefix, type OneColorRole } from "./oneColor.js";
@@ -0,0 +1,8 @@
1
+ /**
2
+ * The AMM Pool script template (pool/Pool.runar.go) over runar-sdk, vendored in
3
+ * src/vendor/runar (README.md, "runar-sdk, vendored").
4
+ * Independent of React.
5
+ */
6
+ export { PoolTemplate, PoolBuildError, RESCINDED_KEY, p2pkhUnlock, } from "./template.js";
7
+ export { poolArtifact, artifactOf, METHOD_INDEX } from "./artifact.js";
8
+ export { decodeOneColor, oneColorValuePrefix } from "./oneColor.js";
@@ -0,0 +1,19 @@
1
+ import { ONE_COLOR_MAX_AMOUNT, type OneColorRole } from "@1sat/templates";
2
+ export { ONE_COLOR_MAX_AMOUNT, type OneColorRole };
3
+ export interface OneColorPrefix {
4
+ role: OneColorRole;
5
+ /** Wire id bytes (32, or 36 for a legacy BRC-161 id), absent on a deploy. */
6
+ idBytes?: Uint8Array;
7
+ amount: bigint;
8
+ payload?: Uint8Array;
9
+ /** Byte length of the prefix (and payload), i.e. where the inner lock starts. */
10
+ length: number;
11
+ }
12
+ /** The 1Color prefix at the start of `script`, or null when it has none (`OneColor.decode`). */
13
+ export declare function decodeOneColor(script: Uint8Array | number[]): OneColorPrefix | null;
14
+ /**
15
+ * The value-output prefix `<push id> <push amount> OP_2DROP` with no payload,
16
+ * as pool/Pool.runar.go's `tokenPrefix` writes it (`OneColor.value(...).prefix()`).
17
+ * The pool's asset id is always the 32-byte form.
18
+ */
19
+ export declare function oneColorValuePrefix(idBytes: Uint8Array | number[], amount: bigint): number[];
@@ -0,0 +1,39 @@
1
+ /**
2
+ * BRC-162 (1Color) prefix for the pool template, over 1sat-sdk's `OneColor`
3
+ * (`@1sat/templates` 0.1.0, the template formerly `Mandala`). One decoder:
4
+ * this file only adapts `OneColor.decode` / `OneColor.value(...).prefix()` to
5
+ * the byte-array shape `template.ts` works in.
6
+ *
7
+ * <push id | OP_0> <push amount | OP_0> OP_2DROP [<push payload> OP_DROP] <lock>
8
+ */
9
+ import { LockingScript, Script } from "@bsv/sdk";
10
+ import { OneColor, ONE_COLOR_MAX_AMOUNT } from "@1sat/templates";
11
+ export { ONE_COLOR_MAX_AMOUNT };
12
+ /** The 1Color prefix at the start of `script`, or null when it has none (`OneColor.decode`). */
13
+ export function decodeOneColor(script) {
14
+ const bytes = Array.from(script);
15
+ const t = OneColor.decode(Script.fromBinary(bytes));
16
+ if (!t)
17
+ return null;
18
+ return {
19
+ role: t.role,
20
+ idBytes: t.idBytes,
21
+ amount: t.amount,
22
+ payload: t.payload,
23
+ length: bytes.length - t.lock.toBinary().length,
24
+ };
25
+ }
26
+ /**
27
+ * The value-output prefix `<push id> <push amount> OP_2DROP` with no payload,
28
+ * as pool/Pool.runar.go's `tokenPrefix` writes it (`OneColor.value(...).prefix()`).
29
+ * The pool's asset id is always the 32-byte form.
30
+ */
31
+ export function oneColorValuePrefix(idBytes, amount) {
32
+ const id = Uint8Array.from(idBytes);
33
+ if (id.length !== 32)
34
+ throw new Error(`oneColorValuePrefix: the id must be 32 bytes, got ${id.length}`);
35
+ if (amount <= 0n || amount > ONE_COLOR_MAX_AMOUNT) {
36
+ throw new Error("oneColorValuePrefix: amount must be between 1 and 2^64-1");
37
+ }
38
+ return OneColor.value(id, amount, { lock: new LockingScript() }).prefix().toBinary();
39
+ }