@varla/polymarket 2.6.1 → 3.0.2
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 +56 -0
- package/dist/clob-orders.d.ts.map +1 -1
- package/dist/clob-trading-client.d.ts.map +1 -1
- package/dist/data-api-client.d.ts +134 -43
- package/dist/data-api-client.d.ts.map +1 -1
- package/dist/gamma-client.d.ts +54 -11
- package/dist/gamma-client.d.ts.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +299 -40
- 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/dist/types.d.ts +104 -20
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1379,7 +1379,12 @@ function buildQuery(params) {
|
|
|
1379
1379
|
for (const [k, v] of Object.entries(params)) {
|
|
1380
1380
|
if (v === undefined)
|
|
1381
1381
|
continue;
|
|
1382
|
-
|
|
1382
|
+
if (Array.isArray(v)) {
|
|
1383
|
+
for (const item of v)
|
|
1384
|
+
q.append(k, item);
|
|
1385
|
+
} else {
|
|
1386
|
+
q.set(k, String(v));
|
|
1387
|
+
}
|
|
1383
1388
|
}
|
|
1384
1389
|
const s = q.toString();
|
|
1385
1390
|
return s ? `?${s}` : "";
|
|
@@ -1460,13 +1465,27 @@ class GammaClient {
|
|
|
1460
1465
|
}
|
|
1461
1466
|
});
|
|
1462
1467
|
}
|
|
1463
|
-
async listMarkets(params) {
|
|
1468
|
+
async listMarkets(params = {}) {
|
|
1464
1469
|
const url = `${this.baseUrl}/markets${buildQuery({
|
|
1465
1470
|
active: params.active,
|
|
1466
1471
|
closed: params.closed,
|
|
1467
1472
|
limit: params.limit ?? 100,
|
|
1468
1473
|
offset: params.offset ?? 0,
|
|
1469
|
-
|
|
1474
|
+
order: params.order,
|
|
1475
|
+
ascending: params.ascending,
|
|
1476
|
+
tag_id: params.tag_id,
|
|
1477
|
+
slug: params.slug,
|
|
1478
|
+
clob_token_ids: params.clob_token_ids,
|
|
1479
|
+
condition_ids: params.condition_ids,
|
|
1480
|
+
market_maker_address: params.market_maker_address,
|
|
1481
|
+
liquidity_num_min: params.liquidity_num_min,
|
|
1482
|
+
liquidity_num_max: params.liquidity_num_max,
|
|
1483
|
+
volume_num_min: params.volume_num_min,
|
|
1484
|
+
volume_num_max: params.volume_num_max,
|
|
1485
|
+
start_date_min: params.start_date_min,
|
|
1486
|
+
start_date_max: params.start_date_max,
|
|
1487
|
+
end_date_min: params.end_date_min,
|
|
1488
|
+
end_date_max: params.end_date_max
|
|
1470
1489
|
})}`;
|
|
1471
1490
|
return this.request(url);
|
|
1472
1491
|
}
|
|
@@ -1522,11 +1541,11 @@ class GammaClient {
|
|
|
1522
1541
|
return this.request(url);
|
|
1523
1542
|
}
|
|
1524
1543
|
async getRelatedTagsById(id) {
|
|
1525
|
-
const url = `${this.baseUrl}/tags/${encodeURIComponent(id)}/related`;
|
|
1544
|
+
const url = `${this.baseUrl}/tags/${encodeURIComponent(id)}/related-tags/tags`;
|
|
1526
1545
|
return this.request(url);
|
|
1527
1546
|
}
|
|
1528
1547
|
async getRelatedTagsBySlug(slug) {
|
|
1529
|
-
const url = `${this.baseUrl}/tags/slug/${encodeURIComponent(slug)}/related`;
|
|
1548
|
+
const url = `${this.baseUrl}/tags/slug/${encodeURIComponent(slug)}/related-tags/tags`;
|
|
1530
1549
|
return this.request(url);
|
|
1531
1550
|
}
|
|
1532
1551
|
async getSports() {
|
|
@@ -1535,7 +1554,7 @@ class GammaClient {
|
|
|
1535
1554
|
}
|
|
1536
1555
|
async listTeams(params = {}) {
|
|
1537
1556
|
const url = `${this.baseUrl}/teams${buildQuery({
|
|
1538
|
-
|
|
1557
|
+
league: params.league,
|
|
1539
1558
|
limit: params.limit ?? 100,
|
|
1540
1559
|
offset: params.offset ?? 0
|
|
1541
1560
|
})}`;
|
|
@@ -1553,15 +1572,20 @@ class GammaClient {
|
|
|
1553
1572
|
return this.request(url);
|
|
1554
1573
|
}
|
|
1555
1574
|
async search(query, params = {}) {
|
|
1556
|
-
const url = `${this.baseUrl}/search${buildQuery({
|
|
1575
|
+
const url = `${this.baseUrl}/public-search${buildQuery({
|
|
1557
1576
|
q: query,
|
|
1558
|
-
|
|
1559
|
-
|
|
1577
|
+
limit_per_type: params.limit_per_type ?? 20,
|
|
1578
|
+
page: params.page,
|
|
1579
|
+
events_status: params.events_status,
|
|
1580
|
+
sort: params.sort,
|
|
1581
|
+
ascending: params.ascending,
|
|
1582
|
+
search_tags: params.search_tags,
|
|
1583
|
+
search_profiles: params.search_profiles
|
|
1560
1584
|
})}`;
|
|
1561
1585
|
return this.request(url);
|
|
1562
1586
|
}
|
|
1563
1587
|
async healthCheck() {
|
|
1564
|
-
const url = `${this.baseUrl}
|
|
1588
|
+
const url = `${this.baseUrl}/status`;
|
|
1565
1589
|
return this.request(url);
|
|
1566
1590
|
}
|
|
1567
1591
|
async getEventTags(eventId) {
|
|
@@ -1573,11 +1597,11 @@ class GammaClient {
|
|
|
1573
1597
|
return this.request(url);
|
|
1574
1598
|
}
|
|
1575
1599
|
async getTagRelationshipsById(id) {
|
|
1576
|
-
const url = `${this.baseUrl}/tags/${encodeURIComponent(id)}/
|
|
1600
|
+
const url = `${this.baseUrl}/tags/${encodeURIComponent(id)}/related-tags`;
|
|
1577
1601
|
return this.request(url);
|
|
1578
1602
|
}
|
|
1579
1603
|
async getTagRelationshipsBySlug(slug) {
|
|
1580
|
-
const url = `${this.baseUrl}/tags/slug/${encodeURIComponent(slug)}/
|
|
1604
|
+
const url = `${this.baseUrl}/tags/slug/${encodeURIComponent(slug)}/related-tags`;
|
|
1581
1605
|
return this.request(url);
|
|
1582
1606
|
}
|
|
1583
1607
|
async getValidSportsMarketTypes() {
|
|
@@ -1586,8 +1610,10 @@ class GammaClient {
|
|
|
1586
1610
|
}
|
|
1587
1611
|
async listComments(params = {}) {
|
|
1588
1612
|
const url = `${this.baseUrl}/comments${buildQuery({
|
|
1589
|
-
|
|
1590
|
-
|
|
1613
|
+
parent_entity_type: params.parent_entity_type,
|
|
1614
|
+
parent_entity_id: params.parent_entity_id,
|
|
1615
|
+
get_positions: params.get_positions,
|
|
1616
|
+
holders_only: params.holders_only,
|
|
1591
1617
|
limit: params.limit ?? 100,
|
|
1592
1618
|
offset: params.offset ?? 0
|
|
1593
1619
|
})}`;
|
|
@@ -1598,7 +1624,7 @@ class GammaClient {
|
|
|
1598
1624
|
return this.request(url);
|
|
1599
1625
|
}
|
|
1600
1626
|
async getCommentsByUser(address, params = {}) {
|
|
1601
|
-
const url = `${this.baseUrl}/comments/
|
|
1627
|
+
const url = `${this.baseUrl}/comments/user_address/${encodeURIComponent(address)}${buildQuery({
|
|
1602
1628
|
limit: params.limit ?? 100,
|
|
1603
1629
|
offset: params.offset ?? 0
|
|
1604
1630
|
})}`;
|
|
@@ -2070,12 +2096,12 @@ function calculateAmounts(params, _options) {
|
|
|
2070
2096
|
return { makerAmount, takerAmount };
|
|
2071
2097
|
}
|
|
2072
2098
|
function generateSalt() {
|
|
2073
|
-
const bytes = new Uint8Array(
|
|
2099
|
+
const bytes = new Uint8Array(6);
|
|
2074
2100
|
crypto.getRandomValues(bytes);
|
|
2075
2101
|
return BigInt(`0x${Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("")}`).toString();
|
|
2076
2102
|
}
|
|
2077
2103
|
function generateNonce() {
|
|
2078
|
-
return
|
|
2104
|
+
return "0";
|
|
2079
2105
|
}
|
|
2080
2106
|
function sideToNumber(side) {
|
|
2081
2107
|
return side === "BUY" ? 0 : 1;
|
|
@@ -2256,7 +2282,12 @@ class ClobTradingClient {
|
|
|
2256
2282
|
orderType
|
|
2257
2283
|
});
|
|
2258
2284
|
const payload = {
|
|
2259
|
-
|
|
2285
|
+
deferExec: false,
|
|
2286
|
+
order: {
|
|
2287
|
+
...signedOrder,
|
|
2288
|
+
salt: Number(signedOrder.salt),
|
|
2289
|
+
signatureType: Number(signedOrder.signatureType)
|
|
2290
|
+
},
|
|
2260
2291
|
owner: this.creds.apiKey,
|
|
2261
2292
|
orderType
|
|
2262
2293
|
};
|
|
@@ -2960,9 +2991,10 @@ class DataApiClient {
|
|
|
2960
2991
|
});
|
|
2961
2992
|
}
|
|
2962
2993
|
async getCurrentPositions(params) {
|
|
2994
|
+
const market = Array.isArray(params.market) ? params.market.join(",") : params.market;
|
|
2963
2995
|
const url = `${this.baseUrl}/positions${buildQuery2({
|
|
2964
2996
|
user: params.address,
|
|
2965
|
-
market
|
|
2997
|
+
market,
|
|
2966
2998
|
eventId: params.eventId?.join(","),
|
|
2967
2999
|
sizeThreshold: params.sizeThreshold,
|
|
2968
3000
|
redeemable: params.redeemable,
|
|
@@ -2976,9 +3008,10 @@ class DataApiClient {
|
|
|
2976
3008
|
return this.request(url);
|
|
2977
3009
|
}
|
|
2978
3010
|
async getClosedPositions(params) {
|
|
3011
|
+
const market = Array.isArray(params.market) ? params.market.join(",") : params.market;
|
|
2979
3012
|
const url = `${this.baseUrl}/closed-positions${buildQuery2({
|
|
2980
3013
|
user: params.address,
|
|
2981
|
-
market
|
|
3014
|
+
market,
|
|
2982
3015
|
title: params.title,
|
|
2983
3016
|
eventId: params.eventId?.join(","),
|
|
2984
3017
|
sortBy: params.sortBy,
|
|
@@ -3005,56 +3038,96 @@ class DataApiClient {
|
|
|
3005
3038
|
}
|
|
3006
3039
|
return all;
|
|
3007
3040
|
}
|
|
3008
|
-
async getTotalPositionValue(address) {
|
|
3009
|
-
const
|
|
3041
|
+
async getTotalPositionValue(address, market) {
|
|
3042
|
+
const marketStr = Array.isArray(market) ? market.join(",") : market;
|
|
3043
|
+
const url = `${this.baseUrl}/value${buildQuery2({ user: address, market: marketStr })}`;
|
|
3010
3044
|
return this.request(url);
|
|
3011
3045
|
}
|
|
3012
3046
|
async getTrades(params) {
|
|
3047
|
+
const market = Array.isArray(params.market) ? params.market.join(",") : params.market;
|
|
3013
3048
|
const url = `${this.baseUrl}/trades${buildQuery2({
|
|
3014
3049
|
user: params.address,
|
|
3015
|
-
|
|
3050
|
+
market,
|
|
3051
|
+
eventId: params.eventId?.join(","),
|
|
3052
|
+
takerOnly: params.takerOnly,
|
|
3053
|
+
filterType: params.filterType,
|
|
3054
|
+
filterAmount: params.filterAmount,
|
|
3055
|
+
side: params.side,
|
|
3016
3056
|
limit: params.limit ?? 100,
|
|
3017
3057
|
offset: params.offset ?? 0
|
|
3018
3058
|
})}`;
|
|
3019
3059
|
return this.request(url);
|
|
3020
3060
|
}
|
|
3021
3061
|
async getUserActivity(params) {
|
|
3062
|
+
const market = Array.isArray(params.market) ? params.market.join(",") : params.market;
|
|
3022
3063
|
const url = `${this.baseUrl}/activity${buildQuery2({
|
|
3023
3064
|
user: params.address,
|
|
3065
|
+
market,
|
|
3066
|
+
eventId: params.eventId?.join(","),
|
|
3067
|
+
type: params.type?.join(","),
|
|
3068
|
+
start: params.start,
|
|
3069
|
+
end: params.end,
|
|
3070
|
+
sortBy: params.sortBy,
|
|
3071
|
+
sortDirection: params.sortDirection,
|
|
3072
|
+
side: params.side,
|
|
3024
3073
|
limit: params.limit ?? 100,
|
|
3025
3074
|
offset: params.offset ?? 0
|
|
3026
3075
|
})}`;
|
|
3027
3076
|
return this.request(url);
|
|
3028
3077
|
}
|
|
3029
3078
|
async getTopHolders(params) {
|
|
3079
|
+
const market = Array.isArray(params.market) ? params.market.join(",") : params.market;
|
|
3030
3080
|
const url = `${this.baseUrl}/holders${buildQuery2({
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
limit: params.limit ??
|
|
3081
|
+
market,
|
|
3082
|
+
minBalance: params.minBalance,
|
|
3083
|
+
limit: params.limit ?? 20
|
|
3034
3084
|
})}`;
|
|
3035
3085
|
return this.request(url);
|
|
3036
3086
|
}
|
|
3037
3087
|
async getTraderLeaderboard(params = {}) {
|
|
3038
|
-
const url = `${this.baseUrl}/leaderboard${buildQuery2({
|
|
3039
|
-
|
|
3088
|
+
const url = `${this.baseUrl}/v1/leaderboard${buildQuery2({
|
|
3089
|
+
timePeriod: params.timePeriod ?? "ALL",
|
|
3040
3090
|
category: params.category,
|
|
3041
|
-
|
|
3042
|
-
limit: params.limit ??
|
|
3043
|
-
offset: params.offset ?? 0
|
|
3091
|
+
orderBy: params.orderBy ?? "PNL",
|
|
3092
|
+
limit: params.limit ?? 25,
|
|
3093
|
+
offset: params.offset ?? 0,
|
|
3094
|
+
user: params.user,
|
|
3095
|
+
userName: params.userName
|
|
3044
3096
|
})}`;
|
|
3045
3097
|
return this.request(url);
|
|
3046
3098
|
}
|
|
3047
3099
|
async getOpenInterest(params = {}) {
|
|
3048
|
-
const
|
|
3049
|
-
|
|
3050
|
-
asset: params.asset
|
|
3051
|
-
})}`;
|
|
3100
|
+
const market = Array.isArray(params.market) ? params.market.join(",") : params.market;
|
|
3101
|
+
const url = `${this.baseUrl}/oi${buildQuery2({ market })}`;
|
|
3052
3102
|
return this.request(url);
|
|
3053
3103
|
}
|
|
3054
3104
|
async getLiveVolume(params) {
|
|
3055
|
-
const url = `${this.baseUrl}/volume${buildQuery2({
|
|
3056
|
-
|
|
3057
|
-
|
|
3105
|
+
const url = `${this.baseUrl}/live-volume${buildQuery2({ id: params.id })}`;
|
|
3106
|
+
return this.request(url);
|
|
3107
|
+
}
|
|
3108
|
+
async getMarketPositions(params) {
|
|
3109
|
+
const url = `${this.baseUrl}/v1/market-positions${buildQuery2({
|
|
3110
|
+
market: params.market,
|
|
3111
|
+
user: params.user,
|
|
3112
|
+
status: params.status,
|
|
3113
|
+
sortBy: params.sortBy,
|
|
3114
|
+
sortDirection: params.sortDirection,
|
|
3115
|
+
limit: params.limit ?? 50,
|
|
3116
|
+
offset: params.offset ?? 0
|
|
3117
|
+
})}`;
|
|
3118
|
+
return this.request(url);
|
|
3119
|
+
}
|
|
3120
|
+
async getBuilderLeaderboard(params = {}) {
|
|
3121
|
+
const url = `${this.baseUrl}/v1/builders/leaderboard${buildQuery2({
|
|
3122
|
+
timePeriod: params.timePeriod ?? "DAY",
|
|
3123
|
+
limit: params.limit ?? 25,
|
|
3124
|
+
offset: params.offset ?? 0
|
|
3125
|
+
})}`;
|
|
3126
|
+
return this.request(url);
|
|
3127
|
+
}
|
|
3128
|
+
async getBuilderVolume(params = {}) {
|
|
3129
|
+
const url = `${this.baseUrl}/v1/builders/volume${buildQuery2({
|
|
3130
|
+
timePeriod: params.timePeriod ?? "DAY"
|
|
3058
3131
|
})}`;
|
|
3059
3132
|
return this.request(url);
|
|
3060
3133
|
}
|
|
@@ -3093,11 +3166,11 @@ class DataApiClient {
|
|
|
3093
3166
|
return this.request(url);
|
|
3094
3167
|
}
|
|
3095
3168
|
async getTotalMarketsTraded(address) {
|
|
3096
|
-
const url = `${this.baseUrl}/
|
|
3169
|
+
const url = `${this.baseUrl}/traded${buildQuery2({ user: address })}`;
|
|
3097
3170
|
return this.request(url);
|
|
3098
3171
|
}
|
|
3099
3172
|
async getAccountingSnapshot(address) {
|
|
3100
|
-
const url = `${this.baseUrl}/accounting${buildQuery2({ user: address })}`;
|
|
3173
|
+
const url = `${this.baseUrl}/v1/accounting/snapshot${buildQuery2({ user: address })}`;
|
|
3101
3174
|
const response = await this.fetchFn(url, {
|
|
3102
3175
|
signal: AbortSignal.timeout(this.timeoutMs)
|
|
3103
3176
|
});
|
|
@@ -3233,6 +3306,180 @@ function collateralPriceFromMarkets(params) {
|
|
|
3233
3306
|
return bestBid;
|
|
3234
3307
|
return Math.min(bestBid, twap);
|
|
3235
3308
|
}
|
|
3309
|
+
// src/trade-impact.ts
|
|
3310
|
+
function parseLevel(level) {
|
|
3311
|
+
return {
|
|
3312
|
+
price: Number.parseFloat(level.price),
|
|
3313
|
+
size: Number.parseFloat(level.size)
|
|
3314
|
+
};
|
|
3315
|
+
}
|
|
3316
|
+
function computeMidPrice(book) {
|
|
3317
|
+
const bestBid = book.bids.length > 0 ? Number.parseFloat(book.bids[0].price) : null;
|
|
3318
|
+
const bestAsk = book.asks.length > 0 ? Number.parseFloat(book.asks[0].price) : null;
|
|
3319
|
+
if (bestBid !== null && bestAsk !== null)
|
|
3320
|
+
return (bestBid + bestAsk) / 2;
|
|
3321
|
+
if (bestBid !== null)
|
|
3322
|
+
return bestBid;
|
|
3323
|
+
if (bestAsk !== null)
|
|
3324
|
+
return bestAsk;
|
|
3325
|
+
return 0;
|
|
3326
|
+
}
|
|
3327
|
+
function computeSlippageBps(midPrice, fillPrice, side) {
|
|
3328
|
+
if (midPrice <= 0)
|
|
3329
|
+
return 0;
|
|
3330
|
+
if (side === "buy") {
|
|
3331
|
+
return (fillPrice - midPrice) / midPrice * 1e4;
|
|
3332
|
+
}
|
|
3333
|
+
return (midPrice - fillPrice) / midPrice * 1e4;
|
|
3334
|
+
}
|
|
3335
|
+
function estimateBuyImpact(book, collateralAmount) {
|
|
3336
|
+
const midPrice = computeMidPrice(book);
|
|
3337
|
+
const fills = [];
|
|
3338
|
+
let remaining = collateralAmount;
|
|
3339
|
+
let totalTokens = 0;
|
|
3340
|
+
let totalSpent = 0;
|
|
3341
|
+
for (const rawLevel of book.asks) {
|
|
3342
|
+
if (remaining <= 0)
|
|
3343
|
+
break;
|
|
3344
|
+
const { price, size } = parseLevel(rawLevel);
|
|
3345
|
+
if (price <= 0 || size <= 0)
|
|
3346
|
+
continue;
|
|
3347
|
+
const levelCost = price * size;
|
|
3348
|
+
const spend = Math.min(remaining, levelCost);
|
|
3349
|
+
const tokensBought = spend / price;
|
|
3350
|
+
totalSpent += spend;
|
|
3351
|
+
totalTokens += tokensBought;
|
|
3352
|
+
remaining -= spend;
|
|
3353
|
+
fills.push({
|
|
3354
|
+
price,
|
|
3355
|
+
size: tokensBought,
|
|
3356
|
+
cumulative: totalTokens
|
|
3357
|
+
});
|
|
3358
|
+
}
|
|
3359
|
+
const avgFillPrice = totalTokens > 0 ? totalSpent / totalTokens : 0;
|
|
3360
|
+
const worstPrice = fills.length > 0 ? fills[fills.length - 1].price : 0;
|
|
3361
|
+
const slippageBps = computeSlippageBps(midPrice, avgFillPrice, "buy");
|
|
3362
|
+
return {
|
|
3363
|
+
side: "buy",
|
|
3364
|
+
avgFillPrice,
|
|
3365
|
+
worstPrice,
|
|
3366
|
+
midPrice,
|
|
3367
|
+
slippageBps,
|
|
3368
|
+
outputAmount: totalTokens,
|
|
3369
|
+
inputAmount: totalSpent,
|
|
3370
|
+
isFilled: remaining <= 0.000000001,
|
|
3371
|
+
levelsConsumed: fills.length,
|
|
3372
|
+
fills
|
|
3373
|
+
};
|
|
3374
|
+
}
|
|
3375
|
+
function estimateSellImpact(book, tokenAmount) {
|
|
3376
|
+
const midPrice = computeMidPrice(book);
|
|
3377
|
+
const fills = [];
|
|
3378
|
+
let remaining = tokenAmount;
|
|
3379
|
+
let totalCollateral = 0;
|
|
3380
|
+
let totalSold = 0;
|
|
3381
|
+
for (const rawLevel of book.bids) {
|
|
3382
|
+
if (remaining <= 0)
|
|
3383
|
+
break;
|
|
3384
|
+
const { price, size } = parseLevel(rawLevel);
|
|
3385
|
+
if (price <= 0 || size <= 0)
|
|
3386
|
+
continue;
|
|
3387
|
+
const sellSize = Math.min(remaining, size);
|
|
3388
|
+
const proceeds = sellSize * price;
|
|
3389
|
+
totalSold += sellSize;
|
|
3390
|
+
totalCollateral += proceeds;
|
|
3391
|
+
remaining -= sellSize;
|
|
3392
|
+
fills.push({
|
|
3393
|
+
price,
|
|
3394
|
+
size: sellSize,
|
|
3395
|
+
cumulative: totalSold
|
|
3396
|
+
});
|
|
3397
|
+
}
|
|
3398
|
+
const avgFillPrice = totalSold > 0 ? totalCollateral / totalSold : 0;
|
|
3399
|
+
const worstPrice = fills.length > 0 ? fills[fills.length - 1].price : 0;
|
|
3400
|
+
const slippageBps = computeSlippageBps(midPrice, avgFillPrice, "sell");
|
|
3401
|
+
return {
|
|
3402
|
+
side: "sell",
|
|
3403
|
+
avgFillPrice,
|
|
3404
|
+
worstPrice,
|
|
3405
|
+
midPrice,
|
|
3406
|
+
slippageBps,
|
|
3407
|
+
outputAmount: totalCollateral,
|
|
3408
|
+
inputAmount: totalSold,
|
|
3409
|
+
isFilled: remaining <= 0.000000001,
|
|
3410
|
+
levelsConsumed: fills.length,
|
|
3411
|
+
fills
|
|
3412
|
+
};
|
|
3413
|
+
}
|
|
3414
|
+
function estimateTradeImpact(book, side, amount) {
|
|
3415
|
+
return side === "buy" ? estimateBuyImpact(book, amount) : estimateSellImpact(book, amount);
|
|
3416
|
+
}
|
|
3417
|
+
function bookDepth(book, side) {
|
|
3418
|
+
const levels = book[side];
|
|
3419
|
+
if (levels.length === 0) {
|
|
3420
|
+
return {
|
|
3421
|
+
totalLiquidity: 0,
|
|
3422
|
+
totalShares: 0,
|
|
3423
|
+
levels: 0,
|
|
3424
|
+
bestPrice: 0,
|
|
3425
|
+
worstPrice: 0,
|
|
3426
|
+
weightedAvgPrice: 0
|
|
3427
|
+
};
|
|
3428
|
+
}
|
|
3429
|
+
let totalLiquidity = 0;
|
|
3430
|
+
let totalShares = 0;
|
|
3431
|
+
for (const rawLevel of levels) {
|
|
3432
|
+
const { price, size } = parseLevel(rawLevel);
|
|
3433
|
+
totalShares += size;
|
|
3434
|
+
totalLiquidity += price * size;
|
|
3435
|
+
}
|
|
3436
|
+
const first = parseLevel(levels[0]);
|
|
3437
|
+
const last = parseLevel(levels[levels.length - 1]);
|
|
3438
|
+
const weightedAvgPrice = totalShares > 0 ? totalLiquidity / totalShares : 0;
|
|
3439
|
+
return {
|
|
3440
|
+
totalLiquidity,
|
|
3441
|
+
totalShares,
|
|
3442
|
+
levels: levels.length,
|
|
3443
|
+
bestPrice: first.price,
|
|
3444
|
+
worstPrice: last.price,
|
|
3445
|
+
weightedAvgPrice
|
|
3446
|
+
};
|
|
3447
|
+
}
|
|
3448
|
+
// src/leverage-adapter.ts
|
|
3449
|
+
function toSdkSlippageBps(estimate) {
|
|
3450
|
+
const clamped = Math.max(0, Math.min(9999, Math.ceil(estimate.slippageBps)));
|
|
3451
|
+
return BigInt(clamped);
|
|
3452
|
+
}
|
|
3453
|
+
function slippageBpsToSdk(slippageBps) {
|
|
3454
|
+
if (!Number.isFinite(slippageBps))
|
|
3455
|
+
return slippageBps > 0 ? 9999n : 0n;
|
|
3456
|
+
const clamped = Math.max(0, Math.min(9999, Math.ceil(slippageBps)));
|
|
3457
|
+
return BigInt(clamped);
|
|
3458
|
+
}
|
|
3459
|
+
async function estimateLeverageBuySlippage(params) {
|
|
3460
|
+
const book = await getBook(params.tokenId);
|
|
3461
|
+
return estimateLeverageBuySlippageFromBook(book, params.collateralAmount);
|
|
3462
|
+
}
|
|
3463
|
+
function estimateLeverageBuySlippageFromBook(book, collateralAmount) {
|
|
3464
|
+
const estimate = estimateBuyImpact(book, collateralAmount);
|
|
3465
|
+
return {
|
|
3466
|
+
slippageBps: toSdkSlippageBps(estimate),
|
|
3467
|
+
rawSlippageBps: estimate.slippageBps,
|
|
3468
|
+
estimate
|
|
3469
|
+
};
|
|
3470
|
+
}
|
|
3471
|
+
async function estimateLeverageSellSlippage(params) {
|
|
3472
|
+
const book = await getBook(params.tokenId);
|
|
3473
|
+
return estimateLeverageSellSlippageFromBook(book, params.tokenAmount);
|
|
3474
|
+
}
|
|
3475
|
+
function estimateLeverageSellSlippageFromBook(book, tokenAmount) {
|
|
3476
|
+
const estimate = estimateSellImpact(book, tokenAmount);
|
|
3477
|
+
return {
|
|
3478
|
+
slippageBps: toSdkSlippageBps(estimate),
|
|
3479
|
+
rawSlippageBps: estimate.slippageBps,
|
|
3480
|
+
estimate
|
|
3481
|
+
};
|
|
3482
|
+
}
|
|
3236
3483
|
// src/bridge-client.ts
|
|
3237
3484
|
function buildQuery3(params) {
|
|
3238
3485
|
const q = new URLSearchParams;
|
|
@@ -3361,6 +3608,8 @@ export {
|
|
|
3361
3608
|
validateOrderParams,
|
|
3362
3609
|
twapFromHistory,
|
|
3363
3610
|
toTokenUnits,
|
|
3611
|
+
toSdkSlippageBps,
|
|
3612
|
+
slippageBpsToSdk,
|
|
3364
3613
|
roundUp,
|
|
3365
3614
|
roundToTickSize,
|
|
3366
3615
|
roundNormal,
|
|
@@ -3391,6 +3640,13 @@ export {
|
|
|
3391
3640
|
fromTokenUnits,
|
|
3392
3641
|
fetchJsonWithMeta,
|
|
3393
3642
|
fetchJson,
|
|
3643
|
+
estimateTradeImpact,
|
|
3644
|
+
estimateSellImpact,
|
|
3645
|
+
estimateLeverageSellSlippageFromBook,
|
|
3646
|
+
estimateLeverageSellSlippage,
|
|
3647
|
+
estimateLeverageBuySlippageFromBook,
|
|
3648
|
+
estimateLeverageBuySlippage,
|
|
3649
|
+
estimateBuyImpact,
|
|
3394
3650
|
erc20Abi,
|
|
3395
3651
|
decimalPlaces,
|
|
3396
3652
|
ctfExchangeAbi,
|
|
@@ -3402,8 +3658,10 @@ export {
|
|
|
3402
3658
|
createClobOrderBuilder,
|
|
3403
3659
|
createClobAuthManager,
|
|
3404
3660
|
configureClobClient,
|
|
3661
|
+
computeSlippageBps,
|
|
3405
3662
|
computePnlSummary,
|
|
3406
3663
|
computeOpenPnl,
|
|
3664
|
+
computeMidPrice,
|
|
3407
3665
|
computeClosedPnl,
|
|
3408
3666
|
collateralPriceFromMarkets,
|
|
3409
3667
|
classifyHttpStatus,
|
|
@@ -3411,6 +3669,7 @@ export {
|
|
|
3411
3669
|
classifyEndpoint,
|
|
3412
3670
|
chunk,
|
|
3413
3671
|
calculateAmounts,
|
|
3672
|
+
bookDepth,
|
|
3414
3673
|
VALIDATE_READONLY_API_KEY,
|
|
3415
3674
|
USER_PNL_LIMIT_PER_10S,
|
|
3416
3675
|
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"}
|