@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.
- package/CHANGELOG.md +18 -0
- package/README.md +2 -2
- package/dist/adapters/sender.d.ts +88 -0
- package/dist/adapters/sender.d.ts.map +1 -0
- package/dist/adapters/sender.js +62 -0
- package/dist/adapters/sender.js.map +1 -0
- package/dist/adapters/tonclient.d.ts +13 -0
- package/dist/adapters/tonclient.d.ts.map +1 -0
- package/dist/adapters/tonclient.js +17 -0
- package/dist/adapters/tonclient.js.map +1 -0
- package/dist/api/index.d.ts +210 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +364 -0
- package/dist/api/index.js.map +1 -0
- package/dist/codec/bins.d.ts +26 -0
- package/dist/codec/bins.d.ts.map +1 -0
- package/dist/codec/bins.js +67 -0
- package/dist/codec/bins.js.map +1 -0
- package/dist/codec/clmm.d.ts +87 -0
- package/dist/codec/clmm.d.ts.map +1 -0
- package/dist/codec/clmm.js +119 -0
- package/dist/codec/clmm.js.map +1 -0
- package/dist/codec/cpmm.d.ts +112 -0
- package/dist/codec/cpmm.d.ts.map +1 -0
- package/dist/codec/cpmm.js +146 -0
- package/dist/codec/cpmm.js.map +1 -0
- package/dist/codec/jetton.d.ts +30 -0
- package/dist/codec/jetton.d.ts.map +1 -0
- package/dist/codec/jetton.js +37 -0
- package/dist/codec/jetton.js.map +1 -0
- package/dist/codec/mempad.d.ts +109 -0
- package/dist/codec/mempad.d.ts.map +1 -0
- package/dist/codec/mempad.js +162 -0
- package/dist/codec/mempad.js.map +1 -0
- package/dist/core/amount.d.ts +33 -0
- package/dist/core/amount.d.ts.map +1 -0
- package/dist/core/amount.js +81 -0
- package/dist/core/amount.js.map +1 -0
- package/dist/core/asset.d.ts +82 -0
- package/dist/core/asset.d.ts.map +1 -0
- package/dist/core/asset.js +143 -0
- package/dist/core/asset.js.map +1 -0
- package/dist/core/errors.d.ts +70 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +114 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/fee.d.ts +36 -0
- package/dist/core/fee.d.ts.map +1 -0
- package/dist/core/fee.js +40 -0
- package/dist/core/fee.js.map +1 -0
- package/dist/core/gas.d.ts +112 -0
- package/dist/core/gas.d.ts.map +1 -0
- package/dist/core/gas.js +265 -0
- package/dist/core/gas.js.map +1 -0
- package/dist/core/percent.d.ts +27 -0
- package/dist/core/percent.d.ts.map +1 -0
- package/dist/core/percent.js +71 -0
- package/dist/core/percent.js.map +1 -0
- package/dist/core/tx.d.ts +36 -0
- package/dist/core/tx.d.ts.map +1 -0
- package/dist/core/tx.js +17 -0
- package/dist/core/tx.js.map +1 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/jetton/index.d.ts +53 -0
- package/dist/jetton/index.d.ts.map +1 -0
- package/dist/jetton/index.js +90 -0
- package/dist/jetton/index.js.map +1 -0
- package/dist/math/clmm-swap.d.ts +80 -0
- package/dist/math/clmm-swap.d.ts.map +1 -0
- package/dist/math/clmm-swap.js +277 -0
- package/dist/math/clmm-swap.js.map +1 -0
- package/dist/math/clmm.d.ts +234 -0
- package/dist/math/clmm.d.ts.map +1 -0
- package/dist/math/clmm.js +270 -0
- package/dist/math/clmm.js.map +1 -0
- package/dist/math/cpmm.d.ts +170 -0
- package/dist/math/cpmm.d.ts.map +1 -0
- package/dist/math/cpmm.js +175 -0
- package/dist/math/cpmm.js.map +1 -0
- package/dist/math/integer.d.ts +10 -0
- package/dist/math/integer.d.ts.map +1 -0
- package/dist/math/integer.js +44 -0
- package/dist/math/integer.js.map +1 -0
- package/dist/math/q128.d.ts +45 -0
- package/dist/math/q128.d.ts.map +1 -0
- package/dist/math/q128.js +108 -0
- package/dist/math/q128.js.map +1 -0
- package/dist/moon.d.ts +245 -0
- package/dist/moon.d.ts.map +1 -0
- package/dist/moon.js +332 -0
- package/dist/moon.js.map +1 -0
- package/dist/ops/jetton-input.d.ts +3 -0
- package/dist/ops/jetton-input.d.ts.map +1 -0
- package/dist/ops/jetton-input.js +8 -0
- package/dist/ops/jetton-input.js.map +1 -0
- package/dist/ops/liquidity.d.ts +157 -0
- package/dist/ops/liquidity.d.ts.map +1 -0
- package/dist/ops/liquidity.js +413 -0
- package/dist/ops/liquidity.js.map +1 -0
- package/dist/ops/mempad.d.ts +143 -0
- package/dist/ops/mempad.d.ts.map +1 -0
- package/dist/ops/mempad.js +248 -0
- package/dist/ops/mempad.js.map +1 -0
- package/dist/ops/pool.d.ts +75 -0
- package/dist/ops/pool.d.ts.map +1 -0
- package/dist/ops/pool.js +129 -0
- package/dist/ops/pool.js.map +1 -0
- package/dist/ops/position.d.ts +47 -0
- package/dist/ops/position.d.ts.map +1 -0
- package/dist/ops/position.js +73 -0
- package/dist/ops/position.js.map +1 -0
- package/dist/ops/swap.d.ts +104 -0
- package/dist/ops/swap.d.ts.map +1 -0
- package/dist/ops/swap.js +248 -0
- package/dist/ops/swap.js.map +1 -0
- package/dist/pool/amounts.d.ts +36 -0
- package/dist/pool/amounts.d.ts.map +1 -0
- package/dist/pool/amounts.js +68 -0
- package/dist/pool/amounts.js.map +1 -0
- package/dist/pool/assets.d.ts +45 -0
- package/dist/pool/assets.d.ts.map +1 -0
- package/dist/pool/assets.js +57 -0
- package/dist/pool/assets.js.map +1 -0
- package/dist/pool/clmm.d.ts +309 -0
- package/dist/pool/clmm.d.ts.map +1 -0
- package/dist/pool/clmm.js +561 -0
- package/dist/pool/clmm.js.map +1 -0
- package/dist/pool/cpmm.d.ts +228 -0
- package/dist/pool/cpmm.d.ts.map +1 -0
- package/dist/pool/cpmm.js +332 -0
- package/dist/pool/cpmm.js.map +1 -0
- package/dist/protocol/generated.d.ts +2752 -0
- package/dist/protocol/generated.d.ts.map +1 -0
- package/dist/protocol/generated.js +3564 -0
- package/dist/protocol/generated.js.map +1 -0
- package/dist/protocol/index.d.ts +19 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +49 -0
- package/dist/protocol/index.js.map +1 -0
- package/dist/state/address.d.ts +121 -0
- package/dist/state/address.d.ts.map +1 -0
- package/dist/state/address.js +143 -0
- package/dist/state/address.js.map +1 -0
- package/dist/state/source.d.ts +26 -0
- package/dist/state/source.d.ts.map +1 -0
- package/dist/state/source.js +32 -0
- package/dist/state/source.js.map +1 -0
- package/dist/state/storage.d.ts +218 -0
- package/dist/state/storage.d.ts.map +1 -0
- package/dist/state/storage.js +273 -0
- package/dist/state/storage.js.map +1 -0
- package/package.json +74 -4
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { LimitExceededError } from "../core/errors.js";
|
|
2
|
+
import { MAX_BINS_PER_SWAP, binBounds, sqrtPriceAtBin, sqrtPriceEnteringBin } from "./clmm.js";
|
|
3
|
+
import { SCALE, q128DivCeil, q128DivFloor, q128Mul, q128MulCeil, q128MulFloor, q128Sqrt, mulDivCeil, mulDivFloor, } from "./q128.js";
|
|
4
|
+
/**
|
|
5
|
+
* Simulating a CLMM swap locally, bin by bin.
|
|
6
|
+
*
|
|
7
|
+
* Mirrors the range's own swap arithmetic step for step. This is what makes a
|
|
8
|
+
* quote possible without asking any backend: given the range's bins and
|
|
9
|
+
* current price — both readable from raw account state — the result is the
|
|
10
|
+
* same arithmetic the range will perform on chain.
|
|
11
|
+
*/
|
|
12
|
+
/** Fee shares are stored as basis points and converted to Q128 fractions. */
|
|
13
|
+
function feeFraction(bps) {
|
|
14
|
+
return mulDivFloor(BigInt(bps), SCALE, 10000n);
|
|
15
|
+
}
|
|
16
|
+
/** How much gross X this bin can absorb before the price reaches its lower bound. */
|
|
17
|
+
function maxXInThisBin(args) {
|
|
18
|
+
if (args.sqrtP <= args.sqrtPa)
|
|
19
|
+
return 0n;
|
|
20
|
+
const invChange = q128DivCeil(SCALE, args.sqrtPa) - q128DivFloor(SCALE, args.sqrtP);
|
|
21
|
+
const needed = q128MulCeil(invChange > 0n ? invChange : 0n, args.liquidity);
|
|
22
|
+
const gross = q128DivCeil(needed, SCALE - args.lpFee);
|
|
23
|
+
return args.remaining < gross ? args.remaining : gross;
|
|
24
|
+
}
|
|
25
|
+
/** How much gross Y this bin can absorb before the price reaches its upper bound. */
|
|
26
|
+
function maxYInThisBin(args) {
|
|
27
|
+
if (args.sqrtP >= args.sqrtPb)
|
|
28
|
+
return 0n;
|
|
29
|
+
const needed = q128MulCeil(args.sqrtPb - args.sqrtP, args.liquidity);
|
|
30
|
+
const gross = q128DivCeil(needed, SCALE - args.lpFee);
|
|
31
|
+
return args.remaining < gross ? args.remaining : gross;
|
|
32
|
+
}
|
|
33
|
+
function stepXToY(args) {
|
|
34
|
+
const { sqrtPa } = binBounds(args.bin, args.binStep);
|
|
35
|
+
const gross = maxXInThisBin({
|
|
36
|
+
remaining: args.remaining,
|
|
37
|
+
sqrtPa,
|
|
38
|
+
liquidity: args.liquidity,
|
|
39
|
+
sqrtP: args.sqrtP,
|
|
40
|
+
lpFee: args.lpFee,
|
|
41
|
+
});
|
|
42
|
+
if (gross === 0n) {
|
|
43
|
+
return {
|
|
44
|
+
bin: args.bin,
|
|
45
|
+
newSqrtPrice: args.sqrtP,
|
|
46
|
+
out: 0n,
|
|
47
|
+
remaining: args.remaining,
|
|
48
|
+
lpFee: 0n,
|
|
49
|
+
protocolFee: 0n,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
const fee = q128Mul(args.lpFee, gross);
|
|
53
|
+
const afterFee = gross - fee;
|
|
54
|
+
const invChange = q128DivFloor(afterFee, args.liquidity);
|
|
55
|
+
const newInv = q128DivFloor(SCALE, args.sqrtP) + invChange;
|
|
56
|
+
const unclamped = q128DivCeil(SCALE, newInv);
|
|
57
|
+
const capped = unclamped < args.sqrtP ? unclamped : args.sqrtP;
|
|
58
|
+
const newSqrtPrice = capped > sqrtPa ? capped : sqrtPa;
|
|
59
|
+
const outGross = q128MulFloor(args.liquidity, args.sqrtP - newSqrtPrice);
|
|
60
|
+
const protocolFee = q128Mul(outGross, args.protocolFee);
|
|
61
|
+
return {
|
|
62
|
+
bin: args.bin,
|
|
63
|
+
newSqrtPrice,
|
|
64
|
+
out: outGross - protocolFee,
|
|
65
|
+
remaining: args.remaining - gross,
|
|
66
|
+
lpFee: fee,
|
|
67
|
+
protocolFee,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function stepYToX(args) {
|
|
71
|
+
const { sqrtPa, sqrtPb } = binBounds(args.bin, args.binStep);
|
|
72
|
+
const gross = maxYInThisBin({
|
|
73
|
+
remaining: args.remaining,
|
|
74
|
+
sqrtPb,
|
|
75
|
+
liquidity: args.liquidity,
|
|
76
|
+
sqrtP: args.sqrtP,
|
|
77
|
+
lpFee: args.lpFee,
|
|
78
|
+
});
|
|
79
|
+
if (gross === 0n) {
|
|
80
|
+
return {
|
|
81
|
+
bin: args.bin,
|
|
82
|
+
newSqrtPrice: args.sqrtP,
|
|
83
|
+
out: 0n,
|
|
84
|
+
remaining: args.remaining,
|
|
85
|
+
lpFee: 0n,
|
|
86
|
+
protocolFee: 0n,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
const fee = q128Mul(args.lpFee, gross);
|
|
90
|
+
const afterFee = gross - fee;
|
|
91
|
+
const change = q128DivFloor(afterFee, args.liquidity);
|
|
92
|
+
const unclamped = args.sqrtP + change;
|
|
93
|
+
const capped = unclamped < sqrtPb ? unclamped : sqrtPb;
|
|
94
|
+
const newSqrtPrice = capped > sqrtPa ? capped : sqrtPa;
|
|
95
|
+
const invOld = q128DivFloor(SCALE, args.sqrtP);
|
|
96
|
+
const invNew = q128DivCeil(SCALE, newSqrtPrice);
|
|
97
|
+
const invDelta = invOld > invNew ? invOld - invNew : 0n;
|
|
98
|
+
const outGross = q128MulFloor(invDelta, args.liquidity);
|
|
99
|
+
const protocolFee = q128Mul(outGross, args.protocolFee);
|
|
100
|
+
return {
|
|
101
|
+
bin: args.bin,
|
|
102
|
+
newSqrtPrice,
|
|
103
|
+
out: outGross - protocolFee,
|
|
104
|
+
remaining: args.remaining - gross,
|
|
105
|
+
lpFee: fee,
|
|
106
|
+
protocolFee,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Runs a swap against a range's bins.
|
|
111
|
+
*
|
|
112
|
+
* `xIn` decides direction: X in walks the price down through lower bins, Y in
|
|
113
|
+
* walks it up. The range stops after forty bins, so a swap large enough to
|
|
114
|
+
* cross more is refused here too, before it is signed.
|
|
115
|
+
*/
|
|
116
|
+
export function simulateClmmSwap(args) {
|
|
117
|
+
if (args.amountIn <= 0n)
|
|
118
|
+
throw new RangeError('amountIn must be positive');
|
|
119
|
+
const lpFee = feeFraction(args.lpFeeBps);
|
|
120
|
+
const protocolFee = feeFraction(args.protocolFeeBps);
|
|
121
|
+
const keys = [...args.bins.keys()].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
122
|
+
const liquidityOf = (bin) => args.bins.get(bin)?.l ?? 0n;
|
|
123
|
+
/** Next bin in the direction of travel, as the range picks it. */
|
|
124
|
+
const nextBin = (from) => {
|
|
125
|
+
if (args.xIn) {
|
|
126
|
+
let found = null;
|
|
127
|
+
for (const key of keys)
|
|
128
|
+
if (key < from)
|
|
129
|
+
found = key;
|
|
130
|
+
return found;
|
|
131
|
+
}
|
|
132
|
+
for (const key of keys)
|
|
133
|
+
if (key > from)
|
|
134
|
+
return key;
|
|
135
|
+
return null;
|
|
136
|
+
};
|
|
137
|
+
let bin = BigInt(args.currentBin);
|
|
138
|
+
let sqrtP = args.currentSqrtPrice;
|
|
139
|
+
// As on chain: a missing current bin is
|
|
140
|
+
// skipped to the nearest one in the direction of travel.
|
|
141
|
+
if (!args.bins.has(bin)) {
|
|
142
|
+
const moved = nextBin(bin);
|
|
143
|
+
if (moved === null) {
|
|
144
|
+
return {
|
|
145
|
+
amountOut: 0n,
|
|
146
|
+
newSqrtPrice: sqrtP,
|
|
147
|
+
binsCrossed: [],
|
|
148
|
+
lpFees: 0n,
|
|
149
|
+
protocolFees: 0n,
|
|
150
|
+
unfilled: args.amountIn,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
bin = moved;
|
|
154
|
+
sqrtP = args.xIn
|
|
155
|
+
? sqrtPriceEnteringBin(Number(bin), args.binStep)
|
|
156
|
+
: sqrtPriceAtBin(Number(bin), args.binStep);
|
|
157
|
+
}
|
|
158
|
+
let remaining = args.amountIn;
|
|
159
|
+
let amountOut = 0n;
|
|
160
|
+
let lpFees = 0n;
|
|
161
|
+
let protocolFees = 0n;
|
|
162
|
+
const binsCrossed = [];
|
|
163
|
+
for (let iteration = 0;; iteration++) {
|
|
164
|
+
if (iteration >= MAX_BINS_PER_SWAP) {
|
|
165
|
+
throw new LimitExceededError('this swap would cross more bins than one transaction can', MAX_BINS_PER_SWAP, iteration + 1, 'Split it into smaller swaps.');
|
|
166
|
+
}
|
|
167
|
+
const liquidity = liquidityOf(bin);
|
|
168
|
+
const step = (args.xIn ? stepXToY : stepYToX)({
|
|
169
|
+
remaining,
|
|
170
|
+
bin: Number(bin),
|
|
171
|
+
liquidity,
|
|
172
|
+
sqrtP,
|
|
173
|
+
lpFee,
|
|
174
|
+
protocolFee,
|
|
175
|
+
binStep: args.binStep,
|
|
176
|
+
});
|
|
177
|
+
sqrtP = step.newSqrtPrice;
|
|
178
|
+
remaining = step.remaining;
|
|
179
|
+
amountOut += step.out;
|
|
180
|
+
lpFees += step.lpFee;
|
|
181
|
+
protocolFees += step.protocolFee;
|
|
182
|
+
if (!binsCrossed.includes(Number(bin)))
|
|
183
|
+
binsCrossed.push(Number(bin));
|
|
184
|
+
if (remaining <= 0n)
|
|
185
|
+
break;
|
|
186
|
+
const moved = nextBin(bin);
|
|
187
|
+
if (moved === null) {
|
|
188
|
+
// The range throws 5 here; report the rest as unfilled instead.
|
|
189
|
+
return {
|
|
190
|
+
amountOut,
|
|
191
|
+
newSqrtPrice: sqrtP,
|
|
192
|
+
binsCrossed,
|
|
193
|
+
lpFees,
|
|
194
|
+
protocolFees,
|
|
195
|
+
unfilled: remaining,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
bin = moved;
|
|
199
|
+
sqrtP = args.xIn
|
|
200
|
+
? sqrtPriceEnteringBin(Number(bin), args.binStep)
|
|
201
|
+
: sqrtPriceAtBin(Number(bin), args.binStep);
|
|
202
|
+
}
|
|
203
|
+
return { amountOut, newSqrtPrice: sqrtP, binsCrossed, lpFees, protocolFees, unfilled: 0n };
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Turns a slippage tolerance into the Q128 price bound the range expects.
|
|
207
|
+
*
|
|
208
|
+
* CLMM bounds a swap by price, not by output: X in must keep the price above
|
|
209
|
+
* the bound, Y in must keep it below. Zero means unbounded, which on a live
|
|
210
|
+
* pool means accepting any price at all.
|
|
211
|
+
*/
|
|
212
|
+
export function sqrtPriceLimitFor(args) {
|
|
213
|
+
if (args.slippageBps <= 0)
|
|
214
|
+
return 0n;
|
|
215
|
+
// Tolerance is on price, the limit is on sqrt price: take the root of the factor.
|
|
216
|
+
const bound = args.xIn ? 10_000 - args.slippageBps : 10_000 + args.slippageBps;
|
|
217
|
+
const factor = mulDivFloor(BigInt(bound), SCALE, 10000n);
|
|
218
|
+
return q128Mul(args.currentSqrtPrice, q128Sqrt(factor));
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Allowance for the range's integer rounding, which the real-number bound
|
|
222
|
+
* in {@link guaranteedOutAtLimit} ignores. Per crossed bin (at most forty)
|
|
223
|
+
* the range can lose up to:
|
|
224
|
+
*
|
|
225
|
+
* input — LP fee `q128Mul`, to nearest: ½ unit
|
|
226
|
+
* input — `q128DivCeil` of the bin's gross need:
|
|
227
|
+
* ~2 units, on every bin the swap exhausts (all but the last)
|
|
228
|
+
* output — `q128MulFloor`: 1 unit
|
|
229
|
+
* output — protocol fee `q128Mul`, to nearest: ½ unit
|
|
230
|
+
*/
|
|
231
|
+
const DUST_IN = 4n * BigInt(MAX_BINS_PER_SWAP);
|
|
232
|
+
const DUST_OUT = 2n * BigInt(MAX_BINS_PER_SWAP);
|
|
233
|
+
/**
|
|
234
|
+
* Output guaranteed by the price limit, the only on-chain guard a CLMM swap has.
|
|
235
|
+
*
|
|
236
|
+
* A CLMM swap message carries no `minOut`: the range checks the price after
|
|
237
|
+
* every bin and refunds the whole input if the limit is crossed. The minimum
|
|
238
|
+
* received is therefore what follows from the limit, and this computes it.
|
|
239
|
+
*
|
|
240
|
+
* Price moves monotonically during a swap, so every chunk of input converts
|
|
241
|
+
* at a price no worse than the limit. The LP fee comes off the input and the
|
|
242
|
+
* protocol fee off the output:
|
|
243
|
+
*
|
|
244
|
+
* xIn: out ≥ (Δin − LP) · p_lim · (1 − f_pr)
|
|
245
|
+
* yIn: out ≥ (Δin − LP) / p_lim · (1 − f_pr), p = (sqrtP/2¹²⁸)²
|
|
246
|
+
*
|
|
247
|
+
* The bound is deliberately loose. The quote places the limit one slippage
|
|
248
|
+
* tolerance beyond the post-trade price, so actual output usually exceeds
|
|
249
|
+
* the guarantee by about that tolerance; equality needs the whole swap to
|
|
250
|
+
* fill at the limit price.
|
|
251
|
+
*
|
|
252
|
+
* Rounding always goes against the caller: both fees round up and both
|
|
253
|
+
* price conversions round down.
|
|
254
|
+
*/
|
|
255
|
+
export function guaranteedOutAtLimit(args) {
|
|
256
|
+
// Zero disables the guard on chain, so nothing is guaranteed.
|
|
257
|
+
if (args.sqrtPriceLimit === 0n)
|
|
258
|
+
return 0n;
|
|
259
|
+
if (args.amountIn <= 0n)
|
|
260
|
+
return 0n;
|
|
261
|
+
const limit = args.sqrtPriceLimit;
|
|
262
|
+
// The range's fee fraction is mulDivFloor(bps, SCALE, 10000), never above
|
|
263
|
+
// bps/10000, so a ceiling is safe.
|
|
264
|
+
const netIn = args.amountIn - mulDivCeil(args.amountIn, BigInt(args.lpFeeBps), 10000n) - DUST_IN;
|
|
265
|
+
if (netIn <= 0n)
|
|
266
|
+
return 0n;
|
|
267
|
+
// Two floored Q128 steps instead of squaring the limit: same answer without
|
|
268
|
+
// a 512-bit intermediate, and each step rounds in the safe direction.
|
|
269
|
+
const grossOut = args.xIn
|
|
270
|
+
? q128MulFloor(q128MulFloor(netIn, limit), limit)
|
|
271
|
+
: q128DivFloor(q128DivFloor(netIn, limit), limit);
|
|
272
|
+
if (grossOut <= 0n)
|
|
273
|
+
return 0n;
|
|
274
|
+
const netOut = grossOut - mulDivCeil(grossOut, BigInt(args.protocolFeeBps), 10000n) - DUST_OUT;
|
|
275
|
+
return netOut > 0n ? netOut : 0n;
|
|
276
|
+
}
|
|
277
|
+
//# sourceMappingURL=clmm-swap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clmm-swap.js","sourceRoot":"","sources":["../../src/math/clmm-swap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAEtD,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAA;AAC9F,OAAO,EACL,KAAK,EACL,WAAW,EACX,YAAY,EACZ,OAAO,EACP,WAAW,EACX,YAAY,EACZ,QAAQ,EACR,UAAU,EACV,WAAW,GACZ,MAAM,WAAW,CAAA;AAElB;;;;;;;GAOG;AAEH,6EAA6E;AAC7E,SAAS,WAAW,CAAC,GAAW;IAC9B,OAAO,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,MAAO,CAAC,CAAA;AACjD,CAAC;AAED,qFAAqF;AACrF,SAAS,aAAa,CAAC,IAMtB;IACC,IAAI,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,MAAM;QAAE,OAAO,EAAE,CAAA;IACxC,MAAM,SAAS,GAAG,WAAW,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,GAAG,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK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,CAAA;IAEjC,0EAA0E;IAC1E,mCAAmC;IACnC,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,GAAG,UAAU,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,MAAO,CAAC,GAAG,OAAO,CAAA;IACjG,IAAI,KAAK,IAAI,EAAE;QAAE,OAAO,EAAE,CAAA;IAE1B,4EAA4E;IAC5E,sEAAsE;IACtE,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG;QACvB,CAAC,CAAC,YAAY,CAAC,YAAY,CAAC,KAAK,EAAE,KAAK,CAAC,EAAE,KAAK,CAAC;QACjD,CAAC,CAAC,YAAY,CAAC,YAAY,CAAC,KAAK,EAAE,KAAK,CAAC,EAAE,KAAK,CAAC,CAAA;IACnD,IAAI,QAAQ,IAAI,EAAE;QAAE,OAAO,EAAE,CAAA;IAE7B,MAAM,MAAM,GAAG,QAAQ,GAAG,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,EAAE,MAAO,CAAC,GAAG,QAAQ,CAAA;IAC/F,OAAO,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAA;AAClC,CAAC"}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLMM bin and price arithmetic, as the ranges compute it on chain.
|
|
3
|
+
*
|
|
4
|
+
* Liquidity is held in discrete bins whose price bounds form a geometric
|
|
5
|
+
* series with ratio `1 + binStep/10000`.
|
|
6
|
+
*
|
|
7
|
+
* Every upper bound is exactly the next bin's lower bound, including odd and
|
|
8
|
+
* negative bins; multiplying the lower bound by sqrt(1+beta) again would
|
|
9
|
+
* round differently from the range.
|
|
10
|
+
*/
|
|
11
|
+
/** Bins per range. A pool is split across several ranges. */
|
|
12
|
+
export declare const BINS_PER_RANGE: number;
|
|
13
|
+
/** Offset that centres range 0 on bin 0. */
|
|
14
|
+
export declare const RANGE_OFFSET: number;
|
|
15
|
+
/** Bin steps the factory accepts, in basis points. */
|
|
16
|
+
export declare const BIN_STEPS: readonly [25, 50, 100, 200];
|
|
17
|
+
/**
|
|
18
|
+
* A bin step the factory will accept.
|
|
19
|
+
*
|
|
20
|
+
* A union rather than an enum, because the package compiles under
|
|
21
|
+
* `erasableSyntaxOnly`; it still rejects `binStep: 30` at compile time.
|
|
22
|
+
*/
|
|
23
|
+
export type BinStep = (typeof BIN_STEPS)[number];
|
|
24
|
+
/**
|
|
25
|
+
* The four bin steps. The digits are the value: `Bps25` is `25`.
|
|
26
|
+
*
|
|
27
|
+
* A narrower step concentrates liquidity harder — better fills, but the market
|
|
28
|
+
* leaves the position sooner.
|
|
29
|
+
*/
|
|
30
|
+
export declare const BinStep: {
|
|
31
|
+
/** 0.25% per bin — "micro bins", stable pairs. */
|
|
32
|
+
readonly Bps25: 25;
|
|
33
|
+
/** 0.50% per bin — "tight bins", stable pairs. */
|
|
34
|
+
readonly Bps50: 50;
|
|
35
|
+
/** 1.00% per bin — "wide bins", volatile pairs. */
|
|
36
|
+
readonly Bps100: 100;
|
|
37
|
+
/** 2.00% per bin — "broad bins", exotic pairs. */
|
|
38
|
+
readonly Bps200: 200;
|
|
39
|
+
};
|
|
40
|
+
/** Whether a number is one of the four the factory accepts. */
|
|
41
|
+
export declare function isBinStep(value: number): value is BinStep;
|
|
42
|
+
/** Range indices the protocol currently deploys. */
|
|
43
|
+
export declare const RANGE_MIN: number;
|
|
44
|
+
export declare const RANGE_MAX: number;
|
|
45
|
+
/**
|
|
46
|
+
* Bins a single position may span.
|
|
47
|
+
*
|
|
48
|
+
* The deposit also aborts once it has burned 940 000 gas, so a wide deposit
|
|
49
|
+
* into an already-crowded range can fail below this cap; there is no way to
|
|
50
|
+
* know that before sending.
|
|
51
|
+
*/
|
|
52
|
+
export declare const MAX_BINS_PER_POSITION = 50;
|
|
53
|
+
/** Bins one swap may cross before the range gives up. */
|
|
54
|
+
export declare const MAX_BINS_PER_SWAP = 40;
|
|
55
|
+
/** First bin belonging to a range. */
|
|
56
|
+
export declare function firstBinOfRange(range: number): number;
|
|
57
|
+
/** Last bin belonging to a range. */
|
|
58
|
+
export declare function lastBinOfRange(range: number): number;
|
|
59
|
+
/**
|
|
60
|
+
* Which range holds a bin.
|
|
61
|
+
*
|
|
62
|
+
* Rounds down, not toward zero. TVM divides toward negative infinity, and the
|
|
63
|
+
* difference is not academic: bin −1025 belongs to range −1, but truncation
|
|
64
|
+
* would put it in range 0 and send the message to a range that does not
|
|
65
|
+
* own that bin.
|
|
66
|
+
*/
|
|
67
|
+
export declare function rangeOfBin(bin: number): number;
|
|
68
|
+
/** Lowest and highest bins any deployed range covers. */
|
|
69
|
+
export declare function binCoverage(): {
|
|
70
|
+
min: number;
|
|
71
|
+
max: number;
|
|
72
|
+
};
|
|
73
|
+
/** `1 + binStep/10000` in Q128, as the range computes it. */
|
|
74
|
+
export declare function onePlusBeta(binStep: number): bigint;
|
|
75
|
+
/** Square root of {@link onePlusBeta}, in Q128. */
|
|
76
|
+
export declare function sqrtOnePlusBeta(binStep: number): bigint;
|
|
77
|
+
/**
|
|
78
|
+
* Lower price bound of a bin, as a Q128 square root.
|
|
79
|
+
*
|
|
80
|
+
* Odd bins are computed as `(1+β)^(n>>1) · sqrt(1+β)` rather than by raising to
|
|
81
|
+
* a half power — matching the range's own shortcut, rounding included.
|
|
82
|
+
*/
|
|
83
|
+
export declare function sqrtPriceAtBin(bin: number, binStep: number): bigint;
|
|
84
|
+
/**
|
|
85
|
+
* Upper bound of a bin, where a swap moving the price down enters it. The
|
|
86
|
+
* range uses this same boundary for liquidity and swap arithmetic.
|
|
87
|
+
*/
|
|
88
|
+
export declare function sqrtPriceEnteringBin(bin: number, binStep: number): bigint;
|
|
89
|
+
/** Both price bounds of a bin, as Q128 square roots. */
|
|
90
|
+
export declare function binBounds(bin: number, binStep: number): {
|
|
91
|
+
sqrtPa: bigint;
|
|
92
|
+
sqrtPb: bigint;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Price from a Q128 square root, as a JavaScript number.
|
|
96
|
+
*
|
|
97
|
+
* For display only. The value is squared and scaled down, so it loses
|
|
98
|
+
* precision — never feed it back into anything that builds a transaction.
|
|
99
|
+
*/
|
|
100
|
+
export declare function sqrtPriceToPrice(sqrtPrice: bigint, decimalsX?: number, decimalsY?: number): number;
|
|
101
|
+
/**
|
|
102
|
+
* The bin containing a price expressed in **raw** units — token Y per token X
|
|
103
|
+
* before decimals are applied.
|
|
104
|
+
*
|
|
105
|
+
* Containment, not proximity. A bin owns the half-open interval
|
|
106
|
+
* `[Pa(n), Pa(n+1))`, so a price anywhere in the upper part of a bin still
|
|
107
|
+
* belongs to that bin and not to the next one. When you want the closest
|
|
108
|
+
* boundary instead, ask for it by name: {@link nearestBinToRawPrice}.
|
|
109
|
+
*
|
|
110
|
+
* Named apart from `pool.binAtPrice`, which takes the price as a person would
|
|
111
|
+
* write it. Feeding a human price here puts the bin thousands of steps away
|
|
112
|
+
* whenever the two assets have different decimals.
|
|
113
|
+
*/
|
|
114
|
+
export declare function binAtRawPrice(rawPrice: number, binStep: number): number;
|
|
115
|
+
/**
|
|
116
|
+
* The bin whose lower bound lies **closest** to a raw price — the nearest
|
|
117
|
+
* boundary, not the bin the price falls into.
|
|
118
|
+
*
|
|
119
|
+
* Only pool creation wants this: the factory accepts any `initBin`, and
|
|
120
|
+
* rounding to the nearest boundary opens the pool at the price the creator
|
|
121
|
+
* actually asked for rather than up to a whole bin below it. Everything that
|
|
122
|
+
* asks "where does this price live?" wants {@link binAtRawPrice}.
|
|
123
|
+
*/
|
|
124
|
+
export declare function nearestBinToRawPrice(rawPrice: number, binStep: number): number;
|
|
125
|
+
/** Human-readable price of a bin, decimals applied. */
|
|
126
|
+
export declare function priceAtBin(bin: number, binStep: number, decimalsX?: number, decimalsY?: number): number;
|
|
127
|
+
/**
|
|
128
|
+
* Converts a human price into a bin, accounting for the two tokens' decimals.
|
|
129
|
+
*
|
|
130
|
+
* A pool of TON (9 decimals) against a 6-decimal jetton prices very differently
|
|
131
|
+
* in raw units than it does on screen; getting this wrong puts a position
|
|
132
|
+
* thousands of bins away from the market.
|
|
133
|
+
*/
|
|
134
|
+
export declare function binAtHumanPrice(args: {
|
|
135
|
+
price: number;
|
|
136
|
+
binStep: number;
|
|
137
|
+
decimalsX: number;
|
|
138
|
+
decimalsY: number;
|
|
139
|
+
}): number;
|
|
140
|
+
/**
|
|
141
|
+
* {@link nearestBinToRawPrice} for a human price, decimals accounted for.
|
|
142
|
+
*
|
|
143
|
+
* The counterpart of {@link binAtHumanPrice}: nearest boundary rather than
|
|
144
|
+
* containment. Pool creation is the one caller that means it.
|
|
145
|
+
*/
|
|
146
|
+
export declare function nearestBinToHumanPrice(args: {
|
|
147
|
+
price: number;
|
|
148
|
+
binStep: number;
|
|
149
|
+
decimalsX: number;
|
|
150
|
+
decimalsY: number;
|
|
151
|
+
}): number;
|
|
152
|
+
export type Contribution = {
|
|
153
|
+
/** Liquidity credited to the position for this bin. */
|
|
154
|
+
liquidity: bigint;
|
|
155
|
+
/** Amounts actually consumed. */
|
|
156
|
+
used: {
|
|
157
|
+
x: bigint;
|
|
158
|
+
y: bigint;
|
|
159
|
+
};
|
|
160
|
+
/** Amounts handed back because the bin could not absorb them. */
|
|
161
|
+
refund: {
|
|
162
|
+
x: bigint;
|
|
163
|
+
y: bigint;
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* What one bin does with the amounts offered to it.
|
|
168
|
+
*
|
|
169
|
+
* A bin below the current price takes only Y; one above takes only X; the bin
|
|
170
|
+
* holding the current price takes both, in the ratio its bounds dictate. The
|
|
171
|
+
* side that runs out first caps the liquidity, and the excess is refunded —
|
|
172
|
+
* which is why an unbalanced deposit silently returns part of itself.
|
|
173
|
+
*/
|
|
174
|
+
export declare function contributeLiquidity(args: {
|
|
175
|
+
bin: number;
|
|
176
|
+
currentSqrtPrice: bigint;
|
|
177
|
+
x: bigint;
|
|
178
|
+
y: bigint;
|
|
179
|
+
binStep: number;
|
|
180
|
+
}): Contribution;
|
|
181
|
+
/**
|
|
182
|
+
* What one bin of a position is worth right now.
|
|
183
|
+
*
|
|
184
|
+
* Mirrors a withdrawal from the range: liquidity is converted
|
|
185
|
+
* back into both sides at the price clamped to the bin's own bounds, so a bin
|
|
186
|
+
* the price has moved past holds only one of them.
|
|
187
|
+
*/
|
|
188
|
+
export declare function binValue(args: {
|
|
189
|
+
bin: number;
|
|
190
|
+
liquidity: bigint;
|
|
191
|
+
currentSqrtPrice: bigint;
|
|
192
|
+
binStep: number;
|
|
193
|
+
}): {
|
|
194
|
+
x: bigint;
|
|
195
|
+
y: bigint;
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* Fees one bin owes a position.
|
|
199
|
+
*
|
|
200
|
+
* Mirrors the on-chain fee payout: the range keeps a per-unit-of-liquidity
|
|
201
|
+
* accumulator per bin, the position keeps the value it last settled at, and
|
|
202
|
+
* what is owed is the growth times the position's liquidity in that bin.
|
|
203
|
+
*/
|
|
204
|
+
export declare function binFees(args: {
|
|
205
|
+
liquidity: bigint;
|
|
206
|
+
/** The range's current accumulators for this bin. */
|
|
207
|
+
binFees: {
|
|
208
|
+
x: bigint;
|
|
209
|
+
y: bigint;
|
|
210
|
+
};
|
|
211
|
+
/** The position's checkpoint of those accumulators. */
|
|
212
|
+
checkpoint: {
|
|
213
|
+
x: bigint;
|
|
214
|
+
y: bigint;
|
|
215
|
+
};
|
|
216
|
+
}): {
|
|
217
|
+
x: bigint;
|
|
218
|
+
y: bigint;
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* Which side or sides a bin can take, given where the price sits.
|
|
222
|
+
*
|
|
223
|
+
* Useful before building a deposit: offering X to a bin that only takes Y
|
|
224
|
+
* gets it refunded, minus the gas spent finding out.
|
|
225
|
+
*/
|
|
226
|
+
export declare function binSides(args: {
|
|
227
|
+
bin: number;
|
|
228
|
+
currentSqrtPrice: bigint;
|
|
229
|
+
binStep: number;
|
|
230
|
+
}): {
|
|
231
|
+
takesX: boolean;
|
|
232
|
+
takesY: boolean;
|
|
233
|
+
};
|
|
234
|
+
//# sourceMappingURL=clmm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clmm.d.ts","sourceRoot":"","sources":["../../src/math/clmm.ts"],"names":[],"mappings":"AAcA;;;;;;;;;GASG;AAEH,6DAA6D;AAC7D,eAAO,MAAM,cAAc,QAA+C,CAAA;AAC1E,4CAA4C;AAC5C,eAAO,MAAM,YAAY,QAA6C,CAAA;AACtE,sDAAsD;AACtD,eAAO,MAAM,SAAS,6BAA8B,CAAA;AAEpD;;;;;GAKG;AACH,MAAM,MAAM,OAAO,GAAG,CAAC,OAAO,SAAS,CAAC,CAAC,MAAM,CAAC,CAAA;AAEhD;;;;;GAKG;AACH,eAAO,MAAM,OAAO;IAClB,kDAAkD;;IAElD,kDAAkD;;IAElD,mDAAmD;;IAEnD,kDAAkD;;CAER,CAAA;AAE5C,+DAA+D;AAC/D,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,OAAO,CAEzD;AACD,oDAAoD;AACpD,eAAO,MAAM,SAAS,QAA0C,CAAA;AAChE,eAAO,MAAM,SAAS,QAA0C,CAAA;AAChE;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,KAAK,CAAA;AACvC,yDAAyD;AACzD,eAAO,MAAM,iBAAiB,KAAK,CAAA;AAEnC,sCAAsC;AACtC,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,qCAAqC;AACrC,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED,yDAAyD;AACzD,wBAAgB,WAAW,IAAI;IAAE,GAAG,EAAE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