@needmoretruth/nmts-cli 0.48.1 → 0.49.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.
- package/CHANGELOG.md +18 -0
- package/README.ko.md +1 -1
- package/README.md +1 -1
- package/dist/artifact-about.d.ts +1 -1
- package/dist/commands/wallet-swap.d.ts +3 -7
- package/dist/commands/wallet-swap.js +110 -86
- package/dist/heavy-self-pay.d.ts +4 -3
- package/dist/heavy-self-pay.js +6 -5
- package/dist/help-options.js +2 -1
- package/dist/help.js +4 -3
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- 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/package.json +1 -1
- package/server.json +2 -2
|
@@ -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. */
|
|
@@ -6,25 +6,38 @@
|
|
|
6
6
|
// ⛔ ONE BUILDER FOR THE FEE AND THE SIGNATURE. `estimateFee` and `wallet-sign.ts` both call
|
|
7
7
|
// `swapTransaction`, so the fee printed is the fee of the transaction that is then signed.
|
|
8
8
|
//
|
|
9
|
-
// ⛔ THE SHAPES ARE THE BROWSER'S, CALL FOR CALL. DeepBook: `pool::
|
|
10
|
-
//
|
|
11
|
-
// coin, and all THREE outputs (base, quote,
|
|
12
|
-
// coin, so a forgotten one aborts the whole
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
// `
|
|
9
|
+
// ⛔ THE SHAPES ARE THE BROWSER'S, CALL FOR CALL. DeepBook: `pool::swap_exact_base_for_quote` when the
|
|
10
|
+
// coin going in is the pool's first coin (base), `swap_exact_quote_for_base` when it is the second,
|
|
11
|
+
// with an EMPTY DEEP coin so the fee comes off the input coin, and all THREE outputs (base, quote,
|
|
12
|
+
// DEEP) sent back to the signer — Move cannot drop a coin, so a forgotten one aborts the whole
|
|
13
|
+
// transaction. Through USDC (WAL ↔ SUI only), the first pool's USDC coin goes straight into the
|
|
14
|
+
// second pool in the same transaction, the minimum is put on the last pool, and the first pool's
|
|
15
|
+
// leftover comes back too. Bluefin: `gateway::swap_assets`, which sends its outputs to the sender
|
|
16
|
+
// itself; type arguments always in pool order (first coin = coin_a), the direction carried by the
|
|
17
|
+
// `a2b` flag, and a sqrt-price limit one step inside the tick range (the exact end aborts). Testnet:
|
|
18
|
+
// the official Walrus facility `wal_exchange::exchange_all_for_wal`, SUI→WAL only, whose package is
|
|
19
|
+
// read off the Exchange object's own type. The ids come from `shared/lib/wallet/venue-ids.ts`,
|
|
20
|
+
// copied byte-for-byte from the browser.
|
|
18
21
|
//
|
|
19
22
|
// ⛔ OUR SHARE IS ZERO. No NMTS address, no fee argument, no output of ours in any of the three.
|
|
20
23
|
import { coinWithBalance, Transaction } from "@mysten/sui/transactions";
|
|
21
24
|
import { TESTNET_WALRUS_PACKAGE_CONFIG } from "@mysten/walrus";
|
|
22
25
|
import { walrusClient, netGasFee } from "./extend-chain.js";
|
|
23
26
|
import { isRecord } from "./guards.js";
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
27
|
+
import { hopFor, routeHops } from "./shared/lib/wallet/swap-routes.js";
|
|
28
|
+
import { directionCoins } from "./shared/lib/wallet/swap-rules.js";
|
|
29
|
+
import { BLUEFIN_GLOBAL_CONFIG_IDS, BLUEFIN_MAX_SQRT_PRICE, BLUEFIN_MIN_SQRT_PRICE, BLUEFIN_PACKAGE_IDS, BLUEFIN_SUI_USDC_POOLS, BLUEFIN_WAL_SUI_POOLS, BLUEFIN_WAL_USDC_POOLS, DEEP_COIN_TYPES, DEEPBOOK_PACKAGE_IDS, } from "./shared/lib/wallet/venue-ids.js";
|
|
30
|
+
import { readBalances, readCoinOfType } from "./wallet.js";
|
|
26
31
|
import { chainReader } from "./wallet-chain.js";
|
|
27
|
-
import { quoteVenue, QUOTE_SENDER } from "./wallet-swap-quote.js";
|
|
32
|
+
import { deepbookPools, pairTypes, poolIdOf, quoteVenue, QUOTE_SENDER, swapCoinTypes, } from "./wallet-swap-quote.js";
|
|
33
|
+
/** Bluefin's pinned pools on a network. */
|
|
34
|
+
export function bluefinPools(network) {
|
|
35
|
+
return {
|
|
36
|
+
WAL_SUI: BLUEFIN_WAL_SUI_POOLS[network],
|
|
37
|
+
SUI_USDC: BLUEFIN_SUI_USDC_POOLS[network],
|
|
38
|
+
WAL_USDC: BLUEFIN_WAL_USDC_POOLS[network],
|
|
39
|
+
};
|
|
40
|
+
}
|
|
28
41
|
/** The rails a network has. Testnet's DeepBook book trades a different WAL, and Bluefin has none. */
|
|
29
42
|
export function railsFor(network) {
|
|
30
43
|
return network === "mainnet" ? ["deepbook", "bluefin"] : ["exchange"];
|
|
@@ -32,36 +45,60 @@ export function railsFor(network) {
|
|
|
32
45
|
export function swapTransaction(input) {
|
|
33
46
|
const tx = new Transaction();
|
|
34
47
|
tx.setSender(input.sender);
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
48
|
+
const types = swapCoinTypes(input.network);
|
|
49
|
+
const { in: coinFrom, out: coinTo } = directionCoins(input.direction);
|
|
50
|
+
// SUI comes off the gas coin; WAL and USDC are gathered from the wallet by their EXACT types.
|
|
51
|
+
const coinOf = (coin) => coin === "SUI"
|
|
52
|
+
? tx.splitCoins(tx.gas, [input.amountInUnits])[0]
|
|
53
|
+
: tx.add(coinWithBalance({ balance: input.amountInUnits, type: types[coin] }));
|
|
40
54
|
if (input.venue === "deepbook") {
|
|
41
|
-
const
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
55
|
+
const route = input.route ?? "direct";
|
|
56
|
+
const hops = routeHops(input.direction, route);
|
|
57
|
+
if (hops === null || hops.length === 0)
|
|
58
|
+
throw new Error(`DeepBook has no ${route} route for ${input.direction}.`);
|
|
59
|
+
const pools = deepbookPools(input.network);
|
|
60
|
+
let coin = coinOf(coinFrom);
|
|
61
|
+
let deep = tx.moveCall({ target: "0x2::coin::zero", typeArguments: [DEEP_COIN_TYPES[input.network]] });
|
|
62
|
+
const back = [];
|
|
63
|
+
hops.forEach((hop, i) => {
|
|
64
|
+
const last = i === hops.length - 1;
|
|
65
|
+
const swapped = tx.moveCall({
|
|
66
|
+
target: `${DEEPBOOK_PACKAGE_IDS[input.network]}::pool::${hop.sellsFirst ? "swap_exact_base_for_quote" : "swap_exact_quote_for_base"}`,
|
|
67
|
+
typeArguments: pairTypes(input.network, hop.pair),
|
|
68
|
+
arguments: [tx.object(poolIdOf(pools, hop.pair, "deepbook")), coin, deep, tx.pure.u64(last ? input.minOutUnits : 0n), tx.object.clock()],
|
|
69
|
+
});
|
|
70
|
+
// The result is indexed, and an index is typed as possibly absent; nothing below can build a
|
|
71
|
+
// transfer of fewer than the three outputs.
|
|
72
|
+
const [baseCoin, quoteCoin, deepCoin] = [swapped[0], swapped[1], swapped[2]];
|
|
73
|
+
if (baseCoin === undefined || quoteCoin === undefined || deepCoin === undefined) {
|
|
74
|
+
throw new Error("DeepBook's swap did not yield its three outputs.");
|
|
75
|
+
}
|
|
76
|
+
if (last) {
|
|
77
|
+
back.push(baseCoin, quoteCoin, deepCoin);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
// The first pool's leftover goes home; what it bought feeds the next pool, with its empty DEEP.
|
|
81
|
+
back.push(hop.sellsFirst ? baseCoin : quoteCoin);
|
|
82
|
+
coin = hop.sellsFirst ? quoteCoin : baseCoin;
|
|
83
|
+
deep = deepCoin;
|
|
84
|
+
}
|
|
46
85
|
});
|
|
47
|
-
|
|
48
|
-
// index is typed as possibly absent; nothing below can build a transfer of fewer than three.
|
|
49
|
-
const outputs = [swapped[0], swapped[1], swapped[2]].filter((coin) => coin !== undefined);
|
|
50
|
-
if (outputs.length !== 3)
|
|
51
|
-
throw new Error("DeepBook's swap did not yield its three outputs.");
|
|
52
|
-
tx.transferObjects(outputs, input.sender);
|
|
86
|
+
tx.transferObjects(back, input.sender);
|
|
53
87
|
}
|
|
54
88
|
else if (input.venue === "bluefin") {
|
|
55
89
|
if (input.bluefin === undefined)
|
|
56
90
|
throw new Error("A Bluefin swap needs its resolved package first.");
|
|
57
|
-
const
|
|
91
|
+
const hop = hopFor(coinFrom, coinTo);
|
|
92
|
+
const a2b = hop.sellsFirst;
|
|
93
|
+
const coinIn = coinOf(coinFrom);
|
|
94
|
+
const coinZero = tx.moveCall({ target: "0x2::coin::zero", typeArguments: [types[coinTo]] });
|
|
58
95
|
tx.moveCall({
|
|
59
96
|
target: `${input.bluefin.packageId}::gateway::swap_assets`,
|
|
60
|
-
typeArguments:
|
|
97
|
+
typeArguments: pairTypes(input.network, hop.pair),
|
|
61
98
|
arguments: [
|
|
62
99
|
tx.object.clock(),
|
|
63
100
|
tx.object(input.bluefin.globalConfigId),
|
|
64
|
-
tx.object(input.bluefin.
|
|
101
|
+
tx.object(poolIdOf(input.bluefin.pools, hop.pair, "bluefin")),
|
|
65
102
|
a2b ? coinIn : coinZero,
|
|
66
103
|
a2b ? coinZero : coinIn,
|
|
67
104
|
tx.pure.bool(a2b),
|
|
@@ -75,8 +112,9 @@ export function swapTransaction(input) {
|
|
|
75
112
|
else {
|
|
76
113
|
if (input.exchange === undefined)
|
|
77
114
|
throw new Error("The testnet exchange needs its object first.");
|
|
78
|
-
if (
|
|
115
|
+
if (input.direction !== "SUI_TO_WAL")
|
|
79
116
|
throw new Error("The testnet facility only turns SUI into WAL.");
|
|
117
|
+
const coinIn = coinOf("SUI");
|
|
80
118
|
const walOut = tx.moveCall({
|
|
81
119
|
target: `${input.exchange.packageId}::wal_exchange::exchange_all_for_wal`,
|
|
82
120
|
arguments: [tx.object(input.exchange.objectId), coinIn],
|
|
@@ -119,7 +157,10 @@ export function swapReads(network) {
|
|
|
119
157
|
let bluefin = null;
|
|
120
158
|
return {
|
|
121
159
|
async readWallet(address) {
|
|
122
|
-
return readBalances(chainReader(network, address),
|
|
160
|
+
return readBalances(chainReader(network, address), swapCoinTypes(network).WAL);
|
|
161
|
+
},
|
|
162
|
+
async readUsdc(address) {
|
|
163
|
+
return readCoinOfType(chainReader(network, address), swapCoinTypes(network).USDC, "USDC");
|
|
123
164
|
},
|
|
124
165
|
async resolveBluefin() {
|
|
125
166
|
// Memoised for the run, and a failure is not memoised: one refusal must not become "no Bluefin".
|
|
@@ -128,11 +169,10 @@ export function swapReads(network) {
|
|
|
128
169
|
const pending = (async () => {
|
|
129
170
|
const packageId = BLUEFIN_PACKAGE_IDS[network];
|
|
130
171
|
const globalConfigId = BLUEFIN_GLOBAL_CONFIG_IDS[network];
|
|
131
|
-
|
|
132
|
-
if (packageId === null || globalConfigId === null || poolId === null) {
|
|
172
|
+
if (packageId === null || globalConfigId === null || BLUEFIN_WAL_SUI_POOLS[network] === null) {
|
|
133
173
|
throw new Error(`This tool knows no Bluefin WAL/SUI pool on ${network}.`);
|
|
134
174
|
}
|
|
135
|
-
return checkedBluefinBinding({ packageId, globalConfigId,
|
|
175
|
+
return checkedBluefinBinding({ packageId, globalConfigId, pools: bluefinPools(network) }, client);
|
|
136
176
|
})();
|
|
137
177
|
bluefin = pending;
|
|
138
178
|
pending.catch(() => {
|
|
@@ -1,22 +1,39 @@
|
|
|
1
1
|
import type { walrusClient } from "./extend-chain.ts";
|
|
2
2
|
import type { Network } from "./network.ts";
|
|
3
|
-
import { type
|
|
3
|
+
import { type SwapPair, type SwapRoute } from "./shared/lib/wallet/swap-routes.ts";
|
|
4
|
+
import { type SwapCoin, type SwapDirection, type SwapVenue } from "./shared/lib/wallet/swap-rules.ts";
|
|
4
5
|
/** The zero address: a quote is a read, and a read needs no wallet. */
|
|
5
6
|
export declare const QUOTE_SENDER: string;
|
|
6
7
|
/** One venue's answer, now. Every field was read from the chain this moment; none is a constant. */
|
|
7
8
|
export interface VenueQuote {
|
|
8
9
|
venue: SwapVenue;
|
|
9
|
-
/** What comes out AFTER the venue's fee, in base units. */
|
|
10
|
+
/** What comes out AFTER the venue's fee, in the output coin's base units. */
|
|
10
11
|
outUnits: bigint;
|
|
11
12
|
/** The fee this quote carries, in bps (two decimals). Null = could not be measured, not 0. */
|
|
12
13
|
feeRateBps: number | null;
|
|
13
14
|
/** Input that would come back unused (DeepBook rounds to its lot size). */
|
|
14
15
|
leftoverInUnits: bigint;
|
|
16
|
+
/** The route this answer came from — the swap is built along exactly this route. */
|
|
17
|
+
route: SwapRoute;
|
|
18
|
+
/** USDC the second pool of a through-USDC route would hand back unused. 0n on a direct route. */
|
|
19
|
+
leftoverMidUnits: bigint;
|
|
15
20
|
}
|
|
21
|
+
/** The pools a venue has on a network, per pair. Null = none there, and nothing is guessed. */
|
|
22
|
+
export type VenuePools = Readonly<Record<SwapPair, string | null>>;
|
|
23
|
+
/** The three exact coin types of a network. ⛔ A coin is matched by this whole type, never a symbol. */
|
|
24
|
+
export declare function swapCoinTypes(network: Network): Readonly<Record<SwapCoin, string>>;
|
|
25
|
+
/** DeepBook's order books on a network. */
|
|
26
|
+
export declare function deepbookPools(network: Network): VenuePools;
|
|
27
|
+
/** The pool's two coin types, in pool order (first coin = DeepBook base = Bluefin coin_a). */
|
|
28
|
+
export declare function pairTypes(network: Network, pair: SwapPair): [string, string];
|
|
29
|
+
/** A pool's id, or a throw when this tool knows none on that network. */
|
|
30
|
+
export declare function poolIdOf(pools: VenuePools, pair: SwapPair, venue: SwapVenue): string;
|
|
31
|
+
/** The routes a venue can build for a direction with the pools it has. */
|
|
32
|
+
export declare function routesWithPools(venue: SwapVenue, direction: SwapDirection, pools: VenuePools): SwapRoute[];
|
|
16
33
|
type Client = ReturnType<typeof walrusClient>;
|
|
17
34
|
/** One venue's answer now. Bluefin needs its resolved binding; DeepBook needs nothing resolved. */
|
|
18
35
|
export declare function quoteVenue(client: Client, network: Network, venue: SwapVenue, direction: SwapDirection, amountInUnits: bigint, bluefin: {
|
|
19
36
|
packageId: string;
|
|
20
|
-
|
|
37
|
+
pools: VenuePools;
|
|
21
38
|
} | null): Promise<VenueQuote>;
|
|
22
39
|
export {};
|