@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
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sliding-window cumulative-sell rate limiter + variable sell-off surge fee.
|
|
3
|
-
*
|
|
4
|
-
* Port of the v5.1 (post-audit) contracts:
|
|
5
|
-
* `programs/cubic-pool/src/math/max_selloff.rs` (`check_and_advance`)
|
|
6
|
-
* `programs/cubic-pool/src/math/surge_fee.rs` (`calc_surge_fee_pct`)
|
|
7
|
-
* `programs/cubic-pool/src/instructions/user/swap.rs` (segmented charge)
|
|
8
|
-
*
|
|
9
|
-
* All arithmetic is bigint, mirroring the Rust u128 intermediates INCLUDING
|
|
10
|
-
* integer-division order — do not "simplify" a `a*b/c` into a different
|
|
11
|
-
* association, parity with the on-chain result depends on it. BN appears
|
|
12
|
-
* only at the SDK boundary.
|
|
13
|
-
*
|
|
14
|
-
* v5.1 curve model: the fee RATE is piecewise-LINEAR in the normalized
|
|
15
|
-
* window-fill coordinate `t ∈ [0, 1]` (threshold → 100% fill):
|
|
16
|
-
* - no kink: low → high over [0, 1]
|
|
17
|
-
* - kinked: low → mid over [0, k], mid → high over [k, 1]
|
|
18
|
-
* and a swap is charged the AVERAGE rate over the span it crosses
|
|
19
|
-
* (integral / width), which makes the fee path-independent — splitting a
|
|
20
|
-
* sell into parts cannot cheapen it (audit M-05). The old exponential
|
|
21
|
-
* convexity curve (A^t) is gone.
|
|
22
|
-
*/
|
|
23
|
-
import BN from "bn.js";
|
|
24
|
-
/** Mirrors cubic-pool::constants::SURGE_FEE_SEGMENTS. */
|
|
25
|
-
export declare const SURGE_FEE_SEGMENTS = 4;
|
|
26
|
-
export interface SelloffWindowInputs {
|
|
27
|
-
maxSelloffPct: number;
|
|
28
|
-
periodLength: number;
|
|
29
|
-
previousSelloff: BN;
|
|
30
|
-
currentSelloff: BN;
|
|
31
|
-
windowStartTimestamp: BN;
|
|
32
|
-
selloffVbSnapshot: BN;
|
|
33
|
-
/** Current pre-trade virtual balance of the token. */
|
|
34
|
-
virtualBalance: BN;
|
|
35
|
-
/** Unix seconds. */
|
|
36
|
-
now: number;
|
|
37
|
-
}
|
|
38
|
-
export interface SelloffWindowStatus {
|
|
39
|
-
/** True when the limiter is configured (`maxSelloffPct > 0`). */
|
|
40
|
-
enabled: boolean;
|
|
41
|
-
cap: BN;
|
|
42
|
-
/** Effective decayed usage WITHOUT a hypothetical trade. */
|
|
43
|
-
used: BN;
|
|
44
|
-
/** `max(0, cap - used)`. */
|
|
45
|
-
remaining: BN;
|
|
46
|
-
/** `used/cap` in PERCENT_SCALE units (0..10_000+); 0 when disabled/cap=0. */
|
|
47
|
-
fillPct: number;
|
|
48
|
-
windowStartTimestamp: BN;
|
|
49
|
-
periodLength: number;
|
|
50
|
-
/** Whether the projection at `now` rotated the window vs stored state. */
|
|
51
|
-
rotated: boolean;
|
|
52
|
-
/** Snapshot the cap was resolved against (post-projection). */
|
|
53
|
-
vbSnapshot: BN;
|
|
54
|
-
}
|
|
55
|
-
/** Result of the pure bigint window projection. */
|
|
56
|
-
export interface WindowProjection {
|
|
57
|
-
opened: boolean;
|
|
58
|
-
windowStart: bigint;
|
|
59
|
-
elapsedInWindow: bigint;
|
|
60
|
-
/** Carried-over previous usage, POST v5.1 snapshot rescale. */
|
|
61
|
-
previous: bigint;
|
|
62
|
-
current: bigint;
|
|
63
|
-
vbSnapshot: bigint;
|
|
64
|
-
cap: bigint;
|
|
65
|
-
weightedPrev: bigint;
|
|
66
|
-
/** weightedPrev + current (decayed effective, WITHOUT a hypothetical trade). */
|
|
67
|
-
usedWithoutTrade: bigint;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Pure bigint projection of the sliding window at `now`. Mirrors v5.1
|
|
71
|
-
* `check_and_advance`'s rotation + snapshot logic (read-only, no
|
|
72
|
-
* `amount_in`), including the audit addition: when the projection OPENS a
|
|
73
|
-
* window against a new vb snapshot, the carried-over `previous` usage is
|
|
74
|
-
* rescaled by `newSnapshot / oldSnapshot` so a liquidity change between
|
|
75
|
-
* windows doesn't distort the carried percentage.
|
|
76
|
-
*/
|
|
77
|
-
export declare function projectSelloffWindow(params: {
|
|
78
|
-
maxSelloffPct: number;
|
|
79
|
-
periodLength: number;
|
|
80
|
-
previousSelloff: bigint;
|
|
81
|
-
currentSelloff: bigint;
|
|
82
|
-
windowStartTimestamp: bigint;
|
|
83
|
-
selloffVbSnapshot: bigint;
|
|
84
|
-
virtualBalance: bigint;
|
|
85
|
-
now: number;
|
|
86
|
-
}): WindowProjection;
|
|
87
|
-
/** Read-only status of a token's max-selloff window at `now`. */
|
|
88
|
-
export declare function computeSelloffWindow(inputs: SelloffWindowInputs): SelloffWindowStatus;
|
|
89
|
-
/**
|
|
90
|
-
* Average surge-fee percentage (PERCENT_SCALE units, 0..=10_000) charged for
|
|
91
|
-
* moving the window fill from `effectiveSelloffBefore` to
|
|
92
|
-
* `effectiveSelloffAfter` against `cap`. Port of v5.1 `calc_surge_fee_pct`:
|
|
93
|
-
* the rate is INTEGRATED over the crossed span (CEIL), so the charge is
|
|
94
|
-
* path-independent. When the span is empty (`after <= before`) the point
|
|
95
|
-
* rate at `after` is returned.
|
|
96
|
-
*/
|
|
97
|
-
export declare function calcSurgeFeePct(effectiveSelloffBefore: bigint, effectiveSelloffAfter: bigint, cap: bigint, thresholdPct: number, slopeLowPct: number, slopeMidPct: number, slopeHighPct: number, kinkPct: number): number;
|
|
98
|
-
/**
|
|
99
|
-
* CEIL fee application of a known percentage on `amount`, clamped to
|
|
100
|
-
* `amount`. NOTE: the contract does NOT charge the whole output at one
|
|
101
|
-
* rate — use {@link calcSegmentedSurgeFeeAmount} to mirror an actual swap.
|
|
102
|
-
* This helper remains for coarse display math only.
|
|
103
|
-
*/
|
|
104
|
-
export declare function calcSurgeFeeAmount(amountOut: bigint, surgePct: number): bigint;
|
|
105
|
-
export interface SegmentedSurgeFeeParams {
|
|
106
|
-
/** Window fill before the trade (`usedWithoutTrade`). */
|
|
107
|
-
effectiveSelloffBefore: bigint;
|
|
108
|
-
/** Window fill after the trade (`before + amountInNet`). */
|
|
109
|
-
effectiveSelloffAfter: bigint;
|
|
110
|
-
cap: bigint;
|
|
111
|
-
thresholdPct: number;
|
|
112
|
-
slopeLowPct: number;
|
|
113
|
-
slopeMidPct: number;
|
|
114
|
-
slopeHighPct: number;
|
|
115
|
-
kinkPct: number;
|
|
116
|
-
/** Post-swap-fee input driving the AMM curve (`amount_in_after_fee`). */
|
|
117
|
-
amountInAfterFee: bigint;
|
|
118
|
-
/** Full curve output of the trade (gross, pre-surge). */
|
|
119
|
-
amountOut: bigint;
|
|
120
|
-
/**
|
|
121
|
-
* Cumulative AMM output for an input slice `x ∈ [0, amountInAfterFee]`
|
|
122
|
-
* against the PRE-TRADE balances (i.e. `calc_out_given_in` with the same
|
|
123
|
-
* arguments that produced `amountOut`).
|
|
124
|
-
*/
|
|
125
|
-
curveOut: (amountInSlice: bigint) => bigint;
|
|
126
|
-
/** Defaults to the contract's SURGE_FEE_SEGMENTS (= 4). */
|
|
127
|
-
segments?: number;
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Surge fee in OUTPUT-token units for one swap — the exact v5.1 on-chain
|
|
131
|
-
* charge (swap.rs): only the output produced ABOVE the threshold is taxed,
|
|
132
|
-
* segment by segment across the taxed span, each segment's REAL curve
|
|
133
|
-
* output at that segment's average rate (CEIL per segment), total clamped
|
|
134
|
-
* to `amountOut`. Charging one average rate on the whole output would
|
|
135
|
-
* re-open the audit M-05 split dodge on asymmetric pools.
|
|
136
|
-
*/
|
|
137
|
-
export declare function calcSegmentedSurgeFeeAmount(p: SegmentedSurgeFeeParams): bigint;
|
|
138
|
-
//# sourceMappingURL=maxSelloff.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"maxSelloff.d.ts","sourceRoot":"","sources":["../../src/math/maxSelloff.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,MAAM,OAAO,CAAC;AAMvB,yDAAyD;AACzD,eAAO,MAAM,kBAAkB,IAAI,CAAC;AAEpC,MAAM,WAAW,mBAAmB;IAClC,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,eAAe,EAAE,EAAE,CAAC;IACpB,cAAc,EAAE,EAAE,CAAC;IACnB,oBAAoB,EAAE,EAAE,CAAC;IACzB,iBAAiB,EAAE,EAAE,CAAC;IACtB,sDAAsD;IACtD,cAAc,EAAE,EAAE,CAAC;IACnB,oBAAoB;IACpB,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,mBAAmB;IAClC,iEAAiE;IACjE,OAAO,EAAE,OAAO,CAAC;IACjB,GAAG,EAAE,EAAE,CAAC;IACR,4DAA4D;IAC5D,IAAI,EAAE,EAAE,CAAC;IACT,4BAA4B;IAC5B,SAAS,EAAE,EAAE,CAAC;IACd,6E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;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAK9E;AAED,MAAM,WAAW,uBAAuB;IACtC,yDAAyD;IACzD,sBAAsB,EAAE,MAAM,CAAC;IAC/B,4DAA4D;IAC5D,qBAAqB,EAAE,MAAM,CAAC;IAC9B,GAAG,EAAE,MAAM,CAAC;IACZ,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,yEAAyE;IACzE,gBAAgB,EAAE,MAAM,CAAC;IACzB,yDAAyD;IACzD,SAAS,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,QAAQ,EAAE,CAAC,aAAa,EAAE,MAAM,KAAK,MAAM,CAAC;IAC5C,2DAA2D;IAC3D,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;GAOG;AACH,wBAAgB,2BAA2B,CAAC,CAAC,EAAE,uBAAuB,GAAG,MAAM,CAwE9E"}
|
package/dist/math/maxSelloff.js
DELETED
|
@@ -1,330 +0,0 @@
|
|
|
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
|
-
const bn_js_1 = __importDefault(require("bn.js"));
|
|
35
|
-
const config_1 = require("../config");
|
|
36
|
-
const fixedPoint_1 = require("./fixedPoint");
|
|
37
|
-
const SCALE = BigInt(config_1.PERCENT_SCALE); // 10_000
|
|
38
|
-
/** Mirrors cubic-pool::constants::SURGE_FEE_SEGMENTS. */
|
|
39
|
-
exports.SURGE_FEE_SEGMENTS = 4;
|
|
40
|
-
/**
|
|
41
|
-
* Pure bigint projection of the sliding window at `now`. Mirrors v5.1
|
|
42
|
-
* `check_and_advance`'s rotation + snapshot logic (read-only, no
|
|
43
|
-
* `amount_in`), including the audit addition: when the projection OPENS a
|
|
44
|
-
* window against a new vb snapshot, the carried-over `previous` usage is
|
|
45
|
-
* rescaled by `newSnapshot / oldSnapshot` so a liquidity change between
|
|
46
|
-
* windows doesn't distort the carried percentage.
|
|
47
|
-
*/
|
|
48
|
-
function projectSelloffWindow(params) {
|
|
49
|
-
const period = BigInt(params.periodLength);
|
|
50
|
-
const storedWs = params.windowStartTimestamp;
|
|
51
|
-
const nowBI = BigInt(Math.trunc(params.now));
|
|
52
|
-
const rawElapsed = nowBI - storedWs;
|
|
53
|
-
const elapsed = rawElapsed < 0n ? 0n : rawElapsed;
|
|
54
|
-
let prev;
|
|
55
|
-
let cur;
|
|
56
|
-
let ws;
|
|
57
|
-
let eiw;
|
|
58
|
-
let opened;
|
|
59
|
-
if (period > 0n && elapsed >= 2n * period) {
|
|
60
|
-
prev = 0n;
|
|
61
|
-
cur = 0n;
|
|
62
|
-
ws = nowBI;
|
|
63
|
-
eiw = 0n;
|
|
64
|
-
opened = true;
|
|
65
|
-
}
|
|
66
|
-
else if (period > 0n && elapsed >= period) {
|
|
67
|
-
ws = storedWs + period;
|
|
68
|
-
const e = nowBI - ws;
|
|
69
|
-
eiw = e < 0n ? 0n : e;
|
|
70
|
-
prev = params.currentSelloff;
|
|
71
|
-
cur = 0n;
|
|
72
|
-
opened = true;
|
|
73
|
-
}
|
|
74
|
-
else if (period <= 0n) {
|
|
75
|
-
// Degenerate config (rejected on-chain by `require!(period > 0)`);
|
|
76
|
-
// treat as a fresh reset so callers never divide by zero.
|
|
77
|
-
prev = 0n;
|
|
78
|
-
cur = 0n;
|
|
79
|
-
ws = nowBI;
|
|
80
|
-
eiw = 0n;
|
|
81
|
-
opened = true;
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
prev = params.previousSelloff;
|
|
85
|
-
cur = params.currentSelloff;
|
|
86
|
-
ws = storedWs;
|
|
87
|
-
eiw = elapsed;
|
|
88
|
-
opened = false;
|
|
89
|
-
}
|
|
90
|
-
const oldSnapshot = params.selloffVbSnapshot;
|
|
91
|
-
const vbSnapshot = opened || oldSnapshot === 0n ? params.virtualBalance : oldSnapshot;
|
|
92
|
-
// v5.1: rescale the carried previous when the window opened against a
|
|
93
|
-
// DIFFERENT snapshot (mirrors max_selloff.rs — integer division order!).
|
|
94
|
-
if (opened && oldSnapshot > 0n && vbSnapshot !== oldSnapshot) {
|
|
95
|
-
prev = (prev * vbSnapshot) / oldSnapshot;
|
|
96
|
-
}
|
|
97
|
-
const cap = (BigInt(params.maxSelloffPct) * vbSnapshot) / SCALE;
|
|
98
|
-
const weightedPrev = period > 0n ? (prev * (period - eiw)) / period : 0n;
|
|
99
|
-
const usedWithoutTrade = weightedPrev + cur;
|
|
100
|
-
return {
|
|
101
|
-
opened,
|
|
102
|
-
windowStart: ws,
|
|
103
|
-
elapsedInWindow: eiw,
|
|
104
|
-
previous: prev,
|
|
105
|
-
current: cur,
|
|
106
|
-
vbSnapshot,
|
|
107
|
-
cap,
|
|
108
|
-
weightedPrev,
|
|
109
|
-
usedWithoutTrade,
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
/** Read-only status of a token's max-selloff window at `now`. */
|
|
113
|
-
function computeSelloffWindow(inputs) {
|
|
114
|
-
const enabled = inputs.maxSelloffPct > 0;
|
|
115
|
-
if (!enabled) {
|
|
116
|
-
return {
|
|
117
|
-
enabled: false,
|
|
118
|
-
cap: new bn_js_1.default(0),
|
|
119
|
-
used: new bn_js_1.default(0),
|
|
120
|
-
remaining: new bn_js_1.default(0),
|
|
121
|
-
fillPct: 0,
|
|
122
|
-
windowStartTimestamp: inputs.windowStartTimestamp,
|
|
123
|
-
periodLength: inputs.periodLength,
|
|
124
|
-
rotated: false,
|
|
125
|
-
vbSnapshot: new bn_js_1.default(0),
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
const proj = projectSelloffWindow({
|
|
129
|
-
maxSelloffPct: inputs.maxSelloffPct,
|
|
130
|
-
periodLength: inputs.periodLength,
|
|
131
|
-
previousSelloff: BigInt(inputs.previousSelloff.toString()),
|
|
132
|
-
currentSelloff: BigInt(inputs.currentSelloff.toString()),
|
|
133
|
-
windowStartTimestamp: BigInt(inputs.windowStartTimestamp.toString()),
|
|
134
|
-
selloffVbSnapshot: BigInt(inputs.selloffVbSnapshot.toString()),
|
|
135
|
-
virtualBalance: BigInt(inputs.virtualBalance.toString()),
|
|
136
|
-
now: inputs.now,
|
|
137
|
-
});
|
|
138
|
-
const used = proj.usedWithoutTrade;
|
|
139
|
-
const remaining = proj.cap > used ? proj.cap - used : 0n;
|
|
140
|
-
const fillPct = proj.cap === 0n ? 0 : Number((used * SCALE) / proj.cap);
|
|
141
|
-
return {
|
|
142
|
-
enabled: true,
|
|
143
|
-
cap: new bn_js_1.default(proj.cap.toString()),
|
|
144
|
-
used: new bn_js_1.default(used.toString()),
|
|
145
|
-
remaining: new bn_js_1.default(remaining.toString()),
|
|
146
|
-
fillPct,
|
|
147
|
-
windowStartTimestamp: new bn_js_1.default(proj.windowStart.toString()),
|
|
148
|
-
periodLength: inputs.periodLength,
|
|
149
|
-
rotated: proj.opened,
|
|
150
|
-
vbSnapshot: new bn_js_1.default(proj.vbSnapshot.toString()),
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
// ── v5.1 piecewise-linear surge curve ───────────────────────────────────────
|
|
154
|
-
/**
|
|
155
|
-
* Kink position in normalized-t space. `kinkPct` is WHOLE percent of window
|
|
156
|
-
* fill (0..=100, u8 on-chain); 0 (or a kink at/outside the [threshold, 100%]
|
|
157
|
-
* band) disables the kink and the curve degenerates to low → high.
|
|
158
|
-
* Port of `surge_fee.rs::kink_t`.
|
|
159
|
-
*/
|
|
160
|
-
function kinkT(kinkPct, thresholdPct) {
|
|
161
|
-
const thr = BigInt(thresholdPct);
|
|
162
|
-
if (kinkPct === 0 || thr >= SCALE)
|
|
163
|
-
return 0n;
|
|
164
|
-
const kinkFill = BigInt(kinkPct) * (SCALE / 100n);
|
|
165
|
-
if (kinkFill <= thr || kinkFill >= SCALE)
|
|
166
|
-
return 0n;
|
|
167
|
-
return ((kinkFill - thr) * fixedPoint_1.ONE) / (SCALE - thr);
|
|
168
|
-
}
|
|
169
|
-
/** Point rate at `t` (CEIL on the ramp, clamped to SCALE). Port of `rate_at`. */
|
|
170
|
-
function rateAt(tFp, slopeLowPct, slopeMidPct, slopeHighPct, kFp) {
|
|
171
|
-
const t = tFp < fixedPoint_1.ONE ? tFp : fixedPoint_1.ONE;
|
|
172
|
-
const low = BigInt(slopeLowPct);
|
|
173
|
-
const mid = BigInt(slopeMidPct);
|
|
174
|
-
const high = BigInt(slopeHighPct);
|
|
175
|
-
let base;
|
|
176
|
-
let span;
|
|
177
|
-
let num;
|
|
178
|
-
let den;
|
|
179
|
-
if (kFp === 0n) {
|
|
180
|
-
base = low;
|
|
181
|
-
span = high > low ? high - low : 0n;
|
|
182
|
-
num = t;
|
|
183
|
-
den = fixedPoint_1.ONE;
|
|
184
|
-
}
|
|
185
|
-
else if (t <= kFp) {
|
|
186
|
-
base = low;
|
|
187
|
-
span = mid > low ? mid - low : 0n;
|
|
188
|
-
num = t;
|
|
189
|
-
den = kFp;
|
|
190
|
-
}
|
|
191
|
-
else {
|
|
192
|
-
base = mid;
|
|
193
|
-
span = high > mid ? high - mid : 0n;
|
|
194
|
-
num = t - kFp;
|
|
195
|
-
den = fixedPoint_1.ONE - kFp;
|
|
196
|
-
}
|
|
197
|
-
const prod = span * num;
|
|
198
|
-
const add = prod / den + (prod % den !== 0n ? 1n : 0n);
|
|
199
|
-
const rate = base + add;
|
|
200
|
-
return rate < SCALE ? rate : SCALE;
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* ∫ rate over [0, t] (1e18-scaled area). Port of `integral_to` — keep the
|
|
204
|
-
* `x*x/(2*den)` before the span multiply, exactly like the contract.
|
|
205
|
-
*/
|
|
206
|
-
function integralTo(tFp, slopeLowPct, slopeMidPct, slopeHighPct, kFp) {
|
|
207
|
-
const t = tFp < fixedPoint_1.ONE ? tFp : fixedPoint_1.ONE;
|
|
208
|
-
const low = BigInt(slopeLowPct);
|
|
209
|
-
const mid = BigInt(slopeMidPct);
|
|
210
|
-
const high = BigInt(slopeHighPct);
|
|
211
|
-
const ramp = (x, span, den) => {
|
|
212
|
-
if (den === 0n || span === 0n || x === 0n)
|
|
213
|
-
return 0n;
|
|
214
|
-
const halfSq = (x * x) / (2n * den);
|
|
215
|
-
return span * halfSq;
|
|
216
|
-
};
|
|
217
|
-
if (kFp === 0n) {
|
|
218
|
-
const span = high > low ? high - low : 0n;
|
|
219
|
-
return low * t + ramp(t, span, fixedPoint_1.ONE);
|
|
220
|
-
}
|
|
221
|
-
if (t <= kFp) {
|
|
222
|
-
const span = mid > low ? mid - low : 0n;
|
|
223
|
-
return low * t + ramp(t, span, kFp);
|
|
224
|
-
}
|
|
225
|
-
const first = (kFp * (low + mid)) / 2n;
|
|
226
|
-
const rest = t - kFp;
|
|
227
|
-
const span = high > mid ? high - mid : 0n;
|
|
228
|
-
return first + mid * rest + ramp(rest, span, fixedPoint_1.ONE - kFp);
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* Average surge-fee percentage (PERCENT_SCALE units, 0..=10_000) charged for
|
|
232
|
-
* moving the window fill from `effectiveSelloffBefore` to
|
|
233
|
-
* `effectiveSelloffAfter` against `cap`. Port of v5.1 `calc_surge_fee_pct`:
|
|
234
|
-
* the rate is INTEGRATED over the crossed span (CEIL), so the charge is
|
|
235
|
-
* path-independent. When the span is empty (`after <= before`) the point
|
|
236
|
-
* rate at `after` is returned.
|
|
237
|
-
*/
|
|
238
|
-
function calcSurgeFeePct(effectiveSelloffBefore, effectiveSelloffAfter, cap, thresholdPct, slopeLowPct, slopeMidPct, slopeHighPct, kinkPct) {
|
|
239
|
-
if (cap === 0n || slopeHighPct === 0)
|
|
240
|
-
return 0;
|
|
241
|
-
const thr = BigInt(thresholdPct);
|
|
242
|
-
if (thr >= SCALE)
|
|
243
|
-
return 0;
|
|
244
|
-
const kFp = kinkT(kinkPct, thresholdPct);
|
|
245
|
-
const fill = (x) => {
|
|
246
|
-
const raw = (x * SCALE) / cap;
|
|
247
|
-
return raw < SCALE ? raw : SCALE;
|
|
248
|
-
};
|
|
249
|
-
const f0 = fill(effectiveSelloffBefore);
|
|
250
|
-
const f1 = fill(effectiveSelloffAfter);
|
|
251
|
-
if (f1 <= thr)
|
|
252
|
-
return 0;
|
|
253
|
-
const denom = SCALE - thr;
|
|
254
|
-
const t1 = ((f1 - thr) * fixedPoint_1.ONE) / denom;
|
|
255
|
-
if (f1 <= f0) {
|
|
256
|
-
return Number(rateAt(t1, slopeLowPct, slopeMidPct, slopeHighPct, kFp));
|
|
257
|
-
}
|
|
258
|
-
const f0Clamped = f0 > thr ? f0 : thr;
|
|
259
|
-
const t0 = ((f0Clamped - thr) * fixedPoint_1.ONE) / denom;
|
|
260
|
-
const integral = integralTo(t1, slopeLowPct, slopeMidPct, slopeHighPct, kFp) -
|
|
261
|
-
integralTo(t0, slopeLowPct, slopeMidPct, slopeHighPct, kFp);
|
|
262
|
-
const integralClamped = integral > 0n ? integral : 0n;
|
|
263
|
-
const numerator = denom * integralClamped;
|
|
264
|
-
const width = f1 - f0Clamped;
|
|
265
|
-
const scaledDen = width * fixedPoint_1.ONE;
|
|
266
|
-
const avg = numerator / scaledDen + (numerator % scaledDen !== 0n ? 1n : 0n);
|
|
267
|
-
return Number(avg < SCALE ? avg : SCALE);
|
|
268
|
-
}
|
|
269
|
-
/**
|
|
270
|
-
* CEIL fee application of a known percentage on `amount`, clamped to
|
|
271
|
-
* `amount`. NOTE: the contract does NOT charge the whole output at one
|
|
272
|
-
* rate — use {@link calcSegmentedSurgeFeeAmount} to mirror an actual swap.
|
|
273
|
-
* This helper remains for coarse display math only.
|
|
274
|
-
*/
|
|
275
|
-
function calcSurgeFeeAmount(amountOut, surgePct) {
|
|
276
|
-
if (surgePct <= 0 || amountOut <= 0n)
|
|
277
|
-
return 0n;
|
|
278
|
-
const num = amountOut * BigInt(surgePct);
|
|
279
|
-
const fee = num / SCALE + (num % SCALE !== 0n ? 1n : 0n);
|
|
280
|
-
return fee < amountOut ? fee : amountOut;
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* Surge fee in OUTPUT-token units for one swap — the exact v5.1 on-chain
|
|
284
|
-
* charge (swap.rs): only the output produced ABOVE the threshold is taxed,
|
|
285
|
-
* segment by segment across the taxed span, each segment's REAL curve
|
|
286
|
-
* output at that segment's average rate (CEIL per segment), total clamped
|
|
287
|
-
* to `amountOut`. Charging one average rate on the whole output would
|
|
288
|
-
* re-open the audit M-05 split dodge on asymmetric pools.
|
|
289
|
-
*/
|
|
290
|
-
function calcSegmentedSurgeFeeAmount(p) {
|
|
291
|
-
const { effectiveSelloffBefore: before, effectiveSelloffAfter: after, cap, thresholdPct, slopeLowPct, slopeMidPct, slopeHighPct, kinkPct, amountInAfterFee, amountOut, } = p;
|
|
292
|
-
// Same precheck as swap.rs: whole-span average of 0 ⇒ nothing to charge.
|
|
293
|
-
const wholeSpanPct = calcSurgeFeePct(before, after, cap, thresholdPct, slopeLowPct, slopeMidPct, slopeHighPct, kinkPct);
|
|
294
|
-
if (wholeSpanPct === 0)
|
|
295
|
-
return 0n;
|
|
296
|
-
const thrUnits = (cap * BigInt(thresholdPct)) / SCALE;
|
|
297
|
-
const span = after > before ? after - before : 0n;
|
|
298
|
-
const taxedLo = before > thrUnits ? before : thrUnits;
|
|
299
|
-
if (span === 0n || after <= taxedLo)
|
|
300
|
-
return 0n;
|
|
301
|
-
const segments = BigInt(p.segments ?? exports.SURGE_FEE_SEGMENTS);
|
|
302
|
-
// Flooring the input slice understates the output, which overstates the
|
|
303
|
-
// taxed remainder — the safe (conservative) direction, as on-chain.
|
|
304
|
-
const cumulativeOut = (u) => {
|
|
305
|
-
if (u <= before)
|
|
306
|
-
return 0n;
|
|
307
|
-
if (u >= after)
|
|
308
|
-
return amountOut;
|
|
309
|
-
const x = (amountInAfterFee * (u - before)) / span;
|
|
310
|
-
return p.curveOut(x);
|
|
311
|
-
};
|
|
312
|
-
const taxedWidth = after - taxedLo;
|
|
313
|
-
let feeAcc = 0n;
|
|
314
|
-
let prevU = taxedLo;
|
|
315
|
-
let prevY = cumulativeOut(taxedLo);
|
|
316
|
-
for (let k = 1n; k <= segments; k++) {
|
|
317
|
-
const u = k === segments ? after : taxedLo + (taxedWidth * k) / segments;
|
|
318
|
-
const y = cumulativeOut(u);
|
|
319
|
-
const segOut = y > prevY ? y - prevY : 0n;
|
|
320
|
-
if (segOut > 0n && u > prevU) {
|
|
321
|
-
const segPct = BigInt(calcSurgeFeePct(prevU, u, cap, thresholdPct, slopeLowPct, slopeMidPct, slopeHighPct, kinkPct));
|
|
322
|
-
const num = segOut * segPct;
|
|
323
|
-
feeAcc += num / SCALE + (num % SCALE !== 0n ? 1n : 0n);
|
|
324
|
-
}
|
|
325
|
-
prevU = u;
|
|
326
|
-
prevY = y;
|
|
327
|
-
}
|
|
328
|
-
return feeAcc < amountOut ? feeAcc : amountOut;
|
|
329
|
-
}
|
|
330
|
-
//# sourceMappingURL=maxSelloff.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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;AAvcD,kDAAuB;AACvB,sCAA0C;AAC1C,6CAAmC;AAEnC,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;IA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,GAAG,KAAK,GAAG,CAAC,GAAG,GAAG,KAAK,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACzD,CAAC;QACD,KAAK,GAAG,CAAC,CAAC;QACV,KAAK,GAAG,CAAC,CAAC;IACZ,CAAC;IAED,OAAO,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;AACjD,CAAC"}
|
package/dist/math/selloff.d.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Off-chain replicas of the cubic-pool v5 sell-off protections:
|
|
3
|
-
*
|
|
4
|
-
* - max-selloff sliding window — `math/max_selloff.rs::check_and_advance`
|
|
5
|
-
* - variable surge-fee curve — `math/surge_fee.rs::calc_surge_fee_pct`
|
|
6
|
-
*
|
|
7
|
-
* Used by routers/quoters to (a) avoid building routes that would revert
|
|
8
|
-
* with `MaxSelloffExceeded` — the window is a HARD cap on how much of a
|
|
9
|
-
* token can be sold into the pool per window — and (b) quote `amount_out`
|
|
10
|
-
* NET of the surge fee, which is the value the contract checks
|
|
11
|
-
* `minimum_amount_out` against.
|
|
12
|
-
*
|
|
13
|
-
* All arithmetic is bigint and mirrors the contract's u128 intermediates
|
|
14
|
-
* and rounding (cap floored, surge fee rounded UP). `powFp` is the same
|
|
15
|
-
* LogExpMath port the pool uses on-chain.
|
|
16
|
-
*/
|
|
17
|
-
/** Scale shared by all `_pct` config fields: 10_000 = 100%. */
|
|
18
|
-
export declare const PERCENT_SCALE = 10000n;
|
|
19
|
-
export interface SelloffWindowInput {
|
|
20
|
-
/** `AssetConfig.max_selloff_pct` (PERCENT_SCALE units). `0` = disabled. */
|
|
21
|
-
maxSelloffPct: number;
|
|
22
|
-
/** `AssetConfig.max_selloff_period_length` (seconds). */
|
|
23
|
-
periodLengthSecs: number;
|
|
24
|
-
/** `AssetDynamics.previous_selloff`. */
|
|
25
|
-
previousSelloff: bigint;
|
|
26
|
-
/** `AssetDynamics.current_selloff`. */
|
|
27
|
-
currentSelloff: bigint;
|
|
28
|
-
/** `AssetDynamics.window_start_timestamp` (unix seconds). */
|
|
29
|
-
windowStartTimestamp: bigint;
|
|
30
|
-
/** `AssetDynamics.selloff_vb_snapshot`. `0` = uninitialised. */
|
|
31
|
-
selloffVbSnapshot: bigint;
|
|
32
|
-
/** The token's CURRENT (pre-swap) `virtual_balance` — becomes the new
|
|
33
|
-
* snapshot if the window (re)opens at `now`. */
|
|
34
|
-
virtualBalance: bigint;
|
|
35
|
-
}
|
|
36
|
-
export interface ResolvedSelloffWindow {
|
|
37
|
-
/** `false` ⇒ no cap configured — unlimited headroom, no surge fee. */
|
|
38
|
-
enabled: boolean;
|
|
39
|
-
/** Snapshot the cap resolves against at `now` (re-baselined on rotation). */
|
|
40
|
-
vbSnapshot: bigint;
|
|
41
|
-
/** Absolute cap: `floor(pct × vbSnapshot / PERCENT_SCALE)`. */
|
|
42
|
-
cap: bigint;
|
|
43
|
-
/**
|
|
44
|
-
* `previous × (period − elapsed) / period + current` after applying any
|
|
45
|
-
* window rotation at `now` — EXCLUDING any new trade amount.
|
|
46
|
-
*/
|
|
47
|
-
effectiveSelloff: bigint;
|
|
48
|
-
/** Max additional `amount_in` the window accepts now (`cap − effective`,
|
|
49
|
-
* clamped ≥ 0). The contract requires `effective + amount_in <= cap`. */
|
|
50
|
-
headroom: bigint;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Resolve a token's sell-off window state at `nowSecs`, WITHOUT mutating
|
|
54
|
-
* anything — the read-only counterpart of the contract's
|
|
55
|
-
* `check_and_advance` (which the real swap will run).
|
|
56
|
-
*
|
|
57
|
-
* Rotation rules (contract-identical):
|
|
58
|
-
* - `elapsed >= 2×period` → both buckets aged out; cleared, re-snapshot.
|
|
59
|
-
* - `elapsed >= period` → `previous = current`, `current = 0`,
|
|
60
|
-
* `window_start += period` (slides one period, not to `now`), re-snapshot.
|
|
61
|
-
* - otherwise → unchanged; reuse stored snapshot (or capture
|
|
62
|
-
* the live vb when the snapshot is still `0` / uninitialised).
|
|
63
|
-
*/
|
|
64
|
-
export declare function resolveSelloffWindow(input: SelloffWindowInput, nowSecs: number): ResolvedSelloffWindow;
|
|
65
|
-
/**
|
|
66
|
-
* Surge-fee percentage (PERCENT_SCALE units, `0..=10_000`) for a swap that
|
|
67
|
-
* pushes the input token's window to `effectiveSelloff` (INCLUDING the
|
|
68
|
-
* swap's own `amount_in`) against `cap`.
|
|
69
|
-
*
|
|
70
|
-
* Curve (contract-identical):
|
|
71
|
-
* ```text
|
|
72
|
-
* fee(t) = slope_low + (slope_high − slope_low) × (A^t − 1) / (A − 1)
|
|
73
|
-
* t = (fill − threshold) / (PERCENT_SCALE − threshold) ∈ [0, 1]
|
|
74
|
-
* fill = min(effective × PERCENT_SCALE / cap, PERCENT_SCALE)
|
|
75
|
-
* ```
|
|
76
|
-
* Returns `0` when disabled (`cap == 0`, `slopeHigh == 0`,
|
|
77
|
-
* `threshold >= PERCENT_SCALE`) or fill is at/below the threshold.
|
|
78
|
-
*/
|
|
79
|
-
export declare function calcSurgeFeePct(effectiveSelloff: bigint, cap: bigint, thresholdPct: number, slopeLowPct: number, slopeHighPct: number): bigint;
|
|
80
|
-
/**
|
|
81
|
-
* Split a gross curve output into the surge fee and the NET amount the
|
|
82
|
-
* user receives. Fee rounds UP (protective fee never undercharges) and is
|
|
83
|
-
* capped at `amountOut` — mirrors `instructions/user/swap.rs`.
|
|
84
|
-
*/
|
|
85
|
-
export declare function applySurgeFee(amountOut: bigint, surgeFeePct: bigint): {
|
|
86
|
-
fee: bigint;
|
|
87
|
-
net: bigint;
|
|
88
|
-
};
|
|
89
|
-
//# sourceMappingURL=selloff.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"selloff.d.ts","sourceRoot":"","sources":["../../src/math/selloff.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAI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