@cubee_ee/sdk 0.5.3 → 0.5.4
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/clients/CubeBackendClient.d.ts +53 -6
- package/dist/clients/CubeBackendClient.d.ts.map +1 -1
- package/dist/clients/CubeBackendClient.js +24 -4
- package/dist/clients/CubeBackendClient.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/clients/CubeBackendClient.ts +71 -7
- package/src/index.ts +3 -0
- package/dist/math/lpLoss.d.ts +0 -44
- package/dist/math/lpLoss.d.ts.map +0 -1
- package/dist/math/lpLoss.js +0 -103
- package/dist/math/lpLoss.js.map +0 -1
- package/dist/math/maxSelloff.d.ts +0 -138
- package/dist/math/maxSelloff.d.ts.map +0 -1
- package/dist/math/maxSelloff.js +0 -330
- package/dist/math/maxSelloff.js.map +0 -1
- package/dist/math/selloff.d.ts +0 -89
- package/dist/math/selloff.d.ts.map +0 -1
- package/dist/math/selloff.js +0 -161
- package/dist/math/selloff.js.map +0 -1
package/dist/math/selloff.js
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Off-chain replicas of the cubic-pool v5 sell-off protections:
|
|
4
|
-
*
|
|
5
|
-
* - max-selloff sliding window — `math/max_selloff.rs::check_and_advance`
|
|
6
|
-
* - variable surge-fee curve — `math/surge_fee.rs::calc_surge_fee_pct`
|
|
7
|
-
*
|
|
8
|
-
* Used by routers/quoters to (a) avoid building routes that would revert
|
|
9
|
-
* with `MaxSelloffExceeded` — the window is a HARD cap on how much of a
|
|
10
|
-
* token can be sold into the pool per window — and (b) quote `amount_out`
|
|
11
|
-
* NET of the surge fee, which is the value the contract checks
|
|
12
|
-
* `minimum_amount_out` against.
|
|
13
|
-
*
|
|
14
|
-
* All arithmetic is bigint and mirrors the contract's u128 intermediates
|
|
15
|
-
* and rounding (cap floored, surge fee rounded UP). `powFp` is the same
|
|
16
|
-
* LogExpMath port the pool uses on-chain.
|
|
17
|
-
*/
|
|
18
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
-
exports.PERCENT_SCALE = void 0;
|
|
20
|
-
exports.resolveSelloffWindow = resolveSelloffWindow;
|
|
21
|
-
exports.calcSurgeFeePct = calcSurgeFeePct;
|
|
22
|
-
exports.applySurgeFee = applySurgeFee;
|
|
23
|
-
const logExp_1 = require("./logExp");
|
|
24
|
-
/** Scale shared by all `_pct` config fields: 10_000 = 100%. */
|
|
25
|
-
exports.PERCENT_SCALE = 10000n;
|
|
26
|
-
const ONE = 1000000000000000000n;
|
|
27
|
-
/** Convexity base A for the surge curve (contracts: SURGE_FEE_CONVEXITY_FP). */
|
|
28
|
-
const SURGE_FEE_CONVEXITY_FP = 4n * ONE;
|
|
29
|
-
/**
|
|
30
|
-
* Resolve a token's sell-off window state at `nowSecs`, WITHOUT mutating
|
|
31
|
-
* anything — the read-only counterpart of the contract's
|
|
32
|
-
* `check_and_advance` (which the real swap will run).
|
|
33
|
-
*
|
|
34
|
-
* Rotation rules (contract-identical):
|
|
35
|
-
* - `elapsed >= 2×period` → both buckets aged out; cleared, re-snapshot.
|
|
36
|
-
* - `elapsed >= period` → `previous = current`, `current = 0`,
|
|
37
|
-
* `window_start += period` (slides one period, not to `now`), re-snapshot.
|
|
38
|
-
* - otherwise → unchanged; reuse stored snapshot (or capture
|
|
39
|
-
* the live vb when the snapshot is still `0` / uninitialised).
|
|
40
|
-
*/
|
|
41
|
-
function resolveSelloffWindow(input, nowSecs) {
|
|
42
|
-
const pct = BigInt(input.maxSelloffPct);
|
|
43
|
-
if (pct === 0n) {
|
|
44
|
-
return {
|
|
45
|
-
enabled: false,
|
|
46
|
-
vbSnapshot: 0n,
|
|
47
|
-
cap: 0n,
|
|
48
|
-
effectiveSelloff: 0n,
|
|
49
|
-
headroom: 0n,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
const period = BigInt(input.periodLengthSecs);
|
|
53
|
-
if (period <= 0n) {
|
|
54
|
-
// Unreachable on-chain (set_max_selloff validates period > 0 when
|
|
55
|
-
// pct > 0); treat defensively as "window accepts nothing".
|
|
56
|
-
return {
|
|
57
|
-
enabled: true,
|
|
58
|
-
vbSnapshot: input.selloffVbSnapshot,
|
|
59
|
-
cap: 0n,
|
|
60
|
-
effectiveSelloff: 0n,
|
|
61
|
-
headroom: 0n,
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
const now = BigInt(Math.floor(nowSecs));
|
|
65
|
-
// Clock skew clamps to 0 rather than rotating backwards.
|
|
66
|
-
let elapsed = now - input.windowStartTimestamp;
|
|
67
|
-
if (elapsed < 0n)
|
|
68
|
-
elapsed = 0n;
|
|
69
|
-
let previous;
|
|
70
|
-
let current;
|
|
71
|
-
let elapsedInWindow;
|
|
72
|
-
let openedWindow;
|
|
73
|
-
if (elapsed >= 2n * period) {
|
|
74
|
-
previous = 0n;
|
|
75
|
-
current = 0n;
|
|
76
|
-
elapsedInWindow = 0n;
|
|
77
|
-
openedWindow = true;
|
|
78
|
-
}
|
|
79
|
-
else if (elapsed >= period) {
|
|
80
|
-
previous = input.currentSelloff;
|
|
81
|
-
current = 0n;
|
|
82
|
-
const newWindowStart = input.windowStartTimestamp + period;
|
|
83
|
-
elapsedInWindow = now - newWindowStart;
|
|
84
|
-
if (elapsedInWindow < 0n)
|
|
85
|
-
elapsedInWindow = 0n;
|
|
86
|
-
openedWindow = true;
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
previous = input.previousSelloff;
|
|
90
|
-
current = input.currentSelloff;
|
|
91
|
-
elapsedInWindow = elapsed;
|
|
92
|
-
openedWindow = false;
|
|
93
|
-
}
|
|
94
|
-
const vbSnapshot = openedWindow || input.selloffVbSnapshot === 0n
|
|
95
|
-
? input.virtualBalance
|
|
96
|
-
: input.selloffVbSnapshot;
|
|
97
|
-
// Cap floored — a user can never sell more than the configured fraction.
|
|
98
|
-
const cap = (pct * vbSnapshot) / exports.PERCENT_SCALE;
|
|
99
|
-
const weightedPrev = (previous * (period - elapsedInWindow)) / period;
|
|
100
|
-
const effectiveSelloff = weightedPrev + current;
|
|
101
|
-
const headroom = cap > effectiveSelloff ? cap - effectiveSelloff : 0n;
|
|
102
|
-
return { enabled: true, vbSnapshot, cap, effectiveSelloff, headroom };
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Surge-fee percentage (PERCENT_SCALE units, `0..=10_000`) for a swap that
|
|
106
|
-
* pushes the input token's window to `effectiveSelloff` (INCLUDING the
|
|
107
|
-
* swap's own `amount_in`) against `cap`.
|
|
108
|
-
*
|
|
109
|
-
* Curve (contract-identical):
|
|
110
|
-
* ```text
|
|
111
|
-
* fee(t) = slope_low + (slope_high − slope_low) × (A^t − 1) / (A − 1)
|
|
112
|
-
* t = (fill − threshold) / (PERCENT_SCALE − threshold) ∈ [0, 1]
|
|
113
|
-
* fill = min(effective × PERCENT_SCALE / cap, PERCENT_SCALE)
|
|
114
|
-
* ```
|
|
115
|
-
* Returns `0` when disabled (`cap == 0`, `slopeHigh == 0`,
|
|
116
|
-
* `threshold >= PERCENT_SCALE`) or fill is at/below the threshold.
|
|
117
|
-
*/
|
|
118
|
-
function calcSurgeFeePct(effectiveSelloff, cap, thresholdPct, slopeLowPct, slopeHighPct) {
|
|
119
|
-
const slopeHigh = BigInt(slopeHighPct);
|
|
120
|
-
if (cap === 0n || slopeHigh === 0n)
|
|
121
|
-
return 0n;
|
|
122
|
-
const thr = BigInt(thresholdPct);
|
|
123
|
-
if (thr >= exports.PERCENT_SCALE)
|
|
124
|
-
return 0n;
|
|
125
|
-
let fill = (effectiveSelloff * exports.PERCENT_SCALE) / cap;
|
|
126
|
-
if (fill > exports.PERCENT_SCALE)
|
|
127
|
-
fill = exports.PERCENT_SCALE;
|
|
128
|
-
if (fill <= thr)
|
|
129
|
-
return 0n;
|
|
130
|
-
// Normalized progress t ∈ (0, 1] in 1e18 fixed point.
|
|
131
|
-
const tFp = ((fill - thr) * ONE) / (exports.PERCENT_SCALE - thr);
|
|
132
|
-
// A^t ∈ [ONE, A·ONE] via the shared LogExpMath port.
|
|
133
|
-
const aPowT = (0, logExp_1.powFp)(SURGE_FEE_CONVEXITY_FP, tFp);
|
|
134
|
-
// factor = (A^t − 1) / (A − 1) ∈ [0, ONE].
|
|
135
|
-
const aMinusOne = SURGE_FEE_CONVEXITY_FP - ONE;
|
|
136
|
-
let factor = ((aPowT > ONE ? aPowT - ONE : 0n) * ONE) / aMinusOne;
|
|
137
|
-
if (factor > ONE)
|
|
138
|
-
factor = ONE;
|
|
139
|
-
// surge = slope_low + span × factor, span portion rounded UP.
|
|
140
|
-
const slopeLow = BigInt(slopeLowPct);
|
|
141
|
-
const span = slopeHigh > slopeLow ? slopeHigh - slopeLow : 0n;
|
|
142
|
-
const prod = span * factor;
|
|
143
|
-
const add = prod / ONE + (prod % ONE !== 0n ? 1n : 0n);
|
|
144
|
-
const surge = slopeLow + add;
|
|
145
|
-
return surge > exports.PERCENT_SCALE ? exports.PERCENT_SCALE : surge;
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Split a gross curve output into the surge fee and the NET amount the
|
|
149
|
-
* user receives. Fee rounds UP (protective fee never undercharges) and is
|
|
150
|
-
* capped at `amountOut` — mirrors `instructions/user/swap.rs`.
|
|
151
|
-
*/
|
|
152
|
-
function applySurgeFee(amountOut, surgeFeePct) {
|
|
153
|
-
if (surgeFeePct <= 0n || amountOut <= 0n)
|
|
154
|
-
return { fee: 0n, net: amountOut };
|
|
155
|
-
const num = amountOut * surgeFeePct;
|
|
156
|
-
let fee = num / exports.PERCENT_SCALE + (num % exports.PERCENT_SCALE !== 0n ? 1n : 0n);
|
|
157
|
-
if (fee > amountOut)
|
|
158
|
-
fee = amountOut;
|
|
159
|
-
return { fee, net: amountOut - fee };
|
|
160
|
-
}
|
|
161
|
-
//# sourceMappingURL=selloff.js.map
|
package/dist/math/sel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