@pear-protocol/utils 0.0.23 → 0.1.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.
Files changed (40) hide show
  1. package/dist/asset/asset.types.d.ts +1 -0
  2. package/dist/asset/compute.d.ts +9 -1
  3. package/dist/asset/compute.test.d.ts +1 -0
  4. package/dist/fills/compute.test.d.ts +1 -0
  5. package/dist/fills/fill.types.d.ts +1 -0
  6. package/dist/fills/index.d.ts +1 -0
  7. package/dist/index.js +1373 -306
  8. package/dist/math/decimal.d.ts +57 -0
  9. package/dist/math/decimal.test.d.ts +1 -0
  10. package/dist/math/index.d.ts +1 -0
  11. package/dist/position/compute.test.d.ts +1 -0
  12. package/dist/position/index.d.ts +1 -0
  13. package/dist/position/key.d.ts +15 -0
  14. package/dist/precise-price.d.ts +5 -0
  15. package/dist/precise-quantity.d.ts +4 -0
  16. package/dist/sync/applicables/basket.d.ts +42 -0
  17. package/dist/sync/applicables/basket.test.d.ts +1 -0
  18. package/dist/sync/applicables/constants.d.ts +19 -0
  19. package/dist/sync/applicables/exposure.d.ts +47 -0
  20. package/dist/sync/applicables/exposure.test.d.ts +1 -0
  21. package/dist/sync/applicables/fills.d.ts +16 -0
  22. package/dist/sync/applicables/fills.test.d.ts +1 -0
  23. package/dist/sync/applicables/index.d.ts +34 -0
  24. package/dist/sync/applicables/index.test.d.ts +1 -0
  25. package/dist/sync/applicables/matching.d.ts +17 -0
  26. package/dist/sync/applicables/matching.test.d.ts +1 -0
  27. package/dist/sync/applicables/position.d.ts +17 -0
  28. package/dist/sync/applicables/position.test.d.ts +1 -0
  29. package/dist/sync/applicables/snapshot.d.ts +41 -0
  30. package/dist/sync/applicables/synthetics.d.ts +52 -0
  31. package/dist/sync/applicables/synthetics.test.d.ts +1 -0
  32. package/dist/sync/applicables/types.d.ts +77 -0
  33. package/dist/sync/compute.d.ts +23 -9
  34. package/dist/sync/cutoff.d.ts +35 -0
  35. package/dist/sync/cutoff.test.d.ts +1 -0
  36. package/dist/sync/index.d.ts +1 -1
  37. package/dist/sync/types.d.ts +12 -13
  38. package/dist/validate-leverage.d.ts +3 -0
  39. package/dist/validate-quantity.d.ts +4 -0
  40. package/package.json +3 -2
package/dist/index.js CHANGED
@@ -1,11 +1,84 @@
1
- import BigNumber7, { BigNumber } from 'bignumber.js';
1
+ import BigNumber9 from 'bignumber.js';
2
+ import { sha256 } from '@noble/hashes/sha256';
3
+ import { bytesToHex } from '@noble/hashes/utils';
2
4
 
3
5
  // src/asset/compute.ts
4
- var ZERO = new BigNumber7(0);
5
- var BIPS = new BigNumber7(1e4);
6
+ function parse(value) {
7
+ return value instanceof BigNumber9 ? value : new BigNumber9(value);
8
+ }
9
+ function toDecimalString(value) {
10
+ return parse(value).toFixed();
11
+ }
12
+ function signed(value) {
13
+ const bn = parse(value);
14
+ return bn.isNegative() && !bn.isZero() ? bn.toFixed() : `+${bn.toFixed()}`;
15
+ }
16
+ function add(a, b) {
17
+ return parse(a).plus(parse(b));
18
+ }
19
+ function subtract(a, b) {
20
+ return parse(a).minus(parse(b));
21
+ }
22
+ function multiply(a, b) {
23
+ return parse(a).times(parse(b));
24
+ }
25
+ function divide(a, b) {
26
+ return parse(a).div(parse(b));
27
+ }
28
+ function negate(value) {
29
+ return parse(value).negated();
30
+ }
31
+ function abs(value) {
32
+ return parse(value).abs();
33
+ }
34
+ function min(a, b) {
35
+ return BigNumber9.min(parse(a), parse(b));
36
+ }
37
+ function isZero(value) {
38
+ return parse(value).isZero();
39
+ }
40
+ function isPositive(value) {
41
+ return parse(value).isPositive();
42
+ }
43
+ function isNegative(value) {
44
+ return parse(value).isNegative();
45
+ }
46
+ function isFiniteDecimal(value) {
47
+ return parse(value).isFinite();
48
+ }
49
+ function isEqualTo(a, b) {
50
+ return parse(a).isEqualTo(parse(b));
51
+ }
52
+ function isGreaterThan(a, b) {
53
+ return parse(a).isGreaterThan(parse(b));
54
+ }
55
+ function isGreaterThanOrEqual(a, b) {
56
+ return parse(a).isGreaterThanOrEqualTo(parse(b));
57
+ }
58
+ function isLessThan(a, b) {
59
+ return parse(a).isLessThan(parse(b));
60
+ }
61
+ function isLessThanOrEqual(a, b) {
62
+ return parse(a).isLessThanOrEqualTo(parse(b));
63
+ }
64
+ function sideSign(side) {
65
+ switch (side) {
66
+ case "BUY":
67
+ return 1;
68
+ case "SELL":
69
+ return -1;
70
+ }
71
+ }
72
+ function signedBySide(value, side) {
73
+ return parse(value).times(sideSign(side));
74
+ }
75
+
76
+ // src/math/index.ts
77
+ var ZERO = new BigNumber9(0);
78
+ var BIPS = new BigNumber9(1e4);
6
79
  function exponentiate(value) {
7
80
  const result = Math.exp(value);
8
- return new BigNumber7(result);
81
+ return new BigNumber9(result);
9
82
  }
