@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,270 @@
|
|
|
1
|
+
import { constantOf } from "../protocol/index.js";
|
|
2
|
+
import { SCALE, q128Div, q128DivCeil, q128DivFloor, q128Mul, q128MulCeil, q128MulFloor, q128Pow, q128Sqrt, mulDivFloor, } from "./q128.js";
|
|
3
|
+
/**
|
|
4
|
+
* CLMM bin and price arithmetic, as the ranges compute it on chain.
|
|
5
|
+
*
|
|
6
|
+
* Liquidity is held in discrete bins whose price bounds form a geometric
|
|
7
|
+
* series with ratio `1 + binStep/10000`.
|
|
8
|
+
*
|
|
9
|
+
* Every upper bound is exactly the next bin's lower bound, including odd and
|
|
10
|
+
* negative bins; multiplying the lower bound by sqrt(1+beta) again would
|
|
11
|
+
* round differently from the range.
|
|
12
|
+
*/
|
|
13
|
+
/** Bins per range. A pool is split across several ranges. */
|
|
14
|
+
export const BINS_PER_RANGE = Number(constantOf('clmm', 'BINS_PER_RANGE'));
|
|
15
|
+
/** Offset that centres range 0 on bin 0. */
|
|
16
|
+
export const RANGE_OFFSET = Number(constantOf('clmm', 'RANGE_OFFSET'));
|
|
17
|
+
/** Bin steps the factory accepts, in basis points. */
|
|
18
|
+
export const BIN_STEPS = [25, 50, 100, 200];
|
|
19
|
+
/**
|
|
20
|
+
* The four bin steps. The digits are the value: `Bps25` is `25`.
|
|
21
|
+
*
|
|
22
|
+
* A narrower step concentrates liquidity harder — better fills, but the market
|
|
23
|
+
* leaves the position sooner.
|
|
24
|
+
*/
|
|
25
|
+
export const BinStep = {
|
|
26
|
+
/** 0.25% per bin — "micro bins", stable pairs. */
|
|
27
|
+
Bps25: 25,
|
|
28
|
+
/** 0.50% per bin — "tight bins", stable pairs. */
|
|
29
|
+
Bps50: 50,
|
|
30
|
+
/** 1.00% per bin — "wide bins", volatile pairs. */
|
|
31
|
+
Bps100: 100,
|
|
32
|
+
/** 2.00% per bin — "broad bins", exotic pairs. */
|
|
33
|
+
Bps200: 200,
|
|
34
|
+
};
|
|
35
|
+
/** Whether a number is one of the four the factory accepts. */
|
|
36
|
+
export function isBinStep(value) {
|
|
37
|
+
return BIN_STEPS.includes(value);
|
|
38
|
+
}
|
|
39
|
+
/** Range indices the protocol currently deploys. */
|
|
40
|
+
export const RANGE_MIN = Number(constantOf('clmm', 'RANGE_MIN'));
|
|
41
|
+
export const RANGE_MAX = Number(constantOf('clmm', 'RANGE_MAX'));
|
|
42
|
+
/**
|
|
43
|
+
* Bins a single position may span.
|
|
44
|
+
*
|
|
45
|
+
* The deposit also aborts once it has burned 940 000 gas, so a wide deposit
|
|
46
|
+
* into an already-crowded range can fail below this cap; there is no way to
|
|
47
|
+
* know that before sending.
|
|
48
|
+
*/
|
|
49
|
+
export const MAX_BINS_PER_POSITION = 50;
|
|
50
|
+
/** Bins one swap may cross before the range gives up. */
|
|
51
|
+
export const MAX_BINS_PER_SWAP = 40;
|
|
52
|
+
/** First bin belonging to a range. */
|
|
53
|
+
export function firstBinOfRange(range) {
|
|
54
|
+
return range * BINS_PER_RANGE - RANGE_OFFSET;
|
|
55
|
+
}
|
|
56
|
+
/** Last bin belonging to a range. */
|
|
57
|
+
export function lastBinOfRange(range) {
|
|
58
|
+
return range * BINS_PER_RANGE + (RANGE_OFFSET - 1);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Which range holds a bin.
|
|
62
|
+
*
|
|
63
|
+
* Rounds down, not toward zero. TVM divides toward negative infinity, and the
|
|
64
|
+
* difference is not academic: bin −1025 belongs to range −1, but truncation
|
|
65
|
+
* would put it in range 0 and send the message to a range that does not
|
|
66
|
+
* own that bin.
|
|
67
|
+
*/
|
|
68
|
+
export function rangeOfBin(bin) {
|
|
69
|
+
return Math.floor((bin + RANGE_OFFSET) / BINS_PER_RANGE);
|
|
70
|
+
}
|
|
71
|
+
/** Lowest and highest bins any deployed range covers. */
|
|
72
|
+
export function binCoverage() {
|
|
73
|
+
return { min: firstBinOfRange(RANGE_MIN), max: lastBinOfRange(RANGE_MAX) };
|
|
74
|
+
}
|
|
75
|
+
/** `1 + binStep/10000` in Q128, as the range computes it. */
|
|
76
|
+
export function onePlusBeta(binStep) {
|
|
77
|
+
return SCALE + mulDivFloor(SCALE, BigInt(binStep), 10000n);
|
|
78
|
+
}
|
|
79
|
+
/** Square root of {@link onePlusBeta}, in Q128. */
|
|
80
|
+
export function sqrtOnePlusBeta(binStep) {
|
|
81
|
+
return q128Sqrt(onePlusBeta(binStep));
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Lower price bound of a bin, as a Q128 square root.
|
|
85
|
+
*
|
|
86
|
+
* Odd bins are computed as `(1+β)^(n>>1) · sqrt(1+β)` rather than by raising to
|
|
87
|
+
* a half power — matching the range's own shortcut, rounding included.
|
|
88
|
+
*/
|
|
89
|
+
export function sqrtPriceAtBin(bin, binStep) {
|
|
90
|
+
const beta = onePlusBeta(binStep);
|
|
91
|
+
const sqrtBeta = sqrtOnePlusBeta(binStep);
|
|
92
|
+
return sqrtPriceAtBinWith(bin, beta, sqrtBeta);
|
|
93
|
+
}
|
|
94
|
+
function sqrtPriceAtBinWith(bin, beta, sqrtBeta) {
|
|
95
|
+
const half = BigInt(bin >> 1);
|
|
96
|
+
const power = q128Pow(beta, half);
|
|
97
|
+
return (bin & 1) === 0 ? power : q128Mul(power, sqrtBeta);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Upper bound of a bin, where a swap moving the price down enters it. The
|
|
101
|
+
* range uses this same boundary for liquidity and swap arithmetic.
|
|
102
|
+
*/
|
|
103
|
+
export function sqrtPriceEnteringBin(bin, binStep) {
|
|
104
|
+
return sqrtPriceAtBin(bin + 1, binStep);
|
|
105
|
+
}
|
|
106
|
+
/** Both price bounds of a bin, as Q128 square roots. */
|
|
107
|
+
export function binBounds(bin, binStep) {
|
|
108
|
+
const beta = onePlusBeta(binStep);
|
|
109
|
+
const sqrtBeta = sqrtOnePlusBeta(binStep);
|
|
110
|
+
const sqrtPa = sqrtPriceAtBinWith(bin, beta, sqrtBeta);
|
|
111
|
+
return { sqrtPa, sqrtPb: sqrtPriceAtBinWith(bin + 1, beta, sqrtBeta) };
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Price from a Q128 square root, as a JavaScript number.
|
|
115
|
+
*
|
|
116
|
+
* For display only. The value is squared and scaled down, so it loses
|
|
117
|
+
* precision — never feed it back into anything that builds a transaction.
|
|
118
|
+
*/
|
|
119
|
+
export function sqrtPriceToPrice(sqrtPrice, decimalsX = 0, decimalsY = 0) {
|
|
120
|
+
const raw = Number(sqrtPrice) / Number(SCALE);
|
|
121
|
+
return raw * raw * 10 ** (decimalsX - decimalsY);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Bin index as a real number: `log(price) / log(1+β)`.
|
|
125
|
+
*
|
|
126
|
+
* The integer part names the bin, the fraction says how far into it the price
|
|
127
|
+
* sits. Shared so that containment and proximity cannot drift apart.
|
|
128
|
+
*/
|
|
129
|
+
function binExponent(rawPrice, binStep) {
|
|
130
|
+
if (!(rawPrice > 0) || !Number.isFinite(rawPrice)) {
|
|
131
|
+
throw new RangeError('price must be a finite number greater than zero');
|
|
132
|
+
}
|
|
133
|
+
return Math.log(rawPrice) / Math.log(1 + binStep / 10_000);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The bin containing a price expressed in **raw** units — token Y per token X
|
|
137
|
+
* before decimals are applied.
|
|
138
|
+
*
|
|
139
|
+
* Containment, not proximity. A bin owns the half-open interval
|
|
140
|
+
* `[Pa(n), Pa(n+1))`, so a price anywhere in the upper part of a bin still
|
|
141
|
+
* belongs to that bin and not to the next one. When you want the closest
|
|
142
|
+
* boundary instead, ask for it by name: {@link nearestBinToRawPrice}.
|
|
143
|
+
*
|
|
144
|
+
* Named apart from `pool.binAtPrice`, which takes the price as a person would
|
|
145
|
+
* write it. Feeding a human price here puts the bin thousands of steps away
|
|
146
|
+
* whenever the two assets have different decimals.
|
|
147
|
+
*/
|
|
148
|
+
export function binAtRawPrice(rawPrice, binStep) {
|
|
149
|
+
// Floor, not round: a bin owns [Pa(n), Pa(n+1)).
|
|
150
|
+
// In doubles (1+β)^bin lands slightly below the exact power (worst exponent
|
|
151
|
+
// error −4.5e-13), so a bare floor breaks bin↔price round trips; 1e-9 absorbs
|
|
152
|
+
// that and is far too small to cross a real bin boundary.
|
|
153
|
+
return Math.floor(binExponent(rawPrice, binStep) + 1e-9);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* The bin whose lower bound lies **closest** to a raw price — the nearest
|
|
157
|
+
* boundary, not the bin the price falls into.
|
|
158
|
+
*
|
|
159
|
+
* Only pool creation wants this: the factory accepts any `initBin`, and
|
|
160
|
+
* rounding to the nearest boundary opens the pool at the price the creator
|
|
161
|
+
* actually asked for rather than up to a whole bin below it. Everything that
|
|
162
|
+
* asks "where does this price live?" wants {@link binAtRawPrice}.
|
|
163
|
+
*/
|
|
164
|
+
export function nearestBinToRawPrice(rawPrice, binStep) {
|
|
165
|
+
return Math.round(binExponent(rawPrice, binStep));
|
|
166
|
+
}
|
|
167
|
+
/** Human-readable price of a bin, decimals applied. */
|
|
168
|
+
export function priceAtBin(bin, binStep, decimalsX = 0, decimalsY = 0) {
|
|
169
|
+
return (1 + binStep / 10_000) ** bin * 10 ** (decimalsX - decimalsY);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Converts a human price into a bin, accounting for the two tokens' decimals.
|
|
173
|
+
*
|
|
174
|
+
* A pool of TON (9 decimals) against a 6-decimal jetton prices very differently
|
|
175
|
+
* in raw units than it does on screen; getting this wrong puts a position
|
|
176
|
+
* thousands of bins away from the market.
|
|
177
|
+
*/
|
|
178
|
+
export function binAtHumanPrice(args) {
|
|
179
|
+
const raw = args.price * 10 ** (args.decimalsY - args.decimalsX);
|
|
180
|
+
return binAtRawPrice(raw, args.binStep);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* {@link nearestBinToRawPrice} for a human price, decimals accounted for.
|
|
184
|
+
*
|
|
185
|
+
* The counterpart of {@link binAtHumanPrice}: nearest boundary rather than
|
|
186
|
+
* containment. Pool creation is the one caller that means it.
|
|
187
|
+
*/
|
|
188
|
+
export function nearestBinToHumanPrice(args) {
|
|
189
|
+
const raw = args.price * 10 ** (args.decimalsY - args.decimalsX);
|
|
190
|
+
return nearestBinToRawPrice(raw, args.binStep);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* What one bin does with the amounts offered to it.
|
|
194
|
+
*
|
|
195
|
+
* A bin below the current price takes only Y; one above takes only X; the bin
|
|
196
|
+
* holding the current price takes both, in the ratio its bounds dictate. The
|
|
197
|
+
* side that runs out first caps the liquidity, and the excess is refunded —
|
|
198
|
+
* which is why an unbalanced deposit silently returns part of itself.
|
|
199
|
+
*/
|
|
200
|
+
export function contributeLiquidity(args) {
|
|
201
|
+
const { sqrtPa, sqrtPb } = binBounds(args.bin, args.binStep);
|
|
202
|
+
const clamped = args.currentSqrtPrice < sqrtPa ? sqrtPa : args.currentSqrtPrice;
|
|
203
|
+
const sqrtP = clamped > sqrtPb ? sqrtPb : clamped;
|
|
204
|
+
const dxPerL = q128Div(SCALE, sqrtP) - q128Div(SCALE, sqrtPb);
|
|
205
|
+
const dyPerL = sqrtP - sqrtPa;
|
|
206
|
+
let liquidity = 0n;
|
|
207
|
+
if (dxPerL > 0n || dyPerL > 0n) {
|
|
208
|
+
const fromX = dxPerL > 0n ? q128DivFloor(args.x, dxPerL) : 1n << 255n;
|
|
209
|
+
const fromY = dyPerL > 0n ? q128DivFloor(args.y, dyPerL) : 1n << 255n;
|
|
210
|
+
liquidity = fromX < fromY ? fromX : fromY;
|
|
211
|
+
}
|
|
212
|
+
// Used amounts round up, as on chain.
|
|
213
|
+
let usedX = q128MulCeil(dxPerL, liquidity);
|
|
214
|
+
let usedY = q128MulCeil(dyPerL, liquidity);
|
|
215
|
+
if (usedX > args.x)
|
|
216
|
+
usedX = args.x;
|
|
217
|
+
if (usedY > args.y)
|
|
218
|
+
usedY = args.y;
|
|
219
|
+
return {
|
|
220
|
+
liquidity,
|
|
221
|
+
used: { x: usedX, y: usedY },
|
|
222
|
+
refund: { x: args.x - usedX, y: args.y - usedY },
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* What one bin of a position is worth right now.
|
|
227
|
+
*
|
|
228
|
+
* Mirrors a withdrawal from the range: liquidity is converted
|
|
229
|
+
* back into both sides at the price clamped to the bin's own bounds, so a bin
|
|
230
|
+
* the price has moved past holds only one of them.
|
|
231
|
+
*/
|
|
232
|
+
export function binValue(args) {
|
|
233
|
+
const { sqrtPa, sqrtPb } = binBounds(args.bin, args.binStep);
|
|
234
|
+
const clamped = args.currentSqrtPrice < sqrtPa ? sqrtPa : args.currentSqrtPrice;
|
|
235
|
+
const sqrtP = clamped > sqrtPb ? sqrtPb : clamped;
|
|
236
|
+
const dxPerL = q128DivFloor(SCALE, sqrtP) - q128DivCeil(SCALE, sqrtPb);
|
|
237
|
+
const dyPerL = sqrtP - sqrtPa;
|
|
238
|
+
const x = q128MulFloor(dxPerL, args.liquidity);
|
|
239
|
+
const y = q128MulFloor(dyPerL, args.liquidity);
|
|
240
|
+
return { x: x > 0n ? x : 0n, y: y > 0n ? y : 0n };
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Fees one bin owes a position.
|
|
244
|
+
*
|
|
245
|
+
* Mirrors the on-chain fee payout: the range keeps a per-unit-of-liquidity
|
|
246
|
+
* accumulator per bin, the position keeps the value it last settled at, and
|
|
247
|
+
* what is owed is the growth times the position's liquidity in that bin.
|
|
248
|
+
*/
|
|
249
|
+
export function binFees(args) {
|
|
250
|
+
const deltaX = args.binFees.x - args.checkpoint.x;
|
|
251
|
+
const deltaY = args.binFees.y - args.checkpoint.y;
|
|
252
|
+
return {
|
|
253
|
+
x: q128MulFloor(args.liquidity, deltaX > 0n ? deltaX : 0n),
|
|
254
|
+
y: q128MulFloor(args.liquidity, deltaY > 0n ? deltaY : 0n),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Which side or sides a bin can take, given where the price sits.
|
|
259
|
+
*
|
|
260
|
+
* Useful before building a deposit: offering X to a bin that only takes Y
|
|
261
|
+
* gets it refunded, minus the gas spent finding out.
|
|
262
|
+
*/
|
|
263
|
+
export function binSides(args) {
|
|
264
|
+
const { sqrtPa, sqrtPb } = binBounds(args.bin, args.binStep);
|
|
265
|
+
return {
|
|
266
|
+
takesX: args.currentSqrtPrice < sqrtPb,
|
|
267
|
+
takesY: args.currentSqrtPrice > sqrtPa,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
//# sourceMappingURL=clmm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clmm.js","sourceRoot":"","sources":["../../src/math/clmm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACjD,OAAO,EACL,KAAK,EACL,OAAO,EACP,WAAW,EACX,YAAY,EACZ,OAAO,EACP,WAAW,EACX,YAAY,EACZ,OAAO,EACP,QAAQ,EACR,WAAW,GACZ,MAAM,WAAW,CAAA;AAElB;;;;;;;;;GASG;AAEH,6DAA6D;AAC7D,MAAM,CAAC,MAAM,cAAc,GAAG,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,gBAAgB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AM,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,CAAA;IACjD,OAAO;QACL,CAAC,EAAE,YAAY,CAAC,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;QAC1D,CAAC,EAAE,YAAY,CAAC,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;KAC3D,CAAA;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,IAIxB;IACC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,CAAA;IAC5D,OAAO;QACL,MAAM,EAAE,IAAI,CAAC,gBAAgB,GAAG,MAAM;QACtC,MAAM,EAAE,IAAI,CAAC,gBAAgB,GAAG,MAAM;KACvC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CPMM arithmetic, as the pool computes it on chain.
|
|
3
|
+
*
|
|
4
|
+
* Constants come from the generated protocol table, so a change on chain
|
|
5
|
+
* shows up as a failing test.
|
|
6
|
+
*/
|
|
7
|
+
/** Fees are shares of 10000. */
|
|
8
|
+
export declare const FEE_DIVIDER: bigint;
|
|
9
|
+
/** Fixed-point scale for per-share fee accumulators: 2^128. */
|
|
10
|
+
export declare const SCALE: bigint;
|
|
11
|
+
export declare const MAX_COINS: bigint;
|
|
12
|
+
/**
|
|
13
|
+
* Liquidity permanently retained by the pool on its first deposit.
|
|
14
|
+
*
|
|
15
|
+
* `sqrt(10000 * 10000)` in the pool. It is minted to nobody and can never
|
|
16
|
+
* be withdrawn, which is what stops the pool from being drained to zero.
|
|
17
|
+
*/
|
|
18
|
+
export declare const INITIAL_LOCKED_LP: bigint;
|
|
19
|
+
export type SwapResult = {
|
|
20
|
+
amountOut: bigint;
|
|
21
|
+
newReserveIn: bigint;
|
|
22
|
+
newReserveOut: bigint;
|
|
23
|
+
/** Portion of the input kept for liquidity providers. */
|
|
24
|
+
lpFee: bigint;
|
|
25
|
+
/** Portion of the input kept for the protocol. */
|
|
26
|
+
protocolFee: bigint;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Output of a constant-product swap, fees included.
|
|
30
|
+
*
|
|
31
|
+
* Note the shape of the formula: fees are taken off the *input* before the
|
|
32
|
+
* curve is applied, and the reserve grows by the input minus both fees — the
|
|
33
|
+
* fee portion is accounted separately rather than left in the reserve.
|
|
34
|
+
*/
|
|
35
|
+
export declare function swapOutput(args: {
|
|
36
|
+
amountIn: bigint;
|
|
37
|
+
reserveIn: bigint;
|
|
38
|
+
reserveOut: bigint;
|
|
39
|
+
lpFeeBps: number;
|
|
40
|
+
protocolFeeBps: number;
|
|
41
|
+
}): SwapResult;
|
|
42
|
+
/**
|
|
43
|
+
* The conditions under which the pool refuses a swap.
|
|
44
|
+
*
|
|
45
|
+
* Checked before building so the caller sees the reason instead of a bounced
|
|
46
|
+
* message — or, on the jetton leg, instead of nothing at all.
|
|
47
|
+
*/
|
|
48
|
+
export declare function swapRejectionReason(args: {
|
|
49
|
+
amountIn: bigint;
|
|
50
|
+
reserveIn: bigint;
|
|
51
|
+
reserveOut: bigint;
|
|
52
|
+
lpTotal: bigint;
|
|
53
|
+
lpFeeBps: number;
|
|
54
|
+
protocolFeeBps: number;
|
|
55
|
+
minOut: bigint;
|
|
56
|
+
}): string | null;
|
|
57
|
+
/**
|
|
58
|
+
* How far the swap moves the price, in basis points.
|
|
59
|
+
*
|
|
60
|
+
* Measured against the mid price before the trade, so a tiny trade in a deep
|
|
61
|
+
* pool reads as ~0 and a trade that eats the pool reads as most of 10000.
|
|
62
|
+
*/
|
|
63
|
+
export declare function priceImpactBps(args: {
|
|
64
|
+
amountIn: bigint;
|
|
65
|
+
amountOut: bigint;
|
|
66
|
+
reserveIn: bigint;
|
|
67
|
+
reserveOut: bigint;
|
|
68
|
+
}): number;
|
|
69
|
+
export type FirstDeposit = {
|
|
70
|
+
/** Total LP the pool will hold. */
|
|
71
|
+
lpTotal: bigint;
|
|
72
|
+
/** LP actually credited to the depositor. */
|
|
73
|
+
userLp: bigint;
|
|
74
|
+
/** LP retained by the pool forever. */
|
|
75
|
+
lockedLp: bigint;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* LP minted by the very first deposit into a pool.
|
|
79
|
+
*
|
|
80
|
+
* `sqrt(x * y)`, less the 10000 units the pool keeps. A first deposit small
|
|
81
|
+
* enough that `sqrt(x * y) <= 10000` mints the depositor nothing.
|
|
82
|
+
*/
|
|
83
|
+
export declare function firstDepositLp(x: bigint, y: bigint): FirstDeposit;
|
|
84
|
+
/**
|
|
85
|
+
* LP minted by a deposit into a pool that already has liquidity.
|
|
86
|
+
*
|
|
87
|
+
* The smaller of the two sides decides — supplying more of one asset than the
|
|
88
|
+
* ratio calls for mints nothing extra for the excess.
|
|
89
|
+
*/
|
|
90
|
+
export declare function depositLp(args: {
|
|
91
|
+
x: bigint;
|
|
92
|
+
y: bigint;
|
|
93
|
+
lpTotal: bigint;
|
|
94
|
+
reserveX: bigint;
|
|
95
|
+
reserveY: bigint;
|
|
96
|
+
}): bigint;
|
|
97
|
+
/**
|
|
98
|
+
* The amounts of each side that match a given amount of the other, at the
|
|
99
|
+
* pool's current ratio. Use it to fill in the second field of a deposit form.
|
|
100
|
+
*/
|
|
101
|
+
export declare function matchingDeposit(args: {
|
|
102
|
+
reserveX: bigint;
|
|
103
|
+
reserveY: bigint;
|
|
104
|
+
x?: bigint;
|
|
105
|
+
y?: bigint;
|
|
106
|
+
}): {
|
|
107
|
+
x: bigint;
|
|
108
|
+
y: bigint;
|
|
109
|
+
};
|
|
110
|
+
export type WithdrawResult = {
|
|
111
|
+
/** Principal returned, before fees. */
|
|
112
|
+
principal: {
|
|
113
|
+
x: bigint;
|
|
114
|
+
y: bigint;
|
|
115
|
+
};
|
|
116
|
+
/** Fees accrued since the position last settled. */
|
|
117
|
+
fees: {
|
|
118
|
+
x: bigint;
|
|
119
|
+
y: bigint;
|
|
120
|
+
};
|
|
121
|
+
/** What actually arrives: principal plus fees. */
|
|
122
|
+
total: {
|
|
123
|
+
x: bigint;
|
|
124
|
+
y: bigint;
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* What withdrawing `lp` returns, fees included.
|
|
129
|
+
*
|
|
130
|
+
* Fees are tracked as a per-share accumulator: the position stores the value
|
|
131
|
+
* of the accumulator when it last settled, and is owed the growth since.
|
|
132
|
+
*/
|
|
133
|
+
export declare function withdrawAmounts(args: {
|
|
134
|
+
lp: bigint;
|
|
135
|
+
lpTotal: bigint;
|
|
136
|
+
reserveX: bigint;
|
|
137
|
+
reserveY: bigint;
|
|
138
|
+
/** Pool-wide fee accumulators, scaled by 2^128. */
|
|
139
|
+
feesLp: {
|
|
140
|
+
x: bigint;
|
|
141
|
+
y: bigint;
|
|
142
|
+
};
|
|
143
|
+
/** The position's checkpoint of those accumulators. */
|
|
144
|
+
checkpoint: {
|
|
145
|
+
x: bigint;
|
|
146
|
+
y: bigint;
|
|
147
|
+
};
|
|
148
|
+
}): WithdrawResult;
|
|
149
|
+
/** Fees owed to a position right now, without touching its liquidity. */
|
|
150
|
+
export declare function pendingFees(args: {
|
|
151
|
+
lp: bigint;
|
|
152
|
+
feesLp: {
|
|
153
|
+
x: bigint;
|
|
154
|
+
y: bigint;
|
|
155
|
+
};
|
|
156
|
+
checkpoint: {
|
|
157
|
+
x: bigint;
|
|
158
|
+
y: bigint;
|
|
159
|
+
};
|
|
160
|
+
}): {
|
|
161
|
+
x: bigint;
|
|
162
|
+
y: bigint;
|
|
163
|
+
};
|
|
164
|
+
/**
|
|
165
|
+
* How much the pool-wide fee accumulator grows when a swap collects `lpFee`.
|
|
166
|
+
*
|
|
167
|
+
* Mirrors the update the pool makes on every swap.
|
|
168
|
+
*/
|
|
169
|
+
export declare function feeShareGrowth(lpFee: bigint, lpTotal: bigint): bigint;
|
|
170
|
+
//# sourceMappingURL=cpmm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cpmm.d.ts","sourceRoot":"","sources":["../../src/math/cpmm.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AAEH,gCAAgC;AAChC,eAAO,MAAM,WAAW,QAAoC,CAAA;AAC5D,+DAA+D;AAC/D,eAAO,MAAM,KAAK,QAA8B,CAAA;AAChD,eAAO,MAAM,SAAS,QAAkC,CAAA;AAExD;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,QAA2B,CAAA;AAEzD,MAAM,MAAM,UAAU,GAAG;IACvB,SAAS,EAAE,MAAM,CAAA;IACjB,YAAY,EAAE,MAAM,CAAA;IACpB,aAAa,EAAE,MAAM,CAAA;IACrB,yDAAyD;IACzD,KAAK,EAAE,MAAM,CAAA;IACb,kDAAkD;IAClD,WAAW,EAAE,MAAM,CAAA;CACpB,CAAA;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE;IAC/B,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;IACjB,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;IAChB,cAAc,EAAE,MAAM,CAAA;CACvB,GAAG,UAAU,CAqBb;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE;IACxC,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;IACjB,UAAU,EAAE,MAAM,CAAA;IAClB,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,MAAM,CAAA;IAChB,cAAc,EAAE,MAAM,CAAA;IACtB,MAAM,EAAE,MAAM,CAAA;CACf,GAAG,MAAM,GAAG,IAAI,CAUhB;AAED;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,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAA;CAChC,CAAA;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE;IACpC,EAAE,EAAE,MAAM,CAAA;IACV,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,mDAAmD;IACnD,MAAM,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAA;IAChC,uDAAuD;IACvD,UAAU,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAA;CACrC,GAAG,cAAc,CAqBjB;AAED,yEAAyE;AACzE,wBAAgB,WAAW,CAAC,IAAI,EAAE;IAChC,EAAE,EAAE,MAAM,CAAA;IACV,MAAM,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAA;IAChC,UAAU,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAA;CACrC,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAK3B;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAGrE"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { constantOf } from "../protocol/index.js";
|
|
2
|
+
import { isqrt, max, min, mulDivFloor } from "./integer.js";
|
|
3
|
+
/**
|
|
4
|
+
* CPMM arithmetic, as the pool computes it on chain.
|
|
5
|
+
*
|
|
6
|
+
* Constants come from the generated protocol table, so a change on chain
|
|
7
|
+
* shows up as a failing test.
|
|
8
|
+
*/
|
|
9
|
+
/** Fees are shares of 10000. */
|
|
10
|
+
export const FEE_DIVIDER = constantOf('cpmm', 'FEE_DIVIDER');
|
|
11
|
+
/** Fixed-point scale for per-share fee accumulators: 2^128. */
|
|
12
|
+
export const SCALE = constantOf('cpmm', 'SCALE');
|
|
13
|
+
export const MAX_COINS = constantOf('cpmm', 'MAX_COINS');
|
|
14
|
+
/**
|
|
15
|
+
* Liquidity permanently retained by the pool on its first deposit.
|
|
16
|
+
*
|
|
17
|
+
* `sqrt(10000 * 10000)` in the pool. It is minted to nobody and can never
|
|
18
|
+
* be withdrawn, which is what stops the pool from being drained to zero.
|
|
19
|
+
*/
|
|
20
|
+
export const INITIAL_LOCKED_LP = isqrt(10000n * 10000n);
|
|
21
|
+
/**
|
|
22
|
+
* Output of a constant-product swap, fees included.
|
|
23
|
+
*
|
|
24
|
+
* Note the shape of the formula: fees are taken off the *input* before the
|
|
25
|
+
* curve is applied, and the reserve grows by the input minus both fees — the
|
|
26
|
+
* fee portion is accounted separately rather than left in the reserve.
|
|
27
|
+
*/
|
|
28
|
+
export function swapOutput(args) {
|
|
29
|
+
const { amountIn, reserveIn, reserveOut } = args;
|
|
30
|
+
if (amountIn <= 0n)
|
|
31
|
+
throw new RangeError('amountIn must be positive');
|
|
32
|
+
if (reserveIn <= 0n || reserveOut <= 0n)
|
|
33
|
+
throw new RangeError('pool has no liquidity');
|
|
34
|
+
const allFees = BigInt(args.lpFeeBps + args.protocolFeeBps);
|
|
35
|
+
if (allFees >= FEE_DIVIDER)
|
|
36
|
+
throw new RangeError('fees cannot reach 100%');
|
|
37
|
+
const effectiveIn = amountIn * (FEE_DIVIDER - allFees);
|
|
38
|
+
const amountOut = mulDivFloor(reserveOut, effectiveIn, reserveIn * FEE_DIVIDER + effectiveIn);
|
|
39
|
+
const lpFee = mulDivFloor(amountIn, BigInt(args.lpFeeBps), FEE_DIVIDER);
|
|
40
|
+
const protocolFee = mulDivFloor(amountIn, BigInt(args.protocolFeeBps), FEE_DIVIDER);
|
|
41
|
+
return {
|
|
42
|
+
amountOut,
|
|
43
|
+
newReserveIn: reserveIn + amountIn - (lpFee + protocolFee),
|
|
44
|
+
newReserveOut: reserveOut - amountOut,
|
|
45
|
+
lpFee,
|
|
46
|
+
protocolFee,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The conditions under which the pool refuses a swap.
|
|
51
|
+
*
|
|
52
|
+
* Checked before building so the caller sees the reason instead of a bounced
|
|
53
|
+
* message — or, on the jetton leg, instead of nothing at all.
|
|
54
|
+
*/
|
|
55
|
+
export function swapRejectionReason(args) {
|
|
56
|
+
if (args.lpTotal <= 0n)
|
|
57
|
+
return 'the pool has no liquidity yet';
|
|
58
|
+
const result = swapOutput(args);
|
|
59
|
+
if (result.amountOut <= 0n)
|
|
60
|
+
return 'this amount is too small to return anything';
|
|
61
|
+
if (args.minOut > 0n && args.minOut > result.amountOut) {
|
|
62
|
+
return `the pool would return ${result.amountOut}, below the requested minimum of ${args.minOut}`;
|
|
63
|
+
}
|
|
64
|
+
if (result.newReserveIn >= MAX_COINS)
|
|
65
|
+
return 'the swap would overflow the pool reserve';
|
|
66
|
+
if (result.newReserveOut <= 0n)
|
|
67
|
+
return 'the swap would drain the pool';
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* How far the swap moves the price, in basis points.
|
|
72
|
+
*
|
|
73
|
+
* Measured against the mid price before the trade, so a tiny trade in a deep
|
|
74
|
+
* pool reads as ~0 and a trade that eats the pool reads as most of 10000.
|
|
75
|
+
*/
|
|
76
|
+
export function priceImpactBps(args) {
|
|
77
|
+
if (args.amountIn <= 0n || args.amountOut <= 0n)
|
|
78
|
+
return 0;
|
|
79
|
+
const ideal = mulDivFloor(args.amountIn, args.reserveOut, args.reserveIn);
|
|
80
|
+
if (ideal <= 0n)
|
|
81
|
+
return 0;
|
|
82
|
+
const lost = ideal > args.amountOut ? ideal - args.amountOut : 0n;
|
|
83
|
+
return Number(mulDivFloor(lost, FEE_DIVIDER, ideal));
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* LP minted by the very first deposit into a pool.
|
|
87
|
+
*
|
|
88
|
+
* `sqrt(x * y)`, less the 10000 units the pool keeps. A first deposit small
|
|
89
|
+
* enough that `sqrt(x * y) <= 10000` mints the depositor nothing.
|
|
90
|
+
*/
|
|
91
|
+
export function firstDepositLp(x, y) {
|
|
92
|
+
if (x <= 0n || y <= 0n)
|
|
93
|
+
throw new RangeError('both sides of the first deposit must be positive');
|
|
94
|
+
const lpTotal = isqrt(x * y);
|
|
95
|
+
return {
|
|
96
|
+
lpTotal,
|
|
97
|
+
userLp: max(lpTotal - INITIAL_LOCKED_LP, 0n),
|
|
98
|
+
lockedLp: min(lpTotal, INITIAL_LOCKED_LP),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* LP minted by a deposit into a pool that already has liquidity.
|
|
103
|
+
*
|
|
104
|
+
* The smaller of the two sides decides — supplying more of one asset than the
|
|
105
|
+
* ratio calls for mints nothing extra for the excess.
|
|
106
|
+
*/
|
|
107
|
+
export function depositLp(args) {
|
|
108
|
+
if (args.lpTotal <= 0n)
|
|
109
|
+
throw new RangeError('use firstDepositLp for an empty pool');
|
|
110
|
+
if (args.reserveX <= 0n || args.reserveY <= 0n)
|
|
111
|
+
throw new RangeError('pool has no reserves');
|
|
112
|
+
const fromX = mulDivFloor(args.x, args.lpTotal, args.reserveX);
|
|
113
|
+
const fromY = mulDivFloor(args.y, args.lpTotal, args.reserveY);
|
|
114
|
+
return min(fromX, fromY);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The amounts of each side that match a given amount of the other, at the
|
|
118
|
+
* pool's current ratio. Use it to fill in the second field of a deposit form.
|
|
119
|
+
*/
|
|
120
|
+
export function matchingDeposit(args) {
|
|
121
|
+
if (args.reserveX <= 0n || args.reserveY <= 0n)
|
|
122
|
+
throw new RangeError('pool has no reserves');
|
|
123
|
+
if (args.x !== undefined) {
|
|
124
|
+
return { x: args.x, y: mulDivFloor(args.x, args.reserveY, args.reserveX) };
|
|
125
|
+
}
|
|
126
|
+
if (args.y !== undefined) {
|
|
127
|
+
return { x: mulDivFloor(args.y, args.reserveX, args.reserveY), y: args.y };
|
|
128
|
+
}
|
|
129
|
+
throw new TypeError('give either x or y');
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* What withdrawing `lp` returns, fees included.
|
|
133
|
+
*
|
|
134
|
+
* Fees are tracked as a per-share accumulator: the position stores the value
|
|
135
|
+
* of the accumulator when it last settled, and is owed the growth since.
|
|
136
|
+
*/
|
|
137
|
+
export function withdrawAmounts(args) {
|
|
138
|
+
if (args.lp <= 0n)
|
|
139
|
+
throw new RangeError('withdraw amount must be positive');
|
|
140
|
+
if (args.lp > args.lpTotal)
|
|
141
|
+
throw new RangeError('cannot withdraw more LP than exists');
|
|
142
|
+
const deltaX = max(args.feesLp.x - args.checkpoint.x, 0n);
|
|
143
|
+
const deltaY = max(args.feesLp.y - args.checkpoint.y, 0n);
|
|
144
|
+
const fees = {
|
|
145
|
+
x: mulDivFloor(args.lp, deltaX, SCALE),
|
|
146
|
+
y: mulDivFloor(args.lp, deltaY, SCALE),
|
|
147
|
+
};
|
|
148
|
+
const principal = {
|
|
149
|
+
x: mulDivFloor(args.reserveX, args.lp, args.lpTotal),
|
|
150
|
+
y: mulDivFloor(args.reserveY, args.lp, args.lpTotal),
|
|
151
|
+
};
|
|
152
|
+
return {
|
|
153
|
+
principal,
|
|
154
|
+
fees,
|
|
155
|
+
total: { x: principal.x + fees.x, y: principal.y + fees.y },
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/** Fees owed to a position right now, without touching its liquidity. */
|
|
159
|
+
export function pendingFees(args) {
|
|
160
|
+
return {
|
|
161
|
+
x: mulDivFloor(args.lp, max(args.feesLp.x - args.checkpoint.x, 0n), SCALE),
|
|
162
|
+
y: mulDivFloor(args.lp, max(args.feesLp.y - args.checkpoint.y, 0n), SCALE),
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* How much the pool-wide fee accumulator grows when a swap collects `lpFee`.
|
|
167
|
+
*
|
|
168
|
+
* Mirrors the update the pool makes on every swap.
|
|
169
|
+
*/
|
|
170
|
+
export function feeShareGrowth(lpFee, lpTotal) {
|
|
171
|
+
if (lpTotal <= 0n)
|
|
172
|
+
throw new RangeError('no liquidity to distribute fees to');
|
|
173
|
+
return mulDivFloor(lpFee, SCALE, lpTotal);
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=cpmm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cpmm.js","sourceRoot":"","sources":["../../src/math/cpmm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACjD,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAE3D;;;;;GAKG;AAEH,gCAAgC;AAChC,MAAM,CAAC,MAAM,WAAW,GAAG,UAAU,CAAC,MAAM,EAAE,aAAa,CAAC,CAAA;AAC5D,+DAA+D;AAC/D,MAAM,CAAC,MAAM,KAAK,GAAG,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AAChD,MAAM,CAAC,MAAM,SAAS,GAAG,UAAU,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;AAExD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,CAAC,MAAO,GAAG,MAAO,CAAC,CAAA;AAYzD;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAAC,IAM1B;IACC,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,IAAI,CAAA;IAChD,IAAI,QAAQ,IAAI,EAAE;QAAE,MAAM,IAAI,UAAU,CAAC,2BAA2B,CAAC,CAAA;IACrE,IAAI,SAAS,IAAI,EAAE,IA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E,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,KAAK,CAAC;QAC1E,CAAC,EAAE,WAAW,CAAC,IAAI,CAAC,EAAE,EAAE,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,KAAK,CAAC;KAC3E,CAAA;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,KAAa,EAAE,OAAe;IAC3D,IAAI,OAAO,IAAI,EAAE;QAAE,MAAM,IAAI,UAAU,CAAC,oCAAoC,CAAC,CAAA;IAC7E,OAAO,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,CAAA;AAC3C,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Integer arithmetic matching the on-chain math bit for bit. */
|
|
2
|
+
/** On-chain `mulDivFloor`: full-precision multiply, then floor division. */
|
|
3
|
+
export declare function mulDivFloor(a: bigint, b: bigint, denominator: bigint): bigint;
|
|
4
|
+
/** Like {@link mulDivFloor}, but rounds up. */
|
|
5
|
+
export declare function mulDivCeil(a: bigint, b: bigint, denominator: bigint): bigint;
|
|
6
|
+
/** Integer square root: the largest `r` such that `r² ≤ value`. */
|
|
7
|
+
export declare function isqrt(value: bigint): bigint;
|
|
8
|
+
export declare function min(a: bigint, b: bigint): bigint;
|
|
9
|
+
export declare function max(a: bigint, b: bigint): bigint;
|
|
10
|
+
//# sourceMappingURL=integer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"integer.d.ts","sourceRoot":"","sources":["../../src/math/integer.ts"],"names":[],"mappings":"AAAA,iEAAiE;AAEjE,4EAA4E;AAC5E,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,CAG7E;AAED,+CAA+C;AAC/C,wBAAgB,UAAU,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,CAI5E;AAED,mEAAmE;AACnE,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAmB3C;AAED,wBAAgB,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED,wBAAgB,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAEhD"}
|