@coffer_so/sdk 1.0.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.
- package/LICENSE +21 -0
- package/README.md +283 -0
- package/dist/clients/AdminClient.d.ts +194 -0
- package/dist/clients/AdminClient.d.ts.map +1 -0
- package/dist/clients/AdminClient.js +482 -0
- package/dist/clients/AdminClient.js.map +1 -0
- package/dist/clients/CofferBackendClient.d.ts +877 -0
- package/dist/clients/CofferBackendClient.d.ts.map +1 -0
- package/dist/clients/CofferBackendClient.js +536 -0
- package/dist/clients/CofferBackendClient.js.map +1 -0
- package/dist/clients/CubeBackendClient.d.ts +877 -0
- package/dist/clients/CubeBackendClient.d.ts.map +1 -0
- package/dist/clients/CubeBackendClient.js +536 -0
- package/dist/clients/CubeBackendClient.js.map +1 -0
- package/dist/clients/CubicPoolClient.d.ts +120 -0
- package/dist/clients/CubicPoolClient.d.ts.map +1 -0
- package/dist/clients/CubicPoolClient.js +687 -0
- package/dist/clients/CubicPoolClient.js.map +1 -0
- package/dist/clients/PoolFactoryClient.d.ts +53 -0
- package/dist/clients/PoolFactoryClient.d.ts.map +1 -0
- package/dist/clients/PoolFactoryClient.js +91 -0
- package/dist/clients/PoolFactoryClient.js.map +1 -0
- package/dist/clients/RpcClient.d.ts +49 -0
- package/dist/clients/RpcClient.d.ts.map +1 -0
- package/dist/clients/RpcClient.js +135 -0
- package/dist/clients/RpcClient.js.map +1 -0
- package/dist/clients/SingleTokenDepositClient.d.ts +67 -0
- package/dist/clients/SingleTokenDepositClient.d.ts.map +1 -0
- package/dist/clients/SingleTokenDepositClient.js +105 -0
- package/dist/clients/SingleTokenDepositClient.js.map +1 -0
- package/dist/clients/contract-instructions.d.ts +24 -0
- package/dist/clients/contract-instructions.d.ts.map +1 -0
- package/dist/clients/contract-instructions.js +130 -0
- package/dist/clients/contract-instructions.js.map +1 -0
- package/dist/clients/index.d.ts +10 -0
- package/dist/clients/index.d.ts.map +1 -0
- package/dist/clients/index.js +26 -0
- package/dist/clients/index.js.map +1 -0
- package/dist/clients/quote-math.d.ts +15 -0
- package/dist/clients/quote-math.d.ts.map +1 -0
- package/dist/clients/quote-math.js +98 -0
- package/dist/clients/quote-math.js.map +1 -0
- package/dist/clients/tx-builders.d.ts +231 -0
- package/dist/clients/tx-builders.d.ts.map +1 -0
- package/dist/clients/tx-builders.js +861 -0
- package/dist/clients/tx-builders.js.map +1 -0
- package/dist/clients/versioned.d.ts +34 -0
- package/dist/clients/versioned.d.ts.map +1 -0
- package/dist/clients/versioned.js +55 -0
- package/dist/clients/versioned.js.map +1 -0
- package/dist/config/index.d.ts +65 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +75 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/networks.d.ts +12 -0
- package/dist/config/networks.d.ts.map +1 -0
- package/dist/config/networks.js +51 -0
- package/dist/config/networks.js.map +1 -0
- package/dist/config/tokens.d.ts +19 -0
- package/dist/config/tokens.d.ts.map +1 -0
- package/dist/config/tokens.js +43 -0
- package/dist/config/tokens.js.map +1 -0
- package/dist/idl/cubic_pool.json +4203 -0
- package/dist/idl/index.d.ts +1769 -0
- package/dist/idl/index.d.ts.map +1 -0
- package/dist/idl/index.js +18 -0
- package/dist/idl/index.js.map +1 -0
- package/dist/idl/protocol_admin.json +2615 -0
- package/dist/idl/single_token_liquidity.json +1121 -0
- package/dist/index.d.ts +75 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +206 -0
- package/dist/index.js.map +1 -0
- package/dist/math/cubicMath.d.ts +40 -0
- package/dist/math/cubicMath.d.ts.map +1 -0
- package/dist/math/cubicMath.js +82 -0
- package/dist/math/cubicMath.js.map +1 -0
- package/dist/math/fixedPoint.d.ts +19 -0
- package/dist/math/fixedPoint.d.ts.map +1 -0
- package/dist/math/fixedPoint.js +86 -0
- package/dist/math/fixedPoint.js.map +1 -0
- package/dist/math/index.d.ts +9 -0
- package/dist/math/index.d.ts.map +1 -0
- package/dist/math/index.js +30 -0
- package/dist/math/index.js.map +1 -0
- package/dist/math/logExp.d.ts +6 -0
- package/dist/math/logExp.d.ts.map +1 -0
- package/dist/math/logExp.js +68 -0
- package/dist/math/logExp.js.map +1 -0
- package/dist/math/lpLoss.d.ts +44 -0
- package/dist/math/lpLoss.d.ts.map +1 -0
- package/dist/math/lpLoss.js +103 -0
- package/dist/math/lpLoss.js.map +1 -0
- package/dist/math/maxSelloff.d.ts +164 -0
- package/dist/math/maxSelloff.d.ts.map +1 -0
- package/dist/math/maxSelloff.js +397 -0
- package/dist/math/maxSelloff.js.map +1 -0
- package/dist/math/singleToken.d.ts +54 -0
- package/dist/math/singleToken.d.ts.map +1 -0
- package/dist/math/singleToken.js +188 -0
- package/dist/math/singleToken.js.map +1 -0
- package/dist/math/slippage.d.ts +13 -0
- package/dist/math/slippage.d.ts.map +1 -0
- package/dist/math/slippage.js +44 -0
- package/dist/math/slippage.js.map +1 -0
- package/dist/math/surgeFee.d.ts +24 -0
- package/dist/math/surgeFee.d.ts.map +1 -0
- package/dist/math/surgeFee.js +85 -0
- package/dist/math/surgeFee.js.map +1 -0
- package/dist/math/weightedMath.d.ts +20 -0
- package/dist/math/weightedMath.d.ts.map +1 -0
- package/dist/math/weightedMath.js +66 -0
- package/dist/math/weightedMath.js.map +1 -0
- package/dist/parsers/borsh.d.ts +28 -0
- package/dist/parsers/borsh.d.ts.map +1 -0
- package/dist/parsers/borsh.js +104 -0
- package/dist/parsers/borsh.js.map +1 -0
- package/dist/parsers/contracts.d.ts +26 -0
- package/dist/parsers/contracts.d.ts.map +1 -0
- package/dist/parsers/contracts.js +62 -0
- package/dist/parsers/contracts.js.map +1 -0
- package/dist/parsers/events.d.ts +3 -0
- package/dist/parsers/events.d.ts.map +1 -0
- package/dist/parsers/events.js +281 -0
- package/dist/parsers/events.js.map +1 -0
- package/dist/parsers/index.d.ts +6 -0
- package/dist/parsers/index.d.ts.map +1 -0
- package/dist/parsers/index.js +22 -0
- package/dist/parsers/index.js.map +1 -0
- package/dist/parsers/mintAccount.d.ts +24 -0
- package/dist/parsers/mintAccount.d.ts.map +1 -0
- package/dist/parsers/mintAccount.js +27 -0
- package/dist/parsers/mintAccount.js.map +1 -0
- package/dist/parsers/poolAccount.d.ts +141 -0
- package/dist/parsers/poolAccount.d.ts.map +1 -0
- package/dist/parsers/poolAccount.js +224 -0
- package/dist/parsers/poolAccount.js.map +1 -0
- package/dist/types/contracts.d.ts +1340 -0
- package/dist/types/contracts.d.ts.map +1 -0
- package/dist/types/contracts.js +3 -0
- package/dist/types/contracts.js.map +1 -0
- package/dist/types/events.d.ts +172 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +3 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +22 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/pool.d.ts +149 -0
- package/dist/types/pool.d.ts.map +1 -0
- package/dist/types/pool.js +3 -0
- package/dist/types/pool.js.map +1 -0
- package/dist/types/result.d.ts +24 -0
- package/dist/types/result.d.ts.map +1 -0
- package/dist/types/result.js +11 -0
- package/dist/types/result.js.map +1 -0
- package/dist/types/tx.d.ts +261 -0
- package/dist/types/tx.d.ts.map +1 -0
- package/dist/types/tx.js +3 -0
- package/dist/types/tx.js.map +1 -0
- package/dist/utils/errors.d.ts +44 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +192 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/extensions.d.ts +98 -0
- package/dist/utils/extensions.d.ts.map +1 -0
- package/dist/utils/extensions.js +187 -0
- package/dist/utils/extensions.js.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +21 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/pda.d.ts +8 -0
- package/dist/utils/pda.d.ts.map +1 -0
- package/dist/utils/pda.js +27 -0
- package/dist/utils/pda.js.map +1 -0
- package/dist/utils/retry.d.ts +22 -0
- package/dist/utils/retry.d.ts.map +1 -0
- package/dist/utils/retry.js +62 -0
- package/dist/utils/retry.js.map +1 -0
- package/package.json +54 -0
- package/src/clients/AdminClient.ts +608 -0
- package/src/clients/CofferBackendClient.ts +1413 -0
- package/src/clients/CubicPoolClient.ts +719 -0
- package/src/clients/PoolFactoryClient.ts +110 -0
- package/src/clients/RpcClient.ts +195 -0
- package/src/clients/SingleTokenDepositClient.ts +143 -0
- package/src/clients/contract-instructions.ts +115 -0
- package/src/clients/index.ts +9 -0
- package/src/clients/quote-math.ts +81 -0
- package/src/clients/tx-builders.ts +1096 -0
- package/src/clients/versioned.ts +80 -0
- package/src/config/index.ts +111 -0
- package/src/config/networks.ts +71 -0
- package/src/config/tokens.ts +52 -0
- package/src/idl/cubic_pool.json +4203 -0
- package/src/idl/index.ts +13 -0
- package/src/idl/protocol_admin.json +2615 -0
- package/src/idl/single_token_liquidity.json +1121 -0
- package/src/index.ts +262 -0
- package/src/math/cubicMath.ts +92 -0
- package/src/math/fixedPoint.ts +71 -0
- package/src/math/index.ts +13 -0
- package/src/math/logExp.ts +61 -0
- package/src/math/lpLoss.ts +160 -0
- package/src/math/maxSelloff.ts +564 -0
- package/src/math/singleToken.ts +251 -0
- package/src/math/slippage.ts +38 -0
- package/src/math/surgeFee.ts +101 -0
- package/src/math/weightedMath.ts +64 -0
- package/src/parsers/borsh.ts +106 -0
- package/src/parsers/contracts.ts +64 -0
- package/src/parsers/events.ts +275 -0
- package/src/parsers/index.ts +5 -0
- package/src/parsers/mintAccount.ts +44 -0
- package/src/parsers/poolAccount.ts +372 -0
- package/src/types/contracts.ts +238 -0
- package/src/types/events.ts +198 -0
- package/src/types/index.ts +5 -0
- package/src/types/pool.ts +147 -0
- package/src/types/result.ts +51 -0
- package/src/types/tx.ts +278 -0
- package/src/utils/errors.ts +210 -0
- package/src/utils/extensions.ts +196 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/pda.ts +58 -0
- package/src/utils/retry.ts +85 -0
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Sliding-window cumulative-sell rate limiter + variable sell-off surge fee.
|
|
4
|
+
*
|
|
5
|
+
* Port of the v5.1 (post-audit) contracts:
|
|
6
|
+
* `programs/cubic-pool/src/math/max_selloff.rs` (`check_and_advance`)
|
|
7
|
+
* `programs/cubic-pool/src/math/surge_fee.rs` (`calc_surge_fee_pct`)
|
|
8
|
+
* `programs/cubic-pool/src/instructions/user/swap.rs` (segmented charge)
|
|
9
|
+
*
|
|
10
|
+
* All arithmetic is bigint, mirroring the Rust u128 intermediates INCLUDING
|
|
11
|
+
* integer-division order — do not "simplify" a `a*b/c` into a different
|
|
12
|
+
* association, parity with the on-chain result depends on it. BN appears
|
|
13
|
+
* only at the SDK boundary.
|
|
14
|
+
*
|
|
15
|
+
* v5.1 curve model: the fee RATE is piecewise-LINEAR in the normalized
|
|
16
|
+
* window-fill coordinate `t ∈ [0, 1]` (threshold → 100% fill):
|
|
17
|
+
* - no kink: low → high over [0, 1]
|
|
18
|
+
* - kinked: low → mid over [0, k], mid → high over [k, 1]
|
|
19
|
+
* and a swap is charged the AVERAGE rate over the span it crosses
|
|
20
|
+
* (integral / width), which makes the fee path-independent — splitting a
|
|
21
|
+
* sell into parts cannot cheapen it (audit M-05). The old exponential
|
|
22
|
+
* convexity curve (A^t) is gone.
|
|
23
|
+
*/
|
|
24
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
25
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
26
|
+
};
|
|
27
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
+
exports.SURGE_FEE_SEGMENTS = void 0;
|
|
29
|
+
exports.projectSelloffWindow = projectSelloffWindow;
|
|
30
|
+
exports.computeSelloffWindow = computeSelloffWindow;
|
|
31
|
+
exports.calcSurgeFeePct = calcSurgeFeePct;
|
|
32
|
+
exports.calcSurgeFeeAmount = calcSurgeFeeAmount;
|
|
33
|
+
exports.calcSegmentedSurgeFeeAmount = calcSegmentedSurgeFeeAmount;
|
|
34
|
+
exports.checkAndAdvanceSelloff = checkAndAdvanceSelloff;
|
|
35
|
+
exports.rescaleSelloffWindow = rescaleSelloffWindow;
|
|
36
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
|
37
|
+
const config_1 = require("../config");
|
|
38
|
+
const fixedPoint_1 = require("./fixedPoint");
|
|
39
|
+
const SCALE = BigInt(config_1.PERCENT_SCALE); // 10_000
|
|
40
|
+
/** Mirrors cubic-pool::constants::SURGE_FEE_SEGMENTS. */
|
|
41
|
+
exports.SURGE_FEE_SEGMENTS = 4;
|
|
42
|
+
/**
|
|
43
|
+
* Pure bigint projection of the sliding window at `now`. Mirrors v5.1
|
|
44
|
+
* `check_and_advance`'s rotation + snapshot logic (read-only, no
|
|
45
|
+
* `amount_in`), including the audit addition: when the projection OPENS a
|
|
46
|
+
* window against a new vb snapshot, the carried-over `previous` usage is
|
|
47
|
+
* rescaled by `newSnapshot / oldSnapshot` so a liquidity change between
|
|
48
|
+
* windows doesn't distort the carried percentage.
|
|
49
|
+
*/
|
|
50
|
+
function projectSelloffWindow(params) {
|
|
51
|
+
const period = BigInt(params.periodLength);
|
|
52
|
+
const storedWs = params.windowStartTimestamp;
|
|
53
|
+
const nowBI = BigInt(Math.trunc(params.now));
|
|
54
|
+
const rawElapsed = nowBI - storedWs;
|
|
55
|
+
const elapsed = rawElapsed < 0n ? 0n : rawElapsed;
|
|
56
|
+
let prev;
|
|
57
|
+
let cur;
|
|
58
|
+
let ws;
|
|
59
|
+
let eiw;
|
|
60
|
+
let opened;
|
|
61
|
+
if (period > 0n && elapsed >= 2n * period) {
|
|
62
|
+
prev = 0n;
|
|
63
|
+
cur = 0n;
|
|
64
|
+
ws = nowBI;
|
|
65
|
+
eiw = 0n;
|
|
66
|
+
opened = true;
|
|
67
|
+
}
|
|
68
|
+
else if (period > 0n && elapsed >= period) {
|
|
69
|
+
ws = storedWs + period;
|
|
70
|
+
const e = nowBI - ws;
|
|
71
|
+
eiw = e < 0n ? 0n : e;
|
|
72
|
+
prev = params.currentSelloff;
|
|
73
|
+
cur = 0n;
|
|
74
|
+
opened = true;
|
|
75
|
+
}
|
|
76
|
+
else if (period <= 0n) {
|
|
77
|
+
// Degenerate config (rejected on-chain by `require!(period > 0)`);
|
|
78
|
+
// treat as a fresh reset so callers never divide by zero.
|
|
79
|
+
prev = 0n;
|
|
80
|
+
cur = 0n;
|
|
81
|
+
ws = nowBI;
|
|
82
|
+
eiw = 0n;
|
|
83
|
+
opened = true;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
prev = params.previousSelloff;
|
|
87
|
+
cur = params.currentSelloff;
|
|
88
|
+
ws = storedWs;
|
|
89
|
+
eiw = elapsed;
|
|
90
|
+
opened = false;
|
|
91
|
+
}
|
|
92
|
+
const oldSnapshot = params.selloffVbSnapshot;
|
|
93
|
+
const vbSnapshot = opened || oldSnapshot === 0n ? params.virtualBalance : oldSnapshot;
|
|
94
|
+
// v5.1: rescale the carried previous when the window opened against a
|
|
95
|
+
// DIFFERENT snapshot (mirrors max_selloff.rs — integer division order!).
|
|
96
|
+
if (opened && oldSnapshot > 0n && vbSnapshot !== oldSnapshot) {
|
|
97
|
+
prev = (prev * vbSnapshot) / oldSnapshot;
|
|
98
|
+
}
|
|
99
|
+
const cap = (BigInt(params.maxSelloffPct) * vbSnapshot) / SCALE;
|
|
100
|
+
const weightedPrev = period > 0n ? (prev * (period - eiw)) / period : 0n;
|
|
101
|
+
const usedWithoutTrade = weightedPrev + cur;
|
|
102
|
+
return {
|
|
103
|
+
opened,
|
|
104
|
+
windowStart: ws,
|
|
105
|
+
elapsedInWindow: eiw,
|
|
106
|
+
previous: prev,
|
|
107
|
+
current: cur,
|
|
108
|
+
vbSnapshot,
|
|
109
|
+
cap,
|
|
110
|
+
weightedPrev,
|
|
111
|
+
usedWithoutTrade,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/** Read-only status of a token's max-selloff window at `now`. */
|
|
115
|
+
function computeSelloffWindow(inputs) {
|
|
116
|
+
const enabled = inputs.maxSelloffPct > 0;
|
|
117
|
+
if (!enabled) {
|
|
118
|
+
return {
|
|
119
|
+
enabled: false,
|
|
120
|
+
cap: new bn_js_1.default(0),
|
|
121
|
+
used: new bn_js_1.default(0),
|
|
122
|
+
remaining: new bn_js_1.default(0),
|
|
123
|
+
fillPct: 0,
|
|
124
|
+
windowStartTimestamp: inputs.windowStartTimestamp,
|
|
125
|
+
periodLength: inputs.periodLength,
|
|
126
|
+
rotated: false,
|
|
127
|
+
vbSnapshot: new bn_js_1.default(0),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
const proj = projectSelloffWindow({
|
|
131
|
+
maxSelloffPct: inputs.maxSelloffPct,
|
|
132
|
+
periodLength: inputs.periodLength,
|
|
133
|
+
previousSelloff: BigInt(inputs.previousSelloff.toString()),
|
|
134
|
+
currentSelloff: BigInt(inputs.currentSelloff.toString()),
|
|
135
|
+
windowStartTimestamp: BigInt(inputs.windowStartTimestamp.toString()),
|
|
136
|
+
selloffVbSnapshot: BigInt(inputs.selloffVbSnapshot.toString()),
|
|
137
|
+
virtualBalance: BigInt(inputs.virtualBalance.toString()),
|
|
138
|
+
now: inputs.now,
|
|
139
|
+
});
|
|
140
|
+
const used = proj.usedWithoutTrade;
|
|
141
|
+
const remaining = proj.cap > used ? proj.cap - used : 0n;
|
|
142
|
+
const fillPct = proj.cap === 0n ? 0 : Number((used * SCALE) / proj.cap);
|
|
143
|
+
return {
|
|
144
|
+
enabled: true,
|
|
145
|
+
cap: new bn_js_1.default(proj.cap.toString()),
|
|
146
|
+
used: new bn_js_1.default(used.toString()),
|
|
147
|
+
remaining: new bn_js_1.default(remaining.toString()),
|
|
148
|
+
fillPct,
|
|
149
|
+
windowStartTimestamp: new bn_js_1.default(proj.windowStart.toString()),
|
|
150
|
+
periodLength: inputs.periodLength,
|
|
151
|
+
rotated: proj.opened,
|
|
152
|
+
vbSnapshot: new bn_js_1.default(proj.vbSnapshot.toString()),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
// ── v5.1 piecewise-linear surge curve ───────────────────────────────────────
|
|
156
|
+
/**
|
|
157
|
+
* Kink position in normalized-t space. `kinkPct` is WHOLE percent of window
|
|
158
|
+
* fill (0..=100, u8 on-chain); 0 (or a kink at/outside the [threshold, 100%]
|
|
159
|
+
* band) disables the kink and the curve degenerates to low → high.
|
|
160
|
+
* Port of `surge_fee.rs::kink_t`.
|
|
161
|
+
*/
|
|
162
|
+
function kinkT(kinkPct, thresholdPct) {
|
|
163
|
+
const thr = BigInt(thresholdPct);
|
|
164
|
+
if (kinkPct === 0 || thr >= SCALE)
|
|
165
|
+
return 0n;
|
|
166
|
+
const kinkFill = BigInt(kinkPct) * (SCALE / 100n);
|
|
167
|
+
if (kinkFill <= thr || kinkFill >= SCALE)
|
|
168
|
+
return 0n;
|
|
169
|
+
return ((kinkFill - thr) * fixedPoint_1.ONE) / (SCALE - thr);
|
|
170
|
+
}
|
|
171
|
+
/** Point rate at `t` (CEIL on the ramp, clamped to SCALE). Port of `rate_at`. */
|
|
172
|
+
function rateAt(tFp, slopeLowPct, slopeMidPct, slopeHighPct, kFp) {
|
|
173
|
+
const t = tFp < fixedPoint_1.ONE ? tFp : fixedPoint_1.ONE;
|
|
174
|
+
const low = BigInt(slopeLowPct);
|
|
175
|
+
const mid = BigInt(slopeMidPct);
|
|
176
|
+
const high = BigInt(slopeHighPct);
|
|
177
|
+
let base;
|
|
178
|
+
let span;
|
|
179
|
+
let num;
|
|
180
|
+
let den;
|
|
181
|
+
if (kFp === 0n) {
|
|
182
|
+
base = low;
|
|
183
|
+
span = high > low ? high - low : 0n;
|
|
184
|
+
num = t;
|
|
185
|
+
den = fixedPoint_1.ONE;
|
|
186
|
+
}
|
|
187
|
+
else if (t <= kFp) {
|
|
188
|
+
base = low;
|
|
189
|
+
span = mid > low ? mid - low : 0n;
|
|
190
|
+
num = t;
|
|
191
|
+
den = kFp;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
base = mid;
|
|
195
|
+
span = high > mid ? high - mid : 0n;
|
|
196
|
+
num = t - kFp;
|
|
197
|
+
den = fixedPoint_1.ONE - kFp;
|
|
198
|
+
}
|
|
199
|
+
const prod = span * num;
|
|
200
|
+
const add = prod / den + (prod % den !== 0n ? 1n : 0n);
|
|
201
|
+
const rate = base + add;
|
|
202
|
+
return rate < SCALE ? rate : SCALE;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* ∫ rate over [0, t] (1e18-scaled area). Port of `integral_to` — keep the
|
|
206
|
+
* `x*x/(2*den)` before the span multiply, exactly like the contract.
|
|
207
|
+
*/
|
|
208
|
+
function integralTo(tFp, slopeLowPct, slopeMidPct, slopeHighPct, kFp) {
|
|
209
|
+
const t = tFp < fixedPoint_1.ONE ? tFp : fixedPoint_1.ONE;
|
|
210
|
+
const low = BigInt(slopeLowPct);
|
|
211
|
+
const mid = BigInt(slopeMidPct);
|
|
212
|
+
const high = BigInt(slopeHighPct);
|
|
213
|
+
const ramp = (x, span, den) => {
|
|
214
|
+
if (den === 0n || span === 0n || x === 0n)
|
|
215
|
+
return 0n;
|
|
216
|
+
const halfSq = (x * x) / (2n * den);
|
|
217
|
+
return span * halfSq;
|
|
218
|
+
};
|
|
219
|
+
if (kFp === 0n) {
|
|
220
|
+
const span = high > low ? high - low : 0n;
|
|
221
|
+
return low * t + ramp(t, span, fixedPoint_1.ONE);
|
|
222
|
+
}
|
|
223
|
+
if (t <= kFp) {
|
|
224
|
+
const span = mid > low ? mid - low : 0n;
|
|
225
|
+
return low * t + ramp(t, span, kFp);
|
|
226
|
+
}
|
|
227
|
+
const first = (kFp * (low + mid)) / 2n;
|
|
228
|
+
const rest = t - kFp;
|
|
229
|
+
const span = high > mid ? high - mid : 0n;
|
|
230
|
+
return first + mid * rest + ramp(rest, span, fixedPoint_1.ONE - kFp);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Average surge-fee percentage (PERCENT_SCALE units, 0..=10_000) charged for
|
|
234
|
+
* moving the window fill from `effectiveSelloffBefore` to
|
|
235
|
+
* `effectiveSelloffAfter` against `cap`. Port of v5.1 `calc_surge_fee_pct`:
|
|
236
|
+
* the rate is INTEGRATED over the crossed span (CEIL), so the charge is
|
|
237
|
+
* path-independent. When the span is empty (`after <= before`) the point
|
|
238
|
+
* rate at `after` is returned.
|
|
239
|
+
*/
|
|
240
|
+
function calcSurgeFeePct(effectiveSelloffBefore, effectiveSelloffAfter, cap, thresholdPct, slopeLowPct, slopeMidPct, slopeHighPct, kinkPct) {
|
|
241
|
+
if (cap === 0n || slopeHighPct === 0)
|
|
242
|
+
return 0;
|
|
243
|
+
const thr = BigInt(thresholdPct);
|
|
244
|
+
if (thr >= SCALE)
|
|
245
|
+
return 0;
|
|
246
|
+
const kFp = kinkT(kinkPct, thresholdPct);
|
|
247
|
+
const fill = (x) => {
|
|
248
|
+
const raw = (x * SCALE) / cap;
|
|
249
|
+
return raw < SCALE ? raw : SCALE;
|
|
250
|
+
};
|
|
251
|
+
const f0 = fill(effectiveSelloffBefore);
|
|
252
|
+
const f1 = fill(effectiveSelloffAfter);
|
|
253
|
+
if (f1 <= thr)
|
|
254
|
+
return 0;
|
|
255
|
+
const denom = SCALE - thr;
|
|
256
|
+
const t1 = ((f1 - thr) * fixedPoint_1.ONE) / denom;
|
|
257
|
+
if (f1 <= f0) {
|
|
258
|
+
return Number(rateAt(t1, slopeLowPct, slopeMidPct, slopeHighPct, kFp));
|
|
259
|
+
}
|
|
260
|
+
const f0Clamped = f0 > thr ? f0 : thr;
|
|
261
|
+
const t0 = ((f0Clamped - thr) * fixedPoint_1.ONE) / denom;
|
|
262
|
+
const integral = integralTo(t1, slopeLowPct, slopeMidPct, slopeHighPct, kFp) -
|
|
263
|
+
integralTo(t0, slopeLowPct, slopeMidPct, slopeHighPct, kFp);
|
|
264
|
+
const integralClamped = integral > 0n ? integral : 0n;
|
|
265
|
+
const numerator = denom * integralClamped;
|
|
266
|
+
const width = f1 - f0Clamped;
|
|
267
|
+
const scaledDen = width * fixedPoint_1.ONE;
|
|
268
|
+
const avg = numerator / scaledDen + (numerator % scaledDen !== 0n ? 1n : 0n);
|
|
269
|
+
return Number(avg < SCALE ? avg : SCALE);
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* CEIL fee application of a known percentage on `amount`, clamped to
|
|
273
|
+
* `amount`. NOTE: the contract does NOT charge the whole output at one
|
|
274
|
+
* rate — use {@link calcSegmentedSurgeFeeAmount} to mirror an actual swap.
|
|
275
|
+
* This helper remains for coarse display math only.
|
|
276
|
+
*/
|
|
277
|
+
function calcSurgeFeeAmount(amountOut, surgePct) {
|
|
278
|
+
if (surgePct <= 0 || amountOut <= 0n)
|
|
279
|
+
return 0n;
|
|
280
|
+
const num = amountOut * BigInt(surgePct);
|
|
281
|
+
const fee = num / SCALE + (num % SCALE !== 0n ? 1n : 0n);
|
|
282
|
+
return fee < amountOut ? fee : amountOut;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Surge fee in OUTPUT-token units for one swap — the exact v5.1 on-chain
|
|
286
|
+
* charge (swap.rs): only the output produced ABOVE the threshold is taxed,
|
|
287
|
+
* segment by segment across the taxed span, each segment's REAL curve
|
|
288
|
+
* output at that segment's average rate (CEIL per segment), total clamped
|
|
289
|
+
* to `amountOut`. Charging one average rate on the whole output would
|
|
290
|
+
* re-open the audit M-05 split dodge on asymmetric pools.
|
|
291
|
+
*/
|
|
292
|
+
function calcSegmentedSurgeFeeAmount(p) {
|
|
293
|
+
const { effectiveSelloffBefore: before, effectiveSelloffAfter: after, cap, thresholdPct, slopeLowPct, slopeMidPct, slopeHighPct, kinkPct, amountInAfterFee, amountOut, } = p;
|
|
294
|
+
// Same precheck as swap.rs: whole-span average of 0 ⇒ nothing to charge.
|
|
295
|
+
const wholeSpanPct = calcSurgeFeePct(before, after, cap, thresholdPct, slopeLowPct, slopeMidPct, slopeHighPct, kinkPct);
|
|
296
|
+
if (wholeSpanPct === 0)
|
|
297
|
+
return 0n;
|
|
298
|
+
const thrUnits = (cap * BigInt(thresholdPct)) / SCALE;
|
|
299
|
+
const span = after > before ? after - before : 0n;
|
|
300
|
+
const taxedLo = before > thrUnits ? before : thrUnits;
|
|
301
|
+
if (span === 0n || after <= taxedLo)
|
|
302
|
+
return 0n;
|
|
303
|
+
const segments = BigInt(p.segments ?? exports.SURGE_FEE_SEGMENTS);
|
|
304
|
+
// Flooring the input slice understates the output, which overstates the
|
|
305
|
+
// taxed remainder — the safe (conservative) direction, as on-chain.
|
|
306
|
+
const cumulativeOut = (u) => {
|
|
307
|
+
if (u <= before)
|
|
308
|
+
return 0n;
|
|
309
|
+
if (u >= after)
|
|
310
|
+
return amountOut;
|
|
311
|
+
const x = (amountInAfterFee * (u - before)) / span;
|
|
312
|
+
return p.curveOut(x);
|
|
313
|
+
};
|
|
314
|
+
const taxedWidth = after - taxedLo;
|
|
315
|
+
let feeAcc = 0n;
|
|
316
|
+
let prevU = taxedLo;
|
|
317
|
+
let prevY = cumulativeOut(taxedLo);
|
|
318
|
+
for (let k = 1n; k <= segments; k++) {
|
|
319
|
+
const u = k === segments ? after : taxedLo + (taxedWidth * k) / segments;
|
|
320
|
+
const y = cumulativeOut(u);
|
|
321
|
+
const segOut = y > prevY ? y - prevY : 0n;
|
|
322
|
+
if (segOut > 0n && u > prevU) {
|
|
323
|
+
const segPct = BigInt(calcSurgeFeePct(prevU, u, cap, thresholdPct, slopeLowPct, slopeMidPct, slopeHighPct, kinkPct));
|
|
324
|
+
const num = segOut * segPct;
|
|
325
|
+
feeAcc += num / SCALE + (num % SCALE !== 0n ? 1n : 0n);
|
|
326
|
+
}
|
|
327
|
+
prevU = u;
|
|
328
|
+
prevY = y;
|
|
329
|
+
}
|
|
330
|
+
return feeAcc < amountOut ? feeAcc : amountOut;
|
|
331
|
+
}
|
|
332
|
+
const I64_MIN = -(1n << 63n), I64_MAX = (1n << 63n) - 1n;
|
|
333
|
+
function i64(v) {
|
|
334
|
+
if (v < I64_MIN || v > I64_MAX)
|
|
335
|
+
throw new Error("timestamp: outside i64");
|
|
336
|
+
return v;
|
|
337
|
+
}
|
|
338
|
+
function saturatingSubI64(a, b) { const v = a - b; return v < I64_MIN ? I64_MIN : v > I64_MAX ? I64_MAX : v; }
|
|
339
|
+
/** Pure: does not modify its input, including on rejection. */
|
|
340
|
+
function checkAndAdvanceSelloff(params) {
|
|
341
|
+
const { state, maxSelloffPct, period, amountIn, virtualBalance, now } = params;
|
|
342
|
+
(0, fixedPoint_1.assertInteger)(maxSelloffPct, 0, 10000, "max selloff percent");
|
|
343
|
+
if (maxSelloffPct === 0)
|
|
344
|
+
return null;
|
|
345
|
+
(0, fixedPoint_1.assertInteger)(period, 1, 0xffffffff, "selloff period");
|
|
346
|
+
(0, fixedPoint_1.assertU64)(amountIn);
|
|
347
|
+
(0, fixedPoint_1.assertU64)(virtualBalance);
|
|
348
|
+
(0, fixedPoint_1.assertU64)(state.previousSelloff);
|
|
349
|
+
(0, fixedPoint_1.assertU64)(state.currentSelloff);
|
|
350
|
+
(0, fixedPoint_1.assertU64)(state.selloffVbSnapshot);
|
|
351
|
+
i64(now);
|
|
352
|
+
i64(state.windowStartTimestamp);
|
|
353
|
+
const p = BigInt(period), rawElapsed = saturatingSubI64(now, state.windowStartTimestamp);
|
|
354
|
+
let elapsed = rawElapsed < 0n ? 0n : rawElapsed;
|
|
355
|
+
let previous = state.previousSelloff, current = state.currentSelloff, windowStart = state.windowStartTimestamp, opened = false;
|
|
356
|
+
if (elapsed >= 2n * p) {
|
|
357
|
+
previous = 0n;
|
|
358
|
+
current = 0n;
|
|
359
|
+
windowStart = now;
|
|
360
|
+
elapsed = 0n;
|
|
361
|
+
opened = true;
|
|
362
|
+
}
|
|
363
|
+
else if (elapsed >= p) {
|
|
364
|
+
previous = current;
|
|
365
|
+
current = 0n;
|
|
366
|
+
windowStart = i64(windowStart + p);
|
|
367
|
+
elapsed = saturatingSubI64(now, windowStart);
|
|
368
|
+
if (elapsed < 0n)
|
|
369
|
+
elapsed = 0n;
|
|
370
|
+
opened = true;
|
|
371
|
+
}
|
|
372
|
+
const snapshot = opened || state.selloffVbSnapshot === 0n ? virtualBalance : state.selloffVbSnapshot;
|
|
373
|
+
if (opened && state.selloffVbSnapshot > 0n && snapshot !== state.selloffVbSnapshot)
|
|
374
|
+
previous = (0, fixedPoint_1.assertU64)((0, fixedPoint_1.assertU128)(previous * snapshot) / state.selloffVbSnapshot);
|
|
375
|
+
const cap = (0, fixedPoint_1.assertU64)(BigInt(maxSelloffPct) * snapshot / 10000n);
|
|
376
|
+
const before = previous * (p - elapsed) / p + current;
|
|
377
|
+
const effective = before + amountIn;
|
|
378
|
+
if (effective > cap)
|
|
379
|
+
throw new Error("MaxSelloffExceeded");
|
|
380
|
+
return { effectiveSelloffBefore: before, effectiveSelloff: effective, maxSelloffCap: cap, vbSnapshot: snapshot, previousSelloff: previous, currentSelloff: (0, fixedPoint_1.assertU64)(current + amountIn), windowStartTimestamp: windowStart };
|
|
381
|
+
}
|
|
382
|
+
/** Mirror of window rescaling after proportional add/remove liquidity. */
|
|
383
|
+
function rescaleSelloffWindow(state, ratio, isIncrease, maxSelloffPct) {
|
|
384
|
+
(0, fixedPoint_1.assertU128)(ratio);
|
|
385
|
+
(0, fixedPoint_1.assertInteger)(maxSelloffPct, 0, 10000, "max selloff percent");
|
|
386
|
+
if (ratio === 0n || maxSelloffPct === 0)
|
|
387
|
+
return { ...state };
|
|
388
|
+
const scale = (value) => {
|
|
389
|
+
(0, fixedPoint_1.assertU64)(value);
|
|
390
|
+
if (value === 0n)
|
|
391
|
+
return 0n;
|
|
392
|
+
const delta = (0, fixedPoint_1.mulDown)(value, ratio);
|
|
393
|
+
return isIncrease ? (0, fixedPoint_1.assertU64)(value + delta) : value > delta ? value - delta : 0n;
|
|
394
|
+
};
|
|
395
|
+
return { ...state, previousSelloff: scale(state.previousSelloff), currentSelloff: scale(state.currentSelloff), selloffVbSnapshot: scale(state.selloffVbSnapshot) };
|
|
396
|
+
}
|
|
397
|
+
//# sourceMappingURL=maxSelloff.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"maxSelloff.js","sourceRoot":"","sources":["../../src/math/maxSelloff.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;;;;;;AAiEH,oDA6EC;AAGD,oDA0CC;AAwGD,0CA8CC;AAQD,gDAKC;AAmCD,kEAwEC;AA2BD,wDAgDC;AAED,oDAQC;AA5hBD,kDAAuB;AACvB,sCAA0C;AAC1C,6CAAkF;AAElF,MAAM,KAAK,GAAG,MAAM,CAAC,sBAAa,CAAC,CAAC,CAAC,SAAS;AAE9C,yDAAyD;AAC5C,QAAA,kBAAkB,GAAG,CAAC,CAAC;AAgDpC;;;;;;;GAOG;AACH,SAAgB,oBAAoB,CAAC,MASpC;IACC,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;IAC3C,MAAM,QAAQ,GAAG,MAAM,CAAC,oBAAoB,CAAC;IAC7C,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;IAE7C,MAAM,UAAU,GAAG,KAAK,GAAG,QAAQ,CAAC;IACpC,MAAM,OAAO,GAAG,UAAU,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC;IAElD,IAAI,IAAY,CAAC;IACjB,IAAI,GAAW,CAAC;IAChB,IAAI,EAAU,CAAC;IACf,IAAI,GAAW,CAAC;IAChB,IAAI,MAAe,CAAC;IAEpB,IAAI,MAAM,GAAG,EAAE,IAAI,OAAO,IAAI,EAAE,GAAG,MAAM,EAAE,CAAC;QAC1C,IAAI,GAAG,EAAE,CAAC;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AAG,KAAK,EAAE,eAAe,EAAE,KAAK,CAAC,KAAK,CAAC,eAAe,CAAC,EAAE,cAAc,EAAE,KAAK,CAAC,KAAK,CAAC,cAAc,CAAC,EAAE,iBAAiB,EAAE,KAAK,CAAC,KAAK,CAAC,iBAAiB,CAAC,EAAE,CAAC;AACvK,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export interface AllocationResult {
|
|
2
|
+
/** Per-token integer amount allocations; sum = amountIn. */
|
|
3
|
+
allocations: bigint[];
|
|
4
|
+
/** Scaled W values, for inspection/debugging. */
|
|
5
|
+
wScaled: bigint[];
|
|
6
|
+
/** Sum of wScaled. */
|
|
7
|
+
sumW: bigint;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Port of stld `compute_allocations` from
|
|
11
|
+
* `contracts/programs/single-token-liquidity/src/math.rs`.
|
|
12
|
+
*
|
|
13
|
+
* W_i = weight_i * min(actual_i, virtual_i) / virtual_i.
|
|
14
|
+
* Division order matches the two integer divisions on-chain.
|
|
15
|
+
* share_i = W_i / ΣW. amount_alloc[i] = amountIn * share_i.
|
|
16
|
+
*
|
|
17
|
+
* Integer truncation of per-token amounts routes the remainder to the
|
|
18
|
+
* input-token slot so Σ allocations == amountIn exactly.
|
|
19
|
+
*/
|
|
20
|
+
export declare function computeAllocations(params: {
|
|
21
|
+
actualBalances: bigint[];
|
|
22
|
+
virtualBalances: bigint[];
|
|
23
|
+
weightsBps: number[];
|
|
24
|
+
amountIn: bigint;
|
|
25
|
+
tokenInIndex: number;
|
|
26
|
+
}): AllocationResult;
|
|
27
|
+
/** @deprecated Analytical base-fee-only optimizer. The deployed helper uses computeAllocations; this does not model selloff/surge. */
|
|
28
|
+
export declare function computeTwoTokenOptimalAllocations(params: {
|
|
29
|
+
actualBalances: [bigint, bigint];
|
|
30
|
+
virtualBalances: [bigint, bigint];
|
|
31
|
+
protocolFeesOwed: [bigint, bigint];
|
|
32
|
+
weightsBps: [number, number];
|
|
33
|
+
amountIn: bigint;
|
|
34
|
+
tokenInIndex: 0 | 1;
|
|
35
|
+
swapFeeRate: number;
|
|
36
|
+
protocolFeeRate: number;
|
|
37
|
+
}): AllocationResult;
|
|
38
|
+
/**
|
|
39
|
+
* Mirror of stld `cap_deposit_amounts_to_lp_ratio`.
|
|
40
|
+
*
|
|
41
|
+
* Stored actual balances already exclude protocol fees. The helper deposits
|
|
42
|
+
* the common raw-actual ratio and refunds the remaining basket.
|
|
43
|
+
*/
|
|
44
|
+
export declare function capDepositAmountsToLpRatio(params: {
|
|
45
|
+
helperBalances: bigint[];
|
|
46
|
+
actualBalances: bigint[];
|
|
47
|
+
/** Retained for compatibility; actual balances already exclude these fees. */
|
|
48
|
+
protocolFeesOwed?: bigint[];
|
|
49
|
+
}): {
|
|
50
|
+
depositAmounts: bigint[];
|
|
51
|
+
refundAmounts: bigint[];
|
|
52
|
+
lpBalancesForAdd: bigint[];
|
|
53
|
+
};
|
|
54
|
+
//# sourceMappingURL=singleToken.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"singleToken.d.ts","sourceRoot":"","sources":["../../src/math/singleToken.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,gBAAgB;IAC/B,4DAA4D;IAC5D,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,iDAAiD;IACjD,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,sBAAsB;IACtB,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE;IACzC,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;CACtB,GAAG,gBAAgB,CAgCnB;AAwFD,sIAAsI;AACtI,wBAAgB,iCAAiC,CAAC,MAAM,EAAE;IACxD,cAAc,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,eAAe,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAClC,gBAAgB,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACnC,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,CAAC,GAAG,CAAC,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,EAAE,MAAM,CAAC;CACzB,GAAG,gBAAgB,CAwCnB;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,MAAM,EAAE;IACjD,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,8EAA8E;IAC9E,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;CAC7B,GAAG;IAAE,cAAc,EAAE,MAAM,EAAE,CAAC;IAAC,aAAa,EAAE,MAAM,EAAE,CAAC;IAAC,gBAAgB,EAAE,MAAM,EAAE,CAAA;CAAE,CAqCpF"}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.computeAllocations = computeAllocations;
|
|
4
|
+
exports.computeTwoTokenOptimalAllocations = computeTwoTokenOptimalAllocations;
|
|
5
|
+
exports.capDepositAmountsToLpRatio = capDepositAmountsToLpRatio;
|
|
6
|
+
const fixedPoint_1 = require("./fixedPoint");
|
|
7
|
+
const cubicMath_1 = require("./cubicMath");
|
|
8
|
+
const slippage_1 = require("./slippage");
|
|
9
|
+
/**
|
|
10
|
+
* Port of stld `compute_allocations` from
|
|
11
|
+
* `contracts/programs/single-token-liquidity/src/math.rs`.
|
|
12
|
+
*
|
|
13
|
+
* W_i = weight_i * min(actual_i, virtual_i) / virtual_i.
|
|
14
|
+
* Division order matches the two integer divisions on-chain.
|
|
15
|
+
* share_i = W_i / ΣW. amount_alloc[i] = amountIn * share_i.
|
|
16
|
+
*
|
|
17
|
+
* Integer truncation of per-token amounts routes the remainder to the
|
|
18
|
+
* input-token slot so Σ allocations == amountIn exactly.
|
|
19
|
+
*/
|
|
20
|
+
function computeAllocations(params) {
|
|
21
|
+
const { actualBalances, virtualBalances, weightsBps, amountIn, tokenInIndex } = params;
|
|
22
|
+
(0, fixedPoint_1.assertU64)(amountIn, "amountIn");
|
|
23
|
+
const n = actualBalances.length;
|
|
24
|
+
if (n !== virtualBalances.length || n !== weightsBps.length) {
|
|
25
|
+
throw new Error("computeAllocations: length mismatch");
|
|
26
|
+
}
|
|
27
|
+
if (!Number.isInteger(tokenInIndex) || tokenInIndex < 0 || tokenInIndex >= n) {
|
|
28
|
+
throw new Error("computeAllocations: tokenInIndex out of range");
|
|
29
|
+
}
|
|
30
|
+
const wScaled = [];
|
|
31
|
+
for (let i = 0; i < n; i++) {
|
|
32
|
+
if (virtualBalances[i] <= 0n) {
|
|
33
|
+
throw new Error(`computeAllocations: virtualBalance[${i}] must be positive`);
|
|
34
|
+
}
|
|
35
|
+
(0, fixedPoint_1.assertU64)(actualBalances[i]);
|
|
36
|
+
(0, fixedPoint_1.assertU64)(virtualBalances[i]);
|
|
37
|
+
(0, fixedPoint_1.assertInteger)(weightsBps[i], 0, 10000, "weight");
|
|
38
|
+
const effectiveActual = actualBalances[i] < virtualBalances[i] ? actualBalances[i] : virtualBalances[i];
|
|
39
|
+
const scaled = (0, fixedPoint_1.mulDivDown)(effectiveActual, fixedPoint_1.ONE, virtualBalances[i]);
|
|
40
|
+
const w = (0, fixedPoint_1.mulDivDown)(scaled, BigInt(weightsBps[i]), 10000n);
|
|
41
|
+
wScaled.push(w);
|
|
42
|
+
}
|
|
43
|
+
const sumW = wScaled.reduce((a, b) => a + b, 0n);
|
|
44
|
+
if (sumW === 0n) {
|
|
45
|
+
throw new Error("computeAllocations: pool not seeded (ΣW = 0)");
|
|
46
|
+
}
|
|
47
|
+
const allocations = wScaled.map((w) => (amountIn * w) / sumW);
|
|
48
|
+
const sumAlloc = allocations.reduce((a, b) => a + b, 0n);
|
|
49
|
+
if (sumAlloc < amountIn) {
|
|
50
|
+
allocations[tokenInIndex] += amountIn - sumAlloc;
|
|
51
|
+
}
|
|
52
|
+
return { allocations, wScaled, sumW };
|
|
53
|
+
}
|
|
54
|
+
function swapFeeAmount(amount, swapFeeRate) {
|
|
55
|
+
return (0, slippage_1.calculateSwapFee)(amount, swapFeeRate);
|
|
56
|
+
}
|
|
57
|
+
function amountAfterSwapFee(amount, swapFeeRate) {
|
|
58
|
+
const fee = swapFeeAmount(amount, swapFeeRate);
|
|
59
|
+
if (swapFeeRate > 0 && fee === 0n) {
|
|
60
|
+
throw new Error("computeTwoTokenOptimalAllocations: swap amount too small, fee rounds to zero");
|
|
61
|
+
}
|
|
62
|
+
return amount - fee;
|
|
63
|
+
}
|
|
64
|
+
function protocolFeeAmount(swapFee, protocolFeeRate) {
|
|
65
|
+
return (0, slippage_1.calculateProtocolFee)(swapFee, protocolFeeRate);
|
|
66
|
+
}
|
|
67
|
+
function scoreTwoTokenSwap(params) {
|
|
68
|
+
const { actualBalances, virtualBalances, protocolFeesOwed, weightsBps, amountIn, tokenInIndex, swapToOther, swapFeeRate, protocolFeeRate, } = params;
|
|
69
|
+
const other = tokenInIndex === 0 ? 1 : 0;
|
|
70
|
+
const remainingInput = amountIn - swapToOther;
|
|
71
|
+
if (swapToOther <= 0n || remainingInput <= 0n)
|
|
72
|
+
return { score: 0n, amountOut: 0n };
|
|
73
|
+
let afterFee;
|
|
74
|
+
try {
|
|
75
|
+
afterFee = amountAfterSwapFee(swapToOther, swapFeeRate);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return { score: 0n, amountOut: 0n };
|
|
79
|
+
}
|
|
80
|
+
const swapFee = swapToOther - afterFee;
|
|
81
|
+
const protocolFee = protocolFeeAmount(swapFee, protocolFeeRate);
|
|
82
|
+
const inLp = (0, slippage_1.lpBalances)(actualBalances[tokenInIndex], virtualBalances[tokenInIndex], protocolFeesOwed[tokenInIndex]);
|
|
83
|
+
const outLp = (0, slippage_1.lpBalances)(actualBalances[other], virtualBalances[other], protocolFeesOwed[other]);
|
|
84
|
+
let amountOut;
|
|
85
|
+
try {
|
|
86
|
+
amountOut = (0, cubicMath_1.calcOutGivenIn)({
|
|
87
|
+
virtualBalanceIn: inLp.lpVirtual,
|
|
88
|
+
weightInBps: BigInt(weightsBps[tokenInIndex]),
|
|
89
|
+
virtualBalanceOut: outLp.lpVirtual,
|
|
90
|
+
weightOutBps: BigInt(weightsBps[other]),
|
|
91
|
+
amountIn: afterFee,
|
|
92
|
+
actualBalanceOut: outLp.lpActual,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
return { score: 0n, amountOut: 0n };
|
|
97
|
+
}
|
|
98
|
+
if (amountOut <= 0n || amountOut >= outLp.lpActual)
|
|
99
|
+
return { score: 0n, amountOut };
|
|
100
|
+
const lpInAfter = inLp.lpActual + swapToOther - protocolFee;
|
|
101
|
+
const lpOutAfter = outLp.lpActual - amountOut;
|
|
102
|
+
if (lpInAfter <= 0n || lpOutAfter <= 0n)
|
|
103
|
+
return { score: 0n, amountOut };
|
|
104
|
+
const ratioIn = (remainingInput * fixedPoint_1.ONE) / lpInAfter;
|
|
105
|
+
const ratioOut = (amountOut * fixedPoint_1.ONE) / lpOutAfter;
|
|
106
|
+
return { score: ratioIn < ratioOut ? ratioIn : ratioOut, amountOut };
|
|
107
|
+
}
|
|
108
|
+
/** @deprecated Analytical base-fee-only optimizer. The deployed helper uses computeAllocations; this does not model selloff/surge. */
|
|
109
|
+
function computeTwoTokenOptimalAllocations(params) {
|
|
110
|
+
const { amountIn, tokenInIndex } = params;
|
|
111
|
+
if (amountIn <= 1n) {
|
|
112
|
+
throw new Error("computeTwoTokenOptimalAllocations: amountIn too small");
|
|
113
|
+
}
|
|
114
|
+
let low = 1n;
|
|
115
|
+
let high = amountIn - 1n;
|
|
116
|
+
for (let i = 0; i < 32 && low < high; i++) {
|
|
117
|
+
const mid = low + (high - low) / 2n;
|
|
118
|
+
const current = scoreTwoTokenSwap({ ...params, swapToOther: mid }).score;
|
|
119
|
+
const next = scoreTwoTokenSwap({
|
|
120
|
+
...params,
|
|
121
|
+
swapToOther: mid + 1n < amountIn ? mid + 1n : amountIn - 1n,
|
|
122
|
+
}).score;
|
|
123
|
+
if (next >= current)
|
|
124
|
+
low = mid + 1n;
|
|
125
|
+
else
|
|
126
|
+
high = mid;
|
|
127
|
+
}
|
|
128
|
+
const center = low < 1n ? 1n : low >= amountIn ? amountIn - 1n : low;
|
|
129
|
+
let bestSwap = center;
|
|
130
|
+
let bestScore = 0n;
|
|
131
|
+
const start = center > 8n ? center - 8n : 1n;
|
|
132
|
+
const end = center + 8n < amountIn ? center + 8n : amountIn - 1n;
|
|
133
|
+
for (let swapToOther = start; swapToOther <= end; swapToOther++) {
|
|
134
|
+
const { score, amountOut } = scoreTwoTokenSwap({ ...params, swapToOther });
|
|
135
|
+
if (amountOut > 0n && score > bestScore) {
|
|
136
|
+
bestScore = score;
|
|
137
|
+
bestSwap = swapToOther;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (bestScore === 0n) {
|
|
141
|
+
throw new Error("computeTwoTokenOptimalAllocations: amount too small");
|
|
142
|
+
}
|
|
143
|
+
const allocations = [0n, 0n];
|
|
144
|
+
const other = tokenInIndex === 0 ? 1 : 0;
|
|
145
|
+
allocations[tokenInIndex] = amountIn - bestSwap;
|
|
146
|
+
allocations[other] = bestSwap;
|
|
147
|
+
return { allocations, wScaled: [], sumW: bestScore };
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Mirror of stld `cap_deposit_amounts_to_lp_ratio`.
|
|
151
|
+
*
|
|
152
|
+
* Stored actual balances already exclude protocol fees. The helper deposits
|
|
153
|
+
* the common raw-actual ratio and refunds the remaining basket.
|
|
154
|
+
*/
|
|
155
|
+
function capDepositAmountsToLpRatio(params) {
|
|
156
|
+
const { helperBalances, actualBalances, protocolFeesOwed } = params;
|
|
157
|
+
const n = helperBalances.length;
|
|
158
|
+
if (n !== actualBalances.length || (protocolFeesOwed && n !== protocolFeesOwed.length)) {
|
|
159
|
+
throw new Error("capDepositAmountsToLpRatio: length mismatch");
|
|
160
|
+
}
|
|
161
|
+
helperBalances.forEach((b) => (0, fixedPoint_1.assertU64)(b));
|
|
162
|
+
const lpBalancesForAdd = actualBalances.map((actual) => (0, fixedPoint_1.assertU64)(actual));
|
|
163
|
+
let ratioMin = null;
|
|
164
|
+
for (let i = 0; i < n; i++) {
|
|
165
|
+
if (lpBalancesForAdd[i] === 0n)
|
|
166
|
+
continue;
|
|
167
|
+
if (helperBalances[i] <= 0n) {
|
|
168
|
+
throw new Error("capDepositAmountsToLpRatio: amount too small");
|
|
169
|
+
}
|
|
170
|
+
const ratio = (helperBalances[i] * fixedPoint_1.ONE) / lpBalancesForAdd[i];
|
|
171
|
+
ratioMin = ratioMin === null || ratio < ratioMin ? ratio : ratioMin;
|
|
172
|
+
}
|
|
173
|
+
if (ratioMin === null || ratioMin === 0n) {
|
|
174
|
+
throw new Error("capDepositAmountsToLpRatio: amount too small");
|
|
175
|
+
}
|
|
176
|
+
const depositAmounts = lpBalancesForAdd.map((lp, i) => {
|
|
177
|
+
if (lp === 0n)
|
|
178
|
+
return 0n;
|
|
179
|
+
const amount = (lp * ratioMin) / fixedPoint_1.ONE;
|
|
180
|
+
if (amount <= 0n) {
|
|
181
|
+
throw new Error("capDepositAmountsToLpRatio: amount too small");
|
|
182
|
+
}
|
|
183
|
+
return amount > helperBalances[i] ? helperBalances[i] : amount;
|
|
184
|
+
});
|
|
185
|
+
const refundAmounts = helperBalances.map((balance, i) => balance > depositAmounts[i] ? balance - depositAmounts[i] : 0n);
|
|
186
|
+
return { depositAmounts, refundAmounts, lpBalancesForAdd };
|
|
187
|
+
}
|
|
188
|
+
//# sourceMappingURL=singleToken.js.map
|