@needmoretruth/nmts-cli 0.47.0 → 0.49.0
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.
- package/CHANGELOG.md +25 -0
- package/README.ko.md +1 -1
- package/README.md +1 -1
- package/dist/artifact-about.d.ts +1 -1
- package/dist/commands/links.d.ts +7 -0
- package/dist/commands/links.js +71 -0
- package/dist/commands/share-dispatch.js +5 -1
- package/dist/commands/wallet-swap.d.ts +3 -7
- package/dist/commands/wallet-swap.js +110 -86
- package/dist/help-options.js +2 -1
- package/dist/help.js +6 -3
- package/dist/links.d.ts +9 -0
- package/dist/links.js +20 -0
- package/dist/main.js +3 -4
- package/dist/portable.d.ts +1 -1
- package/dist/portable.js +1 -1
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/risk.d.ts +11 -0
- package/dist/risk.js +6 -0
- package/dist/shared/lib/wallet/swap-routes.d.ts +43 -0
- package/dist/shared/lib/wallet/swap-routes.js +62 -0
- package/dist/shared/lib/wallet/swap-rules.d.ts +48 -10
- package/dist/shared/lib/wallet/swap-rules.js +109 -21
- package/dist/shared/lib/wallet/venue-ids.d.ts +19 -2
- package/dist/shared/lib/wallet/venue-ids.js +35 -2
- package/dist/wallet-swap-chain.d.ts +12 -4
- package/dist/wallet-swap-chain.js +76 -36
- package/dist/wallet-swap-quote.d.ts +20 -3
- package/dist/wallet-swap-quote.js +101 -13
- package/dist/wallet-swap-words.d.ts +30 -0
- package/dist/wallet-swap-words.js +70 -0
- package/dist/wallet.d.ts +6 -0
- package/dist/wallet.js +11 -3
- package/docs/commands/link.md +9 -2
- package/package.json +1 -1
- package/server.json +2 -2
package/dist/main.js
CHANGED
|
@@ -99,10 +99,8 @@ export async function run(argv) {
|
|
|
99
99
|
sort: args.sort, desc: args.desc, long: args.long, media: args.media,
|
|
100
100
|
});
|
|
101
101
|
}
|
|
102
|
-
case "usage":
|
|
103
|
-
|
|
104
|
-
return await usage({ server: args.server, network: args.network, json: args.json });
|
|
105
|
-
}
|
|
102
|
+
case "usage":
|
|
103
|
+
return await (await import("./commands/usage.js")).usage({ server: args.server, network: args.network, json: args.json });
|
|
106
104
|
case "balance": {
|
|
107
105
|
const { balance } = await import("./commands/balance.js");
|
|
108
106
|
return await balance({ server: args.server, network: args.network, json: args.json });
|
|
@@ -296,6 +294,7 @@ export async function run(argv) {
|
|
|
296
294
|
case "shares":
|
|
297
295
|
case "unshare":
|
|
298
296
|
case "link":
|
|
297
|
+
case "links":
|
|
299
298
|
case "receive": {
|
|
300
299
|
const { runShare } = await import("./commands/share-dispatch.js");
|
|
301
300
|
return await runShare(args.command, args);
|
package/dist/portable.d.ts
CHANGED
|
@@ -57,7 +57,7 @@ export type { PlaintextSink } from "./download-sink.ts";
|
|
|
57
57
|
export { AGGREGATOR_ENV_VAR, readBlob, RELAY_ENV_VAR, SUI_RPC_ENV_VAR } from "./walrus.ts";
|
|
58
58
|
export { relayHost, storageNodesThrough, suiRpcHosts } from "./walrus.ts";
|
|
59
59
|
export type { ReadOptions } from "./walrus.ts";
|
|
60
|
-
export { listLinks, makeLink, openLink, revokeLink } from "./links.ts";
|
|
60
|
+
export { listLinks, listLiveLinks, makeLink, openLink, revokeAllLinks, revokeLink } from "./links.ts";
|
|
61
61
|
export type { LinkAccount, ListedLink, MadeLink, OpenedLinkFile } from "./links.ts";
|
|
62
62
|
export { coinAmount, readBalances, walCoinType, walletAddress } from "./wallet.ts";
|
|
63
63
|
export type { ChainReader, CoinBalance, WalletBalances } from "./wallet.ts";
|
package/dist/portable.js
CHANGED
|
@@ -82,7 +82,7 @@ export { AGGREGATOR_ENV_VAR, readBlob, RELAY_ENV_VAR, SUI_RPC_ENV_VAR } from "./
|
|
|
82
82
|
// caller said first, then the environment, then the network's own.
|
|
83
83
|
export { relayHost, storageNodesThrough, suiRpcHosts } from "./walrus.js";
|
|
84
84
|
// Public links (NCF-3 §5.8): make, list, cut, and open without an account — `nmts link`'s four.
|
|
85
|
-
export { listLinks, makeLink, openLink, revokeLink } from "./links.js";
|
|
85
|
+
export { listLinks, listLiveLinks, makeLink, openLink, revokeAllLinks, revokeLink } from "./links.js";
|
|
86
86
|
// The wallet the NMTS key derives: reading it, and signing with it.
|
|
87
87
|
export { coinAmount, readBalances, walCoinType, walletAddress } from "./wallet.js";
|
|
88
88
|
export { chainReader } from "./wallet-chain.js";
|
package/dist/product.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export declare const BINARY_NAME = "nmts";
|
|
|
9
9
|
* beside it. Here rather than in `main.ts` because the MCP server has to say it too, and a
|
|
10
10
|
* command importing the entry point is a cycle waiting to bite.
|
|
11
11
|
*/
|
|
12
|
-
export declare const VERSION = "0.
|
|
12
|
+
export declare const VERSION = "0.49.0";
|
|
13
13
|
/** Where the product lives, for messages that need to send somebody somewhere real. */
|
|
14
14
|
export declare const HOME_URL = "https://nmts.me";
|
|
15
15
|
/** The source, so a person holding only the built program can find what it was built from. */
|
package/dist/product.js
CHANGED
|
@@ -18,7 +18,7 @@ export const BINARY_NAME = "nmts";
|
|
|
18
18
|
* beside it. Here rather than in `main.ts` because the MCP server has to say it too, and a
|
|
19
19
|
* command importing the entry point is a cycle waiting to bite.
|
|
20
20
|
*/
|
|
21
|
-
export const VERSION = "0.
|
|
21
|
+
export const VERSION = "0.49.0";
|
|
22
22
|
/** Where the product lives, for messages that need to send somebody somewhere real. */
|
|
23
23
|
export const HOME_URL = "https://nmts.me";
|
|
24
24
|
/** The source, so a person holding only the built program can find what it was built from. */
|
package/dist/risk.d.ts
CHANGED
|
@@ -65,6 +65,9 @@ export declare const ACTS: {
|
|
|
65
65
|
readonly link: {
|
|
66
66
|
readonly tier: "none";
|
|
67
67
|
};
|
|
68
|
+
readonly links: {
|
|
69
|
+
readonly tier: "none";
|
|
70
|
+
};
|
|
68
71
|
readonly wallet: {
|
|
69
72
|
readonly tier: "none";
|
|
70
73
|
};
|
|
@@ -189,6 +192,14 @@ export declare const ACTS: {
|
|
|
189
192
|
readonly tier: "low";
|
|
190
193
|
readonly what: "Cut a public link.";
|
|
191
194
|
};
|
|
195
|
+
readonly "links.revoke": {
|
|
196
|
+
readonly tier: "low";
|
|
197
|
+
readonly what: "Cut a public link.";
|
|
198
|
+
};
|
|
199
|
+
readonly "links.revoke-all": {
|
|
200
|
+
readonly tier: "low";
|
|
201
|
+
readonly what: "Cut every public link this account holds.";
|
|
202
|
+
};
|
|
192
203
|
readonly tip: {
|
|
193
204
|
readonly tier: "none";
|
|
194
205
|
};
|
package/dist/risk.js
CHANGED
|
@@ -46,6 +46,8 @@ export const ACTS = {
|
|
|
46
46
|
// Listing this file's public links, and opening somebody's link (no account). `link.make` is under
|
|
47
47
|
// `high` beside `share`; `link.revoke` is under `low` beside `unshare`.
|
|
48
48
|
link: { tier: "none" },
|
|
49
|
+
// Listing every live link the account holds. Its two cuts are under `low` beside `link.revoke`.
|
|
50
|
+
links: { tier: "none" },
|
|
49
51
|
wallet: { tier: "none" },
|
|
50
52
|
get: { tier: "none" },
|
|
51
53
|
pull: { tier: "none" },
|
|
@@ -91,6 +93,8 @@ export const ACTS = {
|
|
|
91
93
|
restore: { tier: "low", what: "Bring these back out of the trash." },
|
|
92
94
|
unshare: { tier: "low", what: "Withdraw this share. Whoever had it cannot download it again." },
|
|
93
95
|
"link.revoke": { tier: "low", what: "Cut a public link." },
|
|
96
|
+
"links.revoke": { tier: "low", what: "Cut a public link." },
|
|
97
|
+
"links.revoke-all": { tier: "low", what: "Cut every public link this account holds." },
|
|
94
98
|
tip: { tier: "none" },
|
|
95
99
|
"tip.set": { tier: "none" },
|
|
96
100
|
"on-collision.set": { tier: "low", what: "Change what an upload does when its name is already taken." },
|
|
@@ -268,6 +272,8 @@ export function actOf(args) {
|
|
|
268
272
|
return sub === "make" ? "handover.make" : "handover";
|
|
269
273
|
case "link":
|
|
270
274
|
return sub === "make" ? "link.make" : sub === "revoke" ? "link.revoke" : "link";
|
|
275
|
+
case "links":
|
|
276
|
+
return sub === "revoke" ? "links.revoke" : sub === "revoke-all" ? "links.revoke-all" : "links";
|
|
271
277
|
case "support":
|
|
272
278
|
return sub === "send" || sub === "reply" ? "support.send" : "support";
|
|
273
279
|
case "platform":
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type SwapCoin, type SwapDirection, type SwapVenue } from "./swap-rules.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The three pools, named FIRST_SECOND. On every pool this product uses, the first coin is
|
|
4
|
+
* DeepBook's base and Bluefin's coin_a, and the pool's on-chain type is `Pool<first, second>` —
|
|
5
|
+
* so the type arguments of a call are always this order, whichever way the trade runs.
|
|
6
|
+
*/
|
|
7
|
+
export type SwapPair = "WAL_SUI" | "SUI_USDC" | "WAL_USDC";
|
|
8
|
+
/** The two coins of each pool, first coin first. */
|
|
9
|
+
export declare const PAIR_COINS: Readonly<Record<SwapPair, readonly [SwapCoin, SwapCoin]>>;
|
|
10
|
+
/** One trade through one pool: which pool, which coin goes in, and whether that is the first coin. */
|
|
11
|
+
export interface SwapHop {
|
|
12
|
+
pair: SwapPair;
|
|
13
|
+
from: SwapCoin;
|
|
14
|
+
to: SwapCoin;
|
|
15
|
+
/** True when the coin going in is the pool's first coin (DeepBook: sells base; Bluefin: a2b). */
|
|
16
|
+
sellsFirst: boolean;
|
|
17
|
+
}
|
|
18
|
+
/** The hop from one coin to another through the one pool that holds both. Throws for one coin twice. */
|
|
19
|
+
export declare function hopFor(from: SwapCoin, to: SwapCoin): SwapHop;
|
|
20
|
+
/**
|
|
21
|
+
* How a venue gets from one coin to the other: straight through the pool that holds both, or —
|
|
22
|
+
* for WAL ↔ SUI only — through USDC, two pools in ONE transaction, where the minimum is enforced
|
|
23
|
+
* on the last pool and so covers the whole trade.
|
|
24
|
+
*/
|
|
25
|
+
export type SwapRoute = "direct" | "viaUsdc";
|
|
26
|
+
/** The pools a route trades through, in order; null when the route does not apply to the direction. */
|
|
27
|
+
export declare function routeHops(direction: SwapDirection, route: SwapRoute): readonly SwapHop[] | null;
|
|
28
|
+
/**
|
|
29
|
+
* The routes a venue quotes for a direction. DeepBook quotes WAL ↔ SUI both ways: its WAL_SUI book
|
|
30
|
+
* measured thin on 2026-09-28 (1,000 WAL → 14.26 SUI against about 29.1 at market, while WAL_USDC
|
|
31
|
+
* then SUI_USDC gave 29.0), but below the USDC books' one-coin minimum the direct book was the
|
|
32
|
+
* only one that paid anything (10 WAL → 0.28 SUI direct, 0 through USDC). Bluefin's WAL/SUI pool
|
|
33
|
+
* is deep, so it trades direct only.
|
|
34
|
+
*/
|
|
35
|
+
export declare function venueRoutes(venue: SwapVenue, direction: SwapDirection): readonly SwapRoute[];
|
|
36
|
+
/**
|
|
37
|
+
* Of one venue's route answers, the one that pays more — that route is what gets built and signed.
|
|
38
|
+
* A tie goes to the direct route (one pool, one fee, fewer coins back). Null when nothing answered.
|
|
39
|
+
*/
|
|
40
|
+
export declare function pickRoute<T extends {
|
|
41
|
+
route: SwapRoute;
|
|
42
|
+
outUnits: bigint;
|
|
43
|
+
}>(answers: readonly T[]): T | null;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Which pools an in-app swap trades through: the three pools among SUI, WAL and USDC, the hop a
|
|
2
|
+
// direction takes, the routes each venue quotes, and how one route is chosen over another.
|
|
3
|
+
// ⚠ PUBLISHED — copied byte-for-byte into the `nmts` command-line package; keep comments
|
|
4
|
+
// self-contained English.
|
|
5
|
+
//
|
|
6
|
+
// CONTRACT: every function is pure — no I/O, no network. Pool ADDRESSES are not here (they are
|
|
7
|
+
// values per network, in `venue-ids.ts`); this file only says which pair and which way.
|
|
8
|
+
import { directionCoins } from "./swap-rules.js";
|
|
9
|
+
/** The two coins of each pool, first coin first. */
|
|
10
|
+
export const PAIR_COINS = {
|
|
11
|
+
WAL_SUI: ["WAL", "SUI"],
|
|
12
|
+
SUI_USDC: ["SUI", "USDC"],
|
|
13
|
+
WAL_USDC: ["WAL", "USDC"],
|
|
14
|
+
};
|
|
15
|
+
const PAIRS = ["WAL_SUI", "SUI_USDC", "WAL_USDC"];
|
|
16
|
+
/** The hop from one coin to another through the one pool that holds both. Throws for one coin twice. */
|
|
17
|
+
export function hopFor(from, to) {
|
|
18
|
+
for (const pair of PAIRS) {
|
|
19
|
+
const [first, second] = PAIR_COINS[pair];
|
|
20
|
+
if (from === first && to === second)
|
|
21
|
+
return { pair, from, to, sellsFirst: true };
|
|
22
|
+
if (from === second && to === first)
|
|
23
|
+
return { pair, from, to, sellsFirst: false };
|
|
24
|
+
}
|
|
25
|
+
throw new Error(`No pool trades ${from} for ${to}.`);
|
|
26
|
+
}
|
|
27
|
+
/** The pools a route trades through, in order; null when the route does not apply to the direction. */
|
|
28
|
+
export function routeHops(direction, route) {
|
|
29
|
+
const { in: from, out: to } = directionCoins(direction);
|
|
30
|
+
if (route === "direct")
|
|
31
|
+
return [hopFor(from, to)];
|
|
32
|
+
if (from === "USDC" || to === "USDC")
|
|
33
|
+
return null;
|
|
34
|
+
return [hopFor(from, "USDC"), hopFor("USDC", to)];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The routes a venue quotes for a direction. DeepBook quotes WAL ↔ SUI both ways: its WAL_SUI book
|
|
38
|
+
* measured thin on 2026-09-28 (1,000 WAL → 14.26 SUI against about 29.1 at market, while WAL_USDC
|
|
39
|
+
* then SUI_USDC gave 29.0), but below the USDC books' one-coin minimum the direct book was the
|
|
40
|
+
* only one that paid anything (10 WAL → 0.28 SUI direct, 0 through USDC). Bluefin's WAL/SUI pool
|
|
41
|
+
* is deep, so it trades direct only.
|
|
42
|
+
*/
|
|
43
|
+
export function venueRoutes(venue, direction) {
|
|
44
|
+
if (venue === "deepbook" && routeHops(direction, "viaUsdc") !== null)
|
|
45
|
+
return ["direct", "viaUsdc"];
|
|
46
|
+
return ["direct"];
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Of one venue's route answers, the one that pays more — that route is what gets built and signed.
|
|
50
|
+
* A tie goes to the direct route (one pool, one fee, fewer coins back). Null when nothing answered.
|
|
51
|
+
*/
|
|
52
|
+
export function pickRoute(answers) {
|
|
53
|
+
let best = null;
|
|
54
|
+
for (const answer of answers) {
|
|
55
|
+
if (best === null ||
|
|
56
|
+
answer.outUnits > best.outUnits ||
|
|
57
|
+
(answer.outUnits === best.outUnits && answer.route === "direct")) {
|
|
58
|
+
best = answer;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return best;
|
|
62
|
+
}
|
|
@@ -1,5 +1,32 @@
|
|
|
1
|
-
/**
|
|
2
|
-
export type
|
|
1
|
+
/** The three coins a swap moves between. Every screen and every command uses these three words. */
|
|
2
|
+
export type SwapCoin = "SUI" | "WAL" | "USDC";
|
|
3
|
+
/** The order the coins are offered in. Not a ranking. */
|
|
4
|
+
export declare const SWAP_COINS: readonly SwapCoin[];
|
|
5
|
+
/** Decimals per coin, from each coin's on-chain metadata: SUI and WAL keep 9, USDC keeps 6. */
|
|
6
|
+
export declare const COIN_DECIMALS: Readonly<Record<SwapCoin, number>>;
|
|
7
|
+
/** Which way the trade runs: any of the three coins into either of the other two. */
|
|
8
|
+
export type SwapDirection = "SUI_TO_WAL" | "WAL_TO_SUI" | "SUI_TO_USDC" | "USDC_TO_SUI" | "WAL_TO_USDC" | "USDC_TO_WAL";
|
|
9
|
+
/** All six directions. */
|
|
10
|
+
export declare const SWAP_DIRECTIONS: readonly SwapDirection[];
|
|
11
|
+
/** The coin that goes in and the coin that comes out. */
|
|
12
|
+
export declare function directionCoins(direction: SwapDirection): {
|
|
13
|
+
in: SwapCoin;
|
|
14
|
+
out: SwapCoin;
|
|
15
|
+
};
|
|
16
|
+
/** The direction from one coin to another, or null when the two are the same coin. */
|
|
17
|
+
export declare function directionOf(inCoin: SwapCoin, outCoin: SwapCoin): SwapDirection | null;
|
|
18
|
+
/**
|
|
19
|
+
* A coin type written out in one shape: the address lower-case and padded to 64 hex digits, the
|
|
20
|
+
* module and struct names untouched (Move names are case-sensitive). Null when it is not
|
|
21
|
+
* `<address>::<module>::<name>`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function normalizeCoinType(coinType: string): string | null;
|
|
24
|
+
/**
|
|
25
|
+
* Which of the three coins a full coin type is, or null. ⛔ THE WHOLE TYPE DECIDES, package address
|
|
26
|
+
* included — never the symbol. Anyone can publish a coin called "USDC"; only Circle's package
|
|
27
|
+
* address makes it USDC. `types` is the network's own table of the three exact types.
|
|
28
|
+
*/
|
|
29
|
+
export declare function swapCoinOfType(coinType: string, types: Readonly<Record<SwapCoin, string>>): SwapCoin | null;
|
|
3
30
|
/** Where the trade runs, on mainnet. */
|
|
4
31
|
export type SwapVenue = "deepbook" | "bluefin";
|
|
5
32
|
/**
|
|
@@ -58,13 +85,18 @@ export declare function feeBand(budgetMist: bigint, estimateMist: bigint | null)
|
|
|
58
85
|
export declare function priceDeviationBps(impliedRate: number, marketRate: number): number | null;
|
|
59
86
|
/** Beyond this distance from the market rate a quote is an extreme (3%). */
|
|
60
87
|
export declare const DEVIATION_WARN_BPS = 300;
|
|
61
|
-
/** Output per unit of input, as a number for the market comparison. Null when either side is 0. */
|
|
62
|
-
export declare function impliedRate(outUnits: bigint, amountInUnits: bigint): number | null;
|
|
63
88
|
/**
|
|
64
|
-
*
|
|
65
|
-
*
|
|
89
|
+
* Output per unit of input in WHOLE COINS, as a number for the market comparison and for reading.
|
|
90
|
+
* The decimals default to 9 (SUI and WAL); a USDC side passes 6, or one USDC reads as a thousandth
|
|
91
|
+
* of a coin. Null when either side is 0.
|
|
66
92
|
*/
|
|
67
|
-
export declare function
|
|
93
|
+
export declare function impliedRate(outUnits: bigint, amountInUnits: bigint, outDecimals?: number, inDecimals?: number): number | null;
|
|
94
|
+
/**
|
|
95
|
+
* The market rate in the DIRECTION'S OWN unit — coins out per coin in, e.g. WAL per SUI going one
|
|
96
|
+
* way and SUI per WAL the other — so the two sides of the ratio are never swapped. Null unless both
|
|
97
|
+
* coins of the direction have a positive price; a missing USDC price is "unknown", not 1.
|
|
98
|
+
*/
|
|
99
|
+
export declare function marketRate(direction: SwapDirection, suiUsd: number | null | undefined, walUsd: number | null | undefined, usdcUsd?: number | null): number | null;
|
|
68
100
|
/**
|
|
69
101
|
* The most SUI a swap may take in: balance minus the gas budget the person set, never negative.
|
|
70
102
|
* Not a fixed reserve — the person chose the budget, so that exact figure is what is kept back.
|
|
@@ -96,10 +128,16 @@ export interface DeepbookQuoteRow {
|
|
|
96
128
|
}
|
|
97
129
|
/**
|
|
98
130
|
* The three values a DeepBook quote returns are always (base side, quote side, DEEP needed); which
|
|
99
|
-
* of the first two is "received" and which is "left over" depends on
|
|
100
|
-
* quote
|
|
131
|
+
* of the first two is "received" and which is "left over" depends on which side goes in. Selling
|
|
132
|
+
* base (the pool's first coin) receives quote and leaves base over; buying base does the opposite.
|
|
133
|
+
*/
|
|
134
|
+
export declare function deepbookRowFrom(sellsBase: boolean, base: bigint, quote: bigint): DeepbookQuoteRow;
|
|
135
|
+
/**
|
|
136
|
+
* The fee rate of two pools traded one after the other, in bps with two decimals: the second fee
|
|
137
|
+
* is charged on what the first left, so 1 − (1 − a)(1 − b), not a + b. Null when either is null —
|
|
138
|
+
* an unmeasured leg makes the whole route unmeasured, never "that leg was free".
|
|
101
139
|
*/
|
|
102
|
-
export declare function
|
|
140
|
+
export declare function combineFeeBps(first: number | null, second: number | null): number | null;
|
|
103
141
|
/**
|
|
104
142
|
* DeepBook's EFFECTIVE fee rate, measured — there is no constant to read. The same amount is
|
|
105
143
|
* quoted in two modes: fee taken from the input coin (the mode used, needs no DEEP) and fee paid in
|
|
@@ -1,14 +1,82 @@
|
|
|
1
|
-
// The rules of an in-app swap
|
|
2
|
-
// quote turns into, the gas-budget bands, the market
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// The rules of an in-app swap among SUI, WAL and USDC — the six directions, the coin-type match,
|
|
2
|
+
// the slippage a person may set, the minimum a quote turns into, the gas-budget bands, the market
|
|
3
|
+
// cross-check, the venue fee arithmetic and the extremes gate. The pools and routes are next door
|
|
4
|
+
// in `swap-routes.ts`.
|
|
5
|
+
// ⚠ PUBLISHED — copied byte-for-byte into the `nmts` command-line package; keep comments
|
|
6
|
+
// self-contained English.
|
|
5
7
|
//
|
|
6
8
|
// CONTRACT: every function is pure — no I/O, no side effects, no network, no dates. Slippage is in
|
|
7
|
-
// bps (1 bps = 0.01%) and is a number because a person picks it; amounts are base units
|
|
8
|
-
//
|
|
9
|
+
// bps (1 bps = 0.01%) and is a number because a person picks it; amounts are base units (1e9 =
|
|
10
|
+
// one SUI or one WAL, 1e6 = one USDC) and are bigint because money is never a float.
|
|
9
11
|
// NON-CUSTODIAL: these helpers only DECIDE the numbers a person's own wallet then signs. NMTS is
|
|
10
12
|
// not a party to the trade and takes nothing from it: no fee argument, no NMTS address.
|
|
11
13
|
import { GAS_BUDGET_MIN_MIST } from "./send-rules.js";
|
|
14
|
+
/** The order the coins are offered in. Not a ranking. */
|
|
15
|
+
export const SWAP_COINS = ["SUI", "WAL", "USDC"];
|
|
16
|
+
/** Decimals per coin, from each coin's on-chain metadata: SUI and WAL keep 9, USDC keeps 6. */
|
|
17
|
+
export const COIN_DECIMALS = { SUI: 9, WAL: 9, USDC: 6 };
|
|
18
|
+
const DIRECTION_COINS = {
|
|
19
|
+
SUI_TO_WAL: { in: "SUI", out: "WAL" },
|
|
20
|
+
WAL_TO_SUI: { in: "WAL", out: "SUI" },
|
|
21
|
+
SUI_TO_USDC: { in: "SUI", out: "USDC" },
|
|
22
|
+
USDC_TO_SUI: { in: "USDC", out: "SUI" },
|
|
23
|
+
WAL_TO_USDC: { in: "WAL", out: "USDC" },
|
|
24
|
+
USDC_TO_WAL: { in: "USDC", out: "WAL" },
|
|
25
|
+
};
|
|
26
|
+
/** All six directions. */
|
|
27
|
+
export const SWAP_DIRECTIONS = [
|
|
28
|
+
"SUI_TO_WAL",
|
|
29
|
+
"WAL_TO_SUI",
|
|
30
|
+
"SUI_TO_USDC",
|
|
31
|
+
"USDC_TO_SUI",
|
|
32
|
+
"WAL_TO_USDC",
|
|
33
|
+
"USDC_TO_WAL",
|
|
34
|
+
];
|
|
35
|
+
/** The coin that goes in and the coin that comes out. */
|
|
36
|
+
export function directionCoins(direction) {
|
|
37
|
+
return DIRECTION_COINS[direction];
|
|
38
|
+
}
|
|
39
|
+
/** The direction from one coin to another, or null when the two are the same coin. */
|
|
40
|
+
export function directionOf(inCoin, outCoin) {
|
|
41
|
+
for (const direction of SWAP_DIRECTIONS) {
|
|
42
|
+
const coins = DIRECTION_COINS[direction];
|
|
43
|
+
if (coins.in === inCoin && coins.out === outCoin)
|
|
44
|
+
return direction;
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A coin type written out in one shape: the address lower-case and padded to 64 hex digits, the
|
|
50
|
+
* module and struct names untouched (Move names are case-sensitive). Null when it is not
|
|
51
|
+
* `<address>::<module>::<name>`.
|
|
52
|
+
*/
|
|
53
|
+
export function normalizeCoinType(coinType) {
|
|
54
|
+
const parts = coinType.trim().split("::");
|
|
55
|
+
if (parts.length !== 3)
|
|
56
|
+
return null;
|
|
57
|
+
const [address, module, name] = parts;
|
|
58
|
+
if (address === undefined || module === undefined || name === undefined)
|
|
59
|
+
return null;
|
|
60
|
+
const hex = address.toLowerCase().replace(/^0x/, "");
|
|
61
|
+
if (!/^[0-9a-f]{1,64}$/.test(hex) || !/^\w+$/.test(module) || !/^\w+$/.test(name))
|
|
62
|
+
return null;
|
|
63
|
+
return `0x${hex.padStart(64, "0")}::${module}::${name}`;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Which of the three coins a full coin type is, or null. ⛔ THE WHOLE TYPE DECIDES, package address
|
|
67
|
+
* included — never the symbol. Anyone can publish a coin called "USDC"; only Circle's package
|
|
68
|
+
* address makes it USDC. `types` is the network's own table of the three exact types.
|
|
69
|
+
*/
|
|
70
|
+
export function swapCoinOfType(coinType, types) {
|
|
71
|
+
const wanted = normalizeCoinType(coinType);
|
|
72
|
+
if (wanted === null)
|
|
73
|
+
return null;
|
|
74
|
+
for (const coin of SWAP_COINS) {
|
|
75
|
+
if (normalizeCoinType(types[coin]) === wanted)
|
|
76
|
+
return coin;
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
12
80
|
/**
|
|
13
81
|
* The order the two venues are listed in. NOT a ranking: neither is a default and nobody
|
|
14
82
|
* recommends one — at some amounts one pays more, at others the other. The order is fixed only so
|
|
@@ -97,23 +165,32 @@ export function priceDeviationBps(impliedRate, marketRate) {
|
|
|
97
165
|
}
|
|
98
166
|
/** Beyond this distance from the market rate a quote is an extreme (3%). */
|
|
99
167
|
export const DEVIATION_WARN_BPS = 300;
|
|
100
|
-
/**
|
|
101
|
-
|
|
168
|
+
/**
|
|
169
|
+
* Output per unit of input in WHOLE COINS, as a number for the market comparison and for reading.
|
|
170
|
+
* The decimals default to 9 (SUI and WAL); a USDC side passes 6, or one USDC reads as a thousandth
|
|
171
|
+
* of a coin. Null when either side is 0.
|
|
172
|
+
*/
|
|
173
|
+
export function impliedRate(outUnits, amountInUnits, outDecimals = 9, inDecimals = 9) {
|
|
102
174
|
if (amountInUnits <= 0n || outUnits <= 0n)
|
|
103
175
|
return null;
|
|
104
|
-
const r = Number(outUnits) / Number(amountInUnits);
|
|
176
|
+
const r = (Number(outUnits) / 10 ** outDecimals) / (Number(amountInUnits) / 10 ** inDecimals);
|
|
105
177
|
return Number.isFinite(r) && r > 0 ? r : null;
|
|
106
178
|
}
|
|
107
179
|
/**
|
|
108
|
-
* The market rate in the DIRECTION'S OWN unit —
|
|
109
|
-
* so the two sides of the ratio are never swapped. Null unless both
|
|
180
|
+
* The market rate in the DIRECTION'S OWN unit — coins out per coin in, e.g. WAL per SUI going one
|
|
181
|
+
* way and SUI per WAL the other — so the two sides of the ratio are never swapped. Null unless both
|
|
182
|
+
* coins of the direction have a positive price; a missing USDC price is "unknown", not 1.
|
|
110
183
|
*/
|
|
111
|
-
export function marketRate(direction, suiUsd, walUsd) {
|
|
112
|
-
|
|
184
|
+
export function marketRate(direction, suiUsd, walUsd, usdcUsd) {
|
|
185
|
+
const usd = { SUI: suiUsd, WAL: walUsd, USDC: usdcUsd };
|
|
186
|
+
const { in: inCoin, out: outCoin } = DIRECTION_COINS[direction];
|
|
187
|
+
const inUsd = usd[inCoin];
|
|
188
|
+
const outUsd = usd[outCoin];
|
|
189
|
+
if (inUsd == null || outUsd == null || inUsd <= 0 || outUsd <= 0)
|
|
113
190
|
return null;
|
|
114
|
-
if (!Number.isFinite(
|
|
191
|
+
if (!Number.isFinite(inUsd) || !Number.isFinite(outUsd))
|
|
115
192
|
return null;
|
|
116
|
-
return
|
|
193
|
+
return inUsd / outUsd;
|
|
117
194
|
}
|
|
118
195
|
/**
|
|
119
196
|
* The most SUI a swap may take in: balance minus the gas budget the person set, never negative.
|
|
@@ -162,13 +239,24 @@ export function feeRateBpsOf(feeUnits, amountInUnits) {
|
|
|
162
239
|
}
|
|
163
240
|
/**
|
|
164
241
|
* The three values a DeepBook quote returns are always (base side, quote side, DEEP needed); which
|
|
165
|
-
* of the first two is "received" and which is "left over" depends on
|
|
166
|
-
* quote
|
|
242
|
+
* of the first two is "received" and which is "left over" depends on which side goes in. Selling
|
|
243
|
+
* base (the pool's first coin) receives quote and leaves base over; buying base does the opposite.
|
|
167
244
|
*/
|
|
168
|
-
export function deepbookRowFrom(
|
|
169
|
-
return
|
|
170
|
-
? { outUnits:
|
|
171
|
-
: { outUnits:
|
|
245
|
+
export function deepbookRowFrom(sellsBase, base, quote) {
|
|
246
|
+
return sellsBase
|
|
247
|
+
? { outUnits: quote, leftoverInUnits: base }
|
|
248
|
+
: { outUnits: base, leftoverInUnits: quote };
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* The fee rate of two pools traded one after the other, in bps with two decimals: the second fee
|
|
252
|
+
* is charged on what the first left, so 1 − (1 − a)(1 − b), not a + b. Null when either is null —
|
|
253
|
+
* an unmeasured leg makes the whole route unmeasured, never "that leg was free".
|
|
254
|
+
*/
|
|
255
|
+
export function combineFeeBps(first, second) {
|
|
256
|
+
if (first === null || second === null)
|
|
257
|
+
return null;
|
|
258
|
+
const combined = first + second - (first * second) / 10_000;
|
|
259
|
+
return Math.round(combined * 100) / 100;
|
|
172
260
|
}
|
|
173
261
|
/**
|
|
174
262
|
* DeepBook's EFFECTIVE fee rate, measured — there is no constant to read. The same amount is
|
|
@@ -12,12 +12,29 @@ export declare const BLUEFIN_UPGRADE_CAP_IDS: Readonly<Record<"mainnet" | "testn
|
|
|
12
12
|
export declare const BLUEFIN_GLOBAL_CONFIG_IDS: Readonly<Record<"mainnet" | "testnet", string | null>>;
|
|
13
13
|
/** Bluefin's WAL/SUI pool, `Pool<WAL, SUI>` (coin_a = WAL, coin_b = SUI). Pinned by address: same-pair empty pools exist. */
|
|
14
14
|
export declare const BLUEFIN_WAL_SUI_POOLS: Readonly<Record<"mainnet" | "testnet", string | null>>;
|
|
15
|
+
/**
|
|
16
|
+
* Circle's native USDC on Sui, per network: 6 decimals, symbol USDC (read with
|
|
17
|
+
* `suix_getCoinMetadata` on both chains). ⛔ A coin is USDC only when its WHOLE type equals this
|
|
18
|
+
* string. Anyone can publish a coin whose symbol is "USDC"; the package address is the proof.
|
|
19
|
+
*/
|
|
20
|
+
export declare const USDC_COIN_TYPES: Readonly<Record<"mainnet" | "testnet", string>>;
|
|
21
|
+
/** DeepBook's SUI_USDC order book (base = SUI, quote = USDC). Its on-chain type is `Pool<SUI, USDC>`. */
|
|
22
|
+
export declare const DEEPBOOK_SUI_USDC_POOLS: Readonly<Record<"mainnet" | "testnet", string | null>>;
|
|
23
|
+
/** DeepBook's WAL_USDC order book (base = WAL, quote = USDC). Its on-chain type is `Pool<WAL, USDC>`. */
|
|
24
|
+
export declare const DEEPBOOK_WAL_USDC_POOLS: Readonly<Record<"mainnet" | "testnet", string | null>>;
|
|
25
|
+
/**
|
|
26
|
+
* Bluefin's SUI/USDC pool, `Pool<SUI, USDC>` (coin_a = SUI, coin_b = USDC). Pinned by address: the
|
|
27
|
+
* deepest of the 51 same-pair pools Bluefin's `PoolCreated` events listed on 2026-09-28.
|
|
28
|
+
*/
|
|
29
|
+
export declare const BLUEFIN_SUI_USDC_POOLS: Readonly<Record<"mainnet" | "testnet", string | null>>;
|
|
30
|
+
/** Bluefin's WAL/USDC pool, `Pool<WAL, USDC>` (coin_a = WAL, coin_b = USDC). The deeper of two, 2026-09-28. */
|
|
31
|
+
export declare const BLUEFIN_WAL_USDC_POOLS: Readonly<Record<"mainnet" | "testnet", string | null>>;
|
|
15
32
|
/**
|
|
16
33
|
* Bluefin's sqrt-price limits, ONE STEP INSIDE the protocol's tick range (`tick_math.move`). The
|
|
17
34
|
* range ends themselves abort a swap (1009) while the quote function accepts them, so a swap built
|
|
18
35
|
* on the ends shows a good quote and fails on chain. Measured on mainnet, both directions,
|
|
19
|
-
* 2026-08-03.
|
|
20
|
-
* protects the person, not this.
|
|
36
|
+
* 2026-08-03. A swap that puts coin_a in (a2b) passes the minimum, one that puts coin_b in the
|
|
37
|
+
* maximum; the minimum-out argument is what protects the person, not this.
|
|
21
38
|
*/
|
|
22
39
|
export declare const BLUEFIN_MIN_SQRT_PRICE: bigint;
|
|
23
40
|
export declare const BLUEFIN_MAX_SQRT_PRICE: bigint;
|
|
@@ -42,12 +42,45 @@ export const BLUEFIN_WAL_SUI_POOLS = {
|
|
|
42
42
|
testnet: null,
|
|
43
43
|
mainnet: "0xe60bc7ade245b9f35b49686dfab0a18e5ca9176d49bef1b90f60d67d06315ff0",
|
|
44
44
|
};
|
|
45
|
+
// ── USDC, and the four pools that trade it. Same rule: values only; null = none there.
|
|
46
|
+
/**
|
|
47
|
+
* Circle's native USDC on Sui, per network: 6 decimals, symbol USDC (read with
|
|
48
|
+
* `suix_getCoinMetadata` on both chains). ⛔ A coin is USDC only when its WHOLE type equals this
|
|
49
|
+
* string. Anyone can publish a coin whose symbol is "USDC"; the package address is the proof.
|
|
50
|
+
*/
|
|
51
|
+
export const USDC_COIN_TYPES = {
|
|
52
|
+
testnet: "0xa1ec7fc00a6f40db9693ad1415d0c193ad3906494428cf252621037bd7117e29::usdc::USDC",
|
|
53
|
+
mainnet: "0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC",
|
|
54
|
+
};
|
|
55
|
+
/** DeepBook's SUI_USDC order book (base = SUI, quote = USDC). Its on-chain type is `Pool<SUI, USDC>`. */
|
|
56
|
+
export const DEEPBOOK_SUI_USDC_POOLS = {
|
|
57
|
+
testnet: null,
|
|
58
|
+
mainnet: "0xe05dafb5133bcffb8d59f4e12465dc0e9faeaa05e3e342a08fe135800e3e4407",
|
|
59
|
+
};
|
|
60
|
+
/** DeepBook's WAL_USDC order book (base = WAL, quote = USDC). Its on-chain type is `Pool<WAL, USDC>`. */
|
|
61
|
+
export const DEEPBOOK_WAL_USDC_POOLS = {
|
|
62
|
+
testnet: null,
|
|
63
|
+
mainnet: "0x56a1c985c1f1123181d6b881714793689321ba24301b3585eec427436eb1c76d",
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Bluefin's SUI/USDC pool, `Pool<SUI, USDC>` (coin_a = SUI, coin_b = USDC). Pinned by address: the
|
|
67
|
+
* deepest of the 51 same-pair pools Bluefin's `PoolCreated` events listed on 2026-09-28.
|
|
68
|
+
*/
|
|
69
|
+
export const BLUEFIN_SUI_USDC_POOLS = {
|
|
70
|
+
testnet: null,
|
|
71
|
+
mainnet: "0x15dbcac854b1fc68fc9467dbd9ab34270447aabd8cc0e04a5864d95ccb86b74a",
|
|
72
|
+
};
|
|
73
|
+
/** Bluefin's WAL/USDC pool, `Pool<WAL, USDC>` (coin_a = WAL, coin_b = USDC). The deeper of two, 2026-09-28. */
|
|
74
|
+
export const BLUEFIN_WAL_USDC_POOLS = {
|
|
75
|
+
testnet: null,
|
|
76
|
+
mainnet: "0xbcc6909d2e85c06cf9cbfe5b292da36f5bfa0f314806474bbf6a0bf9744d37ce",
|
|
77
|
+
};
|
|
45
78
|
/**
|
|
46
79
|
* Bluefin's sqrt-price limits, ONE STEP INSIDE the protocol's tick range (`tick_math.move`). The
|
|
47
80
|
* range ends themselves abort a swap (1009) while the quote function accepts them, so a swap built
|
|
48
81
|
* on the ends shows a good quote and fails on chain. Measured on mainnet, both directions,
|
|
49
|
-
* 2026-08-03.
|
|
50
|
-
* protects the person, not this.
|
|
82
|
+
* 2026-08-03. A swap that puts coin_a in (a2b) passes the minimum, one that puts coin_b in the
|
|
83
|
+
* maximum; the minimum-out argument is what protects the person, not this.
|
|
51
84
|
*/
|
|
52
85
|
export const BLUEFIN_MIN_SQRT_PRICE = 4295048016n + 1n;
|
|
53
86
|
export const BLUEFIN_MAX_SQRT_PRICE = 79226673515401279992447579055n - 1n;
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import { Transaction } from "@mysten/sui/transactions";
|
|
2
2
|
import type { Network } from "./network.ts";
|
|
3
|
-
import
|
|
4
|
-
import { type
|
|
5
|
-
import { type
|
|
3
|
+
import { type SwapRoute } from "./shared/lib/wallet/swap-routes.ts";
|
|
4
|
+
import { type SwapDirection, type SwapVenue } from "./shared/lib/wallet/swap-rules.ts";
|
|
5
|
+
import { type CoinBalance, type WalletBalances } from "./wallet.ts";
|
|
6
|
+
import { type VenuePools, type VenueQuote } from "./wallet-swap-quote.ts";
|
|
6
7
|
/** Where a swap runs: one of the two mainnet venues, or the official testnet facility. */
|
|
7
8
|
export type SwapRail = SwapVenue | "exchange";
|
|
8
9
|
/** Bluefin's addresses: the package PINNED in this release, confirmed on chain before it is used. */
|
|
9
10
|
export interface BluefinBinding {
|
|
10
11
|
packageId: string;
|
|
11
12
|
globalConfigId: string;
|
|
12
|
-
|
|
13
|
+
/** `Pool<WAL, SUI>` · `Pool<SUI, USDC>` · `Pool<WAL, USDC>`, all pinned; null = none on that network. */
|
|
14
|
+
pools: VenuePools;
|
|
13
15
|
}
|
|
16
|
+
/** Bluefin's pinned pools on a network. */
|
|
17
|
+
export declare function bluefinPools(network: Network): VenuePools;
|
|
14
18
|
/** The testnet facility: its object, the package its type names, and its rate (WAL per SUI, as a fraction). */
|
|
15
19
|
export interface ExchangeFacility {
|
|
16
20
|
objectId: string;
|
|
@@ -26,6 +30,8 @@ export interface SwapShape {
|
|
|
26
30
|
minOutUnits: bigint;
|
|
27
31
|
/** A gas ceiling, or undefined to let the SDK set one from its own dry run. */
|
|
28
32
|
gasBudgetMist?: bigint | undefined;
|
|
33
|
+
/** The route the quote answered with (DeepBook WAL ↔ SUI may go through USDC). Absent = direct. */
|
|
34
|
+
route?: SwapRoute | undefined;
|
|
29
35
|
/** Present exactly when `venue` is bluefin: the signature uses the package the quote used. */
|
|
30
36
|
bluefin?: BluefinBinding | undefined;
|
|
31
37
|
/** Present exactly when `venue` is exchange. */
|
|
@@ -40,6 +46,8 @@ export declare function swapTransaction(input: SwapShape & {
|
|
|
40
46
|
/** What `commands/wallet-swap.ts` reads before it prints a review. */
|
|
41
47
|
export interface SwapReads {
|
|
42
48
|
readWallet(address: string): Promise<WalletBalances>;
|
|
49
|
+
/** The USDC balance, counted by Circle's exact coin type only. Read only when USDC is in the trade. */
|
|
50
|
+
readUsdc(address: string): Promise<CoinBalance>;
|
|
43
51
|
/** Bluefin's pinned package, version-checked. Throws when the chain refuses that package. */
|
|
44
52
|
resolveBluefin(): Promise<BluefinBinding>;
|
|
45
53
|
/** The testnet facility and its rate. Throws off testnet, or when the object cannot be read. */
|