@sundaeswap/math 0.0.0-dev-20251113094713 → 0.0.0-dev-20251113101922
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/dist/cjs/PoolMath/StableSwapsPool.js +165 -43
- package/dist/cjs/PoolMath/StableSwapsPool.js.map +1 -1
- package/dist/esm/PoolMath/StableSwapsPool.js +166 -43
- package/dist/esm/PoolMath/StableSwapsPool.js.map +1 -1
- package/dist/types/PoolMath/StableSwapsPool.d.ts +169 -45
- package/dist/types/PoolMath/StableSwapsPool.d.ts.map +1 -1
- package/dist/types/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/PoolMath/StableSwapsPool.ts +169 -43
|
@@ -11,6 +11,20 @@ exports.getTokensForLp = exports.getSwapOutput = exports.getSwapInput = void 0;
|
|
|
11
11
|
exports.liquidityInvariant = liquidityInvariant;
|
|
12
12
|
var _index = require("./index.js");
|
|
13
13
|
var _fraction = require("@sundaeswap/fraction");
|
|
14
|
+
/**
|
|
15
|
+
* Validates that a given sum invariant (D) satisfies the liquidity invariant equation
|
|
16
|
+
* for the Stableswaps curve. This function checks if D is within the valid range by
|
|
17
|
+
* evaluating the invariant function at D and D+1.
|
|
18
|
+
*
|
|
19
|
+
* The liquidity invariant ensures that the curve equation is satisfied:
|
|
20
|
+
* 4A * 4xy * D + D^3 - (4xy * 4A * (x + y) + 4xy * D) should be <= 0 at D and > 0 at D+1
|
|
21
|
+
*
|
|
22
|
+
* @param {bigint} assetA - The reserve amount of asset A (already scaled by RESERVE_PRECISION).
|
|
23
|
+
* @param {bigint} assetB - The reserve amount of asset B (already scaled by RESERVE_PRECISION).
|
|
24
|
+
* @param {bigint} linearAmplification - The amplification coefficient for the pool.
|
|
25
|
+
* @param {bigint} newSumInvariant - The proposed sum invariant (D) to validate.
|
|
26
|
+
* @returns {boolean} True if the sum invariant satisfies the liquidity invariant equation, false otherwise.
|
|
27
|
+
*/
|
|
14
28
|
function liquidityInvariant(assetA, assetB, linearAmplification, newSumInvariant) {
|
|
15
29
|
// 4 * A * 4 * (x*y) * D + D^3 - (4(x*y) * (4A(x + y) + D))
|
|
16
30
|
// 4A * 4xy * D + D^3 - (4xy * 4a * (x + y) + 4xy * D)
|
|
@@ -27,9 +41,44 @@ function liquidityInvariant(assetA, assetB, linearAmplification, newSumInvariant
|
|
|
27
41
|
var f2 = sixteen_a_x_y * d_plus_one + d_plus_one_cubed - (sixteen_a_x_y_x_plus_y + four_x_y * d_plus_one);
|
|
28
42
|
return f1 <= 0n && f2 > 0n;
|
|
29
43
|
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Precision factor for the amplification coefficient calculations.
|
|
47
|
+
* Used to scale the amplification factor for more precise arithmetic operations.
|
|
48
|
+
*/
|
|
30
49
|
var A_PRECISION = exports.A_PRECISION = 200n;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Precision factor for reserve calculations in the Stableswaps curve.
|
|
53
|
+
* Reserves are multiplied by this value to maintain precision during calculations.
|
|
54
|
+
* This allows for accurate fractional calculations with integer arithmetic.
|
|
55
|
+
*/
|
|
31
56
|
var RESERVE_PRECISION = exports.RESERVE_PRECISION = 1000000000000n;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Precision factor for fee calculations, representing basis points.
|
|
60
|
+
* A value of 10,000 means fees are calculated with 0.01% precision (1 basis point).
|
|
61
|
+
*/
|
|
32
62
|
var FEE_PRECISION = exports.FEE_PRECISION = 10000n;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Calculates the sum invariant (D) for a Stableswaps pool using Newton's method.
|
|
66
|
+
* The sum invariant represents the total value in the pool and is used to determine
|
|
67
|
+
* the fair exchange rate between assets.
|
|
68
|
+
*
|
|
69
|
+
* This function iteratively solves for D in the Stableswaps invariant equation:
|
|
70
|
+
* Ann * sum + D_p * 2 = Ann * D + D_p * 3
|
|
71
|
+
* where D_p = D^3 / (4 * x * y) and Ann = A * n^n (with n=2)
|
|
72
|
+
*
|
|
73
|
+
* The iteration continues until convergence (difference <= 1) or fails after 255 iterations.
|
|
74
|
+
*
|
|
75
|
+
* @param {bigint} a - The amplification coefficient for the pool (will be scaled by A_PRECISION).
|
|
76
|
+
* @param {bigint} x - The reserve amount of the first asset (will be scaled by RESERVE_PRECISION).
|
|
77
|
+
* @param {bigint} y - The reserve amount of the second asset (will be scaled by RESERVE_PRECISION).
|
|
78
|
+
* @returns {bigint} The calculated sum invariant (D) scaled by RESERVE_PRECISION.
|
|
79
|
+
* @throws {Error} If the amplification coefficient is not positive.
|
|
80
|
+
* @throws {Error} If convergence fails after 255 iterations.
|
|
81
|
+
*/
|
|
33
82
|
function getSumInvariant(a, x, y) {
|
|
34
83
|
if (a <= 0n) {
|
|
35
84
|
throw new Error("Amplification coefficient must be positive.");
|
|
@@ -67,6 +116,23 @@ function getSumInvariant(a, x, y) {
|
|
|
67
116
|
}
|
|
68
117
|
throw new Error("Failed to converge on D value after 255 iterations.");
|
|
69
118
|
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Calculates the new Y reserve value given a new X reserve value and the sum invariant.
|
|
122
|
+
* This function uses Newton's method to iteratively solve for Y in the Stableswaps equation
|
|
123
|
+
* while maintaining the pool's invariant (D).
|
|
124
|
+
*
|
|
125
|
+
* The function solves: Y^2 + (sum + (D * A_PRECISION) / (2 * Ann) - D) * Y - c = 0
|
|
126
|
+
* where c is derived from D and the pool parameters.
|
|
127
|
+
*
|
|
128
|
+
* This is used during swaps to determine the output reserve after an input is added.
|
|
129
|
+
*
|
|
130
|
+
* @param {bigint} newX - The new reserve amount for asset X (will be scaled by RESERVE_PRECISION internally).
|
|
131
|
+
* @param {bigint} aRaw - The raw amplification coefficient for the pool (will be scaled by A_PRECISION).
|
|
132
|
+
* @param {bigint} sumInvariant - The pool's sum invariant (D), already scaled.
|
|
133
|
+
* @returns {bigint} The calculated Y reserve value scaled by RESERVE_PRECISION.
|
|
134
|
+
* @throws {Error} If convergence fails after 255 iterations.
|
|
135
|
+
*/
|
|
70
136
|
function getNewY(newX, aRaw, sumInvariant) {
|
|
71
137
|
newX = newX * RESERVE_PRECISION;
|
|
72
138
|
var sum = newX;
|
|
@@ -103,32 +169,46 @@ function getNewY(newX, aRaw, sumInvariant) {
|
|
|
103
169
|
}
|
|
104
170
|
|
|
105
171
|
/**
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
172
|
+
* Calculates the initial LP token amount for the first liquidity provision to a pool.
|
|
173
|
+
* This is used when bootstrapping a new pool with initial reserves of assets A and B.
|
|
174
|
+
*
|
|
175
|
+
* The LP tokens minted are derived from the sum invariant (D) divided by the reserve precision,
|
|
176
|
+
* effectively representing the pool's total value at inception.
|
|
177
|
+
*
|
|
178
|
+
* @param {bigint} a - The initial amount of token A being deposited.
|
|
179
|
+
* @param {bigint} b - The initial amount of token B being deposited.
|
|
180
|
+
* @param {bigint} laf - The linear amplification factor for the pool.
|
|
181
|
+
* @returns {bigint} The amount of LP tokens to mint for the first liquidity provision.
|
|
110
182
|
*/
|
|
111
183
|
var getFirstLp = exports.getFirstLp = function getFirstLp(a, b, laf) {
|
|
112
184
|
return getSumInvariant(laf, a, b) / RESERVE_PRECISION;
|
|
113
185
|
};
|
|
114
186
|
|
|
115
187
|
/**
|
|
116
|
-
*
|
|
188
|
+
* Calculates the liquidity parameters for adding liquidity to a Stableswaps pool.
|
|
189
|
+
* This function supports mixed deposits where both assets can be deposited in any proportion.
|
|
190
|
+
*
|
|
191
|
+
* The calculation is based on the change in the sum invariant (D) before and after the deposit.
|
|
192
|
+
* LP tokens are minted proportionally to the increase in D relative to the old D value.
|
|
117
193
|
*
|
|
118
|
-
* TODO: Investigate way to optimize
|
|
194
|
+
* TODO: Investigate way to optimize actual deposited amounts
|
|
119
195
|
*
|
|
120
196
|
* @param {bigint} a - The amount of token A to deposit.
|
|
121
197
|
* @param {bigint} b - The amount of token B to deposit.
|
|
122
198
|
* @param {bigint} aReserve - The current reserve of token A in the pool.
|
|
123
199
|
* @param {bigint} bReserve - The current reserve of token B in the pool.
|
|
124
|
-
* @param {bigint} totalLp - The total
|
|
125
|
-
* @param {bigint} laf - The linear
|
|
126
|
-
* @throws {Error} If the pool doesn't have enough liquidity.
|
|
127
|
-
* @throws {Error} If
|
|
200
|
+
* @param {bigint} totalLp - The total LP tokens for the pool before the deposit.
|
|
201
|
+
* @param {bigint} laf - The linear amplification factor of the pool.
|
|
202
|
+
* @throws {Error} If the pool doesn't have enough liquidity (both reserves are zero).
|
|
203
|
+
* @throws {Error} If both deposit amounts are zero.
|
|
128
204
|
* @returns {Object} An object containing:
|
|
129
|
-
* - nextTotalLp: The total
|
|
130
|
-
* - generatedLp: The amount of
|
|
131
|
-
* - shareAfterDeposit: The depositor's share of the pool after the deposit.
|
|
205
|
+
* - nextTotalLp: The total LP tokens for the pool after the deposit.
|
|
206
|
+
* - generatedLp: The amount of LP tokens generated by the deposit.
|
|
207
|
+
* - shareAfterDeposit: The depositor's share of the pool after the deposit (as a Fraction).
|
|
208
|
+
* - aChange: Always 0n for Stableswaps (no refund needed).
|
|
209
|
+
* - bChange: Always 0n for Stableswaps (no refund needed).
|
|
210
|
+
* - actualDepositedA: The actual amount of A deposited (always equal to input a).
|
|
211
|
+
* - actualDepositedB: The actual amount of B deposited (always equal to input b).
|
|
132
212
|
*/
|
|
133
213
|
var calculateLiquidity = exports.calculateLiquidity = function calculateLiquidity(a, b, aReserve, bReserve, totalLp, laf) {
|
|
134
214
|
if (aReserve === 0n && bReserve === 0n) {
|
|
@@ -153,34 +233,52 @@ var calculateLiquidity = exports.calculateLiquidity = function calculateLiquidit
|
|
|
153
233
|
};
|
|
154
234
|
|
|
155
235
|
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
236
|
+
* Calculates the token amounts that a given LP token amount represents when withdrawing
|
|
237
|
+
* from a Stableswaps pool. The withdrawal is proportional to the LP tokens being redeemed
|
|
238
|
+
* relative to the total LP supply.
|
|
239
|
+
*
|
|
240
|
+
* This uses a simple linear proportion: amount = (lp * reserve) / totalLp for each asset.
|
|
241
|
+
*
|
|
242
|
+
* @param {bigint} lp - The amount of LP tokens being redeemed.
|
|
243
|
+
* @param {bigint} aReserve - The pool's current reserve amount of token A.
|
|
244
|
+
* @param {bigint} bReserve - The pool's current reserve amount of token B.
|
|
245
|
+
* @param {bigint} totalLp - The pool's total minted LP tokens currently in circulation.
|
|
246
|
+
* @returns {SharedPoolMath.TPair} A tuple [a, b] containing the amounts of tokens A and B to withdraw.
|
|
162
247
|
*/
|
|
163
248
|
var getTokensForLp = exports.getTokensForLp = function getTokensForLp(lp, aReserve, bReserve, totalLp) {
|
|
164
249
|
return [new _fraction.Fraction(lp * aReserve, totalLp).quotient, new _fraction.Fraction(lp * bReserve, totalLp).quotient];
|
|
165
250
|
};
|
|
166
251
|
|
|
167
252
|
/**
|
|
168
|
-
*
|
|
253
|
+
* Represents the complete outcome of a swap operation in a Stableswaps pool.
|
|
254
|
+
* Contains all relevant information about the swap including amounts, fees, updated reserves,
|
|
255
|
+
* and price impact.
|
|
169
256
|
*/
|
|
170
257
|
|
|
171
258
|
/**
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
259
|
+
* Calculates the output amount and all swap parameters for a given input amount in a Stableswaps pool.
|
|
260
|
+
* This function determines how many output tokens will be received for a given input, accounting for
|
|
261
|
+
* the Stableswaps curve, LP fees, and protocol fees.
|
|
262
|
+
*
|
|
263
|
+
* The calculation process:
|
|
264
|
+
* 1. Validates inputs and fees
|
|
265
|
+
* 2. Calculates the current sum invariant (D)
|
|
266
|
+
* 3. Determines the new Y value after adding input to X
|
|
267
|
+
* 4. Calculates raw output and applies fees (LP + protocol)
|
|
268
|
+
* 5. Computes price impact by comparing ideal vs actual execution price
|
|
176
269
|
*
|
|
177
|
-
* @param input The
|
|
178
|
-
* @param inputReserve The amount of
|
|
179
|
-
* @param outputReserve The amount of
|
|
180
|
-
* @param fee The liquidity provider fee
|
|
181
|
-
* @param protocolFee The protocol fee
|
|
182
|
-
* @param laf The linear amplification factor
|
|
183
|
-
* @
|
|
270
|
+
* @param {bigint} input - The amount of input tokens to swap.
|
|
271
|
+
* @param {bigint} inputReserve - The current reserve amount of the input token in the pool.
|
|
272
|
+
* @param {bigint} outputReserve - The current reserve amount of the output token in the pool.
|
|
273
|
+
* @param {TFractionLike} fee - The liquidity provider fee as a fraction (e.g., 0.003 for 0.3%).
|
|
274
|
+
* @param {TFractionLike} protocolFee - The protocol fee as a fraction.
|
|
275
|
+
* @param {bigint} laf - The linear amplification factor of the pool.
|
|
276
|
+
* @param {boolean} [roundOutputUp] - Optional flag to round output up instead of down (default: false).
|
|
277
|
+
* @returns {TSwapOutcome} The complete swap outcome including output, fees, reserves, and price impact.
|
|
278
|
+
* @throws {Error} If input or reserves are not positive.
|
|
279
|
+
* @throws {Error} If fee is not in the range [0, 1).
|
|
280
|
+
* @throws {Error} If protocol fee is not in the range [0, 1).
|
|
281
|
+
* @throws {Error} If combined fee is greater than or equal to 1.
|
|
184
282
|
*/
|
|
185
283
|
var getSwapOutput = exports.getSwapOutput = function getSwapOutput(input, inputReserve, outputReserve, fee, protocolFee, laf, roundOutputUp) {
|
|
186
284
|
if (input <= 0 || inputReserve <= 0 || outputReserve <= 0) throw new Error("Input and reserves must be positive");
|
|
@@ -222,6 +320,20 @@ var getSwapOutput = exports.getSwapOutput = function getSwapOutput(input, inputR
|
|
|
222
320
|
priceImpact: priceImpact
|
|
223
321
|
};
|
|
224
322
|
};
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Calculates the current price (exchange rate) of asset A in terms of asset B for a Stableswaps pool.
|
|
326
|
+
* The price is derived from the Stableswaps curve equation and takes into account the amplification factor.
|
|
327
|
+
*
|
|
328
|
+
* The calculation uses the formula:
|
|
329
|
+
* price = (xpA + (dR * aReserve) / bReserve) / (xpA + dR)
|
|
330
|
+
* where xpA = (Ann * aReserve) / A_PRECISION and dR is derived from the sum invariant
|
|
331
|
+
*
|
|
332
|
+
* @param {bigint} aReserve - The current reserve amount of asset A in the pool.
|
|
333
|
+
* @param {bigint} bReserve - The current reserve amount of asset B in the pool.
|
|
334
|
+
* @param {bigint} laf - The linear amplification factor of the pool.
|
|
335
|
+
* @returns {Fraction} The price of asset A in terms of asset B as a Fraction.
|
|
336
|
+
*/
|
|
225
337
|
function getPrice(aReserve, bReserve, laf) {
|
|
226
338
|
// dx_0 / dx_1 only, however can have any number of coins in pool
|
|
227
339
|
var ann = laf * 2n * A_PRECISION;
|
|
@@ -236,19 +348,29 @@ function getPrice(aReserve, bReserve, laf) {
|
|
|
236
348
|
}
|
|
237
349
|
|
|
238
350
|
/**
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
351
|
+
* Calculates the required input amount and all swap parameters for a desired output amount in a Stableswaps pool.
|
|
352
|
+
* This is the reverse operation of getSwapOutput - given how much you want to receive, it determines
|
|
353
|
+
* how much you need to provide as input, accounting for fees and the Stableswaps curve.
|
|
354
|
+
*
|
|
355
|
+
* The calculation process:
|
|
356
|
+
* 1. Validates inputs, fees, and output constraints
|
|
357
|
+
* 2. Calculates raw output needed before fees
|
|
358
|
+
* 3. Determines the new X value to achieve the desired Y reduction
|
|
359
|
+
* 4. Calculates required input and separates LP and protocol fees
|
|
360
|
+
* 5. Computes price impact by comparing ideal vs actual execution price
|
|
244
361
|
*
|
|
245
|
-
* @param output
|
|
246
|
-
* @param inputReserve
|
|
247
|
-
* @param outputReserve
|
|
248
|
-
* @param fee
|
|
249
|
-
* @param protocolFee The protocol fee
|
|
250
|
-
* @param laf The linear amplification factor
|
|
251
|
-
* @returns The swap
|
|
362
|
+
* @param {bigint} output - The desired amount of output tokens to receive.
|
|
363
|
+
* @param {bigint} inputReserve - The current reserve amount of the input token in the pool.
|
|
364
|
+
* @param {bigint} outputReserve - The current reserve amount of the output token in the pool.
|
|
365
|
+
* @param {TFractionLike} fee - The liquidity provider fee as a fraction (e.g., 0.003 for 0.3%).
|
|
366
|
+
* @param {TFractionLike} protocolFee - The protocol fee as a fraction.
|
|
367
|
+
* @param {bigint} laf - The linear amplification factor of the pool.
|
|
368
|
+
* @returns {TSwapOutcome} The complete swap outcome including required input, fees, reserves, and price impact.
|
|
369
|
+
* @throws {Error} If output or reserves are not positive.
|
|
370
|
+
* @throws {Error} If output is greater than or equal to the output reserve.
|
|
371
|
+
* @throws {Error} If fee is not in the range [0, 1).
|
|
372
|
+
* @throws {Error} If protocol fee is not in the range [0, 1).
|
|
373
|
+
* @throws {Error} If combined fee is greater than or equal to 1.
|
|
252
374
|
*/
|
|
253
375
|
var getSwapInput = exports.getSwapInput = function getSwapInput(output, inputReserve, outputReserve, fee, protocolFee, laf) {
|
|
254
376
|
if (output <= 0 || inputReserve <= 0 || outputReserve <= 0) throw new Error("Output and reserves must be positive");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"StableSwapsPool.js","names":["_index","require","_fraction","liquidityInvariant","assetA","assetB","linearAmplification","newSumInvariant","four_a","four_x_y","d_plus_one","d_cubed","d_plus_one_cubed","x_plus_y","sixteen_a_x_y","sixteen_a_x_y_x_plus_y","f1","f2","A_PRECISION","exports","RESERVE_PRECISION","FEE_PRECISION","getSumInvariant","a","x","y","Error","sum","d","ann","i","d_p","d_prev","getNewY","newX","aRaw","sumInvariant","yPrev","c","b","getFirstLp","laf","calculateLiquidity","aReserve","bReserve","totalLp","oldSumInvariant","newLpTokens","newTotalLpTokens","nextTotalLp","generatedLp","shareAfterDeposit","SharedPoolMath","getShare","aChange","bChange","actualDepositedA","actualDepositedB","getTokensForLp","lp","Fraction","quotient","getSwapOutput","input","inputReserve","outputReserve","fee","protocolFee","roundOutputUp","asFraction","lt","ZERO","gte","ONE","combinedFee","add","nextInputReserve","newY","deltaYPrec","deltaY","totalFee","multiply","divide","totalProtocolFee","totalLpFee","output","subtract","safeOutput","numerator","denominator","nextOutputReserve","idealPrice","getPrice","actualPrice","priceImpact","outputLpFee","outputProtocolFee","nextSumInvariant","dR","xpA","p","getSwapInput","rawOutputF","sub","rawOutput"],"sources":["../../../src/PoolMath/StableSwapsPool.ts"],"sourcesContent":["import { SharedPoolMath } from \"./index.js\";\nimport { Fraction, type TFractionLike } from \"@sundaeswap/fraction\";\n\nexport function liquidityInvariant(\n assetA: bigint,\n assetB: bigint,\n linearAmplification: bigint,\n newSumInvariant: bigint,\n) {\n // 4 * A * 4 * (x*y) * D + D^3 - (4(x*y) * (4A(x + y) + D))\n // 4A * 4xy * D + D^3 - (4xy * 4a * (x + y) + 4xy * D)\n // 16Axy * D + D^3 - (16Axy * (x + y) + 4xy * D)\n const four_a = 4n * linearAmplification;\n const four_x_y = 4n * assetA * assetB;\n const d_plus_one = newSumInvariant + 1n;\n const d_cubed = newSumInvariant * newSumInvariant * newSumInvariant;\n const d_plus_one_cubed = d_plus_one * d_plus_one * d_plus_one;\n const x_plus_y = assetA + assetB;\n const sixteen_a_x_y = four_a * four_x_y;\n const sixteen_a_x_y_x_plus_y = sixteen_a_x_y * x_plus_y;\n const f1 =\n sixteen_a_x_y * newSumInvariant +\n d_cubed -\n (sixteen_a_x_y_x_plus_y + four_x_y * newSumInvariant);\n const f2 =\n sixteen_a_x_y * d_plus_one +\n d_plus_one_cubed -\n (sixteen_a_x_y_x_plus_y + four_x_y * d_plus_one);\n return f1 <= 0n && f2 > 0n;\n}\n\nexport const A_PRECISION: bigint = 200n;\nexport const RESERVE_PRECISION: bigint = 1_000_000_000_000n;\nexport const FEE_PRECISION: bigint = 10_000n;\n\nexport function getSumInvariant(a: bigint, x: bigint, y: bigint): bigint {\n if (a <= 0n) {\n throw new Error(\"Amplification coefficient must be positive.\");\n }\n x = x * RESERVE_PRECISION;\n y = y * RESERVE_PRECISION;\n a = a * A_PRECISION;\n const sum: bigint = x + y;\n if (sum === 0n) {\n return 0n;\n }\n\n let d = sum;\n const ann = a * 2n;\n for (let i = 0; i < 255; i++) {\n const d_p = (d * d * d) / (4n * x * y);\n const d_prev = d;\n\n d =\n (((ann * sum) / 100n + d_p * 2n) * d) /\n (((ann - 100n) * d) / 100n + 3n * d_p);\n\n if (d > d_prev) {\n if (d - d_prev <= 1) {\n if (liquidityInvariant(x, y, a / A_PRECISION, d)) {\n return d;\n } else {\n return d - 1n;\n }\n }\n } else {\n if (d_prev - d <= 1) {\n if (liquidityInvariant(x, y, a / A_PRECISION, d)) {\n return d;\n } else {\n return d - 1n;\n }\n }\n }\n }\n throw new Error(\"Failed to converge on D value after 255 iterations.\");\n}\n\nexport function getNewY(\n newX: bigint,\n aRaw: bigint,\n sumInvariant: bigint,\n): bigint {\n newX = newX * RESERVE_PRECISION;\n const sum = newX;\n let yPrev: bigint;\n let c = sumInvariant;\n const ann = aRaw * A_PRECISION * 2n;\n\n c = (c * sumInvariant) / (newX * 2n);\n c = (c * sumInvariant * A_PRECISION) / 2n;\n c = c / (ann * 2n);\n const b = sum + (sumInvariant * A_PRECISION) / 2n / ann;\n let y = sumInvariant;\n for (let i = 0; i < 255; i++) {\n yPrev = y;\n y = (y * y + c) / (y * 2n + b - sumInvariant);\n if (y > yPrev) {\n if (y - yPrev <= 1n) {\n if (liquidityInvariant(newX, y, aRaw, sumInvariant)) {\n return y;\n } else {\n return y + 1n;\n }\n }\n } else {\n if (yPrev - y <= 1n) {\n if (liquidityInvariant(newX, y, aRaw, sumInvariant)) {\n return y;\n } else {\n return y + 1n;\n }\n }\n }\n }\n throw new Error(\"failed to converge on y value after 255 iterations.\");\n}\n\n/**\n * Get the lp token amount for a, b\n * @param a tokenA amount\n * @param b tokenB amount\n * @returns the minted lp token amount\n */\nexport const getFirstLp = (a: bigint, b: bigint, laf: bigint) =>\n getSumInvariant(laf, a, b) / RESERVE_PRECISION;\n\n/**\n * Calculate the Add (Mixed-Deposit) Liquidity parameters\n *\n * TODO: Investigate way to optimize actualdeposited amounts\n *\n * @param {bigint} a - The amount of token A to deposit.\n * @param {bigint} b - The amount of token B to deposit.\n * @param {bigint} aReserve - The current reserve of token A in the pool.\n * @param {bigint} bReserve - The current reserve of token B in the pool.\n * @param {bigint} totalLp - The total lp tokens for the pool before the deposit.\n * @param {bigint} laf - The linear amplifciation factor of the pool.\n * @throws {Error} If the pool doesn't have enough liquidity.\n * @throws {Error} If either of the deposit amounts is zero.\n * @returns {Object} An object containing:\n * - nextTotalLp: The total lp tokens for the pool after the deposit.\n * - generatedLp: The amount of lp tokens generated by the deposit.\n * - shareAfterDeposit: The depositor's share of the pool after the deposit.\n */\nexport const calculateLiquidity = (\n a: bigint,\n b: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n laf: bigint,\n) => {\n if (aReserve === 0n && bReserve === 0n) {\n throw new Error(\"Not enough pool liquidity\");\n }\n\n if (a === 0n && b === 0n) {\n throw new Error(\"Cannot use a deposit asset amount of 0\");\n }\n\n const oldSumInvariant = getSumInvariant(laf, aReserve, bReserve);\n\n const newSumInvariant = getSumInvariant(laf, aReserve + a, bReserve + b);\n\n const newLpTokens =\n ((newSumInvariant - oldSumInvariant) * totalLp) / oldSumInvariant;\n\n const newTotalLpTokens = totalLp + newLpTokens;\n\n return {\n nextTotalLp: newTotalLpTokens,\n generatedLp: newLpTokens,\n shareAfterDeposit: SharedPoolMath.getShare(newLpTokens, newTotalLpTokens),\n aChange: 0n,\n bChange: 0n,\n actualDepositedA: a,\n actualDepositedB: b,\n };\n};\n\n/**\n * Get the token amounts the given lp represents\n * @param lp the lp amount\n * @param aReserve the pool's reserveA amount\n * @param bReserve the pool's reserveB amount\n * @param totalLp the pool's total minted lp currently\n * @returns [a, b] token amounts\n */\nexport const getTokensForLp = (\n lp: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n): SharedPoolMath.TPair => [\n new Fraction(lp * aReserve, totalLp).quotient,\n new Fraction(lp * bReserve, totalLp).quotient,\n];\n\n/**\n * Holds the calculated outcome of a swap\n */\nexport type TSwapOutcome = {\n input: bigint;\n output: bigint;\n outputLpFee: bigint;\n outputProtocolFee: bigint;\n nextInputReserve: bigint;\n nextOutputReserve: bigint;\n nextSumInvariant: bigint;\n priceImpact: Fraction;\n};\n\n/**\n * Calculate swap outcome for a given input and pool parameters (input tokens, output tokens, fee).\n * Throws if\n * - any of the arguments are negative\n * - fee is greater than or equal 1\n *\n * @param input The given amount of tokens to be swapped\n * @param inputReserve The amount of tokens in the input reserve\n * @param outputReserve The amount of tokens in the output reserve\n * @param fee The liquidity provider fee\n * @param protocolFee The protocol fee\n * @param laf The linear amplification factor\n * @returns The swap details\n */\nexport const getSwapOutput = (\n input: bigint,\n inputReserve: bigint,\n outputReserve: bigint,\n fee: TFractionLike,\n protocolFee: TFractionLike,\n laf: bigint,\n roundOutputUp?: boolean,\n): TSwapOutcome => {\n if (input <= 0 || inputReserve <= 0 || outputReserve <= 0)\n throw new Error(\"Input and reserves must be positive\");\n\n fee = Fraction.asFraction(fee);\n if (fee.lt(Fraction.ZERO) || fee.gte(Fraction.ONE))\n throw new Error(\"fee must be between 0 and 1\");\n\n protocolFee = Fraction.asFraction(protocolFee);\n if (protocolFee.lt(Fraction.ZERO) || protocolFee.gte(Fraction.ONE))\n throw new Error(\"protocolFee must be between 0 and 1\");\n\n const combinedFee = fee.add(protocolFee);\n if (combinedFee.gte(Fraction.ONE))\n throw new Error(\"Combined fee must be less than 1\");\n const sumInvariant = getSumInvariant(laf, inputReserve, outputReserve);\n const nextInputReserve = inputReserve + input;\n const newY = getNewY(nextInputReserve, laf, sumInvariant);\n\n const deltaYPrec = outputReserve * RESERVE_PRECISION - newY;\n const deltaY = new Fraction(deltaYPrec, RESERVE_PRECISION);\n const totalFee = combinedFee\n .multiply(FEE_PRECISION)\n .multiply(deltaY.quotient)\n .add(Fraction.asFraction(FEE_PRECISION - 1n))\n .divide(FEE_PRECISION).quotient;\n const totalProtocolFee = protocolFee\n .multiply(totalFee)\n .divide(combinedFee).quotient;\n const totalLpFee = totalFee - totalProtocolFee;\n const output = deltaY.subtract(totalFee);\n const safeOutput = roundOutputUp\n ? (output.numerator + output.denominator - 1n) / output.denominator\n : output.quotient;\n const nextOutputReserve = outputReserve - safeOutput - totalProtocolFee;\n\n // PRICEIMPACT: \"priceImpact\" is slightly misleadingly named in the industry as a whole\n // just by it's name, it would imply that it's the percentage difference between\n // the current price and the price after the swap, but it's actually the percentage\n // difference between the real price of your swap, and the price implied by the current\n // reserves; We got this wrong in v1, but this aligns it with the industry standard\n // Source: https://dailydefi.org/articles/price-impact-and-how-to-calculate/\n const idealPrice = getPrice(inputReserve, outputReserve, laf);\n const actualPrice = new Fraction(input, safeOutput);\n const priceImpact = Fraction.ONE.subtract(idealPrice.divide(actualPrice));\n return {\n input,\n output: safeOutput,\n outputLpFee: totalLpFee,\n outputProtocolFee: totalProtocolFee,\n nextInputReserve,\n nextOutputReserve,\n nextSumInvariant: getSumInvariant(laf, nextInputReserve, nextOutputReserve),\n priceImpact,\n };\n};\n\nexport function getPrice(\n aReserve: bigint,\n bReserve: bigint,\n laf: bigint,\n): Fraction {\n // dx_0 / dx_1 only, however can have any number of coins in pool\n const ann = laf * 2n * A_PRECISION;\n const sumInvariant =\n getSumInvariant(laf, aReserve, bReserve) / RESERVE_PRECISION;\n // D / n^n with n = 2\n let dR = sumInvariant / 4n;\n dR = (dR * sumInvariant) / aReserve;\n dR = (dR * sumInvariant) / bReserve;\n const xpA = (ann * aReserve) / A_PRECISION;\n const p = new Fraction(xpA + (dR * aReserve) / bReserve, xpA + dR);\n return p;\n}\n\n/**\n * Calculate input required for a swap outcome for a given output and pool parameters (input tokens, output tokens, fee).\n * Throws if\n * - any of the arguments are negative\n * - fee is greater than or equal 1\n * - output is greater than or equal to output reserve\n *\n * @param output\n * @param inputReserve\n * @param outputReserve\n * @param fee\n * @param protocolFee The protocol fee\n * @param laf The linear amplification factor\n * @returns The swap details\n */\nexport const getSwapInput = (\n output: bigint,\n inputReserve: bigint,\n outputReserve: bigint,\n fee: TFractionLike,\n protocolFee: TFractionLike,\n laf: bigint,\n): TSwapOutcome => {\n if (output <= 0 || inputReserve <= 0 || outputReserve <= 0)\n throw new Error(\"Output and reserves must be positive\");\n\n if (output >= outputReserve)\n throw new Error(\"Output must be less than output reserve\");\n\n fee = Fraction.asFraction(fee);\n if (fee.lt(Fraction.ZERO) || fee.gte(Fraction.ONE))\n throw new Error(\"fee must be [0,1)\");\n\n protocolFee = Fraction.asFraction(protocolFee);\n if (protocolFee.lt(Fraction.ZERO) || protocolFee.gte(Fraction.ONE))\n throw new Error(\"protocolFee must be between 0 and 1\");\n\n const combinedFee = fee.add(protocolFee);\n if (combinedFee.gte(Fraction.ONE))\n throw new Error(\"Combined fee must be less than 1\");\n\n // output = rawOutput * (1-fee)\n // rawOutput = output / (1-fee)\n const rawOutputF = Fraction.asFraction(output).divide(\n Fraction.ONE.sub(combinedFee),\n );\n const rawOutput =\n (rawOutputF.numerator + rawOutputF.denominator - 1n) /\n rawOutputF.denominator;\n const sumInvariant = getSumInvariant(laf, inputReserve, outputReserve);\n const newX = getNewY(outputReserve - rawOutput, laf, sumInvariant);\n const input =\n (newX - inputReserve * RESERVE_PRECISION + RESERVE_PRECISION - 1n) /\n RESERVE_PRECISION;\n\n const totalFee =\n (combinedFee.multiply(FEE_PRECISION).quotient * rawOutput +\n FEE_PRECISION -\n 1n) /\n FEE_PRECISION;\n const totalProtocolFee = protocolFee\n .multiply(totalFee)\n .divide(combinedFee).quotient;\n\n const totalLpFee = totalFee - totalProtocolFee;\n\n const nextInputReserve = inputReserve + input;\n const nextOutputReserve = outputReserve - output - totalProtocolFee;\n\n // See PRICEIMPACT\n const idealPrice = getPrice(inputReserve, outputReserve, laf);\n const actualPrice = new Fraction(input, output);\n const priceImpact = Fraction.ONE.subtract(idealPrice.divide(actualPrice));\n\n const nextSumInvariant = getSumInvariant(\n laf,\n nextInputReserve,\n nextOutputReserve,\n );\n\n return {\n input,\n output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CAAC;EAC7D,IAAMoD,WAAW,GAAG,IAAIjC,kBAAQ,CAACG,KAAK,EAAEsB,MAAM,CAAC;EAC/C,IAAMS,WAAW,GAAGlC,kBAAQ,CAACa,GAAG,CAACa,QAAQ,CAACK,UAAU,CAACT,MAAM,CAACW,WAAW,CAAC,CAAC;EAEzE,IAAMI,gBAAgB,GAAG3E,eAAe,CACtCmB,GAAG,EACHmC,gBAAgB,EAChBc,iBACF,CAAC;EAED,OAAO;IACL3B,KAAK,EAALA,KAAK;IACLsB,MAAM,EAANA,MAAM;IACNU,WAAW,EAAEX,UAAU;IACvBY,iBAAiB,EAAEb,gBAAgB;IACnCP,gBAAgB,EAAhBA,gBAAgB;IAChBc,iBAAiB,EAAjBA,iBAAiB;IACjBI,WAAW,EAAXA,WAAW;IACXG,gBAAgB,EAAhBA;EACF,CAAC;AACH,CAAC","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"StableSwapsPool.js","names":["_index","require","_fraction","liquidityInvariant","assetA","assetB","linearAmplification","newSumInvariant","four_a","four_x_y","d_plus_one","d_cubed","d_plus_one_cubed","x_plus_y","sixteen_a_x_y","sixteen_a_x_y_x_plus_y","f1","f2","A_PRECISION","exports","RESERVE_PRECISION","FEE_PRECISION","getSumInvariant","a","x","y","Error","sum","d","ann","i","d_p","d_prev","getNewY","newX","aRaw","sumInvariant","yPrev","c","b","getFirstLp","laf","calculateLiquidity","aReserve","bReserve","totalLp","oldSumInvariant","newLpTokens","newTotalLpTokens","nextTotalLp","generatedLp","shareAfterDeposit","SharedPoolMath","getShare","aChange","bChange","actualDepositedA","actualDepositedB","getTokensForLp","lp","Fraction","quotient","getSwapOutput","input","inputReserve","outputReserve","fee","protocolFee","roundOutputUp","asFraction","lt","ZERO","gte","ONE","combinedFee","add","nextInputReserve","newY","deltaYPrec","deltaY","totalFee","multiply","divide","totalProtocolFee","totalLpFee","output","subtract","safeOutput","numerator","denominator","nextOutputReserve","idealPrice","getPrice","actualPrice","priceImpact","outputLpFee","outputProtocolFee","nextSumInvariant","dR","xpA","p","getSwapInput","rawOutputF","sub","rawOutput"],"sources":["../../../src/PoolMath/StableSwapsPool.ts"],"sourcesContent":["import { SharedPoolMath } from \"./index.js\";\nimport { Fraction, type TFractionLike } from \"@sundaeswap/fraction\";\n\n/**\n * Validates that a given sum invariant (D) satisfies the liquidity invariant equation\n * for the Stableswaps curve. This function checks if D is within the valid range by\n * evaluating the invariant function at D and D+1.\n *\n * The liquidity invariant ensures that the curve equation is satisfied:\n * 4A * 4xy * D + D^3 - (4xy * 4A * (x + y) + 4xy * D) should be <= 0 at D and > 0 at D+1\n *\n * @param {bigint} assetA - The reserve amount of asset A (already scaled by RESERVE_PRECISION).\n * @param {bigint} assetB - The reserve amount of asset B (already scaled by RESERVE_PRECISION).\n * @param {bigint} linearAmplification - The amplification coefficient for the pool.\n * @param {bigint} newSumInvariant - The proposed sum invariant (D) to validate.\n * @returns {boolean} True if the sum invariant satisfies the liquidity invariant equation, false otherwise.\n */\nexport function liquidityInvariant(\n assetA: bigint,\n assetB: bigint,\n linearAmplification: bigint,\n newSumInvariant: bigint,\n) {\n // 4 * A * 4 * (x*y) * D + D^3 - (4(x*y) * (4A(x + y) + D))\n // 4A * 4xy * D + D^3 - (4xy * 4a * (x + y) + 4xy * D)\n // 16Axy * D + D^3 - (16Axy * (x + y) + 4xy * D)\n const four_a = 4n * linearAmplification;\n const four_x_y = 4n * assetA * assetB;\n const d_plus_one = newSumInvariant + 1n;\n const d_cubed = newSumInvariant * newSumInvariant * newSumInvariant;\n const d_plus_one_cubed = d_plus_one * d_plus_one * d_plus_one;\n const x_plus_y = assetA + assetB;\n const sixteen_a_x_y = four_a * four_x_y;\n const sixteen_a_x_y_x_plus_y = sixteen_a_x_y * x_plus_y;\n const f1 =\n sixteen_a_x_y * newSumInvariant +\n d_cubed -\n (sixteen_a_x_y_x_plus_y + four_x_y * newSumInvariant);\n const f2 =\n sixteen_a_x_y * d_plus_one +\n d_plus_one_cubed -\n (sixteen_a_x_y_x_plus_y + four_x_y * d_plus_one);\n return f1 <= 0n && f2 > 0n;\n}\n\n/**\n * Precision factor for the amplification coefficient calculations.\n * Used to scale the amplification factor for more precise arithmetic operations.\n */\nexport const A_PRECISION: bigint = 200n;\n\n/**\n * Precision factor for reserve calculations in the Stableswaps curve.\n * Reserves are multiplied by this value to maintain precision during calculations.\n * This allows for accurate fractional calculations with integer arithmetic.\n */\nexport const RESERVE_PRECISION: bigint = 1_000_000_000_000n;\n\n/**\n * Precision factor for fee calculations, representing basis points.\n * A value of 10,000 means fees are calculated with 0.01% precision (1 basis point).\n */\nexport const FEE_PRECISION: bigint = 10_000n;\n\n/**\n * Calculates the sum invariant (D) for a Stableswaps pool using Newton's method.\n * The sum invariant represents the total value in the pool and is used to determine\n * the fair exchange rate between assets.\n *\n * This function iteratively solves for D in the Stableswaps invariant equation:\n * Ann * sum + D_p * 2 = Ann * D + D_p * 3\n * where D_p = D^3 / (4 * x * y) and Ann = A * n^n (with n=2)\n *\n * The iteration continues until convergence (difference <= 1) or fails after 255 iterations.\n *\n * @param {bigint} a - The amplification coefficient for the pool (will be scaled by A_PRECISION).\n * @param {bigint} x - The reserve amount of the first asset (will be scaled by RESERVE_PRECISION).\n * @param {bigint} y - The reserve amount of the second asset (will be scaled by RESERVE_PRECISION).\n * @returns {bigint} The calculated sum invariant (D) scaled by RESERVE_PRECISION.\n * @throws {Error} If the amplification coefficient is not positive.\n * @throws {Error} If convergence fails after 255 iterations.\n */\nexport function getSumInvariant(a: bigint, x: bigint, y: bigint): bigint {\n if (a <= 0n) {\n throw new Error(\"Amplification coefficient must be positive.\");\n }\n x = x * RESERVE_PRECISION;\n y = y * RESERVE_PRECISION;\n a = a * A_PRECISION;\n const sum: bigint = x + y;\n if (sum === 0n) {\n return 0n;\n }\n\n let d = sum;\n const ann = a * 2n;\n for (let i = 0; i < 255; i++) {\n const d_p = (d * d * d) / (4n * x * y);\n const d_prev = d;\n\n d =\n (((ann * sum) / 100n + d_p * 2n) * d) /\n (((ann - 100n) * d) / 100n + 3n * d_p);\n\n if (d > d_prev) {\n if (d - d_prev <= 1) {\n if (liquidityInvariant(x, y, a / A_PRECISION, d)) {\n return d;\n } else {\n return d - 1n;\n }\n }\n } else {\n if (d_prev - d <= 1) {\n if (liquidityInvariant(x, y, a / A_PRECISION, d)) {\n return d;\n } else {\n return d - 1n;\n }\n }\n }\n }\n throw new Error(\"Failed to converge on D value after 255 iterations.\");\n}\n\n/**\n * Calculates the new Y reserve value given a new X reserve value and the sum invariant.\n * This function uses Newton's method to iteratively solve for Y in the Stableswaps equation\n * while maintaining the pool's invariant (D).\n *\n * The function solves: Y^2 + (sum + (D * A_PRECISION) / (2 * Ann) - D) * Y - c = 0\n * where c is derived from D and the pool parameters.\n *\n * This is used during swaps to determine the output reserve after an input is added.\n *\n * @param {bigint} newX - The new reserve amount for asset X (will be scaled by RESERVE_PRECISION internally).\n * @param {bigint} aRaw - The raw amplification coefficient for the pool (will be scaled by A_PRECISION).\n * @param {bigint} sumInvariant - The pool's sum invariant (D), already scaled.\n * @returns {bigint} The calculated Y reserve value scaled by RESERVE_PRECISION.\n * @throws {Error} If convergence fails after 255 iterations.\n */\nexport function getNewY(\n newX: bigint,\n aRaw: bigint,\n sumInvariant: bigint,\n): bigint {\n newX = newX * RESERVE_PRECISION;\n const sum = newX;\n let yPrev: bigint;\n let c = sumInvariant;\n const ann = aRaw * A_PRECISION * 2n;\n\n c = (c * sumInvariant) / (newX * 2n);\n c = (c * sumInvariant * A_PRECISION) / 2n;\n c = c / (ann * 2n);\n const b = sum + (sumInvariant * A_PRECISION) / 2n / ann;\n let y = sumInvariant;\n for (let i = 0; i < 255; i++) {\n yPrev = y;\n y = (y * y + c) / (y * 2n + b - sumInvariant);\n if (y > yPrev) {\n if (y - yPrev <= 1n) {\n if (liquidityInvariant(newX, y, aRaw, sumInvariant)) {\n return y;\n } else {\n return y + 1n;\n }\n }\n } else {\n if (yPrev - y <= 1n) {\n if (liquidityInvariant(newX, y, aRaw, sumInvariant)) {\n return y;\n } else {\n return y + 1n;\n }\n }\n }\n }\n throw new Error(\"failed to converge on y value after 255 iterations.\");\n}\n\n/**\n * Calculates the initial LP token amount for the first liquidity provision to a pool.\n * This is used when bootstrapping a new pool with initial reserves of assets A and B.\n *\n * The LP tokens minted are derived from the sum invariant (D) divided by the reserve precision,\n * effectively representing the pool's total value at inception.\n *\n * @param {bigint} a - The initial amount of token A being deposited.\n * @param {bigint} b - The initial amount of token B being deposited.\n * @param {bigint} laf - The linear amplification factor for the pool.\n * @returns {bigint} The amount of LP tokens to mint for the first liquidity provision.\n */\nexport const getFirstLp = (a: bigint, b: bigint, laf: bigint) =>\n getSumInvariant(laf, a, b) / RESERVE_PRECISION;\n\n/**\n * Calculates the liquidity parameters for adding liquidity to a Stableswaps pool.\n * This function supports mixed deposits where both assets can be deposited in any proportion.\n *\n * The calculation is based on the change in the sum invariant (D) before and after the deposit.\n * LP tokens are minted proportionally to the increase in D relative to the old D value.\n *\n * TODO: Investigate way to optimize actual deposited amounts\n *\n * @param {bigint} a - The amount of token A to deposit.\n * @param {bigint} b - The amount of token B to deposit.\n * @param {bigint} aReserve - The current reserve of token A in the pool.\n * @param {bigint} bReserve - The current reserve of token B in the pool.\n * @param {bigint} totalLp - The total LP tokens for the pool before the deposit.\n * @param {bigint} laf - The linear amplification factor of the pool.\n * @throws {Error} If the pool doesn't have enough liquidity (both reserves are zero).\n * @throws {Error} If both deposit amounts are zero.\n * @returns {Object} An object containing:\n * - nextTotalLp: The total LP tokens for the pool after the deposit.\n * - generatedLp: The amount of LP tokens generated by the deposit.\n * - shareAfterDeposit: The depositor's share of the pool after the deposit (as a Fraction).\n * - aChange: Always 0n for Stableswaps (no refund needed).\n * - bChange: Always 0n for Stableswaps (no refund needed).\n * - actualDepositedA: The actual amount of A deposited (always equal to input a).\n * - actualDepositedB: The actual amount of B deposited (always equal to input b).\n */\nexport const calculateLiquidity = (\n a: bigint,\n b: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n laf: bigint,\n) => {\n if (aReserve === 0n && bReserve === 0n) {\n throw new Error(\"Not enough pool liquidity\");\n }\n\n if (a === 0n && b === 0n) {\n throw new Error(\"Cannot use a deposit asset amount of 0\");\n }\n\n const oldSumInvariant = getSumInvariant(laf, aReserve, bReserve);\n\n const newSumInvariant = getSumInvariant(laf, aReserve + a, bReserve + b);\n\n const newLpTokens =\n ((newSumInvariant - oldSumInvariant) * totalLp) / oldSumInvariant;\n\n const newTotalLpTokens = totalLp + newLpTokens;\n\n return {\n nextTotalLp: newTotalLpTokens,\n generatedLp: newLpTokens,\n shareAfterDeposit: SharedPoolMath.getShare(newLpTokens, newTotalLpTokens),\n aChange: 0n,\n bChange: 0n,\n actualDepositedA: a,\n actualDepositedB: b,\n };\n};\n\n/**\n * Calculates the token amounts that a given LP token amount represents when withdrawing\n * from a Stableswaps pool. The withdrawal is proportional to the LP tokens being redeemed\n * relative to the total LP supply.\n *\n * This uses a simple linear proportion: amount = (lp * reserve) / totalLp for each asset.\n *\n * @param {bigint} lp - The amount of LP tokens being redeemed.\n * @param {bigint} aReserve - The pool's current reserve amount of token A.\n * @param {bigint} bReserve - The pool's current reserve amount of token B.\n * @param {bigint} totalLp - The pool's total minted LP tokens currently in circulation.\n * @returns {SharedPoolMath.TPair} A tuple [a, b] containing the amounts of tokens A and B to withdraw.\n */\nexport const getTokensForLp = (\n lp: bigint,\n aReserve: bigint,\n bReserve: bigint,\n totalLp: bigint,\n): SharedPoolMath.TPair => [\n new Fraction(lp * aReserve, totalLp).quotient,\n new Fraction(lp * bReserve, totalLp).quotient,\n];\n\n/**\n * Represents the complete outcome of a swap operation in a Stableswaps pool.\n * Contains all relevant information about the swap including amounts, fees, updated reserves,\n * and price impact.\n */\nexport type TSwapOutcome = {\n /** The amount of input tokens being swapped */\n input: bigint;\n /** The amount of output tokens received (after all fees) */\n output: bigint;\n /** The portion of fees that goes to liquidity providers */\n outputLpFee: bigint;\n /** The portion of fees that goes to the protocol */\n outputProtocolFee: bigint;\n /** The input asset reserve amount after the swap */\n nextInputReserve: bigint;\n /** The output asset reserve amount after the swap (excluding protocol fees) */\n nextOutputReserve: bigint;\n /** The pool's sum invariant (D) after the swap */\n nextSumInvariant: bigint;\n /** The price impact of the swap as a fraction (difference between ideal and actual price) */\n priceImpact: Fraction;\n};\n\n/**\n * Calculates the output amount and all swap parameters for a given input amount in a Stableswaps pool.\n * This function determines how many output tokens will be received for a given input, accounting for\n * the Stableswaps curve, LP fees, and protocol fees.\n *\n * The calculation process:\n * 1. Validates inputs and fees\n * 2. Calculates the current sum invariant (D)\n * 3. Determines the new Y value after adding input to X\n * 4. Calculates raw output and applies fees (LP + protocol)\n * 5. Computes price impact by comparing ideal vs actual execution price\n *\n * @param {bigint} input - The amount of input tokens to swap.\n * @param {bigint} inputReserve - The current reserve amount of the input token in the pool.\n * @param {bigint} outputReserve - The current reserve amount of the output token in the pool.\n * @param {TFractionLike} fee - The liquidity provider fee as a fraction (e.g., 0.003 for 0.3%).\n * @param {TFractionLike} protocolFee - The protocol fee as a fraction.\n * @param {bigint} laf - The linear amplification factor of the pool.\n * @param {boolean} [roundOutputUp] - Optional flag to round output up instead of down (default: false).\n * @returns {TSwapOutcome} The complete swap outcome including output, fees, reserves, and price impact.\n * @throws {Error} If input or reserves are not positive.\n * @throws {Error} If fee is not in the range [0, 1).\n * @throws {Error} If protocol fee is not in the range [0, 1).\n * @throws {Error} If combined fee is greater than or equal to 1.\n */\nexport const getSwapOutput = (\n input: bigint,\n inputReserve: bigint,\n outputReserve: bigint,\n fee: TFractionLike,\n protocolFee: TFractionLike,\n laf: bigint,\n roundOutputUp?: boolean,\n): TSwapOutcome => {\n if (input <= 0 || inputReserve <= 0 || outputReserve <= 0)\n throw new Error(\"Input and reserves must be positive\");\n\n fee = Fraction.asFraction(fee);\n if (fee.lt(Fraction.ZERO) || fee.gte(Fraction.ONE))\n throw new Error(\"fee must be between 0 and 1\");\n\n protocolFee = Fraction.asFraction(protocolFee);\n if (protocolFee.lt(Fraction.ZERO) || protocolFee.gte(Fraction.ONE))\n throw new Error(\"protocolFee must be between 0 and 1\");\n\n const combinedFee = fee.add(protocolFee);\n if (combinedFee.gte(Fraction.ONE))\n throw new Error(\"Combined fee must be less than 1\");\n const sumInvariant = getSumInvariant(laf, inputReserve, outputReserve);\n const nextInputReserve = inputReserve + input;\n const newY = getNewY(nextInputReserve, laf, sumInvariant);\n\n const deltaYPrec = outputReserve * RESERVE_PRECISION - newY;\n const deltaY = new Fraction(deltaYPrec, RESERVE_PRECISION);\n const totalFee = combinedFee\n .multiply(FEE_PRECISION)\n .multiply(deltaY.quotient)\n .add(Fraction.asFraction(FEE_PRECISION - 1n))\n .divide(FEE_PRECISION).quotient;\n const totalProtocolFee = protocolFee\n .multiply(totalFee)\n .divide(combinedFee).quotient;\n const totalLpFee = totalFee - totalProtocolFee;\n const output = deltaY.subtract(totalFee);\n const safeOutput = roundOutputUp\n ? (output.numerator + output.denominator - 1n) / output.denominator\n : output.quotient;\n const nextOutputReserve = outputReserve - safeOutput - totalProtocolFee;\n\n // PRICEIMPACT: \"priceImpact\" is slightly misleadingly named in the industry as a whole\n // just by it's name, it would imply that it's the percentage difference between\n // the current price and the price after the swap, but it's actually the percentage\n // difference between the real price of your swap, and the price implied by the current\n // reserves; We got this wrong in v1, but this aligns it with the industry standard\n // Source: https://dailydefi.org/articles/price-impact-and-how-to-calculate/\n const idealPrice = getPrice(inputReserve, outputReserve, laf);\n const actualPrice = new Fraction(input, safeOutput);\n const priceImpact = Fraction.ONE.subtract(idealPrice.divide(actualPrice));\n return {\n input,\n output: safeOutput,\n outputLpFee: totalLpFee,\n outputProtocolFee: totalProtocolFee,\n nextInputReserve,\n nextOutputReserve,\n nextSumInvariant: getSumInvariant(laf, nextInputReserve, nextOutputReserve),\n priceImpact,\n };\n};\n\n/**\n * Calculates the current price (exchange rate) of asset A in terms of asset B for a Stableswaps pool.\n * The price is derived from the Stableswaps curve equation and takes into account the amplification factor.\n *\n * The calculation uses the formula:\n * price = (xpA + (dR * aReserve) / bReserve) / (xpA + dR)\n * where xpA = (Ann * aReserve) / A_PRECISION and dR is derived from the sum invariant\n *\n * @param {bigint} aReserve - The current reserve amount of asset A in the pool.\n * @param {bigint} bReserve - The current reserve amount of asset B in the pool.\n * @param {bigint} laf - The linear amplification factor of the pool.\n * @returns {Fraction} The price of asset A in terms of asset B as a Fraction.\n */\nexport function getPrice(\n aReserve: bigint,\n bReserve: bigint,\n laf: bigint,\n): Fraction {\n // dx_0 / dx_1 only, however can have any number of coins in pool\n const ann = laf * 2n * A_PRECISION;\n const sumInvariant =\n getSumInvariant(laf, aReserve, bReserve) / RESERVE_PRECISION;\n // D / n^n with n = 2\n let dR = sumInvariant / 4n;\n dR = (dR * sumInvariant) / aReserve;\n dR = (dR * sumInvariant) / bReserve;\n const xpA = (ann * aReserve) / A_PRECISION;\n const p = new Fraction(xpA + (dR * aReserve) / bReserve, xpA + dR);\n return p;\n}\n\n/**\n * Calculates the required input amount and all swap parameters for a desired output amount in a Stableswaps pool.\n * This is the reverse operation of getSwapOutput - given how much you want to receive, it determines\n * how much you need to provide as input, accounting for fees and the Stableswaps curve.\n *\n * The calculation process:\n * 1. Validates inputs, fees, and output constraints\n * 2. Calculates raw output needed before fees\n * 3. Determines the new X value to achieve the desired Y reduction\n * 4. Calculates required input and separates LP and protocol fees\n * 5. Computes price impact by comparing ideal vs actual execution price\n *\n * @param {bigint} output - The desired amount of output tokens to receive.\n * @param {bigint} inputReserve - The current reserve amount of the input token in the pool.\n * @param {bigint} outputReserve - The current reserve amount of the output token in the pool.\n * @param {TFractionLike} fee - The liquidity provider fee as a fraction (e.g., 0.003 for 0.3%).\n * @param {TFractionLike} protocolFee - The protocol fee as a fraction.\n * @param {bigint} laf - The linear amplification factor of the pool.\n * @returns {TSwapOutcome} The complete swap outcome including required input, fees, reserves, and price impact.\n * @throws {Error} If output or reserves are not positive.\n * @throws {Error} If output is greater than or equal to the output reserve.\n * @throws {Error} If fee is not in the range [0, 1).\n * @throws {Error} If protocol fee is not in the range [0, 1).\n * @throws {Error} If combined fee is greater than or equal to 1.\n */\nexport const getSwapInput = (\n output: bigint,\n inputReserve: bigint,\n outputReserve: bigint,\n fee: TFractionLike,\n protocolFee: TFractionLike,\n laf: bigint,\n): TSwapOutcome => {\n if (output <= 0 || inputReserve <= 0 || outputReserve <= 0)\n throw new Error(\"Output and reserves must be positive\");\n\n if (output >= outputReserve)\n throw new Error(\"Output must be less than output reserve\");\n\n fee = Fraction.asFraction(fee);\n if (fee.lt(Fraction.ZERO) || fee.gte(Fraction.ONE))\n throw new Error(\"fee must be [0,1)\");\n\n protocolFee = Fraction.asFraction(protocolFee);\n if (protocolFee.lt(Fraction.ZERO) || protocolFee.gte(Fraction.ONE))\n throw new Error(\"protocolFee must be between 0 and 1\");\n\n const combinedFee = fee.add(protocolFee);\n if (combinedFee.gte(Fraction.ONE))\n throw new Error(\"Combined fee must be less than 1\");\n\n // output = rawOutput * (1-fee)\n // rawOutput = output / (1-fee)\n const rawOutputF = Fraction.asFraction(output).divide(\n Fraction.ONE.sub(combinedFee),\n );\n const rawOutput =\n (rawOutputF.numerator + rawOutputF.denominator - 1n) /\n rawOutputF.denominator;\n const sumInvariant = getSumInvariant(laf, inputReserve, outputReserve);\n const newX = getNewY(outputReserve - rawOutput, laf, sumInvariant);\n const input =\n (newX - inputReserve * RESERVE_PRECISION + RESERVE_PRECISION - 1n) /\n RESERVE_PRECISION;\n\n const totalFee =\n (combinedFee.multiply(FEE_PRECISION).quotient * rawOutput +\n FEE_PRECISION -\n 1n) /\n FEE_PRECISION;\n const totalProtocolFee = protocolFee\n .multiply(totalFee)\n .divide(combinedFee).quotient;\n\n const totalLpFee = totalFee - totalProtocolFee;\n\n const nextInputReserve = inputReserve + input;\n const nextOutputReserve = outputReserve - output - totalProtocolFee;\n\n // See PRICEIMPACT\n const idealPrice = getPrice(inputReserve, outputReserve, laf);\n const actualPrice = new Fraction(input, output);\n const priceImpact = Fraction.ONE.subtract(idealPrice.divide(actualPrice));\n\n const nextSumInvariant = getSumInvariant(\n laf,\n nextInputReserve,\n nextOutputReserve,\n );\n\n return {\n input,\n output,\n outputLpFee: totalLpFee,\n outputProtocolFee: totalProtocolFee,\n nextInputReserve,\n nextOutputReserve,\n priceImpact,\n nextSumInvariant,\n };\n};\n"],"mappings":";;;;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,SAAA,GAAAD,OAAA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASE,kBAAkBA,CAChCC,MAAc,EACdC,MAAc,EACdC,mBAA2B,EAC3BC,eAAuB,EACvB;EACA;EACA;EACA;EACA,IAAMC,MAAM,GAAG,EAAE,GAAGF,mBAAmB;EACvC,IAAMG,QAAQ,GAAG,EAAE,GAAGL,MAAM,GAAGC,MAAM;EACrC,IAAMK,UAAU,GAAGH,eAAe,GAAG,EAAE;EACvC,IAAMI,OAAO,GAAGJ,eAAe,GAAGA,eAAe,GAAGA,eAAe;EACnE,IAAMK,gBAAgB,GAAGF,UAAU,GAAGA,UAAU,GAAGA,UAAU;EAC7D,IAAMG,QAAQ,GAAGT,MAAM,GAAGC,MAAM;EAChC,IAAMS,aAAa,GAAGN,MAAM,GAAGC,QAAQ;EACvC,IAAMM,sBAAsB,GAAGD,aAAa,GAAGD,QAAQ;EACvD,IAAMG,EAAE,GACNF,aAAa,GAAGP,eAAe,GAC/BI,OAAO,IACNI,sBAAsB,GAAGN,QAAQ,GAAGF,eAAe,CAAC;EACvD,IAAMU,EAAE,GACNH,aAAa,GAAGJ,UAAU,GAC1BE,gBAAgB,IACfG,sBAAsB,GAAGN,QAAQ,GAAGC,UAAU,CAAC;EAClD,OAAOM,EAAE,IAAI,EAAE,IAAIC,EAAE,GAAG,EAAE;AAC5B;;AAEA;AACA;AACA;AACA;AACO,IAAMC,WAAmB,GAAAC,OAAA,CAAAD,WAAA,GAAG,IAAI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