@moon-cx/sdk 0.0.0-stage → 2.0.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 (155) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +2 -2
  3. package/dist/adapters/sender.d.ts +88 -0
  4. package/dist/adapters/sender.d.ts.map +1 -0
  5. package/dist/adapters/sender.js +62 -0
  6. package/dist/adapters/sender.js.map +1 -0
  7. package/dist/adapters/tonclient.d.ts +13 -0
  8. package/dist/adapters/tonclient.d.ts.map +1 -0
  9. package/dist/adapters/tonclient.js +17 -0
  10. package/dist/adapters/tonclient.js.map +1 -0
  11. package/dist/api/index.d.ts +210 -0
  12. package/dist/api/index.d.ts.map +1 -0
  13. package/dist/api/index.js +364 -0
  14. package/dist/api/index.js.map +1 -0
  15. package/dist/codec/bins.d.ts +26 -0
  16. package/dist/codec/bins.d.ts.map +1 -0
  17. package/dist/codec/bins.js +67 -0
  18. package/dist/codec/bins.js.map +1 -0
  19. package/dist/codec/clmm.d.ts +87 -0
  20. package/dist/codec/clmm.d.ts.map +1 -0
  21. package/dist/codec/clmm.js +119 -0
  22. package/dist/codec/clmm.js.map +1 -0
  23. package/dist/codec/cpmm.d.ts +112 -0
  24. package/dist/codec/cpmm.d.ts.map +1 -0
  25. package/dist/codec/cpmm.js +146 -0
  26. package/dist/codec/cpmm.js.map +1 -0
  27. package/dist/codec/jetton.d.ts +30 -0
  28. package/dist/codec/jetton.d.ts.map +1 -0
  29. package/dist/codec/jetton.js +37 -0
  30. package/dist/codec/jetton.js.map +1 -0
  31. package/dist/codec/mempad.d.ts +109 -0
  32. package/dist/codec/mempad.d.ts.map +1 -0
  33. package/dist/codec/mempad.js +162 -0
  34. package/dist/codec/mempad.js.map +1 -0
  35. package/dist/core/amount.d.ts +33 -0
  36. package/dist/core/amount.d.ts.map +1 -0
  37. package/dist/core/amount.js +81 -0
  38. package/dist/core/amount.js.map +1 -0
  39. package/dist/core/asset.d.ts +82 -0
  40. package/dist/core/asset.d.ts.map +1 -0
  41. package/dist/core/asset.js +143 -0
  42. package/dist/core/asset.js.map +1 -0
  43. package/dist/core/errors.d.ts +70 -0
  44. package/dist/core/errors.d.ts.map +1 -0
  45. package/dist/core/errors.js +114 -0
  46. package/dist/core/errors.js.map +1 -0
  47. package/dist/core/fee.d.ts +36 -0
  48. package/dist/core/fee.d.ts.map +1 -0
  49. package/dist/core/fee.js +40 -0
  50. package/dist/core/fee.js.map +1 -0
  51. package/dist/core/gas.d.ts +112 -0
  52. package/dist/core/gas.d.ts.map +1 -0
  53. package/dist/core/gas.js +265 -0
  54. package/dist/core/gas.js.map +1 -0
  55. package/dist/core/percent.d.ts +27 -0
  56. package/dist/core/percent.d.ts.map +1 -0
  57. package/dist/core/percent.js +71 -0
  58. package/dist/core/percent.js.map +1 -0
  59. package/dist/core/tx.d.ts +36 -0
  60. package/dist/core/tx.d.ts.map +1 -0
  61. package/dist/core/tx.js +17 -0
  62. package/dist/core/tx.js.map +1 -0
  63. package/dist/index.d.ts +63 -0
  64. package/dist/index.d.ts.map +1 -0
  65. package/dist/index.js +41 -0
  66. package/dist/index.js.map +1 -0
  67. package/dist/jetton/index.d.ts +53 -0
  68. package/dist/jetton/index.d.ts.map +1 -0
  69. package/dist/jetton/index.js +90 -0
  70. package/dist/jetton/index.js.map +1 -0
  71. package/dist/math/clmm-swap.d.ts +80 -0
  72. package/dist/math/clmm-swap.d.ts.map +1 -0
  73. package/dist/math/clmm-swap.js +277 -0
  74. package/dist/math/clmm-swap.js.map +1 -0
  75. package/dist/math/clmm.d.ts +234 -0
  76. package/dist/math/clmm.d.ts.map +1 -0
  77. package/dist/math/clmm.js +270 -0
  78. package/dist/math/clmm.js.map +1 -0
  79. package/dist/math/cpmm.d.ts +170 -0
  80. package/dist/math/cpmm.d.ts.map +1 -0
  81. package/dist/math/cpmm.js +175 -0
  82. package/dist/math/cpmm.js.map +1 -0
  83. package/dist/math/integer.d.ts +10 -0
  84. package/dist/math/integer.d.ts.map +1 -0
  85. package/dist/math/integer.js +44 -0
  86. package/dist/math/integer.js.map +1 -0
  87. package/dist/math/q128.d.ts +45 -0
  88. package/dist/math/q128.d.ts.map +1 -0
  89. package/dist/math/q128.js +108 -0
  90. package/dist/math/q128.js.map +1 -0
  91. package/dist/moon.d.ts +245 -0
  92. package/dist/moon.d.ts.map +1 -0
  93. package/dist/moon.js +332 -0
  94. package/dist/moon.js.map +1 -0
  95. package/dist/ops/jetton-input.d.ts +3 -0
  96. package/dist/ops/jetton-input.d.ts.map +1 -0
  97. package/dist/ops/jetton-input.js +8 -0
  98. package/dist/ops/jetton-input.js.map +1 -0
  99. package/dist/ops/liquidity.d.ts +157 -0
  100. package/dist/ops/liquidity.d.ts.map +1 -0
  101. package/dist/ops/liquidity.js +413 -0
  102. package/dist/ops/liquidity.js.map +1 -0
  103. package/dist/ops/mempad.d.ts +143 -0
  104. package/dist/ops/mempad.d.ts.map +1 -0
  105. package/dist/ops/mempad.js +248 -0
  106. package/dist/ops/mempad.js.map +1 -0
  107. package/dist/ops/pool.d.ts +75 -0
  108. package/dist/ops/pool.d.ts.map +1 -0
  109. package/dist/ops/pool.js +129 -0
  110. package/dist/ops/pool.js.map +1 -0
  111. package/dist/ops/position.d.ts +47 -0
  112. package/dist/ops/position.d.ts.map +1 -0
  113. package/dist/ops/position.js +73 -0
  114. package/dist/ops/position.js.map +1 -0
  115. package/dist/ops/swap.d.ts +104 -0
  116. package/dist/ops/swap.d.ts.map +1 -0
  117. package/dist/ops/swap.js +248 -0
  118. package/dist/ops/swap.js.map +1 -0
  119. package/dist/pool/amounts.d.ts +36 -0
  120. package/dist/pool/amounts.d.ts.map +1 -0
  121. package/dist/pool/amounts.js +68 -0
  122. package/dist/pool/amounts.js.map +1 -0
  123. package/dist/pool/assets.d.ts +45 -0
  124. package/dist/pool/assets.d.ts.map +1 -0
  125. package/dist/pool/assets.js +57 -0
  126. package/dist/pool/assets.js.map +1 -0
  127. package/dist/pool/clmm.d.ts +309 -0
  128. package/dist/pool/clmm.d.ts.map +1 -0
  129. package/dist/pool/clmm.js +561 -0
  130. package/dist/pool/clmm.js.map +1 -0
  131. package/dist/pool/cpmm.d.ts +228 -0
  132. package/dist/pool/cpmm.d.ts.map +1 -0
  133. package/dist/pool/cpmm.js +332 -0
  134. package/dist/pool/cpmm.js.map +1 -0
  135. package/dist/protocol/generated.d.ts +2752 -0
  136. package/dist/protocol/generated.d.ts.map +1 -0
  137. package/dist/protocol/generated.js +3564 -0
  138. package/dist/protocol/generated.js.map +1 -0
  139. package/dist/protocol/index.d.ts +19 -0
  140. package/dist/protocol/index.d.ts.map +1 -0
  141. package/dist/protocol/index.js +49 -0
  142. package/dist/protocol/index.js.map +1 -0
  143. package/dist/state/address.d.ts +121 -0
  144. package/dist/state/address.d.ts.map +1 -0
  145. package/dist/state/address.js +143 -0
  146. package/dist/state/address.js.map +1 -0
  147. package/dist/state/source.d.ts +26 -0
  148. package/dist/state/source.d.ts.map +1 -0
  149. package/dist/state/source.js +32 -0
  150. package/dist/state/source.js.map +1 -0
  151. package/dist/state/storage.d.ts +218 -0
  152. package/dist/state/storage.d.ts.map +1 -0
  153. package/dist/state/storage.js +273 -0
  154. package/dist/state/storage.js.map +1 -0
  155. package/package.json +74 -4