10
83
  function sign(value) {
11
84
  if (value.isZero()) return 0;
@@ -24,11 +97,11 @@ function computeEntryPriceForAsset(fills) {
24
97
  let entryPrice = ZERO;
25
98
  let positionSize = ZERO;
26
99
  for (const fill of fills) {
27
- const price = new BigNumber7(fill.price);
28
- const quantity = new BigNumber7(fill.quantity);
29
- const signed = fill.side === "BUY" ? quantity : quantity.negated();
30
- const newPositionSize = positionSize.plus(signed);
31
- const isSameDirection = !positionSize.isZero() && positionSize.isPositive() === signed.isPositive();
100
+ const price = new BigNumber9(fill.price);
101
+ const quantity = new BigNumber9(fill.quantity);
102
+ const signed2 = fill.side === "BUY" ? quantity : quantity.negated();
103
+ const newPositionSize = positionSize.plus(signed2);
104
+ const isSameDirection = !positionSize.isZero() && positionSize.isPositive() === signed2.isPositive();
32
105
  const isFlip = !positionSize.isZero() && !newPositionSize.isZero() && newPositionSize.isPositive() !== positionSize.isPositive();
33
106
  if (positionSize.isZero() || isFlip) {
34
107
  entryPrice = price;
@@ -42,56 +115,52 @@ function computeEntryPriceForAsset(fills) {
42
115
 
43
116
  // src/asset/compute.ts
44
117
  function computeAssetEntryNotional(signedQuantity, fills) {
45
- if (signedQuantity.isZero()) return ZERO;
118
+ if (signedQuantity.isZero()) {
119
+ return ZERO;
120
+ }
46
121
  const entryPrice = computeEntryPriceForAsset(fills);
47
- if (entryPrice.isZero()) return ZERO;
122
+ if (entryPrice.isZero()) {
123
+ return ZERO;
124
+ }
48
125
  const unsignedQuantity = signedQuantity.abs();
49
126
  return unsignedQuantity.multipliedBy(entryPrice);
50
127
  }
51
128
  function computeAssetUnrealizedPnL(signedQuantity, currentPrice, fills) {
52
- if (signedQuantity.isZero()) return ZERO;
129
+ if (signedQuantity.isZero()) {
130
+ return ZERO;
131
+ }
53
132
  const entryPrice = computeEntryPriceForAsset(fills);
54
- if (entryPrice.isZero()) return ZERO;
133
+ if (entryPrice.isZero()) {
134
+ return ZERO;
135
+ }
55
136
  const deltaPrice = currentPrice.minus(entryPrice);
56
137
  return signedQuantity.multipliedBy(deltaPrice);
57
138
  }
58
139
  function computeAssetGrossRealizedPnL(fills) {
59
- let entryPrice = ZERO;
60
- let positionSize = ZERO;
61
- let gross = ZERO;
62
- for (const fill of fills) {
63
- const price = new BigNumber7(fill.price);
64
- const quantity = new BigNumber7(fill.quantity);
65
- const signed = fill.side === "BUY" ? quantity : quantity.negated();
66
- const newPositionSize = positionSize.plus(signed);
67
- if (!positionSize.isZero() && positionSize.isPositive() !== signed.isPositive()) {
68
- const closedQty = BigNumber7.min(positionSize.abs(), signed.abs());
69
- if (positionSize.isPositive()) {
70
- gross = gross.plus(closedQty.multipliedBy(price.minus(entryPrice)));
71
- } else {
72
- gross = gross.plus(closedQty.multipliedBy(entryPrice.minus(price)));
73
- }
74
- }
75
- const isSameDirection = !positionSize.isZero() && positionSize.isPositive() === signed.isPositive();
76
- const isFlip = !positionSize.isZero() && !newPositionSize.isZero() && newPositionSize.isPositive() !== positionSize.isPositive();
77
- if (positionSize.isZero() || isFlip) {
78
- entryPrice = price;
79
- } else if (isSameDirection) {
80
- entryPrice = entryPrice.times(positionSize.abs()).plus(price.times(quantity)).div(newPositionSize.abs());
140
+ return walkAssetFills(fills).gross;
141
+ }
142
+ function computeAssetUndeterminedSizes(fills) {
143
+ const result = {};
144
+ for (const asset of computeAssetRealizedPnLs(fills)) {
145
+ if (!asset.undeterminedSize.isZero()) {
146
+ result[asset.id] = asset.undeterminedSize;
81
147
  }
82
- positionSize = newPositionSize;
83
148
  }
84
- return gross;
149
+ return result;
85
150
  }
86
151
  function computeAssetUnrealizedPnLs(exposureMap, priceMap, fills) {
87
152
  const result = [];
88
153
  for (const [id, signedQtyString] of Object.entries(exposureMap)) {
89
154
  const priceString = priceMap[id];
90
- if (!priceString) continue;
91
- const signedQuantity = new BigNumber7(signedQtyString);
92
- if (signedQuantity.isZero()) continue;
93
- const currentPrice = new BigNumber7(priceString);
94
- const assetFills = fills.filter((f) => f.symbol === id);
155
+ if (!priceString) {
156
+ continue;
157
+ }
158
+ const signedQuantity = new BigNumber9(signedQtyString);
159
+ if (signedQuantity.isZero()) {
160
+ continue;
161
+ }
162
+ const currentPrice = new BigNumber9(priceString);
163
+ const assetFills = fills.filter((fill) => fill.symbol === id);
95
164
  const entryPrice = computeEntryPriceForAsset(assetFills);
96
165
  const upnl = signedQuantity.multipliedBy(currentPrice.minus(entryPrice));
97
166
  result.push({
@@ -106,22 +175,23 @@ function computeAssetUnrealizedPnLs(exposureMap, priceMap, fills) {
106
175
  }
107
176
  function computeAssetRealizedPnLs(fills) {
108
177
  const bySymbol = /* @__PURE__ */ new Map();
109
- for (const f of fills) {
110
- const list = bySymbol.get(f.symbol) ?? [];
111
- list.push(f);
112
- bySymbol.set(f.symbol, list);
178
+ for (const fill of fills) {
179
+ const list = bySymbol.get(fill.symbol) ?? [];
180
+ list.push(fill);
181
+ bySymbol.set(fill.symbol, list);
113
182
  }
114
183
  const result = [];
115
184
  for (const [id, assetFills] of bySymbol) {
116
- const tradeFees = assetFills.reduce((acc, f) => acc.plus(new BigNumber7(f.tradeFee ?? "0")), ZERO);
117
- const pearFees = assetFills.reduce((acc, f) => acc.plus(new BigNumber7(f.pearFee ?? "0")), ZERO);
185
+ const tradeFees = assetFills.reduce((acc, fill) => acc.plus(new BigNumber9(fill.tradeFee ?? "0")), ZERO);
186
+ const pearFees = assetFills.reduce((acc, fill) => acc.plus(new BigNumber9(fill.pearFee ?? "0")), ZERO);
118
187
  const totalFees = tradeFees.plus(pearFees);
119
- const grossPnl = computeAssetGrossRealizedPnL(assetFills);
188
+ const { gross: grossPnl, undeterminedSize } = walkAssetFills(assetFills);
120
189
  const netPnl = grossPnl.minus(totalFees);
121
190
  result.push({
122
191
  id,
123
192
  grossPnl,
124
193
  netPnl,
194
+ undeterminedSize,
125
195
  totalFees,
126
196
  tradeFees,
127
197
  pearFees
@@ -129,48 +199,148 @@ function computeAssetRealizedPnLs(fills) {
129
199
  }
130
200
  return result;
131
201
  }
202
+ function walkAssetFills(fills) {
203
+ let entryPrice = ZERO;
204
+ let positionSize = ZERO;
205
+ let basisUnpriced = false;
206
+ let gross = ZERO;
207
+ let undeterminedSize = ZERO;
208
+ for (const fill of fills) {
209
+ const price = parse(fill.price);
210
+ const quantity = parse(fill.quantity);
211
+ const signedQuantity = signedBySide(quantity, fill.side);
212
+ const newPositionSize = add(positionSize, signedQuantity);
213
+ if (!isZero(positionSize) && isPositive(positionSize) !== isPositive(signedQuantity)) {
214
+ const closedQty = min(positionSize.abs(), signedQuantity.abs());
215
+ if (basisUnpriced || isZero(price)) {
216
+ undeterminedSize = add(undeterminedSize, closedQty);
217
+ } else if (isPositive(positionSize)) {
218
+ gross = add(gross, multiply(closedQty, subtract(price, entryPrice)));
219
+ } else {
220
+ gross = add(gross, multiply(closedQty, subtract(entryPrice, price)));
221
+ }
222
+ }
223
+ const isSameDirection = !isZero(positionSize) && isPositive(positionSize) === isPositive(signedQuantity);
224
+ const isFlip = !isZero(positionSize) && !isZero(newPositionSize) && isPositive(newPositionSize) !== isPositive(positionSize);
225
+ if (isZero(positionSize) || isFlip) {
226
+ entryPrice = price;
227
+ basisUnpriced = isZero(price);
228
+ } else if (isSameDirection) {
229
+ entryPrice = entryPrice.times(positionSize.abs()).plus(price.times(quantity)).div(newPositionSize.abs());
230
+ basisUnpriced = basisUnpriced || isZero(price);
231
+ }
232
+ positionSize = newPositionSize;
233
+ }
234
+ return { gross, undeterminedSize };
235
+ }
132
236
  function computeBasketWeightedRatioV1(basket, priceMap) {
133
237
  let logSum = 0;
134
238
  for (const leg of basket) {
135
239
  const price = priceMap[leg.symbol];
136
- if (!price) return null;
240
+ if (!price) {
241
+ return null;
242
+ }
137
243
  const priceNum = Number(price);
138
- if (priceNum <= 0) return null;
244
+ if (priceNum <= 0) {
245
+ return null;
246
+ }
139
247
  const sign2 = leg.side === "BUY" ? 1 : -1;
140
248
  logSum += sign2 * leg.weight * Math.log(priceNum);
141
249
  }
142
250
  return exponentiate(logSum);
143
251
  }
144
252
  function contractsToBase(contracts, contractSize) {
145
- return new BigNumber7(contracts).times(contractSize);
253
+ return new BigNumber9(contracts).times(contractSize);
146
254
  }
147
255
  function baseToContracts(base, contractSize) {
148
- return new BigNumber7(base).div(contractSize);
256
+ return new BigNumber9(base).div(contractSize);
149
257
  }
150
- function computePositionMarkNotional(assetUPnLs) {
151
- return assetUPnLs.reduce((acc, asset) => {
152
- if (asset.signedQuantity.isZero()) return acc;
153
- return acc.plus(asset.signedQuantity.abs().multipliedBy(asset.currentPrice));
154
- }, ZERO);
258
+ function computeRealizedPnlBySymbol(fills) {
259
+ const bySymbol = groupFillsBySymbol(fills);
260
+ const result = /* @__PURE__ */ new Map();
261
+ for (const [symbol, symbolFills] of bySymbol) {
262
+ const sorted = [...symbolFills].sort((a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime());
263
+ result.set(symbol, computeRealizedForSymbol(sorted));
264
+ }
265
+ return result;
155
266
  }
156
- function computePositionEntryNotional(assetUPnLs) {
157
- return assetUPnLs.reduce((acc, asset) => {
158
- if (asset.signedQuantity.isZero()) return acc;
159
- return acc.plus(asset.signedQuantity.abs().multipliedBy(asset.entryPrice));
160
- }, ZERO);
267
+ function computeRealizedPnlFromFills(fills) {
268
+ const byPnl = computeRealizedPnlBySymbol(fills);
269
+ let total = ZERO;
270
+ for (const pnl of byPnl.values()) {
271
+ total = total.plus(pnl);
272
+ }
273
+ return total;
161
274
  }
162
- function computePositionUnrealizedPnL(assetUPnLs) {
163
- return assetUPnLs.reduce((acc, asset) => acc.plus(asset.upnl), ZERO);
275
+ function groupFillsBySymbol(fills) {
276
+ const bySymbol = /* @__PURE__ */ new Map();
277
+ for (const fill of fills) {
278
+ const list = bySymbol.get(fill.symbol) ?? [];
279
+ list.push(fill);
280
+ bySymbol.set(fill.symbol, list);
281
+ }
282
+ return bySymbol;
164
283
  }
165
- function computePositionGrossRealizedPnL(assetPnLs) {
166
- return assetPnLs.reduce((acc, asset) => acc.plus(asset.grossPnl), ZERO);
284
+ function computeRealizedForSymbol(fills) {
285
+ let entryPrice = ZERO;
286
+ let positionSize = ZERO;
287
+ let realized = ZERO;
288
+ for (const fill of fills) {
289
+ const price = new BigNumber9(fill.price);
290
+ const quantity = new BigNumber9(fill.quantity);
291
+ const signed2 = fill.side === "BUY" ? quantity : quantity.negated();
292
+ const newPositionSize = positionSize.plus(signed2);
293
+ if (!positionSize.isZero() && positionSize.isPositive() !== signed2.isPositive()) {
294
+ const closedQty = BigNumber9.min(positionSize.abs(), signed2.abs());
295
+ if (positionSize.isPositive()) {
296
+ realized = realized.plus(closedQty.multipliedBy(price.minus(entryPrice)));
297
+ } else {
298
+ realized = realized.plus(closedQty.multipliedBy(entryPrice.minus(price)));
299
+ }
300
+ }
301
+ const isSameDirection = !positionSize.isZero() && positionSize.isPositive() === signed2.isPositive();
302
+ const isFlip = !positionSize.isZero() && !newPositionSize.isZero() && newPositionSize.isPositive() !== positionSize.isPositive();
303
+ if (positionSize.isZero() || isFlip) {
304
+ entryPrice = price;
305
+ } else if (isSameDirection) {
306
+ entryPrice = entryPrice.times(positionSize.abs()).plus(price.times(quantity)).div(newPositionSize.abs());
307
+ }
308
+ positionSize = newPositionSize;
309
+ }
310
+ return realized;
167
311
  }
168
- function computePositionNetRealizedPnL(assetPnLs) {
169
- return assetPnLs.reduce((acc, asset) => acc.plus(asset.netPnl), ZERO);
312
+ function computeUnrealizedPositionState(exposureMap, priceMap, fills) {
313
+ const assetUPnLs = computeAssetUnrealizedPnLs(exposureMap, priceMap, fills);
314
+ const entryPrice = computePositionEntryPrice(assetUPnLs);
315
+ const entryNotional = computePositionEntryNotional(assetUPnLs);
316
+ const markPrice = computePositionCurrentPrice(assetUPnLs);
317
+ const markNotional = computePositionMarkNotional(assetUPnLs);
318
+ const upnl = computePositionUnrealizedPnL(assetUPnLs);
319
+ const upnlBips = computePositionUnrealizedPnLBips(upnl, entryNotional);
320
+ return {
321
+ assetUPnLs,
322
+ entryPrice,
323
+ entryNotional,
324
+ markPrice,
325
+ markNotional,
326
+ upnl,
327
+ upnlBips
328
+ };
170
329
  }
171
- function computePositionUnrealizedPnLBips(upnl, entryNotional) {
172
- if (upnl.isZero() || entryNotional.isZero()) return ZERO;
173
- return upnl.div(entryNotional).multipliedBy(BIPS);
330
+ function computeRealizedPositionState(fills) {
331
+ const assetPnLs = computeAssetRealizedPnLs(fills);
332
+ const grossPnl = computePositionGrossRealizedPnL(assetPnLs);
333
+ const netPnl = computePositionNetRealizedPnL(assetPnLs);
334
+ const tradeFees = assetPnLs.reduce((acc, asset) => acc.plus(asset.tradeFees), ZERO);
335
+ const pearFees = assetPnLs.reduce((acc, asset) => acc.plus(asset.pearFees), ZERO);
336
+ const totalFees = tradeFees.plus(pearFees);
337
+ return {
338
+ grossPnl,
339
+ netPnl,
340
+ totalFees,
341
+ tradeFees,
342
+ pearFees
343
+ };
174
344
  }
175
345
  function computePositionEntryPrice(assetUPnLs) {
176
346
  const { totalWeightedPrice, totalWeight } = assetUPnLs.reduce(
@@ -191,6 +361,14 @@ function computePositionEntryPrice(assetUPnLs) {
191
361
  }
192
362
  return totalWeightedPrice.dividedBy(totalWeight);
193
363
  }
364
+ function computePositionEntryNotional(assetUPnLs) {
365
+ return assetUPnLs.reduce((acc, asset) => {
366
+ if (asset.signedQuantity.isZero()) {
367
+ return acc;
368
+ }
369
+ return acc.plus(asset.signedQuantity.abs().multipliedBy(asset.entryPrice));
370
+ }, ZERO);
371
+ }
194
372
  function computePositionCurrentPrice(assetUPnLs) {
195
373
  const { totalWeightedPrice, totalWeight } = assetUPnLs.reduce(
196
374
  (acc, { signedQuantity, currentPrice }) => {
@@ -210,42 +388,40 @@ function computePositionCurrentPrice(assetUPnLs) {
210
388
  }
211
389
  return totalWeightedPrice.dividedBy(totalWeight);
212
390
  }
213
- function computeUnrealizedPositionState(exposureMap, priceMap, fills) {
214
- const assetUPnLs = computeAssetUnrealizedPnLs(exposureMap, priceMap, fills);
215
- const entryPrice = computePositionEntryPrice(assetUPnLs);
216
- const entryNotional = computePositionEntryNotional(assetUPnLs);
217
- const markPrice = computePositionCurrentPrice(assetUPnLs);
218
- const markNotional = computePositionMarkNotional(assetUPnLs);
219
- const upnl = computePositionUnrealizedPnL(assetUPnLs);
220
- const upnlBips = computePositionUnrealizedPnLBips(upnl, entryNotional);
221
- return {
222
- assetUPnLs,
223
- entryPrice,
224
- entryNotional,
225
- markPrice,
226
- markNotional,
227
- upnl,
228
- upnlBips
229
- };
391
+ function computePositionMarkNotional(assetUPnLs) {
392
+ return assetUPnLs.reduce((acc, asset) => {
393
+ if (asset.signedQuantity.isZero()) {
394
+ return acc;
395
+ }
396
+ return acc.plus(asset.signedQuantity.abs().multipliedBy(asset.currentPrice));
397
+ }, ZERO);
230
398
  }
231
- function computeRealizedPositionState(fills) {
232
- const assetPnLs = computeAssetRealizedPnLs(fills);
233
- const grossPnl = computePositionGrossRealizedPnL(assetPnLs);
234
- const netPnl = computePositionNetRealizedPnL(assetPnLs);
235
- const tradeFees = assetPnLs.reduce((acc, asset) => acc.plus(asset.tradeFees), ZERO);
236
- const pearFees = assetPnLs.reduce((acc, asset) => acc.plus(asset.pearFees), ZERO);
237
- const totalFees = tradeFees.plus(pearFees);
238
- return {
239
- grossPnl,
240
- netPnl,
241
- totalFees,
242
- tradeFees,
243
- pearFees
244
- };
399
+ function computePositionUnrealizedPnL(assetUPnLs) {
400
+ return assetUPnLs.reduce((acc, asset) => acc.plus(asset.upnl), ZERO);
245
401
  }
246
- BigNumber7.config({
402
+ function computePositionUnrealizedPnLBips(upnl, entryNotional) {
403
+ if (upnl.isZero() || entryNotional.isZero()) {
404
+ return ZERO;
405
+ }
406
+ return upnl.div(entryNotional).multipliedBy(BIPS);
407
+ }
408
+ function computePositionGrossRealizedPnL(assetPnLs) {
409
+ return assetPnLs.reduce((acc, asset) => acc.plus(asset.grossPnl), ZERO);
410
+ }
411
+ function computePositionNetRealizedPnL(assetPnLs) {
412
+ return assetPnLs.reduce((acc, asset) => acc.plus(asset.netPnl), ZERO);
413
+ }
414
+ function generateDeterministicPositionKey(input) {
415
+ const symbols = [...input.symbols].sort((a, b) => a.localeCompare(b));
416
+ const payload = JSON.stringify({
417
+ tradeAccountId: input.tradeAccountId.toLowerCase(),
418
+ symbols
419
+ });
420
+ return bytesToHex(sha256(new TextEncoder().encode(payload)));
421
+ }
422
+ BigNumber9.config({
247
423
  DECIMAL_PLACES: 30,
248
- ROUNDING_MODE: BigNumber7.ROUND_DOWN
424
+ ROUNDING_MODE: BigNumber9.ROUND_DOWN
249
425
  });
250
426
  function precisePrice(connector, raw, precision, side = "BUY", slippageToleranceBps = 0) {
251
427
  const normalized = normalizePrice(raw);
@@ -264,22 +440,22 @@ function withSlippageTolerance(normalized, slippageToleranceBps, side) {
264
440
  if (slippageToleranceBps === 0) {
265
441
  return normalized;
266
442
  }
267
- const bps = new BigNumber7(slippageToleranceBps);
443
+ const bps = new BigNumber9(slippageToleranceBps);
268
444
  const factor = bps.div(1e4);
269
- return side === "BUY" ? normalized.times(new BigNumber7(1).plus(factor)) : normalized.times(new BigNumber7(1).minus(factor));
445
+ return side === "BUY" ? normalized.times(new BigNumber9(1).plus(factor)) : normalized.times(new BigNumber9(1).minus(factor));
270
446
  }
271
447
  function preciseHyperliquidPrice(price, precision, side) {
272
448
  const maxDecimals = precision.price.decimals;
273
449
  const maxSigFigs = precision.price.sigFigs ?? 5;
274
- const rounding = side === "BUY" ? BigNumber7.ROUND_FLOOR : BigNumber7.ROUND_CEIL;
450
+ const rounding = side === "BUY" ? BigNumber9.ROUND_FLOOR : BigNumber9.ROUND_CEIL;
275
451
  price = price.decimalPlaces(maxDecimals, rounding);
276
452
  price = clampSigFigs(price, maxSigFigs, maxDecimals, rounding);
277
453
  return price.toFixed();
278
454
  }
279
455
  function preciseTickGridPrice(price, precision, side) {
280
456
  const tick = precision.price.tick ?? 1e-4;
281
- const rounding = side === "BUY" ? BigNumber7.ROUND_FLOOR : BigNumber7.ROUND_CEIL;
282
- const tickBN = new BigNumber7(tick);
457
+ const rounding = side === "BUY" ? BigNumber9.ROUND_FLOOR : BigNumber9.ROUND_CEIL;
458
+ const tickBN = new BigNumber9(tick);
283
459
  const scaled = price.div(tickBN);
284
460
  const snapped = scaled.integerValue(rounding).times(tickBN);
285
461
  const maxDecimals = precision.price.decimals;
@@ -287,12 +463,12 @@ function preciseTickGridPrice(price, precision, side) {
287
463
  return final.toFixed();
288
464
  }
289
465
  function clampSigFigs(price, maxSigFigs, maxDecimals, rounding) {
290
- const countSigFigs = (x) => x.toFixed().replace(".", "").replace(/^0+/, "").length;
466
+ const countSigFigs = (value) => value.toFixed().replace(".", "").replace(/^0+/, "").length;
291
467
  if (countSigFigs(price) <= maxSigFigs) {
292
468
  return price;
293
469
  }
294
- for (let d = maxDecimals; d >= 0; d--) {
295
- const candidate = price.decimalPlaces(d, rounding);
470
+ for (let decimals = maxDecimals; decimals >= 0; decimals--) {
471
+ const candidate = price.decimalPlaces(decimals, rounding);
296
472
  if (countSigFigs(candidate) <= maxSigFigs) {
297
473
  return candidate;
298
474
  }
@@ -300,12 +476,12 @@ function clampSigFigs(price, maxSigFigs, maxDecimals, rounding) {
300
476
  return price;
301
477
  }
302
478
  function normalizePrice(value) {
303
- const bn = new BigNumber7(value);
304
- return new BigNumber7(bn.toFixed());
479
+ const parsed = new BigNumber9(value);
480
+ return new BigNumber9(parsed.toFixed());
305
481
  }
306
- BigNumber7.config({
482
+ BigNumber9.config({
307
483
  DECIMAL_PLACES: 30,
308
- ROUNDING_MODE: BigNumber7.ROUND_DOWN
484
+ ROUNDING_MODE: BigNumber9.ROUND_DOWN
309
485
  });
310
486
  function preciseQuantity(connector, raw, precision, side) {
311
487
  switch (connector) {
@@ -320,222 +496,1042 @@ function preciseQuantity(connector, raw, precision, side) {
320
496
  }
321
497
  function preciseHyperliquidQuantity(raw, precision, side) {
322
498
  const decimals = precision.quantity.decimals;
323
- const qty = new BigNumber7(raw);
324
- const rounded = qty.decimalPlaces(decimals, side === "BUY" ? BigNumber7.ROUND_FLOOR : BigNumber7.ROUND_CEIL);
499
+ const quantity = new BigNumber9(raw);
500
+ const rounded = quantity.decimalPlaces(decimals, side === "BUY" ? BigNumber9.ROUND_FLOOR : BigNumber9.ROUND_CEIL);
325
501
  return rounded.toFixed();
326
502
  }
327
503
  function preciseStepGridQuantity(raw, precision, side) {
328
504
  const step = precision.quantity.step;
329
- const qty = baseToContracts(raw, precision.quantity.contractSize);
330
- const stepBN = new BigNumber7(step);
331
- const scaled = qty.div(stepBN);
332
- const rounded = side === "BUY" ? scaled.integerValue(BigNumber7.ROUND_FLOOR) : scaled.integerValue(BigNumber7.ROUND_CEIL);
505
+ const contractQuantity = baseToContracts(raw, precision.quantity.contractSize);
506
+ const stepBN = new BigNumber9(step);
507
+ const scaled = contractQuantity.div(stepBN);
508
+ const rounded = side === "BUY" ? scaled.integerValue(BigNumber9.ROUND_FLOOR) : scaled.integerValue(BigNumber9.ROUND_CEIL);
333
509
  const result = rounded.times(stepBN);
334
510
  const maxDecimals = precision.quantity.decimals;
335
- const final = result.decimalPlaces(maxDecimals, side === "BUY" ? BigNumber7.ROUND_FLOOR : BigNumber7.ROUND_CEIL);
511
+ const final = result.decimalPlaces(maxDecimals, side === "BUY" ? BigNumber9.ROUND_FLOOR : BigNumber9.ROUND_CEIL);
336
512
  return final.toFixed();
337
513
  }
338
- async function computeSyncPayload(args) {
339
- const { openPositions, provisionalFills, confirmedExchangeFillIds, externalFills } = args;
340
- const fillsToProcess = _prepareExternalFills(externalFills, confirmedExchangeFillIds);
341
- if (fillsToProcess.length === 0) {
342
- return null;
514
+
515
+ // src/sync/applicables/constants.ts
516
+ var DUST = parse("0.00000001");
517
+ var MISSED_HISTORY_FILL_PREFIX = "opening-synthetic-";
518
+ var BASKET_RULES = [
519
+ // V2 Basket Hyperliquid — legs of one Pear basket order share the order's cloid.
520
+ {
521
+ matches: (fill) => fill.connector === "hyperliquid" && isPearCloid(fill.connector, fill.cloid),
522
+ key: (fill) => `v2-basket-hl ${fill.cloid}`
343
523
  }
344
- const { reconcilables, confirmables, orphanCreatables } = _seggregateExternalFills(provisionalFills, fillsToProcess);
345
- const groupedConfirmables = confirmables.reduce((acc, fill) => {
346
- acc[fill.positionId] = [...acc[fill.positionId] || [], fill];
347
- return acc;
348
- }, {});
349
- const groupedCreatablesBySymbol = _groupCreatablesBySymbol(orphanCreatables, openPositions);
350
- const groupedCreatables = {};
351
- for (const group of groupedCreatablesBySymbol.values()) {
352
- const zeroCrossing = _walkBackwardsToFindLastZeroCrossing(group);
353
- for (const fill of zeroCrossing) {
354
- const existing = groupedCreatables[fill.positionId];
355
- if (existing) {
356
- groupedCreatables[fill.positionId] = [...existing, fill];
357
- } else {
358
- groupedCreatables[fill.positionId] = [fill];
524
+ ];
525
+ function isPearCloid(connector, cloid) {
526
+ if (!cloid) {
527
+ return false;
528
+ }
529
+ return PEAR_CLOID_CHECK[connector]?.(cloid) ?? false;
530
+ }
531
+ var PEAR_CLOID_CHECK = {
532
+ hyperliquid: (cloid) => cloid.startsWith("0x50454152"),
533
+ lighter: (cloid) => cloid.length === 15 && cloid.startsWith("10")
534
+ };
535
+
536
+ // src/sync/applicables/basket.ts
537
+ var Basket = class _Basket {
538
+ constructor(legs) {
539
+ this.legs = legs;
540
+ }
541
+ legs;
542
+ /** Detect baskets from a fill stream, returned in ascending time order. */
543
+ static detect(fills) {
544
+ return groupFillsIntoBaskets(fills).map((group) => new _Basket(combineFillsBySymbolAndSide(group)));
545
+ }
546
+ /** Active long symbol set the matcher compares against open positions. */
547
+ get longs() {
548
+ return new Set(this.legs.filter((leg) => leg.side === "BUY").map((leg) => leg.symbol));
549
+ }
550
+ /** Active short symbol set the matcher compares against open positions. */
551
+ get shorts() {
552
+ return new Set(this.legs.filter((leg) => leg.side === "SELL").map((leg) => leg.symbol));
553
+ }
554
+ };
555
+ var CombinedLeg = class {
556
+ constructor(symbol, side) {
557
+ this.symbol = symbol;
558
+ this.side = side;
559
+ }
560
+ symbol;
561
+ side;
562
+ _totalSize = ZERO;
563
+ queue = [];
564
+ get totalSize() {
565
+ return this._totalSize;
566
+ }
567
+ /** Append a fill to this leg's queue and grow the total size. */
568
+ addFill(fill) {
569
+ this._totalSize = this._totalSize.plus(fill.quantity);
570
+ this.queue.push({ fill, remaining: parse(fill.quantity) });
571
+ }
572
+ /**
573
+ * Take `qty` of fill slices from this leg (FIFO across source fills, consumes the queue).
574
+ * Returns the parts taken, which are consumed from the queue.
575
+ */
576
+ take(qty) {
577
+ const parts = [];
578
+ let need = qty;
579
+ for (const entry of this.queue) {
580
+ if (need.isLessThanOrEqualTo(0)) {
581
+ break;
582
+ }
583
+ if (entry.remaining.isZero()) {
584
+ continue;
359
585
  }
586
+ const taken = min(entry.remaining, need);
587
+ parts.push({ source: entry.fill, quantity: taken.toFixed() });
588
+ entry.remaining = entry.remaining.minus(taken);
589
+ need = need.minus(taken);
360
590
  }
591
+ return parts;
361
592
  }
362
- for (const fills of Object.values(groupedCreatables)) {
363
- fills.sort((a, b) => a.timestamp - b.timestamp);
593
+ };
594
+ function groupFillsIntoBaskets(fills) {
595
+ const buckets = /* @__PURE__ */ new Map();
596
+ for (const fill of fills) {
597
+ const rule = BASKET_RULES.find((r) => r.matches(fill));
598
+ const key = rule ? rule.key(fill) : `id ${fill.exchangeFillId}`;
599
+ const bucket = buckets.get(key) ?? { timestamp: fill.timestamp, fills: [] };
600
+ bucket.timestamp = Math.min(bucket.timestamp, fill.timestamp);
601
+ bucket.fills.push(fill);
602
+ buckets.set(key, bucket);
364
603
  }
365
- return {
366
- reconcilables: Object.keys(reconcilables).length > 0 ? reconcilables : null,
367
- confirmables: Object.keys(groupedConfirmables).length > 0 ? groupedConfirmables : null,
368
- creatables: Object.keys(groupedCreatables).length > 0 ? groupedCreatables : null,
369
- meta: {
370
- externalMinTs: fillsToProcess[0]?.timestamp ?? null,
371
- externalMaxTs: fillsToProcess[fillsToProcess.length - 1]?.timestamp ?? null,
372
- processedCount: fillsToProcess.length
604
+ const sorted = [...buckets.values()].sort((a, b) => a.timestamp - b.timestamp);
605
+ return sorted.map((bucket) => bucket.fills);
606
+ }
607
+ function combineFillsBySymbolAndSide(group) {
608
+ const map = /* @__PURE__ */ new Map();
609
+ for (const fill of group) {
610
+ const key = `${fill.symbol}-${fill.side}`;
611
+ let combined = map.get(key);
612
+ if (!combined) {
613
+ combined = new CombinedLeg(fill.symbol, fill.side);
614
+ map.set(key, combined);
615
+ }
616
+ combined.addFill(fill);
617
+ }
618
+ return [...map.values()];
619
+ }
620
+
621
+ // src/sync/applicables/exposure.ts
622
+ var Exposure = class _Exposure {
623
+ constructor(legs) {
624
+ this.legs = legs;
625
+ }
626
+ legs;
627
+ /** Create an exposure with no legs — the starting state for a new position. */
628
+ static empty() {
629
+ return new _Exposure(/* @__PURE__ */ new Map());
630
+ }
631
+ /** Build from a wire `Record<symbol, quantity>` (existing DB position exposure). */
632
+ static fromRecord(record) {
633
+ const legs = /* @__PURE__ */ new Map();
634
+ for (const [symbol, qty] of Object.entries(record)) {
635
+ legs.set(symbol, parse(qty));
636
+ }
637
+ return new _Exposure(legs);
638
+ }
639
+ /** Net signed exposure a set of fill parts contributes for `symbol` (BUY +, SELL −). */
640
+ static netFromParts(parts, symbol) {
641
+ let total = ZERO;
642
+ for (const part of parts) {
643
+ if (part.source.symbol === symbol) {
644
+ total = total.plus(signedBySide(part.quantity, part.source.side));
645
+ }
646
+ }
647
+ return total;
648
+ }
649
+ /** Return an independent copy of this exposure so mutations do not alias. */
650
+ clone() {
651
+ return new _Exposure(new Map(this.legs));
652
+ }
653
+ /** Signed exposure for `symbol` (0 when absent). */
654
+ get(symbol) {
655
+ return this.legs.get(symbol) ?? ZERO;
656
+ }
657
+ /** Apply a signed `delta` to `symbol`, creating the leg if it does not exist. */
658
+ addDelta(symbol, delta) {
659
+ this.legs.set(symbol, this.get(symbol).plus(delta));
660
+ }
661
+ /** Convert a side+quantity pair to a signed delta and forward to `addDelta`. */
662
+ addSigned(symbol, quantity, side) {
663
+ this.addDelta(symbol, signedBySide(quantity, side));
664
+ }
665
+ /** Active (non-zero) long and short leg sets. */
666
+ activeLegs() {
667
+ const longs = /* @__PURE__ */ new Set();
668
+ const shorts = /* @__PURE__ */ new Set();
669
+ for (const [symbol, qty] of this.legs) {
670
+ switch (sign(qty)) {
671
+ case 1:
672
+ longs.add(symbol);
673
+ break;
674
+ case -1:
675
+ shorts.add(symbol);
676
+ break;
677
+ }
678
+ }
679
+ return { longs, shorts };
680
+ }
681
+ /** True once every leg sits within DUST of zero (effectively closed). */
682
+ isFlat() {
683
+ for (const qty of this.legs.values()) {
684
+ if (isGreaterThanOrEqual(qty.abs(), DUST)) {
685
+ return false;
686
+ }
687
+ }
688
+ return true;
689
+ }
690
+ /** True when every leg is exactly zero (a fully CLOSED position). */
691
+ isAllZero() {
692
+ for (const qty of this.legs.values()) {
693
+ if (!qty.isZero()) {
694
+ return false;
695
+ }
373
696
  }
697
+ return true;
698
+ }
699
+ /**
700
+ * Snap every non-zero leg to exactly zero. Called once a position is flat so it
701
+ * reads CLOSED (all-zero exposure) and never lingers in the open list with stray
702
+ * dust that a later reopen of the same pair could merge into.
703
+ */
704
+ snapToZero() {
705
+ for (const [symbol, qty] of this.legs) {
706
+ if (!qty.isZero()) {
707
+ this.legs.set(symbol, ZERO);
708
+ }
709
+ }
710
+ }
711
+ /** All symbols with a leg, including zeroed ones (a closed leg still carries cost basis). */
712
+ symbols() {
713
+ return [...this.legs.keys()];
714
+ }
715
+ /** Canonical signed-string view for the wire (`+0` for zero). */
716
+ toSigned() {
717
+ const out = {};
718
+ for (const [symbol, qty] of this.legs) {
719
+ out[symbol] = signed(qty);
720
+ }
721
+ return out;
722
+ }
723
+ };
724
+
725
+ // src/sync/applicables/matching.ts
726
+ function matchPositions(args) {
727
+ const { tradeAccountId, orphanFills, pear } = args;
728
+ const book = WorkingBook.load({ tradeAccountId, openPositions: pear.positions() });
729
+ for (const basket of Basket.detect(orphanFills)) {
730
+ const exactCloseTarget = findExactCloseTarget(book.open(), basket.longs, basket.shorts);
731
+ const leftover = applyCloses({ legs: basket.legs, book, exactCloseTarget });
732
+ if (leftover.length === 0) {
733
+ continue;
734
+ }
735
+ const target = resolveLeftoverTarget({ book, leftover });
736
+ applyLeftover(target, leftover);
737
+ }
738
+ return book.toMatched();
739
+ }
740
+ var WorkingBook = class _WorkingBook {
741
+ constructor(tradeAccountId) {
742
+ this.tradeAccountId = tradeAccountId;
743
+ }
744
+ tradeAccountId;
745
+ all = [];
746
+ openByKey = /* @__PURE__ */ new Map();
747
+ /** Seed the book from existing open DB positions (they arrive pre-filtered by connector). */
748
+ static load(args) {
749
+ const { tradeAccountId, openPositions } = args;
750
+ const book = new _WorkingBook(tradeAccountId);
751
+ for (const p of openPositions) {
752
+ const position = {
753
+ positionKey: book.keyFor(Object.keys(p.exposure)),
754
+ exposure: Exposure.fromRecord(p.exposure),
755
+ parts: [],
756
+ closed: false,
757
+ existingOrigin: p.origin
758
+ };
759
+ book.all.push(position);
760
+ book.openByKey.set(position.positionKey, position);
761
+ }
762
+ return book;
763
+ }
764
+ /** Deterministic position key for a symbol set under this book's trade account. */
765
+ keyFor(symbols) {
766
+ return generateDeterministicPositionKey({ tradeAccountId: this.tradeAccountId, symbols });
767
+ }
768
+ /** Currently open working positions, in insertion order. */
769
+ open() {
770
+ return this.all.filter((position) => !position.closed);
771
+ }
772
+ /** Look up an open working position by its positionKey (for dedup on merge). */
773
+ findOpenByKey(key) {
774
+ return this.openByKey.get(key);
775
+ }
776
+ /** Register a fresh empty working position under `key` and return it. */
777
+ create(key) {
778
+ const position = {
779
+ positionKey: key,
780
+ exposure: Exposure.empty(),
781
+ parts: [],
782
+ closed: false
783
+ };
784
+ this.all.push(position);
785
+ this.openByKey.set(key, position);
786
+ return position;
787
+ }
788
+ /** Flip a position to closed once every leg is within DUST of zero. */
789
+ markClosedIfFlat(position) {
790
+ if (position.closed || !position.exposure.isFlat()) {
791
+ return;
792
+ }
793
+ position.exposure.snapToZero();
794
+ position.closed = true;
795
+ this.openByKey.delete(position.positionKey);
796
+ }
797
+ /** Working positions that received fills become MatchedPosition records; untouched ones are dropped. */
798
+ toMatched() {
799
+ return this.all.filter((position) => position.parts.length > 0).map((position) => ({
800
+ positionKey: position.positionKey,
801
+ parts: position.parts,
802
+ closed: position.closed,
803
+ exposure: position.exposure,
804
+ existingOrigin: position.existingOrigin
805
+ }));
806
+ }
807
+ };
808
+ function findExactCloseTarget(openList, basketLongs, basketShorts) {
809
+ return openList.find((position) => {
810
+ const { longs, shorts } = position.exposure.activeLegs();
811
+ return setEq(longs, basketShorts) && setEq(shorts, basketLongs);
812
+ });
813
+ }
814
+ var setEq = (a, b) => a.size === b.size && [...a].every((x) => b.has(x));
815
+ function applyCloses(args) {
816
+ const { legs, book, exactCloseTarget } = args;
817
+ const openList = book.open();
818
+ const leftover = [];
819
+ for (const leg of legs) {
820
+ let remaining = leg.totalSize;
821
+ const closeTargetIsLong = sideSign(leg.side) === -1;
822
+ const holders = openList.filter((position) => {
823
+ const qty = position.exposure.get(leg.symbol);
824
+ if (qty.isZero()) {
825
+ return false;
826
+ }
827
+ return closeTargetIsLong ? qty.isPositive() : qty.isNegative();
828
+ });
829
+ if (exactCloseTarget) {
830
+ holders.sort((a, b) => a === exactCloseTarget ? -1 : b === exactCloseTarget ? 1 : 0);
831
+ }
832
+ for (const position of holders) {
833
+ if (isLessThanOrEqual(remaining, ZERO)) {
834
+ break;
835
+ }
836
+ const have = position.exposure.get(leg.symbol).abs();
837
+ const reduce = min(have, remaining);
838
+ position.parts.push(...leg.take(reduce));
839
+ position.exposure.addDelta(leg.symbol, signedBySide(reduce, leg.side));
840
+ remaining = remaining.minus(reduce);
841
+ book.markClosedIfFlat(position);
842
+ }
843
+ if (isGreaterThanOrEqual(remaining, DUST)) {
844
+ leftover.push({ side: leg.side, qty: remaining, leg });
845
+ }
846
+ }
847
+ return leftover;
848
+ }
849
+ function resolveLeftoverTarget(args) {
850
+ const { book, leftover } = args;
851
+ const leftoverLongs = new Set(leftover.filter((entry) => entry.side === "BUY").map((entry) => entry.leg.symbol));
852
+ const leftoverShorts = new Set(leftover.filter((entry) => entry.side === "SELL").map((entry) => entry.leg.symbol));
853
+ const target = book.open().find((position) => {
854
+ const { longs, shorts } = position.exposure.activeLegs();
855
+ const longOk = [...leftoverLongs].every((a) => longs.has(a));
856
+ const shortOk = [...leftoverShorts].every((a) => shorts.has(a));
857
+ return longOk && shortOk;
858
+ });
859
+ if (target) {
860
+ return target;
861
+ }
862
+ const symbols = [.../* @__PURE__ */ new Set([...leftoverLongs, ...leftoverShorts])];
863
+ const positionKey = book.keyFor(symbols);
864
+ return book.findOpenByKey(positionKey) ?? book.create(positionKey);
865
+ }
866
+ function applyLeftover(target, leftover) {
867
+ for (const entry of leftover) {
868
+ target.parts.push(...entry.leg.take(entry.qty));
869
+ target.exposure.addSigned(entry.leg.symbol, entry.qty.toFixed(), entry.side);
870
+ }
871
+ }
872
+
873
+ // src/sync/applicables/synthetics.ts
874
+ function isMissedHistoryFill(fill) {
875
+ return fill.exchangeFillId.startsWith(MISSED_HISTORY_FILL_PREFIX);
876
+ }
877
+ function addFillsForMissedHistory(args) {
878
+ const missedHistoryFills = buildMissedHistoryFills(args);
879
+ return [...missedHistoryFills, ...args.orphanFills].sort((a, b) => a.timestamp - b.timestamp);
880
+ }
881
+ function alignPositionsWithExchange(args) {
882
+ const { positions, pear, snapshot, tradeAccountId } = args;
883
+ const neededSynthetics = findNeededSynthetics({ positions, pear, snapshot });
884
+ return assignSyntheticsToPositions({ neededSynthetics, positions, pear, tradeAccountId });
885
+ }
886
+ function buildSyntheticFill(args) {
887
+ return {
888
+ // Content-addressed id — no wall-clock, no positionId, so re-syncs collide
889
+ // on the dedup index and synthetic generation stays idempotent.
890
+ exchangeFillId: `synthetic-${args.positionKey}-${args.symbol}-${args.side}`,
891
+ cloid: null,
892
+ connector: args.connector,
893
+ symbol: args.symbol,
894
+ side: args.side,
895
+ quantity: args.quantity,
896
+ // Gone-close: exit price is unknown. "0" is the unpriceable sentinel — real
897
+ // fills are never 0 and missed-history fills derive a positive price, so
898
+ // consumers treat a 0-price fill as unpriceable (excluded from PnL/avg-entry,
899
+ // its closed size routed to undeterminedSize).
900
+ price: "0",
901
+ usd: "0",
902
+ timestamp: args.timestamp,
903
+ reduceOnly: false,
904
+ synthetic: true,
905
+ isExternal: true,
906
+ tradeFee: "0",
907
+ builderFee: "0",
908
+ closedReason: args.closedReason,
909
+ liquidation: null
374
910
  };
375
911
  }
376
- function _prepareExternalFills(externalFills, confirmedExchangeFillIdsInput) {
377
- const dedupedByExchangeFillId = /* @__PURE__ */ new Map();
378
- const confirmedExchangeFillIds = new Set(
379
- Array.from(confirmedExchangeFillIdsInput).map((exchangeFillId) => _normalizeExchangeFillId(exchangeFillId)).filter((exchangeFillId) => exchangeFillId !== void 0)
380
- );
381
- const candidates = externalFills.reduce((acc, fill) => {
382
- const normalizedExchangeFillId = _normalizeExchangeFillId(fill.exchangeFillId);
383
- if (!normalizedExchangeFillId) return acc;
384
- if (!_isValidExternalQuantity(fill.quantity)) return acc;
385
- if (!_isValidExternalTimestamp(fill.timestamp)) return acc;
386
- if (confirmedExchangeFillIds.has(normalizedExchangeFillId)) return acc;
387
- acc.push({ ...fill, exchangeFillId: normalizedExchangeFillId });
388
- return acc;
389
- }, []);
390
- for (const fill of candidates) {
391
- const existing = dedupedByExchangeFillId.get(fill.exchangeFillId);
392
- if (!existing || fill.timestamp < existing.timestamp) {
393
- dedupedByExchangeFillId.set(fill.exchangeFillId, fill);
912
+ function buildMissedHistoryFills(args) {
913
+ const { orphanFills, pear, connector, entryBySymbol, snapshot } = args;
914
+ const fillTotals = /* @__PURE__ */ new Map();
915
+ const orphanNotional = /* @__PURE__ */ new Map();
916
+ for (const fill of orphanFills) {
917
+ fillTotals.set(fill.symbol, add(fillTotals.get(fill.symbol) ?? ZERO, signedBySide(fill.quantity, fill.side)));
918
+ const notional = multiply(fill.quantity, fill.price);
919
+ orphanNotional.set(fill.symbol, add(orphanNotional.get(fill.symbol) ?? ZERO, notional));
920
+ }
921
+ const pearTotals = pear.totalBySymbol();
922
+ const assets = /* @__PURE__ */ new Set([...orphanFills.map((fill) => fill.symbol), ...snapshot.symbols()]);
923
+ const earliestTs = orphanFills.reduce((min2, fill) => Math.min(min2, fill.timestamp), Infinity);
924
+ if (!Number.isFinite(earliestTs)) {
925
+ return [];
926
+ }
927
+ const watermark = Math.max(1, earliestTs - 1);
928
+ const haveBySymbol = (symbol) => add(pearTotals.get(symbol) ?? ZERO, fillTotals.get(symbol) ?? ZERO);
929
+ const result = [];
930
+ for (const gap of gapsToTarget({
931
+ assets,
932
+ haveBySymbol,
933
+ snapshot
934
+ })) {
935
+ const { symbol } = gap;
936
+ const price = priceForMissedHistoryFill({
937
+ exchangePosition: snapshot.get(symbol),
938
+ syntheticQty: gap.quantity,
939
+ pearQty: pearTotals.get(symbol) ?? ZERO,
940
+ pearEntry: entryBySymbol[symbol],
941
+ orphanNotional: orphanNotional.get(symbol) ?? ZERO
942
+ });
943
+ result.push({
944
+ connector,
945
+ exchangeFillId: `${MISSED_HISTORY_FILL_PREFIX}${symbol}-${gap.side.toLowerCase()}`,
946
+ cloid: null,
947
+ symbol,
948
+ price,
949
+ quantity: gap.quantity,
950
+ side: gap.side,
951
+ reduceOnly: false,
952
+ timestamp: watermark,
953
+ tradeFee: "0",
954
+ builderFee: "0",
955
+ liquidation: null
956
+ });
957
+ }
958
+ return result;
959
+ }
960
+ function priceForMissedHistoryFill(args) {
961
+ const { exchangePosition, syntheticQty, pearQty, pearEntry, orphanNotional } = args;
962
+ const fallback = exchangePosition?.entryPrice ?? "0";
963
+ if (!exchangePosition) {
964
+ return fallback;
965
+ }
966
+ let realNotional = orphanNotional;
967
+ if (!isZero(pearQty)) {
968
+ if (pearEntry === void 0) {
969
+ return fallback;
394
970
  }
971
+ realNotional = add(realNotional, multiply(abs(pearQty), pearEntry));
395
972
  }
396
- return Array.from(dedupedByExchangeFillId.values()).sort((a, b) => a.timestamp - b.timestamp);
973
+ const synthQty = parse(syntheticQty);
974
+ if (isZero(synthQty)) {
975
+ return fallback;
976
+ }
977
+ const targetNotional = multiply(exchangePosition.size, exchangePosition.entryPrice);
978
+ const derived = divide(subtract(targetNotional, realNotional), synthQty);
979
+ return derived.isFinite() && derived.isPositive() ? derived.toFixed() : fallback;
397
980
  }
398
- function _groupCreatablesBySymbol(orphanCreatables, openPositions) {
399
- const groupedCreatables = /* @__PURE__ */ new Map();
400
- for (const fill of orphanCreatables) {
401
- const positionCandidates = openPositions.reduce((acc, p) => {
402
- const exposure = p.exposure[fill.symbol];
403
- if (!exposure) return acc;
404
- const quantity = new BigNumber(exposure);
405
- if (quantity.isZero() || quantity.isEqualTo("-0")) return acc;
406
- if (fill.timestamp < Date.parse(p.createdAt)) return acc;
407
- acc.push(p);
408
- return acc;
409
- }, []);
410
- const first = positionCandidates[0];
411
- if (!first) continue;
412
- const splits = _splitMaybeCompositeFillAcrossPositions(fill, [first, ...positionCandidates.slice(1)]);
413
- for (const { position, maybeModifiedFill } of splits) {
414
- const key = `${position.id}-${maybeModifiedFill.symbol}`;
415
- const existing = groupedCreatables.get(key);
416
- if (existing) {
417
- existing.fills.push({ ...maybeModifiedFill, type: "create", positionId: position.id });
418
- groupedCreatables.set(key, existing);
419
- } else {
420
- groupedCreatables.set(key, {
421
- positionId: position.id,
422
- symbol: maybeModifiedFill.symbol,
423
- exposure: new BigNumber(position.exposure[maybeModifiedFill.symbol] ?? 0),
424
- fills: [{ ...maybeModifiedFill, type: "create", positionId: position.id }]
981
+ function findNeededSynthetics(args) {
982
+ const { positions, pear, snapshot } = args;
983
+ const pearTotals = pear.totalBySymbol();
984
+ const assets = /* @__PURE__ */ new Set();
985
+ for (const pos of positions) {
986
+ for (const part of pos.parts) {
987
+ assets.add(part.source.symbol);
988
+ }
989
+ }
990
+ for (const symbol of snapshot.symbols()) {
991
+ assets.add(symbol);
992
+ }
993
+ for (const symbol of pearTotals.keys()) {
994
+ assets.add(symbol);
995
+ }
996
+ const haveBySymbol = (symbol) => {
997
+ let totalFills = ZERO;
998
+ for (const pos of positions) {
999
+ totalFills = add(totalFills, Exposure.netFromParts(pos.parts, symbol));
1000
+ }
1001
+ return add(pearTotals.get(symbol) ?? ZERO, totalFills);
1002
+ };
1003
+ const result = /* @__PURE__ */ new Map();
1004
+ for (const gap of gapsToTarget({
1005
+ assets,
1006
+ haveBySymbol,
1007
+ snapshot
1008
+ })) {
1009
+ switch (gap.kind) {
1010
+ case "gone":
1011
+ result.set(gap.symbol, {
1012
+ symbol: gap.symbol,
1013
+ side: gap.side,
1014
+ quantity: gap.quantity,
1015
+ closedReason: "NETTED"
1016
+ });
1017
+ break;
1018
+ case "gap":
1019
+ result.set(gap.symbol, {
1020
+ symbol: gap.symbol,
1021
+ side: gap.side,
1022
+ quantity: gap.quantity
425
1023
  });
1024
+ break;
1025
+ }
1026
+ }
1027
+ return result;
1028
+ }
1029
+ function assignSyntheticsToPositions(args) {
1030
+ const { neededSynthetics, positions, pear, tradeAccountId } = args;
1031
+ const keyOf = (symbols) => generateDeterministicPositionKey({ tradeAccountId, symbols });
1032
+ const existingBySymbol = pear.positionBySymbol();
1033
+ const created = [];
1034
+ const byPosition = /* @__PURE__ */ new Map();
1035
+ for (const [symbol, spec] of neededSynthetics) {
1036
+ let target = positions.find((pos) => !pos.closed && pos.parts.some((part) => part.source.symbol === symbol));
1037
+ if (!target) {
1038
+ const existing = existingBySymbol.get(symbol);
1039
+ const positionKey = existing ? keyOf(Object.keys(existing.exposure)) : keyOf([symbol]);
1040
+ target = positions.find((pos) => pos.positionKey === positionKey) ?? created.find((pos) => pos.positionKey === positionKey);
1041
+ if (!target) {
1042
+ target = existing ? (
1043
+ // Stand-in for an existing DB position untouched by this batch's fills:
1044
+ // seed its exposure and origin so folding the synthetic yields the true
1045
+ // final state (a gone-close nets to zero -> CLOSED/NETTED, a gap writes
1046
+ // the full total, not just the delta).
1047
+ {
1048
+ positionKey,
1049
+ parts: [],
1050
+ closed: false,
1051
+ exposure: Exposure.fromRecord(existing.exposure),
1052
+ existingOrigin: existing.origin
1053
+ }
1054
+ ) : {
1055
+ positionKey,
1056
+ parts: [],
1057
+ closed: false,
1058
+ exposure: Exposure.empty()
1059
+ };
1060
+ created.push(target);
426
1061
  }
427
1062
  }
1063
+ const list = byPosition.get(target) ?? [];
1064
+ list.push(spec);
1065
+ byPosition.set(target, list);
428
1066
  }
429
- return groupedCreatables;
1067
+ return { positions: [...positions, ...created], synthByPosition: byPosition };
430
1068
  }
431
- function _splitMaybeCompositeFillAcrossPositions(fill, positionCandidates) {
432
- if (positionCandidates.length === 1) {
433
- return [{ position: positionCandidates[0], maybeModifiedFill: fill }];
1069
+ function gapsToTarget(args) {
1070
+ const { assets, haveBySymbol, snapshot } = args;
1071
+ const gaps = [];
1072
+ for (const symbol of assets) {
1073
+ const gap = findGapToTarget({
1074
+ symbol,
1075
+ have: haveBySymbol(symbol),
1076
+ target: snapshot.signedSize(symbol)
1077
+ });
1078
+ if (gap) {
1079
+ gaps.push(gap);
1080
+ }
434
1081
  }
435
- const firstPosition = positionCandidates[0];
436
- const firstExposure = new BigNumber(firstPosition.exposure[fill.symbol] ?? 0);
437
- if (firstExposure.isZero() || firstExposure.isEqualTo("-0")) {
438
- throw new Error("Corrupted state: first position has no exposure");
1082
+ return gaps;
1083
+ }
1084
+ function findGapToTarget(args) {
1085
+ const { symbol, have, target } = args;
1086
+ if (isZero(target)) {
1087
+ if (isGreaterThan(abs(have), DUST)) {
1088
+ return {
1089
+ kind: "gone",
1090
+ symbol,
1091
+ side: have.isPositive() ? "SELL" : "BUY",
1092
+ quantity: have.abs().toFixed()
1093
+ };
1094
+ }
1095
+ return null;
439
1096
  }
440
- const signedFillQuantity = new BigNumber(fill.quantity).multipliedBy(fill.side === "BUY" ? 1 : -1);
441
- const firstSign = firstExposure.isGreaterThan(0) ? 1 : -1;
442
- const fillSign = signedFillQuantity.isGreaterThan(0) ? 1 : -1;
443
- if (firstSign === fillSign) {
444
- return [{ position: firstPosition, maybeModifiedFill: fill }];
1097
+ const residual = subtract(target, have);
1098
+ if (isGreaterThan(abs(residual), DUST)) {
1099
+ return {
1100
+ kind: "gap",
1101
+ symbol,
1102
+ side: residual.isPositive() ? "BUY" : "SELL",
1103
+ quantity: residual.abs().toFixed()
1104
+ };
445
1105
  }
446
- const totalExposure = positionCandidates.reduce((acc, p) => {
447
- acc = acc.plus(new BigNumber(p.exposure[fill.symbol] ?? 0));
448
- return acc;
449
- }, new BigNumber(0));
450
- if (totalExposure.isZero() || totalExposure.isEqualTo("-0")) {
451
- throw new Error("Corrupted state: total exposure is zero");
1106
+ return null;
1107
+ }
1108
+
1109
+ // src/sync/applicables/fills.ts
1110
+ function assignPartIds(positions) {
1111
+ const bySource = /* @__PURE__ */ new Map();
1112
+ for (const pos of positions) {
1113
+ for (const part of pos.parts) {
1114
+ const list = bySource.get(part.source.exchangeFillId) ?? [];
1115
+ list.push(part);
1116
+ bySource.set(part.source.exchangeFillId, list);
1117
+ }
452
1118
  }
453
- const willBalanceToZero = totalExposure.negated().isEqualTo(signedFillQuantity);
454
- if (!willBalanceToZero) {
455
- return [{ position: firstPosition, maybeModifiedFill: fill }];
1119
+ const resolution = /* @__PURE__ */ new Map();
1120
+ for (const [sourceId, parts] of bySource) {
1121
+ const single = parts.length === 1 ? parts[0] : void 0;
1122
+ if (single && isEqualTo(single.quantity, single.source.quantity)) {
1123
+ resolution.set(single, { exchangeFillId: sourceId });
1124
+ continue;
1125
+ }
1126
+ parts.forEach((part, i) => {
1127
+ resolution.set(part, {
1128
+ exchangeFillId: `${sourceId}-p${i}`,
1129
+ original: { exchangeFillId: sourceId, quantity: part.source.quantity }
1130
+ });
1131
+ });
456
1132
  }
457
- return positionCandidates.map((position) => {
458
- const signedQuantity = position.exposure[fill.symbol] ?? "0";
459
- const quantity = new BigNumber(signedQuantity).abs();
1133
+ return (part) => {
1134
+ const id = resolution.get(part);
1135
+ if (!id) {
1136
+ throw new Error("applicables: fill part was never assigned an id");
1137
+ }
1138
+ return id;
1139
+ };
1140
+ }
1141
+ function partToApplicableFill(part, id, connector) {
1142
+ const fill = part.source;
1143
+ const base = {
1144
+ exchangeFillId: id.exchangeFillId,
1145
+ connector,
1146
+ symbol: fill.symbol,
1147
+ side: fill.side,
1148
+ quantity: part.quantity,
1149
+ price: fill.price,
1150
+ usd: multiply(part.quantity, fill.price).toFixed(),
1151
+ timestamp: fill.timestamp
1152
+ };
1153
+ if (isMissedHistoryFill(fill)) {
460
1154
  return {
461
- position,
462
- maybeModifiedFill: {
463
- ...fill,
464
- exchangeFillId: `${fill.exchangeFillId}-${position.id}`,
465
- quantity: quantity.toFixed(),
466
- original: {
467
- exchangeFillId: fill.exchangeFillId,
468
- quantity: fill.quantity
1155
+ ...base,
1156
+ cloid: null,
1157
+ reduceOnly: false,
1158
+ synthetic: true,
1159
+ isExternal: true,
1160
+ tradeFee: "0",
1161
+ builderFee: "0",
1162
+ liquidation: null
1163
+ };
1164
+ }
1165
+ const fromPear = isPearCloid(connector, fill.cloid);
1166
+ return {
1167
+ ...base,
1168
+ cloid: fill.cloid ?? null,
1169
+ reduceOnly: fill.reduceOnly,
1170
+ synthetic: false,
1171
+ isExternal: !fromPear,
1172
+ tradeFee: feeShareForPart(fill.tradeFee, part.quantity, fill.quantity),
1173
+ builderFee: feeShareForPart(fill.builderFee, part.quantity, fill.quantity),
1174
+ liquidation: fill.liquidation,
1175
+ ...id.original ? { original: id.original } : {}
1176
+ };
1177
+ }
1178
+ function feeShareForPart(totalFee, partQty, sourceQty) {
1179
+ if (isEqualTo(partQty, sourceQty)) {
1180
+ return totalFee;
1181
+ }
1182
+ if (isZero(sourceQty)) {
1183
+ return "0";
1184
+ }
1185
+ const feeShare = divide(multiply(totalFee, partQty), sourceQty);
1186
+ return feeShare.toFixed();
1187
+ }
1188
+
1189
+ // src/sync/applicables/position.ts
1190
+ function assembleApplicables(args) {
1191
+ const { positions, connector, synthByPosition, now } = args;
1192
+ const partIdFor = assignPartIds(positions);
1193
+ const applicables = [];
1194
+ for (const pos of positions) {
1195
+ const applicable = assembleOneApplicable(pos, connector, partIdFor, synthByPosition.get(pos) ?? [], now);
1196
+ if (applicable) {
1197
+ applicables.push(applicable);
1198
+ }
1199
+ }
1200
+ return applicables;
1201
+ }
1202
+ function assembleOneApplicable(pos, connector, partIdFor, assignedSynthetics, now) {
1203
+ const realFills = pos.parts.map((part) => partToApplicableFill(part, partIdFor(part), connector)).sort((a, b) => a.timestamp - b.timestamp);
1204
+ const syntheticFills = assignedSynthetics.map(
1205
+ (synthetic) => buildSyntheticFill({
1206
+ positionKey: pos.positionKey,
1207
+ connector,
1208
+ timestamp: anchorSyntheticTimestamp(realFills, synthetic.symbol, now),
1209
+ ...synthetic
1210
+ })
1211
+ );
1212
+ const fills = [...realFills, ...syntheticFills].sort((a, b) => a.timestamp - b.timestamp);
1213
+ if (fills.length === 0) {
1214
+ return null;
1215
+ }
1216
+ const exposure = finalExposure(pos, assignedSynthetics);
1217
+ const closed = exposure.isAllZero();
1218
+ const status = closed ? "CLOSED" : "OPEN";
1219
+ const origin = mergeOrigin(pos.existingOrigin, fills);
1220
+ return {
1221
+ positionKey: pos.positionKey,
1222
+ exposure: exposure.toSigned(),
1223
+ origin,
1224
+ status,
1225
+ ...closed ? { closedReason: "NETTED" } : {},
1226
+ fills,
1227
+ metadata: buildMetadata(new Set(exposure.symbols()), realFills)
1228
+ };
1229
+ }
1230
+ function finalExposure(pos, synthetics) {
1231
+ const exposure = pos.exposure.clone();
1232
+ for (const synthetic of synthetics) {
1233
+ exposure.addSigned(synthetic.symbol, synthetic.quantity, synthetic.side);
1234
+ }
1235
+ return exposure;
1236
+ }
1237
+ function anchorSyntheticTimestamp(realFills, symbol, now) {
1238
+ const oldestForSymbol = realFills.find((fill) => fill.symbol === symbol)?.timestamp;
1239
+ if (oldestForSymbol !== void 0) {
1240
+ return Math.max(1, oldestForSymbol - 1);
1241
+ }
1242
+ const oldestRealTs = realFills[0]?.timestamp;
1243
+ return oldestRealTs !== void 0 ? Math.max(1, oldestRealTs - 1) : now;
1244
+ }
1245
+ function buildMetadata(symbols, realFills) {
1246
+ const initialExposureUSD = {};
1247
+ for (const symbol of symbols) {
1248
+ const legFills = realFills.flatMap(
1249
+ (fill) => fill.symbol === symbol && fill.usd != null ? [{ side: fill.side, usd: fill.usd }] : []
1250
+ );
1251
+ const opener = legFills[0];
1252
+ if (!opener) {
1253
+ continue;
1254
+ }
1255
+ const notional = legFills.filter((fill) => fill.side === opener.side).reduce((sum, fill) => add(sum, fill.usd), ZERO);
1256
+ const signedNotional = signed(opener.side === "BUY" ? notional : notional.negated());
1257
+ initialExposureUSD[symbol] = signedNotional;
1258
+ }
1259
+ return { initialExposureUSD };
1260
+ }
1261
+ function mergeOrigin(existing, fills) {
1262
+ let anyExternal = false;
1263
+ let anyPear = false;
1264
+ switch (existing) {
1265
+ case "EXTERNAL":
1266
+ anyExternal = true;
1267
+ break;
1268
+ case "PEAR":
1269
+ anyPear = true;
1270
+ break;
1271
+ case "PARTIAL":
1272
+ anyExternal = true;
1273
+ anyPear = true;
1274
+ break;
1275
+ }
1276
+ for (const fill of fills) {
1277
+ if (fill.isExternal) {
1278
+ anyExternal = true;
1279
+ } else {
1280
+ anyPear = true;
1281
+ }
1282
+ }
1283
+ if (anyExternal && anyPear) {
1284
+ return "PARTIAL";
1285
+ }
1286
+ return anyPear ? "PEAR" : "EXTERNAL";
1287
+ }
1288
+
1289
+ // src/sync/applicables/snapshot.ts
1290
+ var ExchangeSnapshot = class _ExchangeSnapshot {
1291
+ constructor(bySymbol) {
1292
+ this.bySymbol = bySymbol;
1293
+ }
1294
+ bySymbol;
1295
+ /** Index `positions` by symbol for O(1) lookup. */
1296
+ static from(positions) {
1297
+ return new _ExchangeSnapshot(new Map(positions.map((position) => [position.symbol, position])));
1298
+ }
1299
+ /** Raw exchange position for `symbol`, or `undefined` when the exchange holds nothing. */
1300
+ get(symbol) {
1301
+ return this.bySymbol.get(symbol);
1302
+ }
1303
+ /** Signed exchange exposure for `symbol` (0 when the exchange has no position). */
1304
+ signedSize(symbol) {
1305
+ const position = this.bySymbol.get(symbol);
1306
+ if (!position) {
1307
+ return ZERO;
1308
+ }
1309
+ const size = parse(position.size);
1310
+ switch (position.side) {
1311
+ case "BUY":
1312
+ return size;
1313
+ case "SELL":
1314
+ return size.negated();
1315
+ }
1316
+ }
1317
+ /** All symbols the exchange currently reports a position for. */
1318
+ symbols() {
1319
+ return [...this.bySymbol.keys()];
1320
+ }
1321
+ };
1322
+ var PearHoldings = class _PearHoldings {
1323
+ constructor(filtered) {
1324
+ this.filtered = filtered;
1325
+ }
1326
+ filtered;
1327
+ /** Filter `openPositions` to the given connector and wrap. */
1328
+ static from(args) {
1329
+ const { openPositions, connector } = args;
1330
+ return new _PearHoldings(openPositions.filter((position) => position.connector === connector));
1331
+ }
1332
+ /** The open Pear positions for this connector. */
1333
+ positions() {
1334
+ return this.filtered;
1335
+ }
1336
+ /** Sum existing open Pear exposure per symbol. */
1337
+ totalBySymbol() {
1338
+ const totals = /* @__PURE__ */ new Map();
1339
+ for (const position of this.filtered) {
1340
+ for (const [symbol, signedExposure] of Object.entries(position.exposure)) {
1341
+ if (signedExposure) {
1342
+ const newExposure = add(totals.get(symbol) ?? ZERO, signedExposure);
1343
+ totals.set(symbol, newExposure);
469
1344
  }
470
1345
  }
471
- };
1346
+ }
1347
+ return totals;
1348
+ }
1349
+ /**
1350
+ * Which open DB position holds each active asset — the fallback target for a
1351
+ * synthetic when no matched position already carries that asset. First open
1352
+ * position with a non-zero leg wins per symbol.
1353
+ */
1354
+ positionBySymbol() {
1355
+ const existingBySymbol = /* @__PURE__ */ new Map();
1356
+ for (const position of this.filtered) {
1357
+ for (const [symbol, signedQty] of Object.entries(position.exposure)) {
1358
+ const legIsZero = isZero(signedQty);
1359
+ const exists = existingBySymbol.has(symbol);
1360
+ if (!legIsZero && !exists) {
1361
+ existingBySymbol.set(symbol, position);
1362
+ }
1363
+ }
1364
+ }
1365
+ return existingBySymbol;
1366
+ }
1367
+ };
1368
+
1369
+ // src/sync/applicables/index.ts
1370
+ function buildApplicables(args) {
1371
+ const { tradeAccountId, connector, orphanFills, openPositions, entryBySymbol, exchangePositions } = args;
1372
+ const snapshot = ExchangeSnapshot.from(exchangePositions);
1373
+ const pear = PearHoldings.from({ openPositions, connector });
1374
+ const now = Date.now();
1375
+ const fills = addFillsForMissedHistory({ orphanFills, pear, connector, entryBySymbol, snapshot });
1376
+ const matched = matchPositions({ tradeAccountId, orphanFills: fills, pear });
1377
+ const { positions, synthByPosition } = alignPositionsWithExchange({
1378
+ positions: matched,
1379
+ pear,
1380
+ snapshot,
1381
+ tradeAccountId
472
1382
  });
1383
+ return assembleApplicables({ positions, connector, synthByPosition, now });
473
1384
  }
474
- function _walkBackwardsToFindLastZeroCrossing(group) {
475
- let netDelta = new BigNumber(0);
476
- for (const fill of group.fills) {
477
- if (fill.symbol !== group.symbol) {
478
- throw new Error("Corrupted state: fill symbol mismatch in grouped creatables");
479
- }
480
- const quantity = new BigNumber(fill.quantity);
481
- const sign2 = fill.side === "BUY" ? 1 : -1;
482
- const signedQuantity = quantity.multipliedBy(sign2);
483
- netDelta = netDelta.plus(signedQuantity);
484
- }
485
- if (netDelta.isZero()) {
486
- return group.fills;
487
- }
488
- let walkDelta = netDelta;
489
- const indexedNewFills = group.fills.map((fill, index) => ({ fill, index }));
490
- let cutoff;
491
- for (let i = indexedNewFills.length - 1; i >= 0; i--) {
492
- const indexedFill = indexedNewFills[i];
493
- if (!indexedFill) continue;
494
- const { fill, index } = indexedFill;
495
- const previousExposure = group.exposure;
496
- if (previousExposure.plus(walkDelta).isZero() && cutoff === void 0) {
497
- cutoff = index;
498
- }
499
- const sign2 = fill.side === "BUY" ? 1 : -1;
500
- const quantity = new BigNumber(fill.quantity).multipliedBy(sign2);
501
- walkDelta = walkDelta.minus(quantity);
502
- }
503
- const allowed = new Set(
504
- indexedNewFills.filter(({ index }) => cutoff === void 0 || index <= cutoff).map(({ fill }) => fill)
505
- );
506
- if (allowed.size !== group.fills.length) {
507
- return Array.from(allowed.values());
1385
+
1386
+ // src/sync/cutoff.ts
1387
+ function selectFirstSyncCutoff(args) {
1388
+ const { fills, exchangePositions, connector } = args;
1389
+ if (fills.length === 0) {
1390
+ return null;
1391
+ }
1392
+ const snapshot = ExchangeSnapshot.from(exchangePositions);
1393
+ const book = Exposure.empty();
1394
+ for (const symbol of snapshot.symbols()) {
1395
+ book.addDelta(symbol, snapshot.signedSize(symbol));
508
1396
  }
509
- if (group.exposure.isNegative() && netDelta.isPositive()) {
510
- const newExposure = netDelta.plus(group.exposure);
511
- if (newExposure.isGreaterThan(0)) return [];
1397
+ const fillSymbols = new Set(fills.map((fill) => fill.symbol));
1398
+ for (const symbol of snapshot.symbols()) {
1399
+ if (!snapshot.signedSize(symbol).isZero() && !fillSymbols.has(symbol)) {
1400
+ return null;
1401
+ }
512
1402
  }
513
- if (group.exposure.isPositive() && netDelta.isNegative()) {
514
- const newExposure = netDelta.plus(group.exposure);
515
- if (newExposure.isLessThan(0)) return [];
1403
+ const pearIndex = connector === "hyperliquid" ? findFirstPearFillIndex(connector, fills) : null;
1404
+ for (let boundary = fills.length; boundary >= 0; boundary--) {
1405
+ const withinAnchor = pearIndex === null || boundary <= pearIndex;
1406
+ if (withinAnchor && book.isAllZero() && isStrictTimestampBoundary(fills, boundary)) {
1407
+ return boundary === 0 ? null : { keepFromIndex: boundary };
1408
+ }
1409
+ const preceding = fills[boundary - 1];
1410
+ if (preceding) {
1411
+ book.addDelta(preceding.symbol, signedBySide(preceding.quantity, preceding.side).negated());
1412
+ }
1413
+ }
1414
+ return null;
1415
+ }
1416
+ function findFirstPearFillIndex(connector, fills) {
1417
+ const index = fills.findIndex((fill) => isPearCloid(connector, fill.cloid));
1418
+ if (index === -1) {
1419
+ return null;
1420
+ }
1421
+ return index;
1422
+ }
1423
+ function isStrictTimestampBoundary(fills, boundary) {
1424
+ const before = fills[boundary - 1];
1425
+ const after = fills[boundary];
1426
+ if (!before || !after) {
1427
+ return true;
1428
+ }
1429
+ return before.timestamp < after.timestamp;
1430
+ }
1431
+
1432
+ // src/sync/compute.ts
1433
+ async function computeSyncPayload(args) {
1434
+ const {
1435
+ tradeAccountId,
1436
+ connector,
1437
+ openPositions,
1438
+ entryBySymbol,
1439
+ provisionalFills,
1440
+ confirmedExchangeFillIds,
1441
+ externalFills,
1442
+ exchangePositions,
1443
+ neverSynced
1444
+ } = args;
1445
+ const fillsToProcess = _prepareExternalFills(externalFills, confirmedExchangeFillIds);
1446
+ const {
1447
+ reconcilables,
1448
+ confirmables: confirmableFills,
1449
+ orphans
1450
+ } = _segregateExternalFills(provisionalFills, fillsToProcess);
1451
+ const confirmablesByPosition = groupConfirmablesByPosition(confirmableFills);
1452
+ const { keptOrphans, firstSyncTrim } = applyFirstSyncCutoff({
1453
+ neverSynced,
1454
+ fillsToProcess,
1455
+ orphans,
1456
+ exchangePositions,
1457
+ connector
1458
+ });
1459
+ const applicables = buildApplicables({
1460
+ tradeAccountId,
1461
+ connector,
1462
+ orphanFills: keptOrphans,
1463
+ openPositions,
1464
+ entryBySymbol,
1465
+ exchangePositions
1466
+ });
1467
+ const mustPostFirstSyncCheckpoint = neverSynced && fillsToProcess.length > 0;
1468
+ if (!mustPostFirstSyncCheckpoint && isEmptyCycle(reconcilables, confirmablesByPosition, applicables)) {
1469
+ return null;
1470
+ }
1471
+ const externalMaxTs = latestFillTimestamp(fillsToProcess);
1472
+ return {
1473
+ reconcilables: Object.keys(reconcilables).length > 0 ? reconcilables : null,
1474
+ confirmables: Object.keys(confirmablesByPosition).length > 0 ? confirmablesByPosition : null,
1475
+ applicables,
1476
+ exchangePositions,
1477
+ meta: {
1478
+ externalMaxTs
1479
+ },
1480
+ firstSyncTrim
1481
+ };
1482
+ }
1483
+ function _prepareExternalFills(externalFills, confirmedExchangeFillIdsInput) {
1484
+ const dedupedByExchangeFillId = /* @__PURE__ */ new Map();
1485
+ const confirmedExchangeFillIds = new Set(
1486
+ Array.from(confirmedExchangeFillIdsInput).map((exchangeFillId) => _normalizeExchangeFillId(exchangeFillId)).filter((exchangeFillId) => exchangeFillId !== void 0)
1487
+ );
1488
+ const candidates = externalFills.reduce((acc, fill) => {
1489
+ const normalizedExchangeFillId = _normalizeExchangeFillId(fill.exchangeFillId);
1490
+ if (!normalizedExchangeFillId) {
1491
+ return acc;
1492
+ }
1493
+ if (!_isValidExternalQuantity(fill.quantity)) {
1494
+ return acc;
1495
+ }
1496
+ if (!_isValidExternalTimestamp(fill.timestamp)) {
1497
+ return acc;
1498
+ }
1499
+ if (confirmedExchangeFillIds.has(normalizedExchangeFillId)) {
1500
+ return acc;
1501
+ }
1502
+ acc.push({ ...fill, exchangeFillId: normalizedExchangeFillId });
1503
+ return acc;
1504
+ }, []);
1505
+ for (const fill of candidates) {
1506
+ const existing = dedupedByExchangeFillId.get(fill.exchangeFillId);
1507
+ if (!existing || fill.timestamp < existing.timestamp) {
1508
+ dedupedByExchangeFillId.set(fill.exchangeFillId, fill);
1509
+ }
516
1510
  }
517
- return Array.from(allowed.values());
1511
+ return Array.from(dedupedByExchangeFillId.values()).sort((a, b) => a.timestamp - b.timestamp);
518
1512
  }
519
- function _seggregateExternalFills(provisionalFills, externalFills) {
1513
+ function _segregateExternalFills(provisionalFills, externalFills) {
520
1514
  const provisionalFillsMap = /* @__PURE__ */ new Map();
521
1515
  for (const fill of provisionalFills) {
522
1516
  const cloid = _normalizeCloid(fill.cloid);
523
- if (!cloid) continue;
1517
+ if (!cloid) {
1518
+ continue;
1519
+ }
524
1520
  provisionalFillsMap.set(cloid, fill);
525
1521
  }
526
- const orphanCreatables = [];
527
- const cloidBuffer = /* @__PURE__ */ new Map();
1522
+ const orphans = [];
1523
+ const fillsByCloid = /* @__PURE__ */ new Map();
528
1524
  for (const fill of externalFills) {
529
1525
  const cloid = _normalizeCloid(fill.cloid);
530
1526
  if (!cloid) {
531
- orphanCreatables.push(fill);
1527
+ orphans.push(fill);
532
1528
  } else {
533
- cloidBuffer.set(cloid, [...cloidBuffer.get(cloid) || [], fill]);
1529
+ fillsByCloid.set(cloid, [...fillsByCloid.get(cloid) || [], fill]);
534
1530
  }
535
1531
  }
536
1532
  const reconcilables = {};
537
- const confirmables = [];
538
- for (const [cloid, fills] of cloidBuffer.entries()) {
1533
+ const confirmableFills = [];
1534
+ for (const [cloid, fills] of fillsByCloid.entries()) {
539
1535
  if (fills.length > 1) {
540
1536
  const provisional = provisionalFillsMap.get(cloid);
541
1537
  if (provisional) {
@@ -549,65 +1545,136 @@ function _seggregateExternalFills(provisionalFills, externalFills) {
549
1545
  ...rest.map((fill) => ({ ...fill, cloid }))
550
1546
  ];
551
1547
  } else {
552
- orphanCreatables.push(...fills);
1548
+ orphans.push(...fills);
553
1549
  }
554
1550
  } else {
555
1551
  const fill = fills[0];
556
1552
  const provisional = provisionalFillsMap.get(cloid);
557
1553
  if (provisional) {
558
- confirmables.push({
1554
+ confirmableFills.push({
559
1555
  ...fill,
560
1556
  type: "confirm",
561
1557
  cloid,
562
1558
  positionId: provisional.positionId
563
1559
  });
564
1560
  } else {
565
- orphanCreatables.push(fill);
1561
+ orphans.push(fill);
566
1562
  }
567
1563
  }
568
1564
  }
569
- return { reconcilables, confirmables, orphanCreatables };
1565
+ return { reconcilables, confirmables: confirmableFills, orphans };
570
1566
  }
571
1567
  function _normalizeCloid(cloid) {
572
1568
  const value = cloid?.trim();
573
- return value ? value : void 0;
1569
+ if (!value) {
1570
+ return void 0;
1571
+ }
1572
+ return value;
574
1573
  }
575
1574
  function _normalizeExchangeFillId(exchangeFillId) {
576
1575
  const value = exchangeFillId?.trim().toLowerCase();
577
- return value ? value : void 0;
1576
+ if (!value) {
1577
+ return void 0;
1578
+ }
1579
+ return value;
578
1580
  }
579
1581
  function _isValidExternalQuantity(quantity) {
580
- if (/e/i.test(quantity)) return false;
581
- const parsed = new BigNumber(quantity);
582
- if (parsed.isNaN() || !parsed.isFinite()) return false;
583
- return parsed.isGreaterThan(0);
1582
+ if (/e/i.test(quantity)) {
1583
+ return false;
1584
+ }
1585
+ if (!isFiniteDecimal(quantity)) {
1586
+ return false;
1587
+ }
1588
+ return isGreaterThan(quantity, ZERO);
584
1589
  }
585
1590
  function _isValidExternalTimestamp(timestamp) {
586
1591
  return Number.isFinite(timestamp) && timestamp >= 0;
587
1592
  }
1593
+ function groupConfirmablesByPosition(confirmableFills) {
1594
+ return confirmableFills.reduce((acc, fill) => {
1595
+ acc[fill.positionId] = [...acc[fill.positionId] || [], fill];
1596
+ return acc;
1597
+ }, {});
1598
+ }
1599
+ function latestFillTimestamp(fills) {
1600
+ const last = fills[fills.length - 1];
1601
+ if (!last) {
1602
+ return null;
1603
+ }
1604
+ return last.timestamp;
1605
+ }
1606
+ function applyFirstSyncCutoff(args) {
1607
+ const { neverSynced, fillsToProcess, orphans, exchangePositions, connector } = args;
1608
+ if (!neverSynced || fillsToProcess.length === 0) {
1609
+ return { keptOrphans: orphans, firstSyncTrim: null };
1610
+ }
1611
+ const cutoff = selectFirstSyncCutoff({
1612
+ fills: fillsToProcess,
1613
+ exchangePositions,
1614
+ connector
1615
+ });
1616
+ if (!cutoff) {
1617
+ return { keptOrphans: orphans, firstSyncTrim: null };
1618
+ }
1619
+ const keptIds = new Set(fillsToProcess.slice(cutoff.keepFromIndex).map((fill) => fill.exchangeFillId));
1620
+ const keptOrphans = [];
1621
+ const droppedOrphans = [];
1622
+ for (const orphan of orphans) {
1623
+ (keptIds.has(orphan.exchangeFillId) ? keptOrphans : droppedOrphans).push(orphan);
1624
+ }
1625
+ if (droppedOrphans.length === 0) {
1626
+ return { keptOrphans, firstSyncTrim: null };
1627
+ }
1628
+ const firstKeptFill = fillsToProcess[cutoff.keepFromIndex];
1629
+ return {
1630
+ keptOrphans,
1631
+ firstSyncTrim: {
1632
+ cutoffTs: firstKeptFill ? firstKeptFill.timestamp : null,
1633
+ trimmedCount: droppedOrphans.length,
1634
+ trimmedMaxTs: droppedOrphans.reduce((max, fill) => fill.timestamp > max ? fill.timestamp : max, 0)
1635
+ }
1636
+ };
1637
+ }
1638
+ function isEmptyCycle(reconcilables, confirmablesByPosition, applicables) {
1639
+ return Object.keys(reconcilables).length === 0 && Object.keys(confirmablesByPosition).length === 0 && applicables.length === 0;
1640
+ }
588
1641
 
589
1642
  // src/validate-leverage.ts
590
1643
  function validateLeverage(leverage, instrument) {
591
- if (leverage < 1 || leverage > 100) return false;
592
- if (leverage > instrument.leverage) return false;
1644
+ if (leverage < 1 || leverage > 100) {
1645
+ return false;
1646
+ }
1647
+ if (leverage > instrument.leverage) {
1648
+ return false;
1649
+ }
593
1650
  return true;
594
1651
  }
595
1652
  function validateQuantity(quantity, price, precision) {
596
- const q = new BigNumber7(quantity);
597
- if (!q.isFinite() || q.lte(0)) return false;
598
- const p = new BigNumber7(price);
599
- if (!p.isFinite() || p.lte(0)) return false;
600
- const step = new BigNumber7(precision.quantity.step);
601
- const scaled = q.div(step);
602
- if (!scaled.isInteger()) return false;
603
- const notional = contractsToBase(q, precision.quantity.contractSize).times(p);
1653
+ const parsedQuantity = new BigNumber9(quantity);
1654
+ if (!parsedQuantity.isFinite() || parsedQuantity.lte(0)) {
1655
+ return false;
1656
+ }
1657
+ const parsedPrice = new BigNumber9(price);
1658
+ if (!parsedPrice.isFinite() || parsedPrice.lte(0)) {
1659
+ return false;
1660
+ }
1661
+ const step = new BigNumber9(precision.quantity.step);
1662
+ const scaled = parsedQuantity.div(step);
1663
+ if (!scaled.isInteger()) {
1664
+ return false;
1665
+ }
1666
+ const notional = contractsToBase(parsedQuantity, precision.quantity.contractSize).times(parsedPrice);
604
1667
  if (precision.notional?.min !== void 0 && precision.notional.min !== null && precision.notional.min > 0) {
605
- if (notional.lt(new BigNumber7(precision.notional.min))) return false;
1668
+ if (notional.lt(new BigNumber9(precision.notional.min))) {
1669
+ return false;
1670
+ }
606
1671
  }
607
1672
  if (precision.notional?.max !== void 0 && precision.notional.max !== null && precision.notional.max > 0) {
608
- if (notional.gt(new BigNumber7(precision.notional.max))) return false;
1673
+ if (notional.gt(new BigNumber9(precision.notional.max))) {
1674
+ return false;
1675
+ }
609
1676
  }
610
1677
  return true;
611
1678
  }
612
1679
 
613
- export { BIPS, ZERO, baseToContracts, compareValue, computeAssetEntryNotional, computeAssetGrossRealizedPnL, computeAssetRealizedPnLs, computeAssetUnrealizedPnL, computeAssetUnrealizedPnLs, computeBasketWeightedRatioV1, computeEntryPriceForAsset, computeRealizedPositionState, computeSyncPayload, computeUnrealizedPositionState, contractsToBase, countDecimals, exponentiate, precisePrice, preciseQuantity, sign, validateLeverage, validateQuantity };
1680
+ export { BIPS, ZERO, abs, add, baseToContracts, compareValue, computeAssetEntryNotional, computeAssetGrossRealizedPnL, computeAssetRealizedPnLs, computeAssetUndeterminedSizes, computeAssetUnrealizedPnL, computeAssetUnrealizedPnLs, computeBasketWeightedRatioV1, computeEntryPriceForAsset, computeRealizedPnlBySymbol, computeRealizedPnlFromFills, computeRealizedPositionState, computeSyncPayload, computeUnrealizedPositionState, contractsToBase, countDecimals, divide, exponentiate, generateDeterministicPositionKey, isEqualTo, isFiniteDecimal, isGreaterThan, isGreaterThanOrEqual, isLessThan, isLessThanOrEqual, isNegative, isPositive, isZero, min, multiply, negate, parse, precisePrice, preciseQuantity, sideSign, sign, signed, signedBySide, subtract, toDecimalString, validateLeverage, validateQuantity };