@sundaeswap/math 0.5.0 → 0.6.0

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.
@@ -1,9 +1,9 @@
1
1
  import { AssetAmount, IAssetAmountMetadata } from "@sundaeswap/asset";
2
- import { Fraction, type TFractionLike } from "@sundaeswap/fraction";
2
+ import { Fraction } from "@sundaeswap/fraction";
3
3
  /**
4
- * Holds the calculated outcome of a concentrated-liquidity swap. Mirrors
5
- * {@link ConstantProductPool.TSwapOutcome} so it flows through the same generic
6
- * swap-outcome consumers.
4
+ * Holds the calculated outcome of a concentrated-liquidity swap. It has the
5
+ * same shape as `ConstantProductPool.TSwapOutcome`, so the generic
6
+ * swap-outcome consumers accept it.
7
7
  */
8
8
  export type TSwapOutcome = {
9
9
  input: bigint;
@@ -13,48 +13,234 @@ export type TSwapOutcome = {
13
13
  nextOutputReserve: bigint;
14
14
  priceImpact: Fraction;
15
15
  };
16
- /** A sqrt-price bound from the CL config, as an exact rational `[num, den]`. */
17
- export type TSqrtPrice = [bigint, bigint];
18
16
  /**
19
- * Calculate the swap outcome for a concentrated-liquidity pool.
17
+ * Concentrated liquidity: Sundae v4's ladder invariant.
20
18
  *
21
- * @param inputMetadata Metadata for the supplied (input) asset — used for the fee AssetAmount.
22
- * @param input The pre-fee amount of the input asset being swapped.
23
- * @param aReserve The pool's raw reserve of asset A.
24
- * @param bReserve The pool's raw reserve of asset B.
25
- * @param totalLp The pool's total LP supply (the virtual liquidity `L`).
26
- * @param sqrtPriceA The lower sqrt-price bound `[num, den]` from the CL config.
27
- * @param sqrtPriceB The upper sqrt-price bound `[num, den]` from the CL config.
28
- * @param fee The liquidity-provider fee (plus protocol fee) as a fraction.
29
- * @param isAInput True when asset A is the supplied asset (A→B swap).
30
- * @returns The swap details in the shared {@link TSwapOutcome} shape.
31
- */
32
- export declare const getSwapOutput: (inputMetadata: IAssetAmountMetadata, input: bigint, aReserve: bigint, bReserve: bigint, totalLp: bigint, sqrtPriceA: TSqrtPrice, sqrtPriceB: TSqrtPrice, fee: TFractionLike, isAInput: boolean) => TSwapOutcome;
33
- /**
34
- * Calculate the minimal input required to receive a given output — the inverse
35
- * of {@link getSwapOutput}. The forward swap is linear-fractional in the
36
- * effective input, so it inverts in closed form; a short correction loop
37
- * absorbs the fee floor and integer rounding so the result is the exact minimal
38
- * input whose forward swap yields at least `output`.
39
- */
40
- export declare const getSwapInput: (inputMetadata: IAssetAmountMetadata, output: bigint, aReserve: bigint, bReserve: bigint, totalLp: bigint, sqrtPriceA: TSqrtPrice, sqrtPriceB: TSqrtPrice, fee: TFractionLike, isAInput: boolean) => TSwapOutcome;
41
- /**
42
- * Deposit LP estimate for a concentrated-liquidity pool. CL deposits reuse the
43
- * constant-product *proportional* pinning: minting by the scarcest offered
44
- * asset and ceil-pinning the reserve deltas gives, per asset,
45
- * `a1·L0 ≥ a0·L1` and `b1·L0 ≥ b0·L1` — and those two bounds multiply to
46
- * exactly the on-chain CL non-swap invariant `va1·vb1·L0² ≥ va0·vb0·L1²`
47
- * (`cl_check.ak`). So the proportional deposit is always chain-valid; excess of
48
- * either asset is refunded via aChange/bChange. This mirrors the scooper's
49
- * `resolve_cl_deposit` (which likewise reuses the CP pinning).
50
- */
51
- export declare const calculateLiquidity: (a: bigint, b: bigint, aReserve: bigint, bReserve: bigint, totalLp: bigint) => {
52
- nextTotalLp: bigint;
53
- generatedLp: bigint;
54
- shareAfterDeposit: Fraction;
55
- bChange: bigint;
56
- aChange: bigint;
57
- actualDepositedA: bigint;
58
- actualDepositedB: bigint;
19
+ * A pool is a ladder of bands in ascending sqrt-price order. Each band is a
20
+ * concentrated-liquidity arc (`curve` 0) or a constant-sum bin (`curve` 1)
21
+ * with its own share of the pool's liquidity (`weight / weightTotal`) and
22
+ * its own two fees. LP is one fungible token for the whole ladder; a
23
+ * deposit or withdrawal scales every band.
24
+ *
25
+ * The chain never stores the price. It stores reserves, and the state is a
26
+ * WITNESS `(X, k)`: the ladder counter `X` (the pool's `total_lp`, spec V12)
27
+ * and the active band `k`, which the reserves must prove. Everything here
28
+ * mirrors `lib/modules/cl_check.ak` and the scooper's ladder
29
+ * math, rounding included; where they disagree the chain wins
30
+ * and the transaction fails. The rounding is not negotiable:
31
+ *
32
+ * - `L_k` floors; saturation ceils, in ONE division;
33
+ * - the band fee ceils, per step, on the step's real input;
34
+ * - the prefix sums come from the LADDER INDEX (cumulative coefficients
35
+ * in fixed point at 2^64, spec §2.3), multiplied by the counter and
36
+ * ceiled ONCE. A per-band sum of ceilings is a different number and the
37
+ * chain rejects it.
38
+ */
39
+ /** An exact rational `num / den`. */
40
+ export type TFrac = {
41
+ num: bigint;
42
+ den: bigint;
43
+ };
44
+ /** One band of the ladder. */
45
+ export type TBand = {
46
+ /** The band's LOWER sqrt-price edge. The next band's `start` is its upper edge. */
47
+ start: TFrac;
48
+ /** The band's share of the ladder's liquidity counter. */
49
+ weight: bigint;
50
+ /** 0 = concentrated-liquidity arc, 1 = constant-sum bin. */
51
+ curve: 0 | 1;
52
+ /** Fee on swaps that buy the pool's first asset (B in). */
53
+ feeBuy: TFrac;
54
+ /** Fee on swaps that sell the pool's first asset (A in). */
55
+ feeSell: TFrac;
56
+ };
57
+ /** A ladder: bands, the top edge, and the sum of the weights. */
58
+ export type TLadder = {
59
+ bands: TBand[];
60
+ /** The last band's UPPER sqrt-price edge. */
61
+ closing: TFrac;
62
+ weightTotal: bigint;
63
+ };
64
+ /** The witness the chain checks: the ladder counter and the active band. */
65
+ export type TWitness = {
66
+ x: bigint;
67
+ k: number;
59
68
  };
69
+ /** 2^64, the fixed-point scale of the ladder index. */
70
+ export declare const FIXED_ONE: bigint;
71
+ /**
72
+ * The least counter per unit of weight a ladder may be created with.
73
+ * Mirrors `cl_check.min_counter_per_weight` (sundae-v4 #229, SV4-007):
74
+ * Create requires `total_lp >= MIN_COUNTER_PER_WEIGHT * weightTotal`.
75
+ */
76
+ export declare const MIN_COUNTER_PER_WEIGHT = 1000n;
77
+ /** The least counter (initial `total_lp`) the chain accepts at Create for `ladder`. */
78
+ export declare const minCreateCounter: (ladder: TLadder) => bigint;
79
+ /** Band i's upper sqrt-price edge. */
80
+ export declare const upperEdge: (l: TLadder, i: number) => TFrac;
81
+ /** `D_i = hi.num·lo.den − lo.num·hi.den`: positive exactly when the edges increase. */
82
+ export declare const delta: (lo: TFrac, hi: TFrac) => bigint;
83
+ /** `a_sat_i = ceil(w_i·X·D_i / (W·lo.num·hi.num))`: the A a band above the price holds. */
84
+ export declare const aSat: (l: TLadder, x: bigint, i: number) => bigint;
85
+ /** `b_sat_i = ceil(w_i·X·D_i / (W·lo.den·hi.den))`: the B a band below the price holds. */
86
+ export declare const bSat: (l: TLadder, x: bigint, i: number) => bigint;
87
+ /** `L_k = floor(w_k·X / W)`. */
88
+ export declare const liquidityOf: (l: TLadder, x: bigint, k: number) => bigint;
89
+ /** Band i's A-side saturation coefficient: the part of `a_sat_i` independent of X, rounded up. */
90
+ export declare const coeffA: (l: TLadder, i: number) => bigint;
91
+ /** The same on the B side. */
92
+ export declare const coeffB: (l: TLadder, i: number) => bigint;
93
+ /** One index entry: the cumulative coefficients above and below band k. */
94
+ export type TIndexEntry = {
95
+ ca: bigint;
96
+ cb: bigint;
97
+ };
98
+ /**
99
+ * The ladder index `Create` pins and every spend reads: entry k is the pair
100
+ * of cumulative coefficients for the bands strictly above and strictly
101
+ * below band k. `cl_check.build_index`, to the digit.
102
+ */
103
+ export declare const buildIndex: (l: TLadder) => TIndexEntry[];
104
+ /**
105
+ * A ladder with its index built once. Every probe of the witness search
106
+ * reads the index for every band, and rebuilding it per probe is most of
107
+ * the cost of a quote.
108
+ */
109
+ export type TIndexedLadder = TLadder & {
110
+ index: TIndexEntry[];
111
+ };
112
+ export declare const indexLadder: (l: TLadder) => TIndexedLadder;
113
+ /** `C_A`, `C_B` at (X, k) from the index: one ceiling after the multiply by X. */
114
+ export declare const prefixes: (l: TIndexedLadder, x: bigint, k: number) => {
115
+ ca: bigint;
116
+ cb: bigint;
117
+ };
118
+ /** The band proof, P1 through P7. The chain runs exactly this. */
119
+ export declare const bandProof: (l: TIndexedLadder, A: bigint, B: bigint, x: bigint, k: number) => boolean;
120
+ /**
121
+ * The witness in band k, seeded at `hint` when given: the counter only
122
+ * grows along a swap, so the doubling search starts from the previous
123
+ * witness. `lo` is the largest x with non-negative residuals and G ≥ 0,
124
+ * and a witness is such an x whose successor fails G, so the scan walks
125
+ * DOWN from `lo` first. Mirrors the scooper's `find_witness_in_band_from`.
126
+ */
127
+ export declare const findWitnessInBand: (l: TIndexedLadder, A: bigint, B: bigint, k: number, hint?: bigint) => TWitness | null;
128
+ /**
129
+ * The witness `(X, k)` for a pool's reserves. A hint (the indexed
130
+ * `bandCounter` / `activeBand`) is checked first and seeds the search in
131
+ * its band; a stale hint falls back to every band. Null when the reserves
132
+ * sit on no point of the ladder — the chain would reject them too.
133
+ */
134
+ export declare const findWitness: (ladder: TLadder, A: bigint, B: bigint, hint?: TWitness) => TWitness | null;
135
+ /** The active band's view at a witness: its residual reserves and liquidity. */
136
+ export type TBandView = {
137
+ ra: bigint;
138
+ rb: bigint;
139
+ l: bigint;
140
+ lo: TFrac;
141
+ hi: TFrac;
142
+ curve: 0 | 1;
143
+ feeBuy: TFrac;
144
+ feeSell: TFrac;
145
+ };
146
+ export declare const bandView: (l: TIndexedLadder, A: bigint, B: bigint, w: TWitness) => TBandView;
147
+ /** The band's fee for a direction: `feeSell` when A is sold, `feeBuy` when A is bought. */
148
+ export declare const feeFor: (v: TBandView, isAInput: boolean) => TFrac;
149
+ /**
150
+ * The band fee on a step's real input `dx`: `ceil(dx * num / den)`.
151
+ * Mirrors `cl_check.band_fee` (sundae-v4 #228, SV4-006). The chain charges
152
+ * it per transcript step and rounds it UP, so a step below `den / num`
153
+ * units still pays one unit. A zero rate charges nothing.
154
+ */
155
+ export declare const bandFee: (dx: bigint, fee: TFrac) => bigint;
156
+ /** The output one band pays for `dx` within itself. Mirrors the scooper's `band_output`. */
157
+ export declare const bandOutput: (v: TBandView, isAInput: boolean, dx: bigint) => bigint;
158
+ /** What the band can pay out in a direction: its residual of the output asset. */
159
+ export declare const bandCapacity: (v: TBandView, isAInput: boolean) => bigint;
160
+ /** The largest input whose output stays within the band. */
161
+ export declare const maxDxInBand: (v: TBandView, isAInput: boolean) => bigint;
162
+ /** The next band a fill enters: selling A walks down the ladder, buying A walks up. */
163
+ export declare const nextBand: (k: number, isAInput: boolean, n: number) => number | null;
164
+ /** One band of a fill: what it paid, and the state it left. */
165
+ export type TFillStep = {
166
+ band: number;
167
+ input: bigint;
168
+ output: bigint;
169
+ after: TWitness;
170
+ };
171
+ /**
172
+ * Price `dx` across as many bands as it takes, as the scooper's
173
+ * `swap_steps` does: fill the active band, cross to the next, repeat.
174
+ * Returns the steps, or null when the ladder cannot absorb `dx` (the
175
+ * direction is exhausted, or the remainder pays nothing).
176
+ */
177
+ export declare const swapSteps: (ladder: TLadder, A: bigint, B: bigint, isAInput: boolean, dx: bigint, hint?: TWitness) => TFillStep[] | null;
178
+ /**
179
+ * The swap output of a concentrated-liquidity pool for `input` of one asset, crossing
180
+ * bands as needed. `aReserve`/`bReserve` are the pool's reserves in
181
+ * canonical order; `hint` is the indexed `bandCounter` / `activeBand`,
182
+ * which seeds the witness search and may be stale.
183
+ *
184
+ * The `lpFee` is the fee the bands kept of the input, summed across the
185
+ * steps. `priceImpact` is against the active band's opening marginal
186
+ * price. Throws when the ladder cannot absorb the input.
187
+ */
188
+ export declare const getSwapOutput: (inputMetadata: IAssetAmountMetadata, input: bigint, aReserve: bigint, bReserve: bigint, ladder: TLadder, isAInput: boolean, hint?: TWitness) => TSwapOutcome & {
189
+ steps: TFillStep[];
190
+ };
191
+ /** What each band holds at the pool's reserves. */
192
+ export type TBandState = {
193
+ /** The band's reserves in canonical order. The bands sum to the pool's reserves. */
194
+ quantities: [bigint, bigint];
195
+ liquidity: bigint;
196
+ active: boolean;
197
+ };
198
+ /**
199
+ * Every band's state at the pool's reserves: a band below the price holds
200
+ * only B, a band above it only A, the active band some of both. Null when
201
+ * the reserves sit on no point of the ladder.
202
+ */
203
+ export declare const bandStates: (ladder: TLadder, aReserve: bigint, bReserve: bigint, hint?: TWitness) => TBandState[] | null;
204
+ /** Which band holds a sqrt-price. */
205
+ export declare const bandOf: (l: TLadder, s: TFrac) => number;
206
+ /**
207
+ * The exact reserves a ladder holds at counter `x` with the price at
208
+ * sqrt-price `s`, which must lie in band `k`: what a pool creator deposits
209
+ * for a chosen shape, scale and launch price. The result is floored, so
210
+ * `findWitness` must still be run on it to learn the counter the chain
211
+ * derives — usually `x`, but rounding can move it.
212
+ */
213
+ export declare const reservesAt: (l: TLadder, x: bigint, k: number, s: TFrac) => {
214
+ A: bigint;
215
+ B: bigint;
216
+ };
217
+ /**
218
+ * An equal-weight ladder of `n` bands from `lo` to `hi`, every band a CL
219
+ * arc at `fee` unless named in `curves`. The edges are built over a common
220
+ * denominator and reduced: every rational component of a concentrated-liquidity config
221
+ * must stay below 2^64 (spec V9).
222
+ */
223
+ export declare const uniformLadder: (n: number, lo: TFrac, hi: TFrac, fee: TFrac, curves?: (0 | 1)[]) => TLadder;
224
+ /** Band weight profiles. Every weight is at least 1, as `Create` demands. */
225
+ export type TWeightShape = "flat" | "concentrated" | "skewed-low" | "skewed-high" | "bimodal";
226
+ export declare const bandWeights: (n: number, shape?: TWeightShape, opts?: {
227
+ centre?: number;
228
+ peak?: bigint;
229
+ tail?: bigint;
230
+ }) => bigint[];
231
+ /**
232
+ * A ladder of `n` bands from `lo` to `hi` with a weight shape, optional
233
+ * constant-sum bins and an optional per-band fee. The edges are
234
+ * `uniformLadder`'s exactly; only the weights, curves and fees differ.
235
+ */
236
+ export declare const shapedLadder: (n: number, lo: TFrac, hi: TFrac, fee: TFrac, opts?: {
237
+ shape?: TWeightShape;
238
+ centre?: number;
239
+ peak?: bigint;
240
+ tail?: bigint;
241
+ /** Zero-based band indices to price as constant-sum bins. */
242
+ csBands?: number[];
243
+ /** Overrides `fee` per band. */
244
+ feeAt?: (i: number) => TFrac;
245
+ }) => TLadder;
60
246
  //# sourceMappingURL=ConcentratedLiquidityPool.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ConcentratedLiquidityPool.d.ts","sourceRoot":"","sources":["../../../src/PoolMath/ConcentratedLiquidityPool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAE,KAAK,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAIpE;;;;GAIG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,WAAW,CAAC,oBAAoB,CAAC,CAAC;IACzC,gBAAgB,EAAE,MAAM,CAAC;IACzB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,WAAW,EAAE,QAAQ,CAAC;CACvB,CAAC;AAEF,gFAAgF;AAChF,MAAM,MAAM,UAAU,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AA2D1C;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,aAAa,kBACT,oBAAoB,SAC5B,MAAM,YACH,MAAM,YACN,MAAM,WACP,MAAM,cACH,UAAU,cACV,UAAU,OACjB,aAAa,YACR,OAAO,KAChB,YAuDF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,kBACR,oBAAoB,UAC3B,MAAM,YACJ,MAAM,YACN,MAAM,WACP,MAAM,cACH,UAAU,cACV,UAAU,OACjB,aAAa,YACR,OAAO,KAChB,YA+EF,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,kBAAkB,MAC1B,MAAM,KACN,MAAM,YACC,MAAM,YACN,MAAM,WACP,MAAM;;;;;;;;CAC6D,CAAC"}
1
+ {"version":3,"file":"ConcentratedLiquidityPool.d.ts","sourceRoot":"","sources":["../../../src/PoolMath/ConcentratedL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A6E;AAC7E,MAAM,MAAM,YAAY,GACpB,MAAM,GACN,cAAc,GACd,YAAY,GACZ,aAAa,GACb,SAAS,CAAC;AAEd,eAAO,MAAM,WAAW,MACnB,MAAM,UACF,YAAY,SACb;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,KACtD,MAAM,EAyBR,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,YAAY,MACpB,MAAM,MACL,KAAK,MACL,KAAK,OACJ,KAAK,SACJ;IACJ,KAAK,CAAC,EAAE,YAAY,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,6DAA6D;IAC7D,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,gCAAgC;IAChC,KAAK,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,KAAK,CAAC;CAC9B,KACA,OAmBF,CAAC"}
@@ -180,17 +180,18 @@ export type TSwapOutcome = {
180
180
  */
181
181
  export declare const getSwapOutput: (outputMetadata: IAssetAmountMetadata, input: bigint, inputReserve: bigint, outputReserve: bigint, fee: TFractionLike, protocolFee: TFractionLike, laf: bigint, roundOutputUp?: boolean) => TSwapOutcome;
182
182
  /**
183
- * Calculates the current price (exchange rate) of asset A in terms of asset B for a Stableswaps pool.
184
- * The price is derived from the Stableswaps curve equation and takes into account the amplification factor.
183
+ * The pool's marginal price as raw units of asset A per raw unit of asset B,
184
+ * |dx/dy| at the current reserves. This is the "ideal" price that swap price
185
+ * impact is measured against.
185
186
  *
186
- * The calculation uses the formula:
187
+ * Derived from the Stableswaps invariant 4A(x + y) + D = 4AD + D^3 / (4xy):
187
188
  * price = (xpA + (dR * aReserve) / bReserve) / (xpA + dR)
188
- * where xpA = (Ann * aReserve) / A_PRECISION and dR is derived from the sum invariant
189
+ * where xpA = 4 * laf * aReserve and dR = D^3 / (4 * aReserve * bReserve)
189
190
  *
190
191
  * @param {bigint} aReserve - The current reserve amount of asset A in the pool.
191
192
  * @param {bigint} bReserve - The current reserve amount of asset B in the pool.
192
193
  * @param {bigint} laf - The linear amplification factor of the pool.
193
- * @returns {Fraction} The price of asset A in terms of asset B as a Fraction.
194
+ * @returns {Fraction} Raw A per raw B at the current reserves.
194
195
  */
195
196
  export declare function getPrice(aReserve: bigint, bReserve: bigint, laf: bigint): Fraction;
196
197
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"StableSwapsPool.d.ts","sourceRoot":"","sources":["../../../src/PoolMath/StableSwapsPool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACtE,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,KAAK,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAEpE;;;;;;;;;;;;;GAaG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,mBAAmB,EAAE,MAAM,EAC3B,eAAe,EAAE,MAAM,WAsBxB;AAED;;;GAGG;AACH,eAAO,MAAM,WAAW,EAAE,MAAa,CAAC;AAExC;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,EAAE,MAA2B,CAAC;AAE5D;;;GAGG;AACH,eAAO,MAAM,aAAa,EAAE,MAAgB,CAAC;AAE7C;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAyCvE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,OAAO,CACrB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,MAAM,GACnB,MAAM,CA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