@inversealtruism/cairnx-core 0.1.41 → 0.1.43
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/README.md +3 -1
- package/dist/index.cjs +59 -21
- package/dist/index.js +59 -21
- package/dist/mm/index.cjs +364 -0
- package/dist/mm/index.d.cts +192 -0
- package/dist/mm/index.d.ts +192 -0
- package/dist/mm/index.js +323 -0
- package/package.json +12 -2
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/mm/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
MAX_EXP_ARG_SCALED: () => MAX_EXP_ARG_SCALED,
|
|
24
|
+
acceptBid: () => acceptBid,
|
|
25
|
+
expScaled: () => expScaled,
|
|
26
|
+
expm1Scaled: () => expm1Scaled,
|
|
27
|
+
priorBuyers: () => priorBuyers,
|
|
28
|
+
rfqBuyAllIn: () => rfqBuyAllIn,
|
|
29
|
+
rfqFillAllIn: () => rfqFillAllIn,
|
|
30
|
+
sizeBid: () => sizeBid,
|
|
31
|
+
sunriseAnswerPrice: () => sunriseAnswerPrice,
|
|
32
|
+
sunriseCurve: () => sunriseCurve,
|
|
33
|
+
sunriseOrderCost: () => sunriseOrderCost,
|
|
34
|
+
sunriseTimeMult: () => sunriseTimeMult,
|
|
35
|
+
sunriseTokensFor: () => sunriseTokensFor,
|
|
36
|
+
tierFor: () => tierFor,
|
|
37
|
+
tradeNotional: () => tradeNotional
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(index_exports);
|
|
40
|
+
|
|
41
|
+
// src/mm/size.ts
|
|
42
|
+
function asLevels(levels) {
|
|
43
|
+
const out = [];
|
|
44
|
+
for (const l of levels || []) {
|
|
45
|
+
const size = BigInt(l.size);
|
|
46
|
+
const pricePerUnit = BigInt(l.pricePerUnit);
|
|
47
|
+
if (size <= 0n || pricePerUnit <= 0n) continue;
|
|
48
|
+
out.push({ bps: Number(l.bps || 0), size, pricePerUnit });
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
function packTier(l) {
|
|
53
|
+
return { bps: l.bps, size: l.size.toString(), pricePerUnit: l.pricePerUnit.toString() };
|
|
54
|
+
}
|
|
55
|
+
function tierFor(levels, tokBase, unit) {
|
|
56
|
+
tokBase = BigInt(tokBase);
|
|
57
|
+
unit = BigInt(unit);
|
|
58
|
+
if (tokBase <= 0n || unit <= 0n) return null;
|
|
59
|
+
for (const l of asLevels(levels)) {
|
|
60
|
+
if (l.size * unit >= tokBase) return packTier(l);
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
function sizeBid({ csdBase, decimals, levels, minBidCsd, inventoryBase }) {
|
|
65
|
+
try {
|
|
66
|
+
csdBase = BigInt(csdBase);
|
|
67
|
+
} catch {
|
|
68
|
+
return { ok: false, skip: "zero" };
|
|
69
|
+
}
|
|
70
|
+
if (csdBase <= 0n) return { ok: false, skip: "zero" };
|
|
71
|
+
if (minBidCsd != null && minBidCsd !== "" && csdBase < BigInt(minBidCsd)) return { ok: false, skip: "below-min-bid" };
|
|
72
|
+
const dec = Number(decimals);
|
|
73
|
+
if (!Number.isInteger(dec) || dec < 0 || dec > 18) return { ok: false, skip: "zero" };
|
|
74
|
+
const unit = 10n ** BigInt(dec);
|
|
75
|
+
const lvls = asLevels(levels);
|
|
76
|
+
if (!lvls.length) return { ok: false, skip: "above-top-tier" };
|
|
77
|
+
const exact = [];
|
|
78
|
+
const overflow = [];
|
|
79
|
+
for (const l of lvls) {
|
|
80
|
+
const cap = l.size * unit;
|
|
81
|
+
const tok = csdBase * unit / l.pricePerUnit;
|
|
82
|
+
if (tok <= 0n) return { ok: false, skip: "zero" };
|
|
83
|
+
if (tok <= cap) exact.push({ tok, l });
|
|
84
|
+
else if (csdBase * unit / cap >= l.pricePerUnit) overflow.push({ tok: cap, l });
|
|
85
|
+
}
|
|
86
|
+
if (!exact.length) return { ok: false, skip: "above-top-tier" };
|
|
87
|
+
let best = exact[0];
|
|
88
|
+
for (const c of exact.concat(overflow)) if (c.tok > best.tok) best = c;
|
|
89
|
+
if (inventoryBase != null && inventoryBase !== "" && best.tok > BigInt(inventoryBase)) return { ok: false, skip: "no-inventory" };
|
|
90
|
+
const bidUnit = csdBase * unit / best.tok;
|
|
91
|
+
return { ok: true, tokBase: best.tok.toString(), pricePerUnit: best.l.pricePerUnit.toString(), bidUnit: bidUnit.toString(), tier: packTier(best.l) };
|
|
92
|
+
}
|
|
93
|
+
function acceptBid({ csdBase, tokBase, levels, ref, marginBps, have, remainingEpochs, unit }) {
|
|
94
|
+
try {
|
|
95
|
+
csdBase = BigInt(csdBase);
|
|
96
|
+
tokBase = BigInt(tokBase);
|
|
97
|
+
unit = BigInt(unit);
|
|
98
|
+
ref = BigInt(ref);
|
|
99
|
+
marginBps = BigInt(marginBps);
|
|
100
|
+
} catch {
|
|
101
|
+
return { ok: false, skip: "zero" };
|
|
102
|
+
}
|
|
103
|
+
if (tokBase <= 0n || csdBase <= 0n || unit <= 0n) return { ok: false, skip: "zero" };
|
|
104
|
+
if (remainingEpochs != null && Number(remainingEpochs) < 2) return { ok: false, skip: "expiring" };
|
|
105
|
+
if (have != null && have !== "" && tokBase > BigInt(have)) return { ok: false, skip: "no-inventory" };
|
|
106
|
+
const bidUnit = csdBase * unit / tokBase;
|
|
107
|
+
const floorAccept = ref + ref * marginBps / 10000n;
|
|
108
|
+
if (bidUnit < floorAccept) return { ok: false, skip: "below-margin", bidUnit: bidUnit.toString(), needUnit: floorAccept.toString() };
|
|
109
|
+
const tier = tierFor(levels, tokBase, unit);
|
|
110
|
+
if (!tier) return { ok: false, skip: "above-top-tier", bidUnit: bidUnit.toString() };
|
|
111
|
+
const tierPrice = BigInt(tier.pricePerUnit);
|
|
112
|
+
const needUnit = tierPrice > floorAccept ? tierPrice : floorAccept;
|
|
113
|
+
if (bidUnit < needUnit) return { ok: false, skip: "below-tier", bidUnit: bidUnit.toString(), needUnit: needUnit.toString(), tier };
|
|
114
|
+
return { ok: true, tier, bidUnit: bidUnit.toString(), needUnit: needUnit.toString() };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/mm/notional.ts
|
|
118
|
+
function tradeNotional(offer, entry, opts) {
|
|
119
|
+
const wantRaw = offer?.want && "value" in offer.want ? offer.want.value : 0;
|
|
120
|
+
let want = 0n, paidObserved = 0n, fee = 0n;
|
|
121
|
+
try {
|
|
122
|
+
want = BigInt(wantRaw ?? 0);
|
|
123
|
+
} catch {
|
|
124
|
+
want = 0n;
|
|
125
|
+
}
|
|
126
|
+
const src = entry || offer?.fill || {};
|
|
127
|
+
try {
|
|
128
|
+
paidObserved = BigInt(src.paid ?? 0);
|
|
129
|
+
} catch {
|
|
130
|
+
paidObserved = 0n;
|
|
131
|
+
}
|
|
132
|
+
try {
|
|
133
|
+
fee = BigInt(src.fee ?? 0);
|
|
134
|
+
} catch {
|
|
135
|
+
fee = 0n;
|
|
136
|
+
}
|
|
137
|
+
if (want < 0n) want = 0n;
|
|
138
|
+
if (paidObserved < 0n) paidObserved = 0n;
|
|
139
|
+
if (fee < 0n) fee = 0n;
|
|
140
|
+
const notional = want > 0n && paidObserved > want ? want : paidObserved;
|
|
141
|
+
const extra = paidObserved > notional ? paidObserved - notional : 0n;
|
|
142
|
+
let rebate = 0n;
|
|
143
|
+
if (extra > 0n && typeof opts?.rebate === "function") {
|
|
144
|
+
const payto = String(offer?.want?.payto || "").toLowerCase();
|
|
145
|
+
const seller = String(offer?.seller || "").toLowerCase();
|
|
146
|
+
if (payto && payto === seller) {
|
|
147
|
+
try {
|
|
148
|
+
rebate = BigInt(opts.rebate(want));
|
|
149
|
+
} catch {
|
|
150
|
+
rebate = 0n;
|
|
151
|
+
}
|
|
152
|
+
if (rebate < 0n) rebate = 0n;
|
|
153
|
+
if (rebate > extra) rebate = extra;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const change = extra > rebate ? extra - rebate : 0n;
|
|
157
|
+
return { notional, rebate, change, fee, extra, paidObserved };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// src/mm/curve.ts
|
|
161
|
+
var MAX_EXP_ARG_SCALED = 2200000n;
|
|
162
|
+
function expScaled(xScaled) {
|
|
163
|
+
const S = 1000000n;
|
|
164
|
+
xScaled = BigInt(xScaled);
|
|
165
|
+
if (xScaled === 0n) return S;
|
|
166
|
+
if (xScaled < 0n) {
|
|
167
|
+
const p = expScaled(-xScaled);
|
|
168
|
+
return p === 0n ? 0n : S * S / p;
|
|
169
|
+
}
|
|
170
|
+
if (xScaled > MAX_EXP_ARG_SCALED) xScaled = MAX_EXP_ARG_SCALED;
|
|
171
|
+
const S2 = 1000000000000n;
|
|
172
|
+
const x = xScaled * 1000000n;
|
|
173
|
+
let sum = S2;
|
|
174
|
+
let term = S2;
|
|
175
|
+
for (let n = 1n; n <= 20n; n++) {
|
|
176
|
+
term = term * x / (n * S2);
|
|
177
|
+
sum += term;
|
|
178
|
+
if (term === 0n) break;
|
|
179
|
+
}
|
|
180
|
+
return sum / 1000000n;
|
|
181
|
+
}
|
|
182
|
+
function sunriseTimeMult(rateBps, epochsElapsed) {
|
|
183
|
+
let timeMult = 1000000000000n;
|
|
184
|
+
const r = BigInt(rateBps);
|
|
185
|
+
const n = Math.min(Math.max(Number(epochsElapsed) || 0, 0), 2e4);
|
|
186
|
+
for (let i = 0; i < n; i++) timeMult = timeMult * (10000n + r) / 10000n;
|
|
187
|
+
return timeMult;
|
|
188
|
+
}
|
|
189
|
+
function sunriseCurve({ p0, rateBps, kScaled, E, q, epochsElapsed, capMult, floor = null }) {
|
|
190
|
+
const S1 = 1000000n;
|
|
191
|
+
const S2 = 1000000000000n;
|
|
192
|
+
p0 = BigInt(p0);
|
|
193
|
+
E = BigInt(E);
|
|
194
|
+
q = BigInt(q);
|
|
195
|
+
kScaled = BigInt(kScaled);
|
|
196
|
+
capMult = BigInt(capMult);
|
|
197
|
+
const timeMult = sunriseTimeMult(rateBps, epochsElapsed);
|
|
198
|
+
const qClamped = q < 0n ? 0n : E > 0n && q > E ? E : q;
|
|
199
|
+
const x = E > 0n ? kScaled * qClamped / E : 0n;
|
|
200
|
+
const invMult = expScaled(x);
|
|
201
|
+
let price = p0 * timeMult * invMult / (S2 * S1);
|
|
202
|
+
const cap = p0 * capMult;
|
|
203
|
+
if (price > cap) price = cap;
|
|
204
|
+
if (floor != null && price < BigInt(floor)) price = BigInt(floor);
|
|
205
|
+
return price;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// src/mm/sunrise.ts
|
|
209
|
+
var FIX = 10n ** 18n;
|
|
210
|
+
var FIX30 = 10n ** 30n;
|
|
211
|
+
var TIME_SCALE = 10n ** 12n;
|
|
212
|
+
var MAX_K_SCALED = MAX_EXP_ARG_SCALED;
|
|
213
|
+
var MAX_EXPM1_ARG = 20n * FIX;
|
|
214
|
+
var ceilDiv = (a, b) => (a + b - 1n) / b;
|
|
215
|
+
function expm1Scaled(z) {
|
|
216
|
+
if (z < 0n || z > MAX_EXPM1_ARG) throw new RangeError("expm1Scaled: z outside [0, 20e18]");
|
|
217
|
+
let sum = 0n;
|
|
218
|
+
let term = FIX;
|
|
219
|
+
for (let i = 1n; ; i++) {
|
|
220
|
+
term = term * z / (i * FIX);
|
|
221
|
+
if (term === 0n) return sum;
|
|
222
|
+
sum += term;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function checkedParams(params) {
|
|
226
|
+
const p0 = BigInt(params.p0);
|
|
227
|
+
const kScaled = BigInt(params.kScaled);
|
|
228
|
+
const E = BigInt(params.E);
|
|
229
|
+
const capMult = BigInt(params.capMult);
|
|
230
|
+
const decimals = params.decimals ?? 8;
|
|
231
|
+
if (!Number.isInteger(decimals) || decimals < 0 || decimals > 18) throw new RangeError("sunrise: decimals must be an integer in [0, 18]");
|
|
232
|
+
if (p0 < 0n || capMult < 0n || E < 0n) throw new RangeError("sunrise: p0, capMult and E must be non-negative");
|
|
233
|
+
if (kScaled < 0n || kScaled > MAX_K_SCALED) throw new RangeError(`sunrise: kScaled must be in [0, ${MAX_K_SCALED}]`);
|
|
234
|
+
return { p0, kScaled, E, capMult, rateBps: params.rateBps, U: 10n ** BigInt(decimals) };
|
|
235
|
+
}
|
|
236
|
+
function costFn(params, q, epochsElapsed) {
|
|
237
|
+
const { p0, kScaled, E, capMult, rateBps, U } = checkedParams(params);
|
|
238
|
+
const aNum = p0 * sunriseTimeMult(rateBps, epochsElapsed);
|
|
239
|
+
const capNum = p0 * capMult * TIME_SCALE;
|
|
240
|
+
let lo = BigInt(q);
|
|
241
|
+
if (lo < 0n) lo = 0n;
|
|
242
|
+
if (kScaled === 0n || E === 0n) {
|
|
243
|
+
const flat = aNum < capNum ? aNum : capNum;
|
|
244
|
+
return (n) => n <= 0n ? 0n : ceilDiv(flat * n, TIME_SCALE * U);
|
|
245
|
+
}
|
|
246
|
+
const g = (y) => expm1Scaled(kScaled * y * TIME_SCALE / E);
|
|
247
|
+
const priceNum = (y) => aNum * (FIX + g(y));
|
|
248
|
+
const capNum30 = capNum * FIX;
|
|
249
|
+
let s = E;
|
|
250
|
+
let flatNum = priceNum(E);
|
|
251
|
+
if (flatNum >= capNum30) {
|
|
252
|
+
flatNum = capNum30;
|
|
253
|
+
let a = 0n;
|
|
254
|
+
let b = E;
|
|
255
|
+
while (a < b) {
|
|
256
|
+
const mid = (a + b) / 2n;
|
|
257
|
+
if (priceNum(mid) >= capNum30) b = mid;
|
|
258
|
+
else a = mid + 1n;
|
|
259
|
+
}
|
|
260
|
+
s = a;
|
|
261
|
+
}
|
|
262
|
+
const curveScale = aNum * E * 1000000n;
|
|
263
|
+
const flatScale = flatNum * kScaled;
|
|
264
|
+
const antiderivative = (x) => x <= s ? curveScale * g(x) : curveScale * g(s) + flatScale * (x - s);
|
|
265
|
+
const base = antiderivative(lo);
|
|
266
|
+
const denom = kScaled * U * FIX30;
|
|
267
|
+
return (n) => {
|
|
268
|
+
if (n <= 0n) return 0n;
|
|
269
|
+
const d = antiderivative(lo + n) - base;
|
|
270
|
+
return d <= 0n ? 0n : ceilDiv(d, denom);
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
function answerFn(params, q, epochsElapsed, marginBps, floorPerToken) {
|
|
274
|
+
const { U } = checkedParams(params);
|
|
275
|
+
const margin = BigInt(marginBps);
|
|
276
|
+
if (margin < 0n) throw new RangeError("sunrise: marginBps must be non-negative");
|
|
277
|
+
const floor = floorPerToken == null ? 0n : BigInt(floorPerToken);
|
|
278
|
+
if (floor < 0n) throw new RangeError("sunrise: floorPerToken must be non-negative");
|
|
279
|
+
const cost = costFn(params, q, epochsElapsed);
|
|
280
|
+
const mult = 10000n + margin;
|
|
281
|
+
return (n) => {
|
|
282
|
+
if (n <= 0n) return 0n;
|
|
283
|
+
const fromCurve = ceilDiv(cost(n) * mult, 10000n);
|
|
284
|
+
const fromFloor = ceilDiv(floor * n * mult, U * 10000n);
|
|
285
|
+
return fromCurve > fromFloor ? fromCurve : fromFloor;
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
function sunriseOrderCost(params, q, n, epochsElapsed) {
|
|
289
|
+
return costFn(params, q, epochsElapsed)(BigInt(n));
|
|
290
|
+
}
|
|
291
|
+
function sunriseAnswerPrice(params, q, n, epochsElapsed, marginBps, floorPerToken) {
|
|
292
|
+
return answerFn(params, q, epochsElapsed, marginBps, floorPerToken)(BigInt(n));
|
|
293
|
+
}
|
|
294
|
+
function sunriseTokensFor(params, q, budget, epochsElapsed, marginBps, floorPerToken, room) {
|
|
295
|
+
const price = answerFn(params, q, epochsElapsed, marginBps, floorPerToken);
|
|
296
|
+
const cap = BigInt(budget);
|
|
297
|
+
let lo = 0n;
|
|
298
|
+
let hi = BigInt(room);
|
|
299
|
+
if (cap < 0n || hi <= 0n) return 0n;
|
|
300
|
+
while (lo < hi) {
|
|
301
|
+
const mid = (lo + hi + 1n) / 2n;
|
|
302
|
+
if (price(mid) <= cap) lo = mid;
|
|
303
|
+
else hi = mid - 1n;
|
|
304
|
+
}
|
|
305
|
+
return lo;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// src/mm/allin.ts
|
|
309
|
+
var import_csd_codec2 = require("@inversealtruism/csd-codec");
|
|
310
|
+
|
|
311
|
+
// src/types.ts
|
|
312
|
+
var import_csd_codec = require("@inversealtruism/csd-codec");
|
|
313
|
+
var MAX_AMOUNT = (1n << 96n) - 1n;
|
|
314
|
+
var CLAIM_WINDOW_BLOCKS_V20 = 40;
|
|
315
|
+
var CLAIM_FILL_GRACE_BLOCKS = 5;
|
|
316
|
+
var FEE_BPS = 100;
|
|
317
|
+
var FEE_BPS_V16 = 150;
|
|
318
|
+
var REBATE_FLAT = 25000000n;
|
|
319
|
+
var REBATE_BPS = 50;
|
|
320
|
+
var ZERO_ADDR = "0x" + "00".repeat(20);
|
|
321
|
+
var tradeFee = (want, bps = FEE_BPS) => (want * BigInt(bps) + 9999n) / 10000n;
|
|
322
|
+
var makerRebate = (value) => REBATE_FLAT + (value * BigInt(REBATE_BPS) + 9999n) / 10000n;
|
|
323
|
+
var COMMIT_MAX_BLOCKS = 8 * import_csd_codec.EPOCH_LEN;
|
|
324
|
+
var FCLAIM_WINDOW_MIN = CLAIM_WINDOW_BLOCKS_V20 + CLAIM_FILL_GRACE_BLOCKS + 1;
|
|
325
|
+
|
|
326
|
+
// src/mm/allin.ts
|
|
327
|
+
function rfqFillAllIn(n) {
|
|
328
|
+
return n + tradeFee(n, FEE_BPS_V16) + makerRebate(n) + BigInt(import_csd_codec2.MIN_FEE_ATTEST);
|
|
329
|
+
}
|
|
330
|
+
function rfqBuyAllIn(n) {
|
|
331
|
+
return rfqFillAllIn(n) + BigInt(import_csd_codec2.MIN_FEE_PROPOSE);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// src/mm/buyers.ts
|
|
335
|
+
var lower = (a) => String(a ?? "").toLowerCase();
|
|
336
|
+
function priorBuyers(offers, maker) {
|
|
337
|
+
const me = lower(maker);
|
|
338
|
+
const out = /* @__PURE__ */ new Set();
|
|
339
|
+
if (!me) return out;
|
|
340
|
+
for (const o of Object.values(offers || {})) {
|
|
341
|
+
if (!o || lower(o.seller) !== me) continue;
|
|
342
|
+
const rows = o.fills?.length ? o.fills : o.fill ? [o.fill] : [];
|
|
343
|
+
for (const f of rows) if (f?.buyer) out.add(lower(f.buyer));
|
|
344
|
+
}
|
|
345
|
+
return out;
|
|
346
|
+
}
|
|
347
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
348
|
+
0 && (module.exports = {
|
|
349
|
+
MAX_EXP_ARG_SCALED,
|
|
350
|
+
acceptBid,
|
|
351
|
+
expScaled,
|
|
352
|
+
expm1Scaled,
|
|
353
|
+
priorBuyers,
|
|
354
|
+
rfqBuyAllIn,
|
|
355
|
+
rfqFillAllIn,
|
|
356
|
+
sizeBid,
|
|
357
|
+
sunriseAnswerPrice,
|
|
358
|
+
sunriseCurve,
|
|
359
|
+
sunriseOrderCost,
|
|
360
|
+
sunriseTimeMult,
|
|
361
|
+
sunriseTokensFor,
|
|
362
|
+
tierFor,
|
|
363
|
+
tradeNotional
|
|
364
|
+
});
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/** Anything BigInt() accepts on the wire: base-unit strings, numbers, bigints. */
|
|
2
|
+
type BigIntish = bigint | number | string;
|
|
3
|
+
/** One indicative ladder rung as published (size in whole tokens, price per whole token in base CSD). */
|
|
4
|
+
interface LevelIn {
|
|
5
|
+
bps?: number | string;
|
|
6
|
+
size: BigIntish;
|
|
7
|
+
pricePerUnit: BigIntish;
|
|
8
|
+
}
|
|
9
|
+
interface Tier {
|
|
10
|
+
bps: number;
|
|
11
|
+
size: string;
|
|
12
|
+
pricePerUnit: string;
|
|
13
|
+
}
|
|
14
|
+
type SizeBidResult = {
|
|
15
|
+
ok: true;
|
|
16
|
+
tokBase: string;
|
|
17
|
+
pricePerUnit: string;
|
|
18
|
+
bidUnit: string;
|
|
19
|
+
tier: Tier;
|
|
20
|
+
} | {
|
|
21
|
+
ok: false;
|
|
22
|
+
skip: string;
|
|
23
|
+
};
|
|
24
|
+
type AcceptBidResult = {
|
|
25
|
+
ok: true;
|
|
26
|
+
tier: Tier;
|
|
27
|
+
bidUnit: string;
|
|
28
|
+
needUnit: string;
|
|
29
|
+
} | {
|
|
30
|
+
ok: false;
|
|
31
|
+
skip: string;
|
|
32
|
+
bidUnit?: string;
|
|
33
|
+
needUnit?: string;
|
|
34
|
+
tier?: Tier;
|
|
35
|
+
};
|
|
36
|
+
/** Cheapest (first) indicative tier whose size covers tokBase, or null if above the top rung. */
|
|
37
|
+
declare function tierFor(levels: readonly LevelIn[] | null | undefined, tokBase: BigIntish, unit: BigIntish): Tier | null;
|
|
38
|
+
/**
|
|
39
|
+
* Size a CSD spend across the indicative ladder.
|
|
40
|
+
* tok = floor(csdBase * 10^decimals / price_k); first tier with tok ≤ size_k wins.
|
|
41
|
+
* → { ok, tokBase, pricePerUnit, bidUnit, tier } | { ok:false, skip }
|
|
42
|
+
*/
|
|
43
|
+
declare function sizeBid({ csdBase, decimals, levels, minBidCsd, inventoryBase }: {
|
|
44
|
+
csdBase: BigIntish;
|
|
45
|
+
decimals: number | string;
|
|
46
|
+
levels: readonly LevelIn[] | null | undefined;
|
|
47
|
+
minBidCsd?: BigIntish | null;
|
|
48
|
+
inventoryBase?: BigIntish | null;
|
|
49
|
+
}): SizeBidResult;
|
|
50
|
+
/**
|
|
51
|
+
* Bot-side accept of an already-posted bid (csdBase paid, tokBase wanted).
|
|
52
|
+
* Enforces floor+margin, the size-tier price, inventory, and a 2-epoch expiry floor.
|
|
53
|
+
*/
|
|
54
|
+
declare function acceptBid({ csdBase, tokBase, levels, ref, marginBps, have, remainingEpochs, unit }: {
|
|
55
|
+
csdBase: BigIntish;
|
|
56
|
+
tokBase: BigIntish;
|
|
57
|
+
levels: readonly LevelIn[] | null | undefined;
|
|
58
|
+
ref: BigIntish;
|
|
59
|
+
marginBps: BigIntish;
|
|
60
|
+
have?: BigIntish | null;
|
|
61
|
+
remainingEpochs?: number | null;
|
|
62
|
+
unit: BigIntish;
|
|
63
|
+
}): AcceptBidResult;
|
|
64
|
+
|
|
65
|
+
type Amount = bigint | number | string | null | undefined;
|
|
66
|
+
interface FillAmounts {
|
|
67
|
+
paid?: Amount;
|
|
68
|
+
fee?: Amount;
|
|
69
|
+
}
|
|
70
|
+
/** The fields read from an offer row (a resolver OfferState fits). */
|
|
71
|
+
interface NotionalOffer {
|
|
72
|
+
seller?: string;
|
|
73
|
+
want?: {
|
|
74
|
+
value?: Amount;
|
|
75
|
+
payto?: string;
|
|
76
|
+
};
|
|
77
|
+
fill?: FillAmounts | null;
|
|
78
|
+
}
|
|
79
|
+
interface TradeNotional {
|
|
80
|
+
notional: bigint;
|
|
81
|
+
rebate: bigint;
|
|
82
|
+
change: bigint;
|
|
83
|
+
fee: bigint;
|
|
84
|
+
extra: bigint;
|
|
85
|
+
paidObserved: bigint;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Contractual CSD of one fill row.
|
|
89
|
+
* `opts.rebate(want)` is optional; when payto===seller the extra above want
|
|
90
|
+
* is split into rebate vs change. Without it, extra stays lumped in `change`.
|
|
91
|
+
*/
|
|
92
|
+
declare function tradeNotional(offer: NotionalOffer | null | undefined, entry?: FillAmounts | null, opts?: {
|
|
93
|
+
rebate?: (want: bigint) => bigint | number | string;
|
|
94
|
+
} | null): TradeNotional;
|
|
95
|
+
|
|
96
|
+
/** The Sunrise curve's fixed parameters (all BigInt()-ed). */
|
|
97
|
+
interface SunriseCurveParams {
|
|
98
|
+
p0: BigIntish;
|
|
99
|
+
rateBps: BigIntish;
|
|
100
|
+
kScaled: BigIntish;
|
|
101
|
+
E: BigIntish;
|
|
102
|
+
capMult: BigIntish;
|
|
103
|
+
}
|
|
104
|
+
/** The largest exponent the spot curve evaluates (2.2 at scale 1e6); above it expScaled clamps, so a curve
|
|
105
|
+
* whose k exceeds it would quote a spot price that disagrees with its own order cost. */
|
|
106
|
+
declare const MAX_EXP_ARG_SCALED = 2200000n;
|
|
107
|
+
/** e^x at scale 1e6. 12+ Taylor terms; |x| is clamped to MAX_EXP_ARG_SCALED so the series stays honest. */
|
|
108
|
+
declare function expScaled(xScaled: BigIntish): bigint;
|
|
109
|
+
/** (1 + rateBps/10000)^epochs at scale 1e12, flooring each step; epochs is clamped to [0, 20000]. */
|
|
110
|
+
declare function sunriseTimeMult(rateBps: BigIntish, epochsElapsed: BigIntish): bigint;
|
|
111
|
+
/** Sunrise floor: P0 · (1+r)^epochs · e^{k q/E}, clamped to [floor, P0·capMult]. Integer-only. */
|
|
112
|
+
declare function sunriseCurve({ p0, rateBps, kScaled, E, q, epochsElapsed, capMult, floor }: SunriseCurveParams & {
|
|
113
|
+
q: BigIntish;
|
|
114
|
+
epochsElapsed: BigIntish;
|
|
115
|
+
floor?: BigIntish | null;
|
|
116
|
+
}): bigint;
|
|
117
|
+
|
|
118
|
+
/** The curve parameters plus the token's decimals (default 8). */
|
|
119
|
+
interface SunriseOrderParams extends SunriseCurveParams {
|
|
120
|
+
decimals?: number;
|
|
121
|
+
}
|
|
122
|
+
/** e^z - 1 at scale 1e18 for 0 <= z <= 20e18. Summed from the series directly, so small z keeps every digit. */
|
|
123
|
+
declare function expm1Scaled(z: bigint): bigint;
|
|
124
|
+
/** Integral of the spot curve over [q, q+n], in CSD base units, rounded up. */
|
|
125
|
+
declare function sunriseOrderCost(params: SunriseOrderParams, q: BigIntish, n: BigIntish, epochsElapsed: BigIntish): bigint;
|
|
126
|
+
/** What the maker asks for n tokens: max(order cost, floor cost) times (1 + margin), rounded up. */
|
|
127
|
+
declare function sunriseAnswerPrice(params: SunriseOrderParams, q: BigIntish, n: BigIntish, epochsElapsed: BigIntish, marginBps: BigIntish, floorPerToken: BigIntish | null | undefined): bigint;
|
|
128
|
+
/** The largest n in [0, room] whose answer price is <= budget (the inverse of sunriseAnswerPrice). */
|
|
129
|
+
declare function sunriseTokensFor(params: SunriseOrderParams, q: BigIntish, budget: BigIntish, epochsElapsed: BigIntish, marginBps: BigIntish, floorPerToken: BigIntish | null | undefined, room: BigIntish): bigint;
|
|
130
|
+
|
|
131
|
+
/** What the taker pays to fill an answer worth n: the value, the 1.5% fee, the maker fee and the fill attest. */
|
|
132
|
+
declare function rfqFillAllIn(n: bigint): bigint;
|
|
133
|
+
/** The buyer's total for a bid at value n: rfqFillAllIn(n) plus the propose that anchors the bid. */
|
|
134
|
+
declare function rfqBuyAllIn(n: bigint): bigint;
|
|
135
|
+
|
|
136
|
+
type Give = {
|
|
137
|
+
ticker: string;
|
|
138
|
+
amount: string;
|
|
139
|
+
} | {
|
|
140
|
+
name: string;
|
|
141
|
+
};
|
|
142
|
+
type OfferStatus = "open" | "filled" | "cancelled" | "expired";
|
|
143
|
+
interface FillEntry {
|
|
144
|
+
buyer: string;
|
|
145
|
+
txid: string;
|
|
146
|
+
height: number;
|
|
147
|
+
paid: string;
|
|
148
|
+
fee: string;
|
|
149
|
+
got?: string;
|
|
150
|
+
}
|
|
151
|
+
interface OfferState {
|
|
152
|
+
id: string;
|
|
153
|
+
seller: string;
|
|
154
|
+
give: Give;
|
|
155
|
+
want: {
|
|
156
|
+
value: string;
|
|
157
|
+
payto: string;
|
|
158
|
+
} | {
|
|
159
|
+
ticker: string;
|
|
160
|
+
amount: string;
|
|
161
|
+
payto: string;
|
|
162
|
+
};
|
|
163
|
+
taker?: string;
|
|
164
|
+
status: OfferStatus;
|
|
165
|
+
expiresEpoch: number;
|
|
166
|
+
height: number;
|
|
167
|
+
feeBps: number;
|
|
168
|
+
/** v1.2 partial fills: the maker's per-fill payment floor; presence ⇒ partially fillable */
|
|
169
|
+
min?: string;
|
|
170
|
+
/** v1.2: cumulative payment received / give delivered so far (partial offers only) */
|
|
171
|
+
paid?: string;
|
|
172
|
+
delivered?: string;
|
|
173
|
+
/** v1.2: every partial fill in apply order */
|
|
174
|
+
fills?: FillEntry[];
|
|
175
|
+
/** v1.2: the bid this offer responds to */
|
|
176
|
+
bid?: string;
|
|
177
|
+
/** v1.7 claim-to-fill: the current exclusive claimer + the height their window ends. Lazy lapse —
|
|
178
|
+
* a past claimUntilHeight just means no live claim (the fields persist as the last-claim record). */
|
|
179
|
+
claimedBy?: string;
|
|
180
|
+
claimUntilHeight?: number;
|
|
181
|
+
/** v2.8 fclaim (§31, V28+): the txid of the granting fclaim Propose. LAST-WRITE-WINS (re-assigned on every
|
|
182
|
+
* grant, never set-once, never cleared), so fill-routing (claimTxid === fclaimTxid) tracks the CURRENT holder.
|
|
183
|
+
* Present only once granted at V28+; a canonical offer field (§5.1). */
|
|
184
|
+
claimTxid?: string;
|
|
185
|
+
fill?: FillEntry;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Lowercased buyers of every fill (partial fills included) of an offer sold by `maker`. A fill of another
|
|
189
|
+
* seller's offer does not count: it costs a sybil about 0.3 CSD, while a buy from the maker is a real purchase. */
|
|
190
|
+
declare function priorBuyers(offers: Record<string, OfferState> | readonly OfferState[] | null | undefined, maker: string): Set<string>;
|
|
191
|
+
|
|
192
|
+
export { type AcceptBidResult, type BigIntish, type LevelIn, MAX_EXP_ARG_SCALED, type NotionalOffer, type SizeBidResult, type SunriseCurveParams, type SunriseOrderParams, type Tier, type TradeNotional, acceptBid, expScaled, expm1Scaled, priorBuyers, rfqBuyAllIn, rfqFillAllIn, sizeBid, sunriseAnswerPrice, sunriseCurve, sunriseOrderCost, sunriseTimeMult, sunriseTokensFor, tierFor, tradeNotional };
|