@varla/polymarket 3.0.0 → 3.0.3
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/CHANGELOG.md +29 -0
- package/dist/clob-orders.d.ts.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +193 -3
- package/dist/leverage-adapter.d.ts +68 -0
- package/dist/leverage-adapter.d.ts.map +1 -0
- package/dist/trade-impact.d.ts +124 -0
- package/dist/trade-impact.d.ts.map +1 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
# @varla/polymarket
|
|
2
2
|
|
|
3
|
+
## 3.0.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- c0b2c17: Adjusted order price validation to use tick-size-based bounds for more accurate market limits.
|
|
8
|
+
|
|
9
|
+
- Enforce minimum and maximum prices based on the market tick size in order validation
|
|
10
|
+
- Tightened isValidPrice to reject prices outside tick-size-derived bounds
|
|
11
|
+
|
|
12
|
+
## 3.0.2
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- eeb2d28: No functional changes; package metadata formatting adjusted.
|
|
17
|
+
|
|
18
|
+
- No runtime or API changes in this release.
|
|
19
|
+
|
|
20
|
+
- 99e25e5: Minor formatting and import cleanup across trade impact and leverage adapter code and tests.
|
|
21
|
+
|
|
22
|
+
- Cleaned up formatting and import ordering in trade impact and leverage adapter modules and related tests.
|
|
23
|
+
|
|
24
|
+
## 3.0.1
|
|
25
|
+
|
|
26
|
+
### Patch Changes
|
|
27
|
+
|
|
28
|
+
- a0cf69f: Refined package.json formatting without changing functionality.
|
|
29
|
+
|
|
30
|
+
- Reformat the files list in package metadata for consistency.
|
|
31
|
+
|
|
3
32
|
## 3.0.0
|
|
4
33
|
|
|
5
34
|
### Major Changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clob-orders.d.ts","sourceRoot":"","sources":["../src/clob-orders.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AACzC,OAAO,KAAK,EACV,aAAa,EACb,WAAW,EAEX,kBAAkB,EAClB,WAAW,EACZ,MAAM,yBAAyB,CAAC;AA4DjC,qBAAa,oBAAqB,SAAQ,KAAK;gBACjC,OAAO,EAAE,MAAM;CAI5B;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,aAAa,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"clob-orders.d.ts","sourceRoot":"","sources":["../src/clob-orders.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AACzC,OAAO,KAAK,EACV,aAAa,EACb,WAAW,EAEX,kBAAkB,EAClB,WAAW,EACZ,MAAM,yBAAyB,CAAC;AA4DjC,qBAAa,oBAAqB,SAAQ,KAAK;gBACjC,OAAO,EAAE,MAAM;CAI5B;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,aAAa,GAAG,IAAI,CA4CrF;AAMD;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,WAAW,EACnB,QAAQ,EAAE,aAAa,GACtB;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,CAkB9C;AA8CD,MAAM,MAAM,sBAAsB,GAAG;IACnC,yCAAyC;IACzC,aAAa,CAAC,EAAE,kBAAkB,CAAC;IACnC,yCAAyC;IACzC,aAAa,CAAC,EAAE,KAAK,MAAM,EAAE,CAAC;IAC9B,4CAA4C;IAC5C,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;GAEG;AACH,qBAAa,gBAAgB;IAMzB,OAAO,CAAC,QAAQ,CAAC,YAAY;IAL/B,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAqB;IACnD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAgB;IAC/C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;gBAGjB,YAAY,EAAE,YAAY,EAC3C,MAAM,GAAE,sBAA2B;IAWrC;;OAEG;IACH,eAAe,IAAI,KAAK,MAAM,EAAE;IAWhC;;OAEG;IACH,gBAAgB,IAAI,KAAK,MAAM,EAAE;IAQjC;;;;;;OAMG;IACG,WAAW,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,WAAW,CAAC;CAoDrF;AAMD;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,YAAY,EAAE,YAAY,EAC1B,MAAM,CAAC,EAAE,sBAAsB,GAC9B,gBAAgB,CAElB;AAMD;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAgBvE;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAOrE;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAIzD"}
|
package/dist/index.d.ts
CHANGED
|
@@ -20,5 +20,7 @@ export { parseDecimalToBigint } from "./utils/decimal.js";
|
|
|
20
20
|
export { twapFromHistory } from "./utils/twap.js";
|
|
21
21
|
export { probToE8, collateralPriceFromMarkets } from "./utils/pricing.js";
|
|
22
22
|
export { polymarketEventUrl } from "./utils/url.js";
|
|
23
|
+
export * from "./trade-impact.js";
|
|
24
|
+
export * from "./leverage-adapter.js";
|
|
23
25
|
export * from "./bridge-client.js";
|
|
24
26
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,mBAAmB,CAAC;AAMlC,cAAc,gBAAgB,CAAC;AAM/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAM9B,cAAc,yBAAyB,CAAC;AACxC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AAMjC,cAAc,sBAAsB,CAAC;AAMrC,cAAc,UAAU,CAAC;AAMzB,cAAc,kBAAkB,CAAC;AAMjC,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,0BAA0B,EAAE,MAAM,oBAAoB,CAAC;AAC1E,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAMpD,cAAc,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,mBAAmB,CAAC;AAMlC,cAAc,gBAAgB,CAAC;AAM/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAM9B,cAAc,yBAAyB,CAAC;AACxC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AAMjC,cAAc,sBAAsB,CAAC;AAMrC,cAAc,UAAU,CAAC;AAMzB,cAAc,kBAAkB,CAAC;AAMjC,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,0BAA0B,EAAE,MAAM,oBAAoB,CAAC;AAC1E,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAMpD,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AAMtC,cAAc,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -2056,13 +2056,15 @@ class OrderValidationError extends Error {
|
|
|
2056
2056
|
}
|
|
2057
2057
|
}
|
|
2058
2058
|
function validateOrderParams(params, options) {
|
|
2059
|
-
if (params.price < 0.01 || params.price > 0.99) {
|
|
2060
|
-
throw new OrderValidationError(`Price must be between 0.01 and 0.99, got ${params.price}`);
|
|
2061
|
-
}
|
|
2062
2059
|
const tickSize = Number.parseFloat(options.tickSize);
|
|
2063
2060
|
if (Number.isNaN(tickSize) || tickSize <= 0) {
|
|
2064
2061
|
throw new OrderValidationError(`Invalid tick size: ${options.tickSize}`);
|
|
2065
2062
|
}
|
|
2063
|
+
const minPrice = tickSize;
|
|
2064
|
+
const maxPrice = 1 - tickSize;
|
|
2065
|
+
if (params.price < minPrice || params.price > maxPrice) {
|
|
2066
|
+
throw new OrderValidationError(`Price must be between ${minPrice} and ${maxPrice} for tick size ${options.tickSize}, got ${params.price}`);
|
|
2067
|
+
}
|
|
2066
2068
|
const priceInTicks = Math.round(params.price / tickSize);
|
|
2067
2069
|
const alignedPrice = priceInTicks * tickSize;
|
|
2068
2070
|
if (Math.abs(alignedPrice - params.price) > 0.000000001) {
|
|
@@ -2202,6 +2204,8 @@ function isValidPrice(price, tickSize) {
|
|
|
2202
2204
|
const tick = Number.parseFloat(tickSize);
|
|
2203
2205
|
if (Number.isNaN(tick) || tick <= 0)
|
|
2204
2206
|
return false;
|
|
2207
|
+
if (price < tick || price > 1 - tick)
|
|
2208
|
+
return false;
|
|
2205
2209
|
const rounded = roundToTickSize(price, tickSize);
|
|
2206
2210
|
return Math.abs(rounded - price) < 0.000000001;
|
|
2207
2211
|
}
|
|
@@ -3306,6 +3310,180 @@ function collateralPriceFromMarkets(params) {
|
|
|
3306
3310
|
return bestBid;
|
|
3307
3311
|
return Math.min(bestBid, twap);
|
|
3308
3312
|
}
|
|
3313
|
+
// src/trade-impact.ts
|
|
3314
|
+
function parseLevel(level) {
|
|
3315
|
+
return {
|
|
3316
|
+
price: Number.parseFloat(level.price),
|
|
3317
|
+
size: Number.parseFloat(level.size)
|
|
3318
|
+
};
|
|
3319
|
+
}
|
|
3320
|
+
function computeMidPrice(book) {
|
|
3321
|
+
const bestBid = book.bids.length > 0 ? Number.parseFloat(book.bids[0].price) : null;
|
|
3322
|
+
const bestAsk = book.asks.length > 0 ? Number.parseFloat(book.asks[0].price) : null;
|
|
3323
|
+
if (bestBid !== null && bestAsk !== null)
|
|
3324
|
+
return (bestBid + bestAsk) / 2;
|
|
3325
|
+
if (bestBid !== null)
|
|
3326
|
+
return bestBid;
|
|
3327
|
+
if (bestAsk !== null)
|
|
3328
|
+
return bestAsk;
|
|
3329
|
+
return 0;
|
|
3330
|
+
}
|
|
3331
|
+
function computeSlippageBps(midPrice, fillPrice, side) {
|
|
3332
|
+
if (midPrice <= 0)
|
|
3333
|
+
return 0;
|
|
3334
|
+
if (side === "buy") {
|
|
3335
|
+
return (fillPrice - midPrice) / midPrice * 1e4;
|
|
3336
|
+
}
|
|
3337
|
+
return (midPrice - fillPrice) / midPrice * 1e4;
|
|
3338
|
+
}
|
|
3339
|
+
function estimateBuyImpact(book, collateralAmount) {
|
|
3340
|
+
const midPrice = computeMidPrice(book);
|
|
3341
|
+
const fills = [];
|
|
3342
|
+
let remaining = collateralAmount;
|
|
3343
|
+
let totalTokens = 0;
|
|
3344
|
+
let totalSpent = 0;
|
|
3345
|
+
for (const rawLevel of book.asks) {
|
|
3346
|
+
if (remaining <= 0)
|
|
3347
|
+
break;
|
|
3348
|
+
const { price, size } = parseLevel(rawLevel);
|
|
3349
|
+
if (price <= 0 || size <= 0)
|
|
3350
|
+
continue;
|
|
3351
|
+
const levelCost = price * size;
|
|
3352
|
+
const spend = Math.min(remaining, levelCost);
|
|
3353
|
+
const tokensBought = spend / price;
|
|
3354
|
+
totalSpent += spend;
|
|
3355
|
+
totalTokens += tokensBought;
|
|
3356
|
+
remaining -= spend;
|
|
3357
|
+
fills.push({
|
|
3358
|
+
price,
|
|
3359
|
+
size: tokensBought,
|
|
3360
|
+
cumulative: totalTokens
|
|
3361
|
+
});
|
|
3362
|
+
}
|
|
3363
|
+
const avgFillPrice = totalTokens > 0 ? totalSpent / totalTokens : 0;
|
|
3364
|
+
const worstPrice = fills.length > 0 ? fills[fills.length - 1].price : 0;
|
|
3365
|
+
const slippageBps = computeSlippageBps(midPrice, avgFillPrice, "buy");
|
|
3366
|
+
return {
|
|
3367
|
+
side: "buy",
|
|
3368
|
+
avgFillPrice,
|
|
3369
|
+
worstPrice,
|
|
3370
|
+
midPrice,
|
|
3371
|
+
slippageBps,
|
|
3372
|
+
outputAmount: totalTokens,
|
|
3373
|
+
inputAmount: totalSpent,
|
|
3374
|
+
isFilled: remaining <= 0.000000001,
|
|
3375
|
+
levelsConsumed: fills.length,
|
|
3376
|
+
fills
|
|
3377
|
+
};
|
|
3378
|
+
}
|
|
3379
|
+
function estimateSellImpact(book, tokenAmount) {
|
|
3380
|
+
const midPrice = computeMidPrice(book);
|
|
3381
|
+
const fills = [];
|
|
3382
|
+
let remaining = tokenAmount;
|
|
3383
|
+
let totalCollateral = 0;
|
|
3384
|
+
let totalSold = 0;
|
|
3385
|
+
for (const rawLevel of book.bids) {
|
|
3386
|
+
if (remaining <= 0)
|
|
3387
|
+
break;
|
|
3388
|
+
const { price, size } = parseLevel(rawLevel);
|
|
3389
|
+
if (price <= 0 || size <= 0)
|
|
3390
|
+
continue;
|
|
3391
|
+
const sellSize = Math.min(remaining, size);
|
|
3392
|
+
const proceeds = sellSize * price;
|
|
3393
|
+
totalSold += sellSize;
|
|
3394
|
+
totalCollateral += proceeds;
|
|
3395
|
+
remaining -= sellSize;
|
|
3396
|
+
fills.push({
|
|
3397
|
+
price,
|
|
3398
|
+
size: sellSize,
|
|
3399
|
+
cumulative: totalSold
|
|
3400
|
+
});
|
|
3401
|
+
}
|
|
3402
|
+
const avgFillPrice = totalSold > 0 ? totalCollateral / totalSold : 0;
|
|
3403
|
+
const worstPrice = fills.length > 0 ? fills[fills.length - 1].price : 0;
|
|
3404
|
+
const slippageBps = computeSlippageBps(midPrice, avgFillPrice, "sell");
|
|
3405
|
+
return {
|
|
3406
|
+
side: "sell",
|
|
3407
|
+
avgFillPrice,
|
|
3408
|
+
worstPrice,
|
|
3409
|
+
midPrice,
|
|
3410
|
+
slippageBps,
|
|
3411
|
+
outputAmount: totalCollateral,
|
|
3412
|
+
inputAmount: totalSold,
|
|
3413
|
+
isFilled: remaining <= 0.000000001,
|
|
3414
|
+
levelsConsumed: fills.length,
|
|
3415
|
+
fills
|
|
3416
|
+
};
|
|
3417
|
+
}
|
|
3418
|
+
function estimateTradeImpact(book, side, amount) {
|
|
3419
|
+
return side === "buy" ? estimateBuyImpact(book, amount) : estimateSellImpact(book, amount);
|
|
3420
|
+
}
|
|
3421
|
+
function bookDepth(book, side) {
|
|
3422
|
+
const levels = book[side];
|
|
3423
|
+
if (levels.length === 0) {
|
|
3424
|
+
return {
|
|
3425
|
+
totalLiquidity: 0,
|
|
3426
|
+
totalShares: 0,
|
|
3427
|
+
levels: 0,
|
|
3428
|
+
bestPrice: 0,
|
|
3429
|
+
worstPrice: 0,
|
|
3430
|
+
weightedAvgPrice: 0
|
|
3431
|
+
};
|
|
3432
|
+
}
|
|
3433
|
+
let totalLiquidity = 0;
|
|
3434
|
+
let totalShares = 0;
|
|
3435
|
+
for (const rawLevel of levels) {
|
|
3436
|
+
const { price, size } = parseLevel(rawLevel);
|
|
3437
|
+
totalShares += size;
|
|
3438
|
+
totalLiquidity += price * size;
|
|
3439
|
+
}
|
|
3440
|
+
const first = parseLevel(levels[0]);
|
|
3441
|
+
const last = parseLevel(levels[levels.length - 1]);
|
|
3442
|
+
const weightedAvgPrice = totalShares > 0 ? totalLiquidity / totalShares : 0;
|
|
3443
|
+
return {
|
|
3444
|
+
totalLiquidity,
|
|
3445
|
+
totalShares,
|
|
3446
|
+
levels: levels.length,
|
|
3447
|
+
bestPrice: first.price,
|
|
3448
|
+
worstPrice: last.price,
|
|
3449
|
+
weightedAvgPrice
|
|
3450
|
+
};
|
|
3451
|
+
}
|
|
3452
|
+
// src/leverage-adapter.ts
|
|
3453
|
+
function toSdkSlippageBps(estimate) {
|
|
3454
|
+
const clamped = Math.max(0, Math.min(9999, Math.ceil(estimate.slippageBps)));
|
|
3455
|
+
return BigInt(clamped);
|
|
3456
|
+
}
|
|
3457
|
+
function slippageBpsToSdk(slippageBps) {
|
|
3458
|
+
if (!Number.isFinite(slippageBps))
|
|
3459
|
+
return slippageBps > 0 ? 9999n : 0n;
|
|
3460
|
+
const clamped = Math.max(0, Math.min(9999, Math.ceil(slippageBps)));
|
|
3461
|
+
return BigInt(clamped);
|
|
3462
|
+
}
|
|
3463
|
+
async function estimateLeverageBuySlippage(params) {
|
|
3464
|
+
const book = await getBook(params.tokenId);
|
|
3465
|
+
return estimateLeverageBuySlippageFromBook(book, params.collateralAmount);
|
|
3466
|
+
}
|
|
3467
|
+
function estimateLeverageBuySlippageFromBook(book, collateralAmount) {
|
|
3468
|
+
const estimate = estimateBuyImpact(book, collateralAmount);
|
|
3469
|
+
return {
|
|
3470
|
+
slippageBps: toSdkSlippageBps(estimate),
|
|
3471
|
+
rawSlippageBps: estimate.slippageBps,
|
|
3472
|
+
estimate
|
|
3473
|
+
};
|
|
3474
|
+
}
|
|
3475
|
+
async function estimateLeverageSellSlippage(params) {
|
|
3476
|
+
const book = await getBook(params.tokenId);
|
|
3477
|
+
return estimateLeverageSellSlippageFromBook(book, params.tokenAmount);
|
|
3478
|
+
}
|
|
3479
|
+
function estimateLeverageSellSlippageFromBook(book, tokenAmount) {
|
|
3480
|
+
const estimate = estimateSellImpact(book, tokenAmount);
|
|
3481
|
+
return {
|
|
3482
|
+
slippageBps: toSdkSlippageBps(estimate),
|
|
3483
|
+
rawSlippageBps: estimate.slippageBps,
|
|
3484
|
+
estimate
|
|
3485
|
+
};
|
|
3486
|
+
}
|
|
3309
3487
|
// src/bridge-client.ts
|
|
3310
3488
|
function buildQuery3(params) {
|
|
3311
3489
|
const q = new URLSearchParams;
|
|
@@ -3434,6 +3612,8 @@ export {
|
|
|
3434
3612
|
validateOrderParams,
|
|
3435
3613
|
twapFromHistory,
|
|
3436
3614
|
toTokenUnits,
|
|
3615
|
+
toSdkSlippageBps,
|
|
3616
|
+
slippageBpsToSdk,
|
|
3437
3617
|
roundUp,
|
|
3438
3618
|
roundToTickSize,
|
|
3439
3619
|
roundNormal,
|
|
@@ -3464,6 +3644,13 @@ export {
|
|
|
3464
3644
|
fromTokenUnits,
|
|
3465
3645
|
fetchJsonWithMeta,
|
|
3466
3646
|
fetchJson,
|
|
3647
|
+
estimateTradeImpact,
|
|
3648
|
+
estimateSellImpact,
|
|
3649
|
+
estimateLeverageSellSlippageFromBook,
|
|
3650
|
+
estimateLeverageSellSlippage,
|
|
3651
|
+
estimateLeverageBuySlippageFromBook,
|
|
3652
|
+
estimateLeverageBuySlippage,
|
|
3653
|
+
estimateBuyImpact,
|
|
3467
3654
|
erc20Abi,
|
|
3468
3655
|
decimalPlaces,
|
|
3469
3656
|
ctfExchangeAbi,
|
|
@@ -3475,8 +3662,10 @@ export {
|
|
|
3475
3662
|
createClobOrderBuilder,
|
|
3476
3663
|
createClobAuthManager,
|
|
3477
3664
|
configureClobClient,
|
|
3665
|
+
computeSlippageBps,
|
|
3478
3666
|
computePnlSummary,
|
|
3479
3667
|
computeOpenPnl,
|
|
3668
|
+
computeMidPrice,
|
|
3480
3669
|
computeClosedPnl,
|
|
3481
3670
|
collateralPriceFromMarkets,
|
|
3482
3671
|
classifyHttpStatus,
|
|
@@ -3484,6 +3673,7 @@ export {
|
|
|
3484
3673
|
classifyEndpoint,
|
|
3485
3674
|
chunk,
|
|
3486
3675
|
calculateAmounts,
|
|
3676
|
+
bookDepth,
|
|
3487
3677
|
VALIDATE_READONLY_API_KEY,
|
|
3488
3678
|
USER_PNL_LIMIT_PER_10S,
|
|
3489
3679
|
UPDATE_BALANCE_ALLOWANCE,
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bridge between `@varla/polymarket` trade impact estimates and
|
|
3
|
+
* `@varla/sdk/leverage` planner conventions (bigint bps).
|
|
4
|
+
*
|
|
5
|
+
* These helpers convert floating-point slippage estimates from the CLOB
|
|
6
|
+
* orderbook walker into the bigint `slippageBps` parameter expected by
|
|
7
|
+
* `planLeverageLoop()` and `planDeleverage()`.
|
|
8
|
+
*/
|
|
9
|
+
import type { TradeImpactEstimate } from "./trade-impact.js";
|
|
10
|
+
import type { ClobBookResponse } from "./types.js";
|
|
11
|
+
/**
|
|
12
|
+
* Convert a `TradeImpactEstimate.slippageBps` (float) to the bigint bps
|
|
13
|
+
* expected by `@varla/sdk/leverage` planners.
|
|
14
|
+
*
|
|
15
|
+
* - Rounds UP to be conservative (user assumes worst case).
|
|
16
|
+
* - Clamps to [0, 9999] — the SDK rejects >= 10_000.
|
|
17
|
+
* - Negative slippage (price improvement) is clamped to 0 because the
|
|
18
|
+
* planner models worst-case; positive slippage flows through at runtime.
|
|
19
|
+
*/
|
|
20
|
+
export declare function toSdkSlippageBps(estimate: TradeImpactEstimate): bigint;
|
|
21
|
+
/**
|
|
22
|
+
* Convert a raw float slippage bps value to SDK-compatible bigint.
|
|
23
|
+
* Same clamping rules as `toSdkSlippageBps`.
|
|
24
|
+
*/
|
|
25
|
+
export declare function slippageBpsToSdk(slippageBps: number): bigint;
|
|
26
|
+
export type LeverageSlippageEstimate = {
|
|
27
|
+
/** Slippage in bigint bps, ready to pass to `planLeverageLoop({ slippageBps })`. */
|
|
28
|
+
slippageBps: bigint;
|
|
29
|
+
/** Raw float slippage bps (may be negative for price improvement). */
|
|
30
|
+
rawSlippageBps: number;
|
|
31
|
+
/** Full trade impact estimate for inspection / UI display. */
|
|
32
|
+
estimate: TradeImpactEstimate;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Fetch the live orderbook and estimate buy slippage for a leverage loop.
|
|
36
|
+
*
|
|
37
|
+
* Use this to dynamically compute `slippageBps` from real market depth
|
|
38
|
+
* before calling `planLeverageLoop()`.
|
|
39
|
+
*
|
|
40
|
+
* @param tokenId - Polymarket position token ID.
|
|
41
|
+
* @param collateralAmount - Total collateral the leverage loop will spend buying tokens.
|
|
42
|
+
* @returns Slippage estimate with SDK-compatible bigint bps.
|
|
43
|
+
*/
|
|
44
|
+
export declare function estimateLeverageBuySlippage(params: {
|
|
45
|
+
tokenId: string;
|
|
46
|
+
collateralAmount: number;
|
|
47
|
+
}): Promise<LeverageSlippageEstimate>;
|
|
48
|
+
/**
|
|
49
|
+
* Same as `estimateLeverageBuySlippage` but accepts a pre-fetched book.
|
|
50
|
+
* Useful when you already have the book (e.g., from a WebSocket subscription).
|
|
51
|
+
*/
|
|
52
|
+
export declare function estimateLeverageBuySlippageFromBook(book: ClobBookResponse, collateralAmount: number): LeverageSlippageEstimate;
|
|
53
|
+
/**
|
|
54
|
+
* Fetch the live orderbook and estimate sell slippage for a deleverage loop.
|
|
55
|
+
*
|
|
56
|
+
* @param tokenId - Polymarket position token ID.
|
|
57
|
+
* @param tokenAmount - Total tokens the deleverage loop will sell.
|
|
58
|
+
* @returns Slippage estimate with SDK-compatible bigint bps.
|
|
59
|
+
*/
|
|
60
|
+
export declare function estimateLeverageSellSlippage(params: {
|
|
61
|
+
tokenId: string;
|
|
62
|
+
tokenAmount: number;
|
|
63
|
+
}): Promise<LeverageSlippageEstimate>;
|
|
64
|
+
/**
|
|
65
|
+
* Same as `estimateLeverageSellSlippage` but accepts a pre-fetched book.
|
|
66
|
+
*/
|
|
67
|
+
export declare function estimateLeverageSellSlippageFromBook(book: ClobBookResponse, tokenAmount: number): LeverageSlippageEstimate;
|
|
68
|
+
//# sourceMappingURL=leverage-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"leverage-adapter.d.ts","sourceRoot":"","sources":["../src/leverage-adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAMnD;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,mBAAmB,GAAG,MAAM,CAItE;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAI5D;AAMD,MAAM,MAAM,wBAAwB,GAAG;IACrC,oFAAoF;IACpF,WAAW,EAAE,MAAM,CAAC;IACpB,sEAAsE;IACtE,cAAc,EAAE,MAAM,CAAC;IACvB,8DAA8D;IAC9D,QAAQ,EAAE,mBAAmB,CAAC;CAC/B,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAsB,2BAA2B,CAAC,MAAM,EAAE;IACxD,OAAO,EAAE,MAAM,CAAC;IAChB,gBAAgB,EAAE,MAAM,CAAC;CAC1B,GAAG,OAAO,CAAC,wBAAwB,CAAC,CAGpC;AAED;;;GAGG;AACH,wBAAgB,mCAAmC,CACjD,IAAI,EAAE,gBAAgB,EACtB,gBAAgB,EAAE,MAAM,GACvB,wBAAwB,CAO1B;AAED;;;;;;GAMG;AACH,wBAAsB,4BAA4B,CAAC,MAAM,EAAE;IACzD,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;CACrB,GAAG,OAAO,CAAC,wBAAwB,CAAC,CAGpC;AAED;;GAEG;AACH,wBAAgB,oCAAoC,CAClD,IAAI,EAAE,gBAAgB,EACtB,WAAW,EAAE,MAAM,GAClB,wBAAwB,CAO1B"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trade impact & slippage estimation for Polymarket CLOB orderbooks.
|
|
3
|
+
*
|
|
4
|
+
* Pure functions — zero I/O, zero side-effects. They accept a `ClobBookResponse`
|
|
5
|
+
* (from `ClobClient.getBook()`) and return rich fill estimates including weighted
|
|
6
|
+
* average fill price, slippage in bps, per-level breakdown, and depth summaries.
|
|
7
|
+
*
|
|
8
|
+
* Slippage sign convention:
|
|
9
|
+
* - **Positive** = adverse (user pays more on buy / receives less on sell)
|
|
10
|
+
* - **Negative** = favorable (price improvement — user gets a better deal than mid)
|
|
11
|
+
* - **Zero** = fill exactly at mid
|
|
12
|
+
*/
|
|
13
|
+
import type { ClobBookResponse } from "./types.js";
|
|
14
|
+
/** A single fill at one price level. */
|
|
15
|
+
export type FillLevel = {
|
|
16
|
+
/** Price at this level. */
|
|
17
|
+
price: number;
|
|
18
|
+
/** Amount filled at this level (collateral for buys, tokens for sells). */
|
|
19
|
+
size: number;
|
|
20
|
+
/** Cumulative filled amount up to and including this level. */
|
|
21
|
+
cumulative: number;
|
|
22
|
+
};
|
|
23
|
+
/** Full trade impact estimate from walking the orderbook. */
|
|
24
|
+
export type TradeImpactEstimate = {
|
|
25
|
+
/** Trade direction. */
|
|
26
|
+
side: "buy" | "sell";
|
|
27
|
+
/** Weighted average fill price across all levels consumed. */
|
|
28
|
+
avgFillPrice: number;
|
|
29
|
+
/** Worst (furthest from mid) price level touched. */
|
|
30
|
+
worstPrice: number;
|
|
31
|
+
/** Mid price at time of estimation: (bestBid + bestAsk) / 2. */
|
|
32
|
+
midPrice: number;
|
|
33
|
+
/**
|
|
34
|
+
* Slippage from mid to avgFillPrice in basis points.
|
|
35
|
+
*
|
|
36
|
+
* - Positive = adverse (user loses value vs mid)
|
|
37
|
+
* - Negative = favorable (price improvement)
|
|
38
|
+
*/
|
|
39
|
+
slippageBps: number;
|
|
40
|
+
/** Total tokens acquired (buy) or collateral received (sell). */
|
|
41
|
+
outputAmount: number;
|
|
42
|
+
/** Total collateral spent (buy) or tokens sold (sell). */
|
|
43
|
+
inputAmount: number;
|
|
44
|
+
/** Whether the full requested amount can be filled from the book. */
|
|
45
|
+
isFilled: boolean;
|
|
46
|
+
/** Number of price levels consumed. */
|
|
47
|
+
levelsConsumed: number;
|
|
48
|
+
/** Per-level fill breakdown. */
|
|
49
|
+
fills: FillLevel[];
|
|
50
|
+
};
|
|
51
|
+
/** Summary of one side of the orderbook. */
|
|
52
|
+
export type BookDepthSummary = {
|
|
53
|
+
/** Total notional liquidity (collateral value). */
|
|
54
|
+
totalLiquidity: number;
|
|
55
|
+
/** Total share volume available. */
|
|
56
|
+
totalShares: number;
|
|
57
|
+
/** Number of price levels. */
|
|
58
|
+
levels: number;
|
|
59
|
+
/** Best (closest to mid) price. */
|
|
60
|
+
bestPrice: number;
|
|
61
|
+
/** Worst (furthest from mid) price. */
|
|
62
|
+
worstPrice: number;
|
|
63
|
+
/** Volume-weighted average price across all levels. */
|
|
64
|
+
weightedAvgPrice: number;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Compute the mid price from a CLOB book response.
|
|
68
|
+
*
|
|
69
|
+
* - If both sides have orders: `(bestBid + bestAsk) / 2`
|
|
70
|
+
* - If only one side: returns that side's best price
|
|
71
|
+
* - If empty: returns 0
|
|
72
|
+
*/
|
|
73
|
+
export declare function computeMidPrice(book: ClobBookResponse): number;
|
|
74
|
+
/**
|
|
75
|
+
* Compute slippage in basis points between a mid price and a fill price.
|
|
76
|
+
*
|
|
77
|
+
* For buys: slippage = (fillPrice - midPrice) / midPrice * 10_000
|
|
78
|
+
* → positive when you pay more than mid (adverse)
|
|
79
|
+
* → negative when you pay less than mid (favorable / price improvement)
|
|
80
|
+
*
|
|
81
|
+
* For sells: slippage = (midPrice - fillPrice) / midPrice * 10_000
|
|
82
|
+
* → positive when you receive less than mid (adverse)
|
|
83
|
+
* → negative when you receive more than mid (favorable)
|
|
84
|
+
*/
|
|
85
|
+
export declare function computeSlippageBps(midPrice: number, fillPrice: number, side: "buy" | "sell"): number;
|
|
86
|
+
/**
|
|
87
|
+
* Estimate the impact of buying tokens by spending a given collateral amount.
|
|
88
|
+
*
|
|
89
|
+
* Walks the ask side of the book (sorted ascending — best ask first).
|
|
90
|
+
* For each level, the collateral cost is `price × size`. The function consumes
|
|
91
|
+
* levels until `collateralAmount` is exhausted or the book runs out.
|
|
92
|
+
*
|
|
93
|
+
* @param book - CLOB book response (from `getBook()`).
|
|
94
|
+
* @param collateralAmount - Total collateral (e.g., USDC) to spend.
|
|
95
|
+
* @returns Full trade impact estimate.
|
|
96
|
+
*/
|
|
97
|
+
export declare function estimateBuyImpact(book: ClobBookResponse, collateralAmount: number): TradeImpactEstimate;
|
|
98
|
+
/**
|
|
99
|
+
* Estimate the impact of selling a given token amount.
|
|
100
|
+
*
|
|
101
|
+
* Walks the bid side of the book (sorted descending — best bid first).
|
|
102
|
+
* For each level, the function sells tokens until `tokenAmount` is exhausted.
|
|
103
|
+
*
|
|
104
|
+
* @param book - CLOB book response.
|
|
105
|
+
* @param tokenAmount - Total tokens to sell.
|
|
106
|
+
* @returns Full trade impact estimate.
|
|
107
|
+
*/
|
|
108
|
+
export declare function estimateSellImpact(book: ClobBookResponse, tokenAmount: number): TradeImpactEstimate;
|
|
109
|
+
/**
|
|
110
|
+
* Estimate trade impact for a buy or sell.
|
|
111
|
+
*
|
|
112
|
+
* @param book - CLOB book response.
|
|
113
|
+
* @param side - "buy" or "sell".
|
|
114
|
+
* @param amount - Collateral to spend (buy) or tokens to sell (sell).
|
|
115
|
+
*/
|
|
116
|
+
export declare function estimateTradeImpact(book: ClobBookResponse, side: "buy" | "sell", amount: number): TradeImpactEstimate;
|
|
117
|
+
/**
|
|
118
|
+
* Compute a summary of depth on one side of the book.
|
|
119
|
+
*
|
|
120
|
+
* @param book - CLOB book response.
|
|
121
|
+
* @param side - "bids" or "asks".
|
|
122
|
+
*/
|
|
123
|
+
export declare function bookDepth(book: ClobBookResponse, side: "bids" | "asks"): BookDepthSummary;
|
|
124
|
+
//# sourceMappingURL=trade-impact.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trade-impact.d.ts","sourceRoot":"","sources":["../src/trade-impact.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAiB,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAMlE,wCAAwC;AACxC,MAAM,MAAM,SAAS,GAAG;IACtB,2BAA2B;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,2EAA2E;IAC3E,IAAI,EAAE,MAAM,CAAC;IACb,+DAA+D;IAC/D,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,6DAA6D;AAC7D,MAAM,MAAM,mBAAmB,GAAG;IAChC,uBAAuB;IACvB,IAAI,EAAE,KAAK,GAAG,MAAM,CAAC;IACrB,8DAA8D;IAC9D,YAAY,EAAE,MAAM,CAAC;IACrB,qDAAqD;IACrD,UAAU,EAAE,MAAM,CAAC;IACnB,gEAAgE;IAChE,QAAQ,EAAE,MAAM,CAAC;IACjB;;;;;OAKG;IACH,WAAW,EAAE,MAAM,CAAC;IACpB,iEAAiE;IACjE,YAAY,EAAE,MAAM,CAAC;IACrB,0DAA0D;IAC1D,WAAW,EAAE,MAAM,CAAC;IACpB,qEAAqE;IACrE,QAAQ,EAAE,OAAO,CAAC;IAClB,uCAAuC;IACvC,cAAc,EAAE,MAAM,CAAC;IACvB,gCAAgC;IAChC,KAAK,EAAE,SAAS,EAAE,CAAC;CACpB,CAAC;AAEF,4CAA4C;AAC5C,MAAM,MAAM,gBAAgB,GAAG;IAC7B,mDAAmD;IACnD,cAAc,EAAE,MAAM,CAAC;IACvB,oCAAoC;IACpC,WAAW,EAAE,MAAM,CAAC;IACpB,8BAA8B;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,mCAAmC;IACnC,SAAS,EAAE,MAAM,CAAC;IAClB,uCAAuC;IACvC,UAAU,EAAE,MAAM,CAAC;IACnB,uDAAuD;IACvD,gBAAgB,EAAE,MAAM,CAAC;CAC1B,CAAC;AAiBF;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,gBAAgB,GAAG,MAAM,CAS9D;AAMD;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,KAAK,GAAG,MAAM,GACnB,MAAM,CAOR;AAMD;;;;;;;;;;GAUG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,gBAAgB,EACtB,gBAAgB,EAAE,MAAM,GACvB,mBAAmB,CA8CrB;AAMD;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,gBAAgB,EACtB,WAAW,EAAE,MAAM,GAClB,mBAAmB,CA4CrB;AAMD;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,gBAAgB,EACtB,IAAI,EAAE,KAAK,GAAG,MAAM,EACpB,MAAM,EAAE,MAAM,GACb,mBAAmB,CAErB;AAMD;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,gBAAgB,CAkCzF"}
|