@@ -0,0 +1,104 @@
1
+ import type { Address } from '@ton/core';
2
+ import { Amount } from '../core/amount.ts';
3
+ import type { Asset } from '../core/asset.ts';
4
+ import { Percent } from '../core/percent.ts';
5
+ import { type GasRequirement } from '../core/gas.ts';
6
+ import { type BuiltOperation } from '../core/tx.ts';
7
+ import type { ClmmRangeState, CpmmPoolState } from '../state/storage.ts';
8
+ /**
9
+ * A swap quote computed locally from on-chain state, with no backend involved.
10
+ * It is only as fresh as the state it was computed from.
11
+ */
12
+ export type SwapQuote = {
13
+ protocol: 'cpmm' | 'clmm';
14
+ /** Where the swap is sent: a pool for CPMM, a range for CLMM. */
15
+ target: Address;
16
+ assetIn: Asset;
17
+ assetOut: Asset;
18
+ amountIn: Amount;
19
+ /** What the pool returns at the state this quote was built from. */
20
+ amountOut: Amount;
21
+ /**
22
+ * Minimum received, rounded down.
23
+ *
24
+ * CPMM: written into the message as `minOut` and enforced by the pool.
25
+ *
26
+ * CLMM: the range's swap message has no `minOut`;
27
+ * the message carries `sqrtPriceLimit` and this value is derived from it
28
+ * (`guaranteedOutAtLimit`) using the fees read at quote time. It holds only
29
+ * while those fees do not change: the range owner can raise `lpFee` and
30
+ * `protocolFee` to 100 bps each, which
31
+ * can cut the output by up to 1 − 0.99² / ((1 − lpFee)(1 − protocolFee)) < 2%.
32
+ * Overriding this field has no effect on CLMM.
33
+ */
34
+ minAmountOut: Amount;
35
+ priceImpactBps: number;
36
+ lpFee: Amount;
37
+ protocolFee: Amount;
38
+ /** True when the input side is X. Decides which leg and which direction. */
39
+ xIn: boolean;
40
+ gas: GasRequirement;
41
+ /** CLMM only: the Q128 price bound written into the message. */
42
+ sqrtPriceLimit?: bigint;
43
+ /** CLMM only: bins the swap is expected to cross. */
44
+ binsCrossed?: number[];
45
+ };
46
+ /**
47
+ * Quotes a CPMM swap from the pool's own reserves.
48
+ *
49
+ * Rejects up front for every reason the pool would reject, naming which one —
50
+ * a bounced message on the native leg is merely wasteful, but on the jetton
51
+ * leg the tokens are gone.
52
+ */
53
+ export declare function quoteCpmmSwap(args: {
54
+ pool: Address;
55
+ state: CpmmPoolState;
56
+ assetIn: Asset;
57
+ assetOut: Asset;
58
+ amountIn: Amount;
59
+ slippage: Percent;
60
+ }): SwapQuote;
61
+ /**
62
+ * Quotes a CLMM swap by walking the range's bins.
63
+ *
64
+ * A partially fillable swap is refused rather than silently truncated: the
65
+ * range would take what it can and stop, and a caller who was quoted the
66
+ * full amount would not know why they received less.
67
+ */
68
+ export declare function quoteClmmSwap(args: {
69
+ range: Address;
70
+ state: ClmmRangeState;
71
+ pool: {
72
+ masters: {
73
+ x: Address | null;
74
+ y: Address;
75
+ };
76
+ };
77
+ assetIn: Asset;
78
+ assetOut: Asset;
79
+ amountIn: Amount;
80
+ slippage: Percent;
81
+ }): SwapQuote;
82
+ export type SwapBuildArgs = {
83
+ quote: SwapQuote;
84
+ /** The wallet signing and paying. Also where leftover TON is returned. */
85
+ sender: Address;
86
+ /**
87
+ * The sender's own jetton wallet for the input asset.
88
+ *
89
+ * Required whenever the input is a jetton: a jetton transfer starts at the
90
+ * sender's wallet, and its address cannot be derived from the master without
91
+ * asking the master itself.
92
+ */
93
+ senderJettonWallet?: Address;
94
+ queryId?: bigint;
95
+ };
96
+ /** Builds the messages for a quoted swap. Nothing is sent. */
97
+ export declare function buildSwap(args: SwapBuildArgs): BuiltOperation<SwapQuote>;
98
+ /** Convenience: the pair ordered the way the factory orders it. */
99
+ export declare function poolSides(a: Asset, b: Asset): {
100
+ x: Asset;
101
+ y: Asset;
102
+ inverted: boolean;
103
+ };
104
+ //# sourceMappingURL=swap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swap.d.ts","sourceRoot":"","sources":["../../src/ops/swap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC1C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAG7C,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAC5C,OAAO,EAKL,KAAK,cAAc,EACpB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAiB,KAAK,cAAc,EAAkB,MAAM,eAAe,CAAA;AAMlF,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAGxE;;;GAGG;AACH,MAAM,MAAM,SAAS,GAAG;IACtB,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAA;IACzB,iEAAiE;IACjE,MAAM,EAAE,OAAO,CAAA;IACf,OAAO,EAAE,KAAK,CAAA;IACd,QAAQ,EAAE,KAAK,CA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IAC5B,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,8DAA8D;AAC9D,wBAAgB,SAAS,CAAC,IAAI,EAAE,aAAa,GAAG,cAAc,CAAC,SAAS,CAAC,CA8ExE;AAED,mEAAmE;AACnE,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,GAAG;IAAE,CAAC,EAAE,KAAK,CAAC;IAAC,CAAC,EAAE,KAAK,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAA;CAAE,CAEvF"}
@@ -0,0 +1,248 @@
1
+ import { Amount } from "../core/amount.js";
2
+ import { orderAssets } from "../core/asset.js";
3
+ import { BuildError, PoolNotReadyError } from "../core/errors.js";
4
+ import { Percent } from "../core/percent.js";
5
+ import { clmmSwapGas, clmmSwapTonGas, cpmmSwapJettonForward, cpmmSwapTonGas, } from "../core/gas.js";
6
+ import { randomQueryId } from "../core/tx.js";
7
+ import * as cpmmCodec from "../codec/cpmm.js";
8
+ import * as clmmCodec from "../codec/clmm.js";
9
+ import { buildJettonTransfer } from "../codec/jetton.js";
10
+ import { priceImpactBps, swapOutput, swapRejectionReason } from "../math/cpmm.js";
11
+ import { guaranteedOutAtLimit, simulateClmmSwap, sqrtPriceLimitFor } from "../math/clmm-swap.js";
12
+ import { assertJettonInput } from "./jetton-input.js";
13
+ function sideOf(pool, asset) {
14
+ if (asset.isNative()) {
15
+ if (pool.masters.x === null)
16
+ return 'x';
17
+ throw new BuildError('this pool does not hold native TON');
18
+ }
19
+ const master = asset.master;
20
+ if (pool.masters.x !== null && pool.masters.x.equals(master))
21
+ return 'x';
22
+ if (pool.masters.y.equals(master))
23
+ return 'y';
24
+ throw new BuildError(`${asset.toString()} is not one of this pool's assets`);
25
+ }
26
+ /**
27
+ * Quotes a CPMM swap from the pool's own reserves.
28
+ *
29
+ * Rejects up front for every reason the pool would reject, naming which one —
30
+ * a bounced message on the native leg is merely wasteful, but on the jetton
31
+ * leg the tokens are gone.
32
+ */
33
+ export function quoteCpmmSwap(args) {
34
+ if (!args.state.walletsResolved) {
35
+ throw new PoolNotReadyError(args.pool, 'the pool has not resolved its jetton wallets yet and would misroute the swap');
36
+ }
37
+ const inSide = sideOf(args.state, args.assetIn);
38
+ const outSide = sideOf(args.state, args.assetOut);
39
+ if (inSide === outSide)
40
+ throw new BuildError('input and output are the same side of the pool');
41
+ if (!args.assetIn.isNative())
42
+ assertJettonInput('cpmm', args.amountIn.raw);
43
+ const xIn = inSide === 'x';
44
+ const reserveIn = xIn ? args.state.reserves.x : args.state.reserves.y;
45
+ const reserveOut = xIn ? args.state.reserves.y : args.state.reserves.x;
46
+ const rejection = swapRejectionReason({
47
+ amountIn: args.amountIn.raw,
48
+ reserveIn,
49
+ reserveOut,
50
+ lpTotal: args.state.lpTotal,
51
+ lpFeeBps: args.state.lpFeeBps,
52
+ protocolFeeBps: args.state.protocolFeeBps,
53
+ minOut: 0n,
54
+ });
55
+ if (rejection !== null)
56
+ throw new BuildError(rejection);
57
+ const result = swapOutput({
58
+ amountIn: args.amountIn.raw,
59
+ reserveIn,
60
+ reserveOut,
61
+ lpFeeBps: args.state.lpFeeBps,
62
+ protocolFeeBps: args.state.protocolFeeBps,
63
+ });
64
+ const gas = args.assetIn.isNative()
65
+ ? cpmmSwapTonGas(args.amountIn.raw)
66
+ : cpmmSwapJettonForward();
67
+ return {
68
+ protocol: 'cpmm',
69
+ target: args.pool,
70
+ assetIn: args.assetIn,
71
+ assetOut: args.assetOut,
72
+ amountIn: args.amountIn,
73
+ amountOut: new Amount(result.amountOut, args.assetOut),
74
+ minAmountOut: new Amount(args.slippage.applyDown(result.amountOut), args.assetOut),
75
+ priceImpactBps: priceImpactBps({
76
+ amountIn: args.amountIn.raw,
77
+ amountOut: result.amountOut,
78
+ reserveIn,
79
+ reserveOut,
80
+ }),
81
+ lpFee: new Amount(result.lpFee, args.assetIn),
82
+ protocolFee: new Amount(result.protocolFee, args.assetIn),
83
+ xIn,
84
+ gas,
85
+ };
86
+ }
87
+ /**
88
+ * Quotes a CLMM swap by walking the range's bins.
89
+ *
90
+ * A partially fillable swap is refused rather than silently truncated: the
91
+ * range would take what it can and stop, and a caller who was quoted the
92
+ * full amount would not know why they received less.
93
+ */
94
+ export function quoteClmmSwap(args) {
95
+ const inSide = sideOf(args.pool, args.assetIn);
96
+ const outSide = sideOf(args.pool, args.assetOut);
97
+ if (inSide === outSide)
98
+ throw new BuildError('input and output are the same side of the pool');
99
+ if (!args.assetIn.isNative())
100
+ assertJettonInput('clmm', args.amountIn.raw);
101
+ const xIn = inSide === 'x';
102
+ const simulation = simulateClmmSwap({
103
+ xIn,
104
+ amountIn: args.amountIn.raw,
105
+ bins: args.state.bins,
106
+ currentBin: args.state.currentBin,
107
+ currentSqrtPrice: args.state.currentSqrtPrice,
108
+ binStep: args.state.binStep,
109
+ lpFeeBps: args.state.lpFeeBps,
110
+ protocolFeeBps: args.state.protocolFeeBps,
111
+ });
112
+ if (simulation.unfilled > 0n) {
113
+ throw new BuildError(`this range can only fill ${args.amountIn.raw - simulation.unfilled} of ${args.amountIn.raw}: ` +
114
+ 'it runs out of liquidity part way through. Try a smaller amount.');
115
+ }
116
+ if (simulation.amountOut <= 0n) {
117
+ throw new BuildError('this amount is too small to return anything at the current price');
118
+ }
119
+ // The bound is taken from the post-trade price: the range checks it after each
120
+ // bin step, so a bound from the starting
121
+ // price would reject any swap whose own impact reaches the tolerance.
122
+ const sqrtPriceLimit = sqrtPriceLimitFor({
123
+ xIn,
124
+ currentSqrtPrice: simulation.newSqrtPrice,
125
+ slippageBps: args.slippage.bps,
126
+ });
127
+ // The range's comparison is strict; a bound that rejects the swap itself only burns gas.
128
+ if (sqrtPriceLimit !== 0n) {
129
+ const passes = xIn
130
+ ? simulation.newSqrtPrice > sqrtPriceLimit
131
+ : simulation.newSqrtPrice < sqrtPriceLimit;
132
+ if (!passes) {
133
+ throw new BuildError('this swap moves the price past its own tolerance and the range would reject it — ' +
134
+ 'widen the slippage or trade a smaller amount');
135
+ }
136
+ }
137
+ // Only the price bound is enforced on chain, so the minimum is derived from the
138
+ // exact bound that goes into the message, not from "output minus tolerance".
139
+ const guaranteed = guaranteedOutAtLimit({
140
+ xIn,
141
+ amountIn: args.amountIn.raw,
142
+ sqrtPriceLimit,
143
+ lpFeeBps: args.state.lpFeeBps,
144
+ protocolFeeBps: args.state.protocolFeeBps,
145
+ });
146
+ // The bound comes from this swap's own final price, so the guarantee cannot exceed
147
+ // the simulated output. If it does, the two disagree; fail loudly instead of clamping.
148
+ if (guaranteed > simulation.amountOut) {
149
+ throw new BuildError(`internal: the price bound promises ${guaranteed} but the simulation returns ` +
150
+ `${simulation.amountOut} — bound and simulation disagree`);
151
+ }
152
+ // Price impact from the sqrt-price shift, linearised: |p1/p0 - 1| ~ 2 * |s1/s0 - 1|.
153
+ const before = args.state.currentSqrtPrice;
154
+ const after = simulation.newSqrtPrice;
155
+ const moved = before > after ? before - after : after - before;
156
+ const impact = Number((moved * 20000n) / before);
157
+ return {
158
+ protocol: 'clmm',
159
+ target: args.range,
160
+ assetIn: args.assetIn,
161
+ assetOut: args.assetOut,
162
+ amountIn: args.amountIn,
163
+ amountOut: new Amount(simulation.amountOut, args.assetOut),
164
+ minAmountOut: new Amount(guaranteed, args.assetOut),
165
+ priceImpactBps: impact,
166
+ lpFee: new Amount(simulation.lpFees, args.assetIn),
167
+ protocolFee: new Amount(simulation.protocolFees, args.assetOut),
168
+ xIn,
169
+ gas: args.assetIn.isNative() ? clmmSwapTonGas(args.amountIn.raw) : clmmSwapGas(),
170
+ sqrtPriceLimit,
171
+ binsCrossed: simulation.binsCrossed,
172
+ };
173
+ }
174
+ /** Builds the messages for a quoted swap. Nothing is sent. */
175
+ export function buildSwap(args) {
176
+ const queryId = args.queryId ?? randomQueryId();
177
+ const { quote } = args;
178
+ if (!quote.assetIn.isNative())
179
+ assertJettonInput(quote.protocol, quote.amountIn.raw);
180
+ const warnings = [];
181
+ // CPMM enforces minOut, CLMM enforces a price bound; on CLMM a zero bound comes
182
+ // from zero tolerance, not from 100%.
183
+ if (quote.protocol === 'clmm') {
184
+ if (quote.sqrtPriceLimit === 0n) {
185
+ warnings.push('no price bound is set: the range will fill this swap at any price it reaches');
186
+ }
187
+ else if (quote.minAmountOut.raw === 0n) {
188
+ warnings.push('the price bound is set, but this amount is too small for it to guarantee any output');
189
+ }
190
+ }
191
+ else if (quote.minAmountOut.raw === 0n) {
192
+ warnings.push('there is no minimum output: the swap will go through at any price');
193
+ }
194
+ else if (quote.minAmountOut.raw === quote.amountOut.raw) {
195
+ warnings.push('slippage tolerance is zero: the swap reverts if the price moves at all before it lands');
196
+ }
197
+ if (quote.priceImpactBps >= 500) {
198
+ warnings.push(`this trade moves the price by about ${(quote.priceImpactBps / 100).toFixed(2)}%`);
199
+ }
200
+ let txs;
201
+ if (quote.assetIn.isNative()) {
202
+ const body = quote.protocol === 'cpmm'
203
+ ? cpmmCodec.encodeSwap({
204
+ queryId,
205
+ swapAmount: quote.amountIn.raw,
206
+ excess: args.sender,
207
+ minOut: quote.minAmountOut.raw,
208
+ })
209
+ : clmmCodec.encodeSwap({
210
+ queryId,
211
+ swapAmount: quote.amountIn.raw,
212
+ excess: args.sender,
213
+ sqrtPriceLimit: quote.sqrtPriceLimit ?? 0n,
214
+ });
215
+ txs = [{ to: quote.target, value: quote.gas.attach, body }];
216
+ }
217
+ else {
218
+ if (args.senderJettonWallet === undefined) {
219
+ throw new BuildError(`swapping ${quote.assetIn.toString()} needs the sender's jetton wallet address — ` +
220
+ 'pass senderJettonWallet');
221
+ }
222
+ const payload = quote.protocol === 'cpmm'
223
+ ? cpmmCodec.encodeJettonSwapPayload({
224
+ excess: args.sender,
225
+ minOut: quote.minAmountOut.raw,
226
+ })
227
+ : clmmCodec.encodeJettonSwapPayload({
228
+ excess: args.sender,
229
+ sqrtPriceLimit: quote.sqrtPriceLimit ?? 0n,
230
+ });
231
+ const body = buildJettonTransfer({
232
+ queryId,
233
+ amount: quote.amountIn.raw,
234
+ destination: quote.target,
235
+ responseDestination: args.sender,
236
+ forwardTonAmount: quote.gas.attach,
237
+ forwardPayload: payload,
238
+ });
239
+ // Extra 0.1 TON on top of the forward amount pays for the jetton transfer itself.
240
+ txs = [{ to: args.senderJettonWallet, value: quote.gas.attach + 100000000n, body }];
241
+ }
242
+ return { txs, queryId, details: quote, warnings };
243
+ }
244
+ /** Convenience: the pair ordered the way the factory orders it. */
245
+ export function poolSides(a, b) {
246
+ return orderAssets(a, b);
247
+ }
248
+ //# sourceMappingURL=swap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swap.js","sourceRoot":"","sources":["../../src/ops/swap.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAE1C,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AACjE,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAC5C,OAAO,EACL,WAAW,EACX,cAAc,EACd,qBAAqB,EACrB,cAAc,GAEf,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,aAAa,EAAuC,MAAM,eAAe,CAAA;AAClF,OAAO,KAAK,SAAS,MAAM,kBAAkB,CAAA;AAC7C,OAAO,KAAK,SAAS,MAAM,kBAAkB,CAAA;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAA;AACxD,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AACjF,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AAEhG,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAyCrD,SAAS,MAAM,CAAC,IAAoD,EAAE,KAAY;IAChF,IAAI,KAAK,CAAC,QAAQ,EAAE,EAAE,CAAC;QACrB,IAAI,I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CAAC,GAAG;aAC/B,CAAC;YACJ,CAAC,CAAC,SAAS,CAAC,uBAAuB,CAAC;gBAChC,MAAM,EAAE,IAAI,CAAC,MAAM;gBACnB,cAAc,EAAE,KAAK,CAAC,cAAc,IAAI,EAAE;aAC3C,CAAC,CAAA;QAER,MAAM,IAAI,GAAG,mBAAmB,CAAC;YAC/B,OAAO;YACP,MAAM,EAAE,KAAK,CAAC,QAAQ,CAAC,GAAG;YAC1B,WAAW,EAAE,KAAK,CAAC,MAAM;YACzB,mBAAmB,EAAE,IAAI,CAAC,MAAM;YAChC,gBAAgB,EAAE,KAAK,CAAC,GAAG,CAAC,MAAM;YAClC,cAAc,EAAE,OAAO;SACxB,CAAC,CAAA;QACF,kFAAkF;QAClF,GAAG,GAAG,CAAC,EAAE,EAAE,EAAE,IAAI,CAAC,kBAAkB,EAAE,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,MAAM,GAAG,UAAY,EAAE,IAAI,EAAE,CAAC,CAAA;IACvF,CAAC;IAED,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAA;AACnD,CAAC;AAED,mEAAmE;AACnE,MAAM,UAAU,SAAS,CAAC,CAAQ,EAAE,CAAQ;IAC1C,OAAO,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;AAC1B,CAAC"}
@@ -0,0 +1,36 @@
1
+ import { Amount } from '../core/amount.ts';
2
+ import type { Asset } from '../core/asset.ts';
3
+ import type { PoolAssets } from './cpmm.ts';
4
+ import type { DecimalsSource, PoolDecimalsSource } from './assets.ts';
5
+ /**
6
+ * An amount as written.
7
+ *
8
+ * - `'50'` or `50`: fifty tokens; the asset's decimals are applied.
9
+ * - `50n`: fifty of the smallest unit, exactly as the chain counts them.
10
+ * - an {@link Amount}: already built, and checked against the asset.
11
+ *
12
+ * For a nine-decimal asset `50` is a billion times `50n`; use `bigint` only for raw units.
13
+ */
14
+ export type AmountLike = Amount | string | number | bigint;
15
+ /**
16
+ * The pool's own version of an asset, matched on the master address.
17
+ *
18
+ * Decimals must agree with the pool's copy, or this throws. With `decimalsSource`,
19
+ * a side whose decimals were read from the jetton master (`'chain'`) wins instead;
20
+ * without it every disagreement is refused.
21
+ */
22
+ export declare function canonicalAsset(assets: PoolAssets, asset: Asset, decimalsSource?: PoolDecimalsSource): Asset;
23
+ /**
24
+ * {@link toAmount} for one side of a pool, aware of where its decimals came from.
25
+ *
26
+ * On a side whose decimals are `'assumed'` or `'listed'`, only a raw `bigint` is
27
+ * accepted: a string, number or {@link Amount} would be converted at unverified decimals.
28
+ */
29
+ export declare function poolAmount(value: AmountLike, asset: Asset, source: DecimalsSource): Amount;
30
+ /**
31
+ * Reads an amount against a known asset. A ready-made {@link Amount} must agree
32
+ * with that asset on both identity and decimals; mismatched decimals are refused,
33
+ * not converted.
34
+ */
35
+ export declare function toAmount(value: AmountLike, asset: Asset): Amount;
36
+ //# sourceMappingURL=amounts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"amounts.d.ts","sourceRoot":"","sources":["../../src/pool/amounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC1C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAE7C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAC3C,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAErE;;;;;;;;GAQG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAA;AAE1D;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,UAAU,EAClB,KAAK,EAAE,KAAK,EACZ,cAAc,CAAC,EAAE,kBAAkB,GAClC,KAAK,CAuBP;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,cAAc,GAAG,MAAM,CAgB1F;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,GAAG,MAAM,CAgBhE"}
@@ -0,0 +1,68 @@
1
+ import { Amount } from "../core/amount.js";
2
+ import { BuildError } from "../core/errors.js";
3
+ /**
4
+ * The pool's own version of an asset, matched on the master address.
5
+ *
6
+ * Decimals must agree with the pool's copy, or this throws. With `decimalsSource`,
7
+ * a side whose decimals were read from the jetton master (`'chain'`) wins instead;
8
+ * without it every disagreement is refused.
9
+ */
10
+ export function canonicalAsset(assets, asset, decimalsSource) {
11
+ const side = asset.equals(assets.x) ? 'x' : asset.equals(assets.y) ? 'y' : null;
12
+ if (side === null) {
13
+ throw new BuildError(`${asset.toString()} is not one of this pool's assets ` +
14
+ `(${assets.x.toString()} / ${assets.y.toString()})`);
15
+ }
16
+ const own = assets[side];
17
+ if (asset.decimals === own.decimals || decimalsSource?.[side] === 'chain')
18
+ return own;
19
+ const where = decimalsSource?.[side] === 'assumed'
20
+ ? 'nothing read them from the master and none were passed, so it assumes nine'
21
+ : decimalsSource?.[side] === 'listed'
22
+ ? 'they came from the pool listing, not from the master'
23
+ : 'they were passed in, not read from the master';
24
+ throw new BuildError(`${asset.toString()} has ${asset.decimals} decimals in your asset, but this pool holds it ` +
25
+ `with ${own.decimals} (${where}). One of them is wrong, and a thousandfold hangs on ` +
26
+ 'which: open the pool with a client that can read jetton metadata, or pass the ' +
27
+ 'correct assets when opening it');
28
+ }
29
+ /**
30
+ * {@link toAmount} for one side of a pool, aware of where its decimals came from.
31
+ *
32
+ * On a side whose decimals are `'assumed'` or `'listed'`, only a raw `bigint` is
33
+ * accepted: a string, number or {@link Amount} would be converted at unverified decimals.
34
+ */
35
+ export function poolAmount(value, asset, source) {
36
+ if ((source === 'assumed' || source === 'listed') && typeof value !== 'bigint') {
37
+ // An Amount is refused too: its raw units may come from the same unverified decimals.
38
+ const why = source === 'assumed'
39
+ ? 'nothing read them from the master and none were passed'
40
+ : 'they came from the pool listing, and nothing could check them against the master';
41
+ const what = value instanceof Amount ? `an Amount (${value.toString()})` : `'${String(value)}'`;
42
+ throw new BuildError(`the decimals of ${asset.toString()} are unverified in this pool: ${why}, so ${what} ` +
43
+ 'cannot be turned into raw units safely. Pass raw units as a bigint (an Amount you ' +
44
+ 'trust: its .raw), or open the pool with a client that can read jetton metadata, or ' +
45
+ 'with { assets } naming the decimals yourself');
46
+ }
47
+ return toAmount(value, asset);
48
+ }
49
+ /**
50
+ * Reads an amount against a known asset. A ready-made {@link Amount} must agree
51
+ * with that asset on both identity and decimals; mismatched decimals are refused,
52
+ * not converted.
53
+ */
54
+ export function toAmount(value, asset) {
55
+ if (typeof value === 'bigint')
56
+ return Amount.raw(value, asset);
57
+ if (!(value instanceof Amount))
58
+ return Amount.of(value, asset);
59
+ if (!value.asset.equals(asset)) {
60
+ throw new BuildError(`this amount is in ${value.asset.toString()}, but the operation is in ${asset.toString()}`);
61
+ }
62
+ if (value.asset.decimals !== asset.decimals) {
63
+ throw new BuildError(`this amount was built with ${value.asset.decimals} decimals, but ${asset.toString()} ` +
64
+ `has ${asset.decimals}. Pass the number instead and let the pool apply its own decimals.`);
65
+ }
66
+ return value;
67
+ }
68
+ //# sourceMappingURL=amounts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"amounts.js","sourceRoot":"","sources":["../../src/pool/amounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAE1C,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAe9C;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAC5B,MAAkB,EAClB,KAAY,EACZ,cAAmC;IAEnC,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAA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2
+ import { Asset } from '../core/asset.ts';
3
+ import type { Jettons } from '../jetton/index.ts';
4
+ import type { PoolAssets } from './cpmm.ts';
5
+ /**
6
+ * Where a pool side's decimals came from.
7
+ *
8
+ * - `'chain'`: read from the jetton master's metadata, or native TON (always nine).
9
+ * The only source treated as verified.
10
+ * - `'given'`: passed in `assets` when the pool was opened; not checked.
11
+ * - `'listed'`: from a pool listing entry (`moon.cpmmPool(info)`, `assetsOf`,
12
+ * `PoolList.assets()`); not checked. Display only: amounts must be a raw `bigint`.
13
+ * - `'assumed'`: no source; nine decimals for display, amounts must be a raw `bigint`.
14
+ *
15
+ * Prices (`pool.price`, `binAtPrice`) use these decimals whatever their source.
16
+ */
17
+ export type DecimalsSource = 'chain' | 'given' | 'listed' | 'assumed';
18
+ /** Decimals provenance, per pool side. */
19
+ export type PoolDecimalsSource = {
20
+ x: DecimalsSource;
21
+ y: DecimalsSource;
22
+ };
23
+ /** Marks an asset built from a pool listing entry. Internal. */
24
+ export declare function listedAsset(asset: Asset): Asset;
25
+ /**
26
+ * The pool's assets, checked against the pool's own storage.
27
+ *
28
+ * Identity and order come only from `masters`: each given asset must be the token
29
+ * on the same side on chain, or this throws (a swapped pair is refused, not reordered).
30
+ * Decimals are read from the jetton master when `jettons` is available; otherwise
31
+ * given decimals are used and marked unverified (`'listed'` if from a listing).
32
+ */
33
+ export declare function resolvePoolAssets(args: {
34
+ pool: Address;
35
+ masters: {
36
+ x: Address | null;
37
+ y: Address;
38
+ };
39
+ given: Partial<PoolAssets> | undefined;
40
+ jettons: Jettons | null;
41
+ }): Promise<{
42
+ assets: PoolAssets;
43
+ decimalsSource: PoolDecimalsSource;
44
+ }>;
45
+ //# sourceMappingURL=assets.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assets.d.ts","sourceRoot":"","sources":["../../src/pool/assets.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAExC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAE3C;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,OAAO,GAAG,QAAQ,GAAG,SAAS,CAAA;AAErE,0CAA0C;AAC1C,MAAM,MAAM,kBAAkB,GAAG;IAAE,CAAC,EAAE,cAAc,CAAC;IAAC,CAAC,EAAE,cAAc,CAAA;CAAE,CAAA;AAMzE,gEAAgE;AAChE,wBAAgB,WAAW,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAG/C;AAED;;;;;;;GAOG;AACH,wBAAsB,iBAAiB,CAAC,IAAI,EAAE;IAC5C,IAAI,EAAE,OAAO,CAAA;IACb,OAAO,EAAE;QAAE,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;QAAC,CAAC,EAAE,OAAO,CAAA;KAAE,CAAA;IAC1C,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,GAAG,SAAS,CAAA;IACtC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAA;CACxB,GAAG,OAAO,CAAC;IAAE,MAAM,EAAE,UAAU,CAAC;IAAC,cAAc,EAAE,kBAAkB,CAAA;CAAE,CAAC,CAwCtE"}
@@ -0,0 +1,57 @@
1
+ import { Asset } from "../core/asset.js";
2
+ import { BuildError } from "../core/errors.js";
3
+ // Assets built from a listing response. The mark lives on the object so it survives
4
+ // being passed back in by an integrator (assetsOf, PoolList.assets()).
5
+ const fromListing = new WeakSet();
6
+ /** Marks an asset built from a pool listing entry. Internal. */
7
+ export function listedAsset(asset) {
8
+ fromListing.add(asset);
9
+ return asset;
10
+ }
11
+ /**
12
+ * The pool's assets, checked against the pool's own storage.
13
+ *
14
+ * Identity and order come only from `masters`: each given asset must be the token
15
+ * on the same side on chain, or this throws (a swapped pair is refused, not reordered).
16
+ * Decimals are read from the jetton master when `jettons` is available; otherwise
17
+ * given decimals are used and marked unverified (`'listed'` if from a listing).
18
+ */
19
+ export async function resolvePoolAssets(args) {
20
+ const { pool, masters, given, jettons } = args;
21
+ const own = {
22
+ x: masters.x === null ? Asset.native() : Asset.jetton(masters.x),
23
+ y: Asset.jetton(masters.y),
24
+ };
25
+ for (const side of ['x', 'y']) {
26
+ const asset = given?.[side];
27
+ if (asset === undefined || asset.equals(own[side]))
28
+ continue;
29
+ const other = side === 'x' ? 'y' : 'x';
30
+ const order = `x = ${own.x.toString()}, y = ${own.y.toString()}`;
31
+ throw new BuildError(asset.equals(own[other])
32
+ ? `assets.${side} is ${asset.toString()}, but in pool ${pool.toString()} that token ` +
33
+ `is ${other}: the pool's own order is ${order}. Pass the assets in that order, ` +
34
+ 'or open the pool by address alone'
35
+ : `assets.${side} is ${asset.toString()}, which pool ${pool.toString()} does not hold: ` +
36
+ `its storage names ${order}`);
37
+ }
38
+ if (jettons !== null) {
39
+ const [x, y] = await Promise.all([jettons.assetOf(masters.x), jettons.asset(masters.y)]);
40
+ return { assets: { x, y }, decimalsSource: { x: 'chain', y: 'chain' } };
41
+ }
42
+ const pick = (side) => {
43
+ const asset = given?.[side];
44
+ // Native TON always has nine decimals.
45
+ if (own[side].isNative()) {
46
+ return [asset !== undefined && asset.decimals === 9 ? asset : Asset.native(), 'chain'];
47
+ }
48
+ if (asset === undefined)
49
+ return [own[side], 'assumed'];
50
+ // Listing decimals cannot be verified here; poolAmount restricts that side to raw input.
51
+ return [asset, fromListing.has(asset) ? 'listed' : 'given'];
52
+ };
53
+ const [x, xFrom] = pick('x');
54
+ const [y, yFrom] = pick('y');
55
+ return { assets: { x, y }, decimalsSource: { x: xFrom, y: yFrom } };
56
+ }
57
+ //# sourceMappingURL=assets.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assets.js","sourceRoot":"","sources":["../../src/pool/assets.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB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