@pear-protocol/utils 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/fills/compute.d.ts +4 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +476 -188
- package/dist/sync/applicables/basket.d.ts +1 -0
- package/dist/sync/applicables/constants.d.ts +7 -0
- package/dist/sync/applicables/matching.d.ts +5 -0
- package/dist/sync/applicables/types.d.ts +5 -0
- package/dist/vaults/action-typed-data.d.ts +181 -0
- package/dist/vaults/index.d.ts +1 -0
- package/package.json +2 -2
package/dist/fills/compute.d.ts
CHANGED
|
@@ -8,5 +8,9 @@ import type { Fill } from './fill.types';
|
|
|
8
8
|
* - Reduce position : entry price unchanged
|
|
9
9
|
* - Close to zero : keep last entry price (for tracking purpose)
|
|
10
10
|
* - Flip sign : entry price resets to flip fill's price
|
|
11
|
+
*
|
|
12
|
+
* A 0-price (unpriceable synthetic) fill that opens or adds to the held size taints the
|
|
13
|
+
* basis until the size flattens or flips: the entry is then unknown and reported as zero,
|
|
14
|
+
* the same "no entry" answer callers already get for an empty ledger.
|
|
11
15
|
*/
|
|
12
16
|
export declare function computeEntryPriceForAsset(fills: Fill[]): BigNumber;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import BigNumber7 from 'bignumber.js';
|
|
2
2
|
import { sha256 } from '@noble/hashes/sha2';
|
|
3
|
-
import { bytesToHex,
|
|
3
|
+
import { bytesToHex, utf8ToBytes, randomBytes } from '@noble/hashes/utils';
|
|
4
4
|
import { WATERMARK_FUTURE_SKEW_MS } from '@pear-protocol/types';
|
|
5
|
+
import { keccak_256 } from '@noble/hashes/sha3';
|
|
5
6
|
import { base64urlnopad } from '@scure/base';
|
|
6
7
|
|
|
7
8
|
var __defProp = Object.defineProperty;
|
|
@@ -124,6 +125,7 @@ __name(compareValue, "compareValue");
|
|
|
124
125
|
function computeEntryPriceForAsset(fills) {
|
|
125
126
|
let entryPrice = ZERO;
|
|
126
127
|
let positionSize = ZERO;
|
|
128
|
+
let unpriced = false;
|
|
127
129
|
for (const fill of fills) {
|
|
128
130
|
const price = new BigNumber7(fill.price);
|
|
129
131
|
const quantity = new BigNumber7(fill.quantity);
|
|
@@ -133,11 +135,16 @@ function computeEntryPriceForAsset(fills) {
|
|
|
133
135
|
const isFlip = !positionSize.isZero() && !newPositionSize.isZero() && newPositionSize.isPositive() !== positionSize.isPositive();
|
|
134
136
|
if (positionSize.isZero() || isFlip) {
|
|
135
137
|
entryPrice = price;
|
|
138
|
+
unpriced = price.isZero();
|
|
136
139
|
} else if (isSameDirection) {
|
|
137
140
|
entryPrice = entryPrice.times(positionSize.abs()).plus(price.times(quantity)).div(newPositionSize.abs());
|
|
141
|
+
unpriced = unpriced || price.isZero();
|
|
138
142
|
}
|
|
139
143
|
positionSize = newPositionSize;
|
|
140
144
|
}
|
|
145
|
+
if (unpriced) {
|
|
146
|
+
return ZERO;
|
|
147
|
+
}
|
|
141
148
|
return entryPrice;
|
|
142
149
|
}
|
|
143
150
|
__name(computeEntryPriceForAsset, "computeEntryPriceForAsset");
|
|
@@ -381,6 +388,9 @@ function computeAssetUnrealizedPnLs(exposureMap, priceMap, fills) {
|
|
|
381
388
|
const currentPrice = new BigNumber7(priceString);
|
|
382
389
|
const assetFills = fills.filter((fill) => fill.symbol === id);
|
|
383
390
|
const entryPrice = computeEntryPriceForAsset(assetFills);
|
|
391
|
+
if (entryPrice.isZero()) {
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
384
394
|
const upnl = signedQuantity.multipliedBy(currentPrice.minus(entryPrice));
|
|
385
395
|
result.push({
|
|
386
396
|
id,
|
|
@@ -1268,6 +1278,7 @@ __name(proxyFetch, "proxyFetch");
|
|
|
1268
1278
|
|
|
1269
1279
|
// src/sync/applicables/constants.ts
|
|
1270
1280
|
var DUST = parse("0.00000001");
|
|
1281
|
+
var LATE_FILL_GRACE_MS = 5 * 6e4;
|
|
1271
1282
|
var MISSED_HISTORY_FILL_PREFIX = "opening-synthetic-";
|
|
1272
1283
|
var BASKET_RULES = [
|
|
1273
1284
|
// V2 Basket Hyperliquid — legs of one Pear basket order share the order's cloid.
|
|
@@ -1303,12 +1314,14 @@ var Basket = class _Basket {
|
|
|
1303
1314
|
__name(this, "Basket");
|
|
1304
1315
|
}
|
|
1305
1316
|
legs;
|
|
1306
|
-
|
|
1317
|
+
fills;
|
|
1318
|
+
constructor(legs, fills) {
|
|
1307
1319
|
this.legs = legs;
|
|
1320
|
+
this.fills = fills;
|
|
1308
1321
|
}
|
|
1309
1322
|
/** Detect baskets from a fill stream, returned in ascending time order. */
|
|
1310
1323
|
static detect(fills) {
|
|
1311
|
-
return groupFillsIntoBaskets(fills).map((group) => new _Basket(combineFillsBySymbolAndSide(group)));
|
|
1324
|
+
return groupFillsIntoBaskets(fills).map((group) => new _Basket(combineFillsBySymbolAndSide(group), group));
|
|
1312
1325
|
}
|
|
1313
1326
|
/** Active long symbol set the matcher compares against open positions. */
|
|
1314
1327
|
get longs() {
|
|
@@ -1515,189 +1528,6 @@ var Exposure = class _Exposure {
|
|
|
1515
1528
|
}
|
|
1516
1529
|
};
|
|
1517
1530
|
|
|
1518
|
-
// src/sync/applicables/matching.ts
|
|
1519
|
-
function matchPositions(args) {
|
|
1520
|
-
const { tradeAccountId, orphanFills, pear } = args;
|
|
1521
|
-
const book = WorkingBook.load({
|
|
1522
|
-
tradeAccountId,
|
|
1523
|
-
openPositions: pear.positions()
|
|
1524
|
-
});
|
|
1525
|
-
for (const basket of Basket.detect(orphanFills)) {
|
|
1526
|
-
const exactCloseTarget = findExactCloseTarget(book.open(), basket.longs, basket.shorts);
|
|
1527
|
-
const leftover = applyCloses({
|
|
1528
|
-
legs: basket.legs,
|
|
1529
|
-
book,
|
|
1530
|
-
exactCloseTarget
|
|
1531
|
-
});
|
|
1532
|
-
if (leftover.length === 0) {
|
|
1533
|
-
continue;
|
|
1534
|
-
}
|
|
1535
|
-
const target = resolveLeftoverTarget({
|
|
1536
|
-
book,
|
|
1537
|
-
leftover
|
|
1538
|
-
});
|
|
1539
|
-
applyLeftover(target, leftover);
|
|
1540
|
-
}
|
|
1541
|
-
return book.toMatched();
|
|
1542
|
-
}
|
|
1543
|
-
__name(matchPositions, "matchPositions");
|
|
1544
|
-
var WorkingBook = class WorkingBook2 {
|
|
1545
|
-
static {
|
|
1546
|
-
__name(this, "WorkingBook");
|
|
1547
|
-
}
|
|
1548
|
-
tradeAccountId;
|
|
1549
|
-
all = [];
|
|
1550
|
-
openByKey = /* @__PURE__ */ new Map();
|
|
1551
|
-
constructor(tradeAccountId) {
|
|
1552
|
-
this.tradeAccountId = tradeAccountId;
|
|
1553
|
-
}
|
|
1554
|
-
/** Seed the book from existing open DB positions (they arrive pre-filtered by connector). */
|
|
1555
|
-
static load(args) {
|
|
1556
|
-
const { tradeAccountId, openPositions } = args;
|
|
1557
|
-
const book = new WorkingBook2(tradeAccountId);
|
|
1558
|
-
for (const p of openPositions) {
|
|
1559
|
-
const position = {
|
|
1560
|
-
// Trust the position's own stored key — never recompute it from exposure.
|
|
1561
|
-
// A partial fill on the original order leaves exposure short of the
|
|
1562
|
-
// symbols the key was hashed from, so a recomputed key would never match.
|
|
1563
|
-
positionKey: p.positionKey,
|
|
1564
|
-
exposure: Exposure.fromRecord(p.exposure),
|
|
1565
|
-
parts: [],
|
|
1566
|
-
closed: false,
|
|
1567
|
-
existingOrigin: p.origin
|
|
1568
|
-
};
|
|
1569
|
-
book.all.push(position);
|
|
1570
|
-
book.openByKey.set(position.positionKey, position);
|
|
1571
|
-
}
|
|
1572
|
-
return book;
|
|
1573
|
-
}
|
|
1574
|
-
/** Deterministic position key for a symbol set under this book's trade account. */
|
|
1575
|
-
keyFor(symbols) {
|
|
1576
|
-
return generateDeterministicPositionKey({
|
|
1577
|
-
tradeAccountId: this.tradeAccountId,
|
|
1578
|
-
symbols
|
|
1579
|
-
});
|
|
1580
|
-
}
|
|
1581
|
-
/** Currently open working positions, in insertion order. */
|
|
1582
|
-
open() {
|
|
1583
|
-
return this.all.filter((position) => !position.closed);
|
|
1584
|
-
}
|
|
1585
|
-
/** Look up an open working position by its positionKey (for dedup on merge). */
|
|
1586
|
-
findOpenByKey(key) {
|
|
1587
|
-
return this.openByKey.get(key);
|
|
1588
|
-
}
|
|
1589
|
-
/** Register a fresh empty working position under `key` and return it. */
|
|
1590
|
-
create(key) {
|
|
1591
|
-
const position = {
|
|
1592
|
-
positionKey: key,
|
|
1593
|
-
exposure: Exposure.empty(),
|
|
1594
|
-
parts: [],
|
|
1595
|
-
closed: false
|
|
1596
|
-
};
|
|
1597
|
-
this.all.push(position);
|
|
1598
|
-
this.openByKey.set(key, position);
|
|
1599
|
-
return position;
|
|
1600
|
-
}
|
|
1601
|
-
/** Flip a position to closed once every leg is within DUST of zero. */
|
|
1602
|
-
markClosedIfFlat(position) {
|
|
1603
|
-
if (position.closed || !position.exposure.isFlat()) {
|
|
1604
|
-
return;
|
|
1605
|
-
}
|
|
1606
|
-
position.exposure.snapToZero();
|
|
1607
|
-
position.closed = true;
|
|
1608
|
-
this.openByKey.delete(position.positionKey);
|
|
1609
|
-
}
|
|
1610
|
-
/** Working positions that received fills become MatchedPosition records; untouched ones are dropped. */
|
|
1611
|
-
toMatched() {
|
|
1612
|
-
return this.all.filter((position) => position.parts.length > 0).map((position) => ({
|
|
1613
|
-
positionKey: position.positionKey,
|
|
1614
|
-
parts: position.parts,
|
|
1615
|
-
closed: position.closed,
|
|
1616
|
-
exposure: position.exposure,
|
|
1617
|
-
existingOrigin: position.existingOrigin
|
|
1618
|
-
}));
|
|
1619
|
-
}
|
|
1620
|
-
};
|
|
1621
|
-
function findExactCloseTarget(openList, basketLongs, basketShorts) {
|
|
1622
|
-
return openList.find((position) => {
|
|
1623
|
-
const { longs, shorts } = position.exposure.activeLegs();
|
|
1624
|
-
return setEq(longs, basketShorts) && setEq(shorts, basketLongs);
|
|
1625
|
-
});
|
|
1626
|
-
}
|
|
1627
|
-
__name(findExactCloseTarget, "findExactCloseTarget");
|
|
1628
|
-
var setEq = /* @__PURE__ */ __name((a, b) => a.size === b.size && [
|
|
1629
|
-
...a
|
|
1630
|
-
].every((x) => b.has(x)), "setEq");
|
|
1631
|
-
function applyCloses(args) {
|
|
1632
|
-
const { legs, book, exactCloseTarget } = args;
|
|
1633
|
-
const openList = book.open();
|
|
1634
|
-
const leftover = [];
|
|
1635
|
-
for (const leg of legs) {
|
|
1636
|
-
let remaining = leg.totalSize;
|
|
1637
|
-
const closeTargetIsLong = sideSign(leg.side) === -1;
|
|
1638
|
-
const holders = openList.filter((position) => {
|
|
1639
|
-
const qty = position.exposure.get(leg.symbol);
|
|
1640
|
-
if (qty.isZero()) {
|
|
1641
|
-
return false;
|
|
1642
|
-
}
|
|
1643
|
-
return closeTargetIsLong ? qty.isPositive() : qty.isNegative();
|
|
1644
|
-
});
|
|
1645
|
-
if (exactCloseTarget) {
|
|
1646
|
-
holders.sort((a, b) => a === exactCloseTarget ? -1 : b === exactCloseTarget ? 1 : 0);
|
|
1647
|
-
}
|
|
1648
|
-
for (const position of holders) {
|
|
1649
|
-
if (isLessThanOrEqual(remaining, ZERO)) {
|
|
1650
|
-
break;
|
|
1651
|
-
}
|
|
1652
|
-
const have = position.exposure.get(leg.symbol).abs();
|
|
1653
|
-
const reduce = min(have, remaining);
|
|
1654
|
-
position.parts.push(...leg.take(reduce));
|
|
1655
|
-
position.exposure.addDelta(leg.symbol, signedBySide(reduce, leg.side));
|
|
1656
|
-
remaining = remaining.minus(reduce);
|
|
1657
|
-
book.markClosedIfFlat(position);
|
|
1658
|
-
}
|
|
1659
|
-
if (isGreaterThanOrEqual(remaining, DUST)) {
|
|
1660
|
-
leftover.push({
|
|
1661
|
-
side: leg.side,
|
|
1662
|
-
qty: remaining,
|
|
1663
|
-
leg
|
|
1664
|
-
});
|
|
1665
|
-
}
|
|
1666
|
-
}
|
|
1667
|
-
return leftover;
|
|
1668
|
-
}
|
|
1669
|
-
__name(applyCloses, "applyCloses");
|
|
1670
|
-
function resolveLeftoverTarget(args) {
|
|
1671
|
-
const { book, leftover } = args;
|
|
1672
|
-
const leftoverLongs = new Set(leftover.filter((entry) => entry.side === "BUY").map((entry) => entry.leg.symbol));
|
|
1673
|
-
const leftoverShorts = new Set(leftover.filter((entry) => entry.side === "SELL").map((entry) => entry.leg.symbol));
|
|
1674
|
-
const target = book.open().find((position) => {
|
|
1675
|
-
const { longs, shorts } = position.exposure.activeLegs();
|
|
1676
|
-
const sameActiveShape = setEq(leftoverLongs, longs) && setEq(leftoverShorts, shorts);
|
|
1677
|
-
const noDormantLegs = position.exposure.symbols().length === longs.size + shorts.size;
|
|
1678
|
-
return sameActiveShape && noDormantLegs;
|
|
1679
|
-
});
|
|
1680
|
-
if (target) {
|
|
1681
|
-
return target;
|
|
1682
|
-
}
|
|
1683
|
-
const symbols = [
|
|
1684
|
-
.../* @__PURE__ */ new Set([
|
|
1685
|
-
...leftoverLongs,
|
|
1686
|
-
...leftoverShorts
|
|
1687
|
-
])
|
|
1688
|
-
];
|
|
1689
|
-
const positionKey = book.keyFor(symbols);
|
|
1690
|
-
return book.findOpenByKey(positionKey) ?? book.create(positionKey);
|
|
1691
|
-
}
|
|
1692
|
-
__name(resolveLeftoverTarget, "resolveLeftoverTarget");
|
|
1693
|
-
function applyLeftover(target, leftover) {
|
|
1694
|
-
for (const entry of leftover) {
|
|
1695
|
-
target.parts.push(...entry.leg.take(entry.qty));
|
|
1696
|
-
target.exposure.addSigned(entry.leg.symbol, entry.qty.toFixed(), entry.side);
|
|
1697
|
-
}
|
|
1698
|
-
}
|
|
1699
|
-
__name(applyLeftover, "applyLeftover");
|
|
1700
|
-
|
|
1701
1531
|
// src/sync/applicables/synthetics.ts
|
|
1702
1532
|
function isMissedHistoryFill(fill) {
|
|
1703
1533
|
return fill.exchangeFillId.startsWith(MISSED_HISTORY_FILL_PREFIX);
|
|
@@ -1892,7 +1722,8 @@ function assignSyntheticsToPositions(args) {
|
|
|
1892
1722
|
const positionKey = existing ? existing.positionKey : keyOf([
|
|
1893
1723
|
symbol
|
|
1894
1724
|
]);
|
|
1895
|
-
|
|
1725
|
+
const isKeyTarget = /* @__PURE__ */ __name((pos) => pos.positionKey === positionKey && (!existing || pos.existingOrigin !== void 0), "isKeyTarget");
|
|
1726
|
+
target = positions.find(isKeyTarget) ?? created.find(isKeyTarget);
|
|
1896
1727
|
if (!target) {
|
|
1897
1728
|
target = existing ? (
|
|
1898
1729
|
// seed its exposure and origin so folding the synthetic yields the true
|
|
@@ -1969,6 +1800,223 @@ function findGapToTarget(args) {
|
|
|
1969
1800
|
}
|
|
1970
1801
|
__name(findGapToTarget, "findGapToTarget");
|
|
1971
1802
|
|
|
1803
|
+
// src/sync/applicables/matching.ts
|
|
1804
|
+
function matchPositions(args) {
|
|
1805
|
+
const { tradeAccountId, orphanFills, pear } = args;
|
|
1806
|
+
const book = WorkingBook.load({
|
|
1807
|
+
tradeAccountId,
|
|
1808
|
+
openPositions: pear.positions()
|
|
1809
|
+
});
|
|
1810
|
+
for (const basket of Basket.detect(orphanFills)) {
|
|
1811
|
+
const at = basketTime(basket);
|
|
1812
|
+
book.foldIntoLive(at);
|
|
1813
|
+
const exactCloseTarget = findExactCloseTarget(book.live(at), basket.longs, basket.shorts);
|
|
1814
|
+
const leftover = applyCloses({
|
|
1815
|
+
legs: basket.legs,
|
|
1816
|
+
book,
|
|
1817
|
+
exactCloseTarget,
|
|
1818
|
+
at
|
|
1819
|
+
});
|
|
1820
|
+
if (leftover.length === 0) {
|
|
1821
|
+
continue;
|
|
1822
|
+
}
|
|
1823
|
+
const target = resolveLeftoverTarget({
|
|
1824
|
+
book,
|
|
1825
|
+
leftover,
|
|
1826
|
+
at
|
|
1827
|
+
});
|
|
1828
|
+
applyLeftover(target, leftover);
|
|
1829
|
+
}
|
|
1830
|
+
book.foldIntoLive(Number.POSITIVE_INFINITY);
|
|
1831
|
+
return book.toMatched();
|
|
1832
|
+
}
|
|
1833
|
+
__name(matchPositions, "matchPositions");
|
|
1834
|
+
function basketTime(basket) {
|
|
1835
|
+
const realTimestamps = basket.fills.filter((fill) => !isMissedHistoryFill(fill)).map((fill) => fill.timestamp);
|
|
1836
|
+
if (realTimestamps.length === 0) {
|
|
1837
|
+
return Number.POSITIVE_INFINITY;
|
|
1838
|
+
}
|
|
1839
|
+
return Math.min(...realTimestamps);
|
|
1840
|
+
}
|
|
1841
|
+
__name(basketTime, "basketTime");
|
|
1842
|
+
var WorkingBook = class WorkingBook2 {
|
|
1843
|
+
static {
|
|
1844
|
+
__name(this, "WorkingBook");
|
|
1845
|
+
}
|
|
1846
|
+
tradeAccountId;
|
|
1847
|
+
all = [];
|
|
1848
|
+
constructor(tradeAccountId) {
|
|
1849
|
+
this.tradeAccountId = tradeAccountId;
|
|
1850
|
+
}
|
|
1851
|
+
/** Seed the book from existing open DB positions (they arrive pre-filtered by connector). */
|
|
1852
|
+
static load(args) {
|
|
1853
|
+
const { tradeAccountId, openPositions } = args;
|
|
1854
|
+
const book = new WorkingBook2(tradeAccountId);
|
|
1855
|
+
for (const p of openPositions) {
|
|
1856
|
+
const position = {
|
|
1857
|
+
// Trust the position's own stored key — never recompute it from exposure.
|
|
1858
|
+
// A partial fill on the original order leaves exposure short of the
|
|
1859
|
+
// symbols the key was hashed from, so a recomputed key would never match.
|
|
1860
|
+
positionKey: p.positionKey,
|
|
1861
|
+
exposure: Exposure.fromRecord(p.exposure),
|
|
1862
|
+
parts: [],
|
|
1863
|
+
closed: false,
|
|
1864
|
+
existingOrigin: p.origin,
|
|
1865
|
+
// A seeded position takes fills from its creation, less the late-arrival grace.
|
|
1866
|
+
liveFrom: Date.parse(p.createdAt) - LATE_FILL_GRACE_MS
|
|
1867
|
+
};
|
|
1868
|
+
book.all.push(position);
|
|
1869
|
+
}
|
|
1870
|
+
return book;
|
|
1871
|
+
}
|
|
1872
|
+
/** Deterministic position key for a symbol set under this book's trade account. */
|
|
1873
|
+
keyFor(symbols) {
|
|
1874
|
+
return generateDeterministicPositionKey({
|
|
1875
|
+
tradeAccountId: this.tradeAccountId,
|
|
1876
|
+
symbols
|
|
1877
|
+
});
|
|
1878
|
+
}
|
|
1879
|
+
/** Working positions open and already existing at basket time `at`, in insertion order. */
|
|
1880
|
+
live(at) {
|
|
1881
|
+
return this.all.filter((position) => !position.closed && position.liveFrom <= at);
|
|
1882
|
+
}
|
|
1883
|
+
/** Look up a position open and existing at `at` by its positionKey (for dedup on merge). */
|
|
1884
|
+
findLiveByKey(key, at) {
|
|
1885
|
+
return this.live(at).find((position) => position.positionKey === key);
|
|
1886
|
+
}
|
|
1887
|
+
/**
|
|
1888
|
+
* Fold every open position opened mid-replay into the open seeded position sharing its
|
|
1889
|
+
* key once that seeded position is live at `at`. The earlier open was still holding
|
|
1890
|
+
* exposure when the seeded position was created, and the server keeps at most one open
|
|
1891
|
+
* row per key. Ownership is ambiguous here — exposure and timestamps cannot prove the
|
|
1892
|
+
* seeded position traded on top of that history — so the fold conservatively keeps
|
|
1893
|
+
* the attribution the matcher gave before #1067.
|
|
1894
|
+
*/
|
|
1895
|
+
foldIntoLive(at) {
|
|
1896
|
+
for (const seeded of this.live(at).filter((position) => position.existingOrigin !== void 0)) {
|
|
1897
|
+
const earlier = this.all.filter((position) => position.existingOrigin === void 0 && !position.closed && position.positionKey === seeded.positionKey);
|
|
1898
|
+
for (const position of earlier) {
|
|
1899
|
+
seeded.parts.push(...position.parts);
|
|
1900
|
+
for (const symbol of position.exposure.symbols()) {
|
|
1901
|
+
seeded.exposure.addDelta(symbol, position.exposure.get(symbol));
|
|
1902
|
+
}
|
|
1903
|
+
this.all = this.all.filter((candidate) => candidate !== position);
|
|
1904
|
+
}
|
|
1905
|
+
this.markClosedIfFlat(seeded);
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
/** Register a fresh empty working position under `key` and return it. */
|
|
1909
|
+
create(key) {
|
|
1910
|
+
const position = {
|
|
1911
|
+
positionKey: key,
|
|
1912
|
+
exposure: Exposure.empty(),
|
|
1913
|
+
parts: [],
|
|
1914
|
+
closed: false,
|
|
1915
|
+
// Opened by this replay's own fills, so it takes fills from the start.
|
|
1916
|
+
liveFrom: Number.NEGATIVE_INFINITY
|
|
1917
|
+
};
|
|
1918
|
+
this.all.push(position);
|
|
1919
|
+
return position;
|
|
1920
|
+
}
|
|
1921
|
+
/** Flip a position to closed once every leg is within DUST of zero. */
|
|
1922
|
+
markClosedIfFlat(position) {
|
|
1923
|
+
if (position.closed || !position.exposure.isFlat()) {
|
|
1924
|
+
return;
|
|
1925
|
+
}
|
|
1926
|
+
position.exposure.snapToZero();
|
|
1927
|
+
position.closed = true;
|
|
1928
|
+
}
|
|
1929
|
+
/** Working positions that received fills become MatchedPosition records; untouched ones are dropped. */
|
|
1930
|
+
toMatched() {
|
|
1931
|
+
return this.all.filter((position) => position.parts.length > 0).map((position) => ({
|
|
1932
|
+
positionKey: position.positionKey,
|
|
1933
|
+
parts: position.parts,
|
|
1934
|
+
closed: position.closed,
|
|
1935
|
+
exposure: position.exposure,
|
|
1936
|
+
existingOrigin: position.existingOrigin
|
|
1937
|
+
}));
|
|
1938
|
+
}
|
|
1939
|
+
};
|
|
1940
|
+
function findExactCloseTarget(openList, basketLongs, basketShorts) {
|
|
1941
|
+
return openList.find((position) => {
|
|
1942
|
+
const { longs, shorts } = position.exposure.activeLegs();
|
|
1943
|
+
return setEq(longs, basketShorts) && setEq(shorts, basketLongs);
|
|
1944
|
+
});
|
|
1945
|
+
}
|
|
1946
|
+
__name(findExactCloseTarget, "findExactCloseTarget");
|
|
1947
|
+
var setEq = /* @__PURE__ */ __name((a, b) => a.size === b.size && [
|
|
1948
|
+
...a
|
|
1949
|
+
].every((x) => b.has(x)), "setEq");
|
|
1950
|
+
function applyCloses(args) {
|
|
1951
|
+
const { legs, book, exactCloseTarget, at } = args;
|
|
1952
|
+
const openList = book.live(at);
|
|
1953
|
+
const leftover = [];
|
|
1954
|
+
for (const leg of legs) {
|
|
1955
|
+
let remaining = leg.totalSize;
|
|
1956
|
+
const closeTargetIsLong = sideSign(leg.side) === -1;
|
|
1957
|
+
const holders = openList.filter((position) => {
|
|
1958
|
+
const qty = position.exposure.get(leg.symbol);
|
|
1959
|
+
if (qty.isZero()) {
|
|
1960
|
+
return false;
|
|
1961
|
+
}
|
|
1962
|
+
return closeTargetIsLong ? qty.isPositive() : qty.isNegative();
|
|
1963
|
+
});
|
|
1964
|
+
if (exactCloseTarget) {
|
|
1965
|
+
holders.sort((a, b) => a === exactCloseTarget ? -1 : b === exactCloseTarget ? 1 : 0);
|
|
1966
|
+
}
|
|
1967
|
+
for (const position of holders) {
|
|
1968
|
+
if (isLessThanOrEqual(remaining, ZERO)) {
|
|
1969
|
+
break;
|
|
1970
|
+
}
|
|
1971
|
+
const have = position.exposure.get(leg.symbol).abs();
|
|
1972
|
+
const reduce = min(have, remaining);
|
|
1973
|
+
position.parts.push(...leg.take(reduce));
|
|
1974
|
+
position.exposure.addDelta(leg.symbol, signedBySide(reduce, leg.side));
|
|
1975
|
+
remaining = remaining.minus(reduce);
|
|
1976
|
+
book.markClosedIfFlat(position);
|
|
1977
|
+
}
|
|
1978
|
+
if (isGreaterThanOrEqual(remaining, DUST)) {
|
|
1979
|
+
leftover.push({
|
|
1980
|
+
side: leg.side,
|
|
1981
|
+
qty: remaining,
|
|
1982
|
+
leg
|
|
1983
|
+
});
|
|
1984
|
+
}
|
|
1985
|
+
}
|
|
1986
|
+
return leftover;
|
|
1987
|
+
}
|
|
1988
|
+
__name(applyCloses, "applyCloses");
|
|
1989
|
+
function resolveLeftoverTarget(args) {
|
|
1990
|
+
const { book, leftover, at } = args;
|
|
1991
|
+
const leftoverLongs = new Set(leftover.filter((entry) => entry.side === "BUY").map((entry) => entry.leg.symbol));
|
|
1992
|
+
const leftoverShorts = new Set(leftover.filter((entry) => entry.side === "SELL").map((entry) => entry.leg.symbol));
|
|
1993
|
+
const target = book.live(at).find((position) => {
|
|
1994
|
+
const { longs, shorts } = position.exposure.activeLegs();
|
|
1995
|
+
const sameActiveShape = setEq(leftoverLongs, longs) && setEq(leftoverShorts, shorts);
|
|
1996
|
+
const noDormantLegs = position.exposure.symbols().length === longs.size + shorts.size;
|
|
1997
|
+
return sameActiveShape && noDormantLegs;
|
|
1998
|
+
});
|
|
1999
|
+
if (target) {
|
|
2000
|
+
return target;
|
|
2001
|
+
}
|
|
2002
|
+
const symbols = [
|
|
2003
|
+
.../* @__PURE__ */ new Set([
|
|
2004
|
+
...leftoverLongs,
|
|
2005
|
+
...leftoverShorts
|
|
2006
|
+
])
|
|
2007
|
+
];
|
|
2008
|
+
const positionKey = book.keyFor(symbols);
|
|
2009
|
+
return book.findLiveByKey(positionKey, at) ?? book.create(positionKey);
|
|
2010
|
+
}
|
|
2011
|
+
__name(resolveLeftoverTarget, "resolveLeftoverTarget");
|
|
2012
|
+
function applyLeftover(target, leftover) {
|
|
2013
|
+
for (const entry of leftover) {
|
|
2014
|
+
target.parts.push(...entry.leg.take(entry.qty));
|
|
2015
|
+
target.exposure.addSigned(entry.leg.symbol, entry.qty.toFixed(), entry.side);
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
__name(applyLeftover, "applyLeftover");
|
|
2019
|
+
|
|
1972
2020
|
// src/sync/applicables/fills.ts
|
|
1973
2021
|
function assignPartIds(positions) {
|
|
1974
2022
|
const bySource = /* @__PURE__ */ new Map();
|
|
@@ -2629,6 +2677,246 @@ function validateQuantity(quantity, price, precision) {
|
|
|
2629
2677
|
return true;
|
|
2630
2678
|
}
|
|
2631
2679
|
__name(validateQuantity, "validateQuantity");
|
|
2680
|
+
var VAULT_ACTION_DOMAIN = {
|
|
2681
|
+
name: "PearVaultService",
|
|
2682
|
+
version: "1",
|
|
2683
|
+
chainId: 999
|
|
2684
|
+
};
|
|
2685
|
+
var VAULT_ACTION_TYPES = {
|
|
2686
|
+
"withdraw-to-evm": {
|
|
2687
|
+
WithdrawToEvm: [
|
|
2688
|
+
{
|
|
2689
|
+
name: "vault",
|
|
2690
|
+
type: "address"
|
|
2691
|
+
},
|
|
2692
|
+
{
|
|
2693
|
+
name: "amount",
|
|
2694
|
+
type: "string"
|
|
2695
|
+
},
|
|
2696
|
+
{
|
|
2697
|
+
name: "token",
|
|
2698
|
+
type: "string"
|
|
2699
|
+
},
|
|
2700
|
+
{
|
|
2701
|
+
name: "nonce",
|
|
2702
|
+
type: "string"
|
|
2703
|
+
},
|
|
2704
|
+
{
|
|
2705
|
+
name: "deadline",
|
|
2706
|
+
type: "uint256"
|
|
2707
|
+
}
|
|
2708
|
+
]
|
|
2709
|
+
},
|
|
2710
|
+
"set-trader": {
|
|
2711
|
+
SetTrader: [
|
|
2712
|
+
{
|
|
2713
|
+
name: "vault",
|
|
2714
|
+
type: "address"
|
|
2715
|
+
},
|
|
2716
|
+
{
|
|
2717
|
+
name: "trader",
|
|
2718
|
+
type: "address"
|
|
2719
|
+
},
|
|
2720
|
+
{
|
|
2721
|
+
name: "nonce",
|
|
2722
|
+
type: "string"
|
|
2723
|
+
},
|
|
2724
|
+
{
|
|
2725
|
+
name: "deadline",
|
|
2726
|
+
type: "uint256"
|
|
2727
|
+
}
|
|
2728
|
+
]
|
|
2729
|
+
},
|
|
2730
|
+
"agent-approve": {
|
|
2731
|
+
AgentApprove: [
|
|
2732
|
+
{
|
|
2733
|
+
name: "vault",
|
|
2734
|
+
type: "address"
|
|
2735
|
+
},
|
|
2736
|
+
{
|
|
2737
|
+
name: "nonce",
|
|
2738
|
+
type: "string"
|
|
2739
|
+
},
|
|
2740
|
+
{
|
|
2741
|
+
name: "deadline",
|
|
2742
|
+
type: "uint256"
|
|
2743
|
+
}
|
|
2744
|
+
]
|
|
2745
|
+
},
|
|
2746
|
+
"set-unified-account": {
|
|
2747
|
+
SetUnifiedAccount: [
|
|
2748
|
+
{
|
|
2749
|
+
name: "vault",
|
|
2750
|
+
type: "address"
|
|
2751
|
+
},
|
|
2752
|
+
{
|
|
2753
|
+
name: "nonce",
|
|
2754
|
+
type: "string"
|
|
2755
|
+
},
|
|
2756
|
+
{
|
|
2757
|
+
name: "deadline",
|
|
2758
|
+
type: "uint256"
|
|
2759
|
+
}
|
|
2760
|
+
]
|
|
2761
|
+
},
|
|
2762
|
+
"update-metadata": {
|
|
2763
|
+
UpdateVaultMetadata: [
|
|
2764
|
+
{
|
|
2765
|
+
name: "vault",
|
|
2766
|
+
type: "address"
|
|
2767
|
+
},
|
|
2768
|
+
{
|
|
2769
|
+
name: "name",
|
|
2770
|
+
type: "string"
|
|
2771
|
+
},
|
|
2772
|
+
{
|
|
2773
|
+
name: "about",
|
|
2774
|
+
type: "string"
|
|
2775
|
+
},
|
|
2776
|
+
{
|
|
2777
|
+
name: "imageUri",
|
|
2778
|
+
type: "string"
|
|
2779
|
+
},
|
|
2780
|
+
{
|
|
2781
|
+
name: "nonce",
|
|
2782
|
+
type: "string"
|
|
2783
|
+
},
|
|
2784
|
+
{
|
|
2785
|
+
name: "deadline",
|
|
2786
|
+
type: "uint256"
|
|
2787
|
+
}
|
|
2788
|
+
]
|
|
2789
|
+
}
|
|
2790
|
+
};
|
|
2791
|
+
var CREATE_VAULT_TYPES = {
|
|
2792
|
+
CreateVault: [
|
|
2793
|
+
{
|
|
2794
|
+
name: "leader",
|
|
2795
|
+
type: "address"
|
|
2796
|
+
},
|
|
2797
|
+
{
|
|
2798
|
+
name: "name",
|
|
2799
|
+
type: "string"
|
|
2800
|
+
},
|
|
2801
|
+
{
|
|
2802
|
+
name: "description",
|
|
2803
|
+
type: "string"
|
|
2804
|
+
},
|
|
2805
|
+
{
|
|
2806
|
+
name: "imageUri",
|
|
2807
|
+
type: "string"
|
|
2808
|
+
},
|
|
2809
|
+
{
|
|
2810
|
+
name: "configId",
|
|
2811
|
+
type: "uint256"
|
|
2812
|
+
},
|
|
2813
|
+
{
|
|
2814
|
+
name: "maxWithdrawalPeriodDays",
|
|
2815
|
+
type: "uint256"
|
|
2816
|
+
},
|
|
2817
|
+
{
|
|
2818
|
+
name: "nonce",
|
|
2819
|
+
type: "string"
|
|
2820
|
+
},
|
|
2821
|
+
{
|
|
2822
|
+
name: "deadline",
|
|
2823
|
+
type: "uint256"
|
|
2824
|
+
}
|
|
2825
|
+
]
|
|
2826
|
+
};
|
|
2827
|
+
function buildLeaderActionTypedData(action, vaultAddress, body) {
|
|
2828
|
+
const vault = checksumAddress(vaultAddress);
|
|
2829
|
+
const nonce = String(body.nonce ?? "");
|
|
2830
|
+
const deadline = BigInt(body.deadline ?? 0);
|
|
2831
|
+
switch (action) {
|
|
2832
|
+
case "withdraw-to-evm":
|
|
2833
|
+
return {
|
|
2834
|
+
types: VAULT_ACTION_TYPES[action],
|
|
2835
|
+
primaryType: "WithdrawToEvm",
|
|
2836
|
+
message: {
|
|
2837
|
+
vault,
|
|
2838
|
+
amount: String(body.amount ?? ""),
|
|
2839
|
+
token: String(body.tokenId ?? "0"),
|
|
2840
|
+
nonce,
|
|
2841
|
+
deadline
|
|
2842
|
+
}
|
|
2843
|
+
};
|
|
2844
|
+
case "set-trader":
|
|
2845
|
+
return {
|
|
2846
|
+
types: VAULT_ACTION_TYPES[action],
|
|
2847
|
+
primaryType: "SetTrader",
|
|
2848
|
+
message: {
|
|
2849
|
+
vault,
|
|
2850
|
+
trader: checksumAddress(String(body.traderAddress ?? "")),
|
|
2851
|
+
nonce,
|
|
2852
|
+
deadline
|
|
2853
|
+
}
|
|
2854
|
+
};
|
|
2855
|
+
case "agent-approve":
|
|
2856
|
+
return {
|
|
2857
|
+
types: VAULT_ACTION_TYPES[action],
|
|
2858
|
+
primaryType: "AgentApprove",
|
|
2859
|
+
message: {
|
|
2860
|
+
vault,
|
|
2861
|
+
nonce,
|
|
2862
|
+
deadline
|
|
2863
|
+
}
|
|
2864
|
+
};
|
|
2865
|
+
case "set-unified-account":
|
|
2866
|
+
return {
|
|
2867
|
+
types: VAULT_ACTION_TYPES[action],
|
|
2868
|
+
primaryType: "SetUnifiedAccount",
|
|
2869
|
+
message: {
|
|
2870
|
+
vault,
|
|
2871
|
+
nonce,
|
|
2872
|
+
deadline
|
|
2873
|
+
}
|
|
2874
|
+
};
|
|
2875
|
+
case "update-metadata":
|
|
2876
|
+
return {
|
|
2877
|
+
types: VAULT_ACTION_TYPES[action],
|
|
2878
|
+
primaryType: "UpdateVaultMetadata",
|
|
2879
|
+
message: {
|
|
2880
|
+
vault,
|
|
2881
|
+
name: String(body.name ?? ""),
|
|
2882
|
+
about: String(body.about ?? ""),
|
|
2883
|
+
imageUri: String(body.imageUri ?? ""),
|
|
2884
|
+
nonce,
|
|
2885
|
+
deadline
|
|
2886
|
+
}
|
|
2887
|
+
};
|
|
2888
|
+
}
|
|
2889
|
+
}
|
|
2890
|
+
__name(buildLeaderActionTypedData, "buildLeaderActionTypedData");
|
|
2891
|
+
function buildCreateVaultMessage(body) {
|
|
2892
|
+
return {
|
|
2893
|
+
leader: checksumAddress(String(body.leader ?? "")),
|
|
2894
|
+
name: String(body.name ?? ""),
|
|
2895
|
+
description: String(body.description ?? ""),
|
|
2896
|
+
imageUri: String(body.imageUri ?? ""),
|
|
2897
|
+
configId: BigInt(String(body.configId ?? 0)),
|
|
2898
|
+
maxWithdrawalPeriodDays: BigInt(String(body.maxWithdrawalPeriodDays ?? 0)),
|
|
2899
|
+
nonce: String(body.nonce ?? ""),
|
|
2900
|
+
deadline: BigInt(body.deadline ?? 0)
|
|
2901
|
+
};
|
|
2902
|
+
}
|
|
2903
|
+
__name(buildCreateVaultMessage, "buildCreateVaultMessage");
|
|
2904
|
+
function checksumAddress(address) {
|
|
2905
|
+
if (!/^0x[0-9a-fA-F]{40}$/.test(address)) {
|
|
2906
|
+
throw new Error(`invalid address: ${address}`);
|
|
2907
|
+
}
|
|
2908
|
+
const lower = address.slice(2).toLowerCase();
|
|
2909
|
+
const hash = keccak_256(utf8ToBytes(lower));
|
|
2910
|
+
let out = "";
|
|
2911
|
+
for (const [index, byte] of hash.subarray(0, 20).entries()) {
|
|
2912
|
+
const high = lower.charAt(index * 2);
|
|
2913
|
+
const low = lower.charAt(index * 2 + 1);
|
|
2914
|
+
out += byte >> 4 >= 8 ? high.toUpperCase() : high;
|
|
2915
|
+
out += (byte & 15) >= 8 ? low.toUpperCase() : low;
|
|
2916
|
+
}
|
|
2917
|
+
return `0x${out}`;
|
|
2918
|
+
}
|
|
2919
|
+
__name(checksumAddress, "checksumAddress");
|
|
2632
2920
|
var STORAGE_PREFIX = "pear.x-handoff.";
|
|
2633
2921
|
function startXHandoff(storage) {
|
|
2634
2922
|
const handoffId = base64urlnopad.encode(randomBytes(16));
|
|
@@ -2700,4 +2988,4 @@ function takeVerifier(handoffId, storage) {
|
|
|
2700
2988
|
}
|
|
2701
2989
|
__name(takeVerifier, "takeVerifier");
|
|
2702
2990
|
|
|
2703
|
-
export { BIPS, ZERO, abs, add, aggregateRealizedPositionState, baseToContracts, buildApplicables, compareValue, computeAssetEntryNotional, computeAssetGrossRealizedPnL, computeAssetRealizedPnLs, computeAssetUndeterminedSizes, computeAssetUnrealizedPnL, computeAssetUnrealizedPnLs, computeBasketWeightedRatioV1, computeDeployedCapital, computeDeployedCapitalState, computeEntryPriceForAsset, computeFillTransitions, computeMaxBasketSize, computeMultiQuoteBasketSize, computePositionEntryPrices, computeRealizedPnlBySymbol, computeRealizedPnlFromFills, computeRealizedPositionState, computeSyncPayload, computeUnrealizedPositionState, computeUnrealizedPositionStateFromEntryPrices, configureProxy, contractsToBase, countDecimals, deriveBasketKey, divide, exponentiate, finishXHandoff, generateDeterministicPositionKey, groupFillsByAsset, groupFillsByPosition, isEqualTo, isFiniteDecimal, isGreaterThan, isGreaterThanOrEqual, isLessThan, isLessThanOrEqual, isNegative, isPositive, isZero, min, multiply, negate, parse, precisePrice, preciseQuantity, proxyFetch, sideSign, sign, signed, signedBySide, sizeBasket, sizeBasketByLeverageMode, sizeBasketPerAssetLeverage, startXHandoff, stripXReturn, subtract, sumAttributedFunding, toDecimalString, validateLeverage, validateQuantity, walkPositionLedger, walkPositionTransitions };
|
|
2991
|
+
export { BIPS, CREATE_VAULT_TYPES, VAULT_ACTION_DOMAIN, VAULT_ACTION_TYPES, ZERO, abs, add, aggregateRealizedPositionState, baseToContracts, buildApplicables, buildCreateVaultMessage, buildLeaderActionTypedData, compareValue, computeAssetEntryNotional, computeAssetGrossRealizedPnL, computeAssetRealizedPnLs, computeAssetUndeterminedSizes, computeAssetUnrealizedPnL, computeAssetUnrealizedPnLs, computeBasketWeightedRatioV1, computeDeployedCapital, computeDeployedCapitalState, computeEntryPriceForAsset, computeFillTransitions, computeMaxBasketSize, computeMultiQuoteBasketSize, computePositionEntryPrices, computeRealizedPnlBySymbol, computeRealizedPnlFromFills, computeRealizedPositionState, computeSyncPayload, computeUnrealizedPositionState, computeUnrealizedPositionStateFromEntryPrices, configureProxy, contractsToBase, countDecimals, deriveBasketKey, divide, exponentiate, finishXHandoff, generateDeterministicPositionKey, groupFillsByAsset, groupFillsByPosition, isEqualTo, isFiniteDecimal, isGreaterThan, isGreaterThanOrEqual, isLessThan, isLessThanOrEqual, isNegative, isPositive, isZero, min, multiply, negate, parse, precisePrice, preciseQuantity, proxyFetch, sideSign, sign, signed, signedBySide, sizeBasket, sizeBasketByLeverageMode, sizeBasketPerAssetLeverage, startXHandoff, stripXReturn, subtract, sumAttributedFunding, toDecimalString, validateLeverage, validateQuantity, walkPositionLedger, walkPositionTransitions };
|
|
@@ -12,6 +12,7 @@ import type { FillPart } from './types';
|
|
|
12
12
|
*/
|
|
13
13
|
export declare class Basket {
|
|
14
14
|
readonly legs: CombinedLeg[];
|
|
15
|
+
readonly fills: HttpTypes.UnknownExternalFill[];
|
|
15
16
|
private constructor();
|
|
16
17
|
/** Detect baskets from a fill stream, returned in ascending time order. */
|
|
17
18
|
static detect(fills: HttpTypes.UnknownExternalFill[]): Basket[];
|
|
@@ -9,6 +9,13 @@ import type { BasketRule } from './types';
|
|
|
9
9
|
* tolerance: a genuine partial close (>= 1e-8) stays open, as it should.
|
|
10
10
|
*/
|
|
11
11
|
export declare const DUST: BigNumber;
|
|
12
|
+
/**
|
|
13
|
+
* How long before an open position's creation a fill may still belong to it. Fills can
|
|
14
|
+
* reach sync out of order by seconds across cycles (observed up to ~30s), so a strict
|
|
15
|
+
* cutoff would split one position in two; anything older predates the position and must
|
|
16
|
+
* not be replayed into it (see pear-protocol/backend#1067).
|
|
17
|
+
*/
|
|
18
|
+
export declare const LATE_FILL_GRACE_MS: number;
|
|
12
19
|
export declare const MISSED_HISTORY_FILL_PREFIX = "opening-synthetic-";
|
|
13
20
|
export declare const BASKET_RULES: BasketRule[];
|
|
14
21
|
/**
|
|
@@ -9,6 +9,11 @@ import type { MatchedPosition } from './types';
|
|
|
9
9
|
* - resolveLeftoverTarget merges into a position whose FULL leg set (active and
|
|
10
10
|
* dormant) is exactly the leftover's, else opens new (same-shape ADDs consolidate here).
|
|
11
11
|
* The asset SET of a position never grows, so positionKey == key(opening symbols).
|
|
12
|
+
*
|
|
13
|
+
* Every phase only sees positions that already existed when the basket happened: a
|
|
14
|
+
* basket older than an open position's creation (beyond LATE_FILL_GRACE_MS) cannot
|
|
15
|
+
* close, reduce or merge into it, so a late-fetched earlier lifecycle is not bound to a
|
|
16
|
+
* position opened after it (pear-protocol/backend#1067).
|
|
12
17
|
*/
|
|
13
18
|
export declare function matchPositions(args: {
|
|
14
19
|
tradeAccountId: string;
|
|
@@ -31,6 +31,11 @@ export type WorkingPosition = {
|
|
|
31
31
|
closed: boolean;
|
|
32
32
|
/** Origin of the seeded existing position; undefined for positions opened mid-replay. */
|
|
33
33
|
existingOrigin?: HttpTypes.PositionOrigin;
|
|
34
|
+
/**
|
|
35
|
+
* Earliest basket time (ms) this position may take fills from: its creation minus the
|
|
36
|
+
* late-fill grace for a seeded position, -Infinity for one opened mid-replay.
|
|
37
|
+
*/
|
|
38
|
+
liveFrom: number;
|
|
34
39
|
};
|
|
35
40
|
/** A matched position that received fill slices, ready to become an applicable. */
|
|
36
41
|
export type MatchedPosition = {
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every action a leader-signed route may carry. A new route must add its member
|
|
3
|
+
* here, which forces the typed-data switch below to name it at build time.
|
|
4
|
+
*/
|
|
5
|
+
export type LeaderActionName = 'withdraw-to-evm' | 'set-trader' | 'agent-approve' | 'set-unified-account' | 'update-metadata';
|
|
6
|
+
/**
|
|
7
|
+
* EIP-712 domain for off-chain vault-service auth. `chainId` is pinned to 999
|
|
8
|
+
* (HyperEVM) by the shared contract — signing is offline, so there is no chain
|
|
9
|
+
* switch and this must NOT be read from env. `verifyingContract` is omitted on
|
|
10
|
+
* purpose: this is service auth, not an on-chain contract call.
|
|
11
|
+
*/
|
|
12
|
+
export declare const VAULT_ACTION_DOMAIN: {
|
|
13
|
+
readonly name: "PearVaultService";
|
|
14
|
+
readonly version: "1";
|
|
15
|
+
readonly chainId: 999;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* EIP-712 types per action — the one copy both signer and verifier import.
|
|
19
|
+
* One struct per action: every signed parameter is a readable field in the
|
|
20
|
+
* wallet prompt, and the primary-type name itself discriminates the action,
|
|
21
|
+
* so structurally identical actions can never replay into each other.
|
|
22
|
+
*/
|
|
23
|
+
export declare const VAULT_ACTION_TYPES: {
|
|
24
|
+
readonly 'withdraw-to-evm': {
|
|
25
|
+
readonly WithdrawToEvm: readonly [{
|
|
26
|
+
readonly name: "vault";
|
|
27
|
+
readonly type: "address";
|
|
28
|
+
}, {
|
|
29
|
+
readonly name: "amount";
|
|
30
|
+
readonly type: "string";
|
|
31
|
+
}, {
|
|
32
|
+
readonly name: "token";
|
|
33
|
+
readonly type: "string";
|
|
34
|
+
}, {
|
|
35
|
+
readonly name: "nonce";
|
|
36
|
+
readonly type: "string";
|
|
37
|
+
}, {
|
|
38
|
+
readonly name: "deadline";
|
|
39
|
+
readonly type: "uint256";
|
|
40
|
+
}];
|
|
41
|
+
};
|
|
42
|
+
readonly 'set-trader': {
|
|
43
|
+
readonly SetTrader: readonly [{
|
|
44
|
+
readonly name: "vault";
|
|
45
|
+
readonly type: "address";
|
|
46
|
+
}, {
|
|
47
|
+
readonly name: "trader";
|
|
48
|
+
readonly type: "address";
|
|
49
|
+
}, {
|
|
50
|
+
readonly name: "nonce";
|
|
51
|
+
readonly type: "string";
|
|
52
|
+
}, {
|
|
53
|
+
readonly name: "deadline";
|
|
54
|
+
readonly type: "uint256";
|
|
55
|
+
}];
|
|
56
|
+
};
|
|
57
|
+
readonly 'agent-approve': {
|
|
58
|
+
readonly AgentApprove: readonly [{
|
|
59
|
+
readonly name: "vault";
|
|
60
|
+
readonly type: "address";
|
|
61
|
+
}, {
|
|
62
|
+
readonly name: "nonce";
|
|
63
|
+
readonly type: "string";
|
|
64
|
+
}, {
|
|
65
|
+
readonly name: "deadline";
|
|
66
|
+
readonly type: "uint256";
|
|
67
|
+
}];
|
|
68
|
+
};
|
|
69
|
+
readonly 'set-unified-account': {
|
|
70
|
+
readonly SetUnifiedAccount: readonly [{
|
|
71
|
+
readonly name: "vault";
|
|
72
|
+
readonly type: "address";
|
|
73
|
+
}, {
|
|
74
|
+
readonly name: "nonce";
|
|
75
|
+
readonly type: "string";
|
|
76
|
+
}, {
|
|
77
|
+
readonly name: "deadline";
|
|
78
|
+
readonly type: "uint256";
|
|
79
|
+
}];
|
|
80
|
+
};
|
|
81
|
+
readonly 'update-metadata': {
|
|
82
|
+
readonly UpdateVaultMetadata: readonly [{
|
|
83
|
+
readonly name: "vault";
|
|
84
|
+
readonly type: "address";
|
|
85
|
+
}, {
|
|
86
|
+
readonly name: "name";
|
|
87
|
+
readonly type: "string";
|
|
88
|
+
}, {
|
|
89
|
+
readonly name: "about";
|
|
90
|
+
readonly type: "string";
|
|
91
|
+
}, {
|
|
92
|
+
readonly name: "imageUri";
|
|
93
|
+
readonly type: "string";
|
|
94
|
+
}, {
|
|
95
|
+
readonly name: "nonce";
|
|
96
|
+
readonly type: "string";
|
|
97
|
+
}, {
|
|
98
|
+
readonly name: "deadline";
|
|
99
|
+
readonly type: "uint256";
|
|
100
|
+
}];
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* EIP-712 type for a create — the one copy both signer and verifier import.
|
|
105
|
+
* Every signed parameter is a readable field in the wallet prompt.
|
|
106
|
+
*/
|
|
107
|
+
export declare const CREATE_VAULT_TYPES: {
|
|
108
|
+
readonly CreateVault: readonly [{
|
|
109
|
+
readonly name: "leader";
|
|
110
|
+
readonly type: "address";
|
|
111
|
+
}, {
|
|
112
|
+
readonly name: "name";
|
|
113
|
+
readonly type: "string";
|
|
114
|
+
}, {
|
|
115
|
+
readonly name: "description";
|
|
116
|
+
readonly type: "string";
|
|
117
|
+
}, {
|
|
118
|
+
readonly name: "imageUri";
|
|
119
|
+
readonly type: "string";
|
|
120
|
+
}, {
|
|
121
|
+
readonly name: "configId";
|
|
122
|
+
readonly type: "uint256";
|
|
123
|
+
}, {
|
|
124
|
+
readonly name: "maxWithdrawalPeriodDays";
|
|
125
|
+
readonly type: "uint256";
|
|
126
|
+
}, {
|
|
127
|
+
readonly name: "nonce";
|
|
128
|
+
readonly type: "string";
|
|
129
|
+
}, {
|
|
130
|
+
readonly name: "deadline";
|
|
131
|
+
readonly type: "uint256";
|
|
132
|
+
}];
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* One action's complete signable typed data. Widened (non-generic) on purpose:
|
|
136
|
+
* viem's generic overloads cannot take a union of per-action shapes at one call site.
|
|
137
|
+
*/
|
|
138
|
+
export type LeaderActionTypedData = {
|
|
139
|
+
types: Record<string, readonly {
|
|
140
|
+
name: string;
|
|
141
|
+
type: string;
|
|
142
|
+
}[]>;
|
|
143
|
+
primaryType: string;
|
|
144
|
+
message: {
|
|
145
|
+
vault: `0x${string}`;
|
|
146
|
+
amount?: string;
|
|
147
|
+
token?: string;
|
|
148
|
+
trader?: `0x${string}`;
|
|
149
|
+
name?: string;
|
|
150
|
+
about?: string;
|
|
151
|
+
imageUri?: string;
|
|
152
|
+
nonce: string;
|
|
153
|
+
deadline: bigint;
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Canonical typed-data reconstruction from the request path + body. Signer and
|
|
158
|
+
* verifier MUST call this same builder with these same normalized values:
|
|
159
|
+
* - vault = the :vaultAddress path segment (checksummed inside the signed EIP-712 message; storage stays lowercase)
|
|
160
|
+
* - amount = body.amount, verbatim decimal string (withdraw only)
|
|
161
|
+
* - token = body.tokenId ?? '0', verbatim string (withdraw only)
|
|
162
|
+
* - trader = body.traderAddress, checksummed (set-trader only)
|
|
163
|
+
* - name / about / imageUri = the full display set, verbatim strings; '' means cleared (update-metadata only)
|
|
164
|
+
* - nonce = body.nonce (client-generated; salts the signature for idempotency)
|
|
165
|
+
* - deadline = body.deadline (unix seconds) as uint256
|
|
166
|
+
*/
|
|
167
|
+
export declare function buildLeaderActionTypedData(action: LeaderActionName, vaultAddress: string, body: Record<string, unknown>): LeaderActionTypedData;
|
|
168
|
+
/**
|
|
169
|
+
* Canonical CreateVault reconstruction from the request body. Signer and verifier
|
|
170
|
+
* MUST call this same builder; absent optionals sign as '' and 0.
|
|
171
|
+
*/
|
|
172
|
+
export declare function buildCreateVaultMessage(body: Record<string, unknown>): {
|
|
173
|
+
leader: `0x${string}`;
|
|
174
|
+
name: string;
|
|
175
|
+
description: string;
|
|
176
|
+
imageUri: string;
|
|
177
|
+
configId: bigint;
|
|
178
|
+
maxWithdrawalPeriodDays: bigint;
|
|
179
|
+
nonce: string;
|
|
180
|
+
deadline: bigint;
|
|
181
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './action-typed-data';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pear-protocol/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Pear Protocol Utility functions",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@noble/hashes": "^1.3.2",
|
|
30
|
-
"@pear-protocol/types": "^2.
|
|
30
|
+
"@pear-protocol/types": "^2.10.0",
|
|
31
31
|
"@scure/base": "^2.3.0",
|
|
32
32
|
"bignumber.js": "9.3.1"
|
|
33
33
|
},
|