@varla/polymarket 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @varla/polymarket
2
2
 
3
+ ## 2.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 24360c5: Added utility exports for decimal parsing, TWAP calculation, and pricing helpers.
8
+
9
+ - Exported new utilities: parseDecimalToBigint, twapFromHistory, probToE8, and collateralPriceFromMarkets,- Added deterministic decimal parsing, TWAP computation, and collateral pricing helpers for market data workflows
10
+
11
+ ### Patch Changes
12
+
13
+ - af66ffb: No functional changes; package metadata formatting was adjusted.
14
+
15
+ - Reformat the package.json files array without altering published contents
16
+
17
+ ## 2.0.1
18
+
19
+ ### Patch Changes
20
+
21
+ - eb1325e: No functional changes; package metadata formatting was updated.
22
+
23
+ - No code or API changes in this release
24
+
3
25
  ## 2.0.0
4
26
 
5
27
  ### Major Changes
package/dist/index.d.ts CHANGED
@@ -15,5 +15,8 @@ export * from "./clob-ws.js";
15
15
  export * from "./clob-errors.js";
16
16
  export * from "./data-api-client.js";
17
17
  export * from "./pnl.js";
18
+ export { parseDecimalToBigint } from "./utils/decimal.js";
19
+ export { twapFromHistory } from "./utils/twap.js";
20
+ export { probToE8, collateralPriceFromMarkets } from "./utils/pricing.js";
18
21
  export * from "./bridge-client.js";
19
22
  //# sourceMappingURL=index.d.ts.map
@@ -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,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,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;AAM1E,cAAc,oBAAoB,CAAC"}
package/dist/index.js CHANGED
@@ -2840,6 +2840,97 @@ function computePnlSummary(openPositions, closedPositions) {
2840
2840
  openCount: openPositions.length
2841
2841
  };
2842
2842
  }
2843
+ // src/utils/decimal.ts
2844
+ function parseDecimalToBigint(value, decimals) {
2845
+ if (!Number.isInteger(decimals) || decimals < 0 || decimals > 77) {
2846
+ throw new Error(`invalid decimals=${decimals}`);
2847
+ }
2848
+ const s = value.trim();
2849
+ if (!s)
2850
+ throw new Error("empty decimal string");
2851
+ if (s.includes("e") || s.includes("E")) {
2852
+ throw new Error(`scientific notation not supported: ${value}`);
2853
+ }
2854
+ const neg = s.startsWith("-");
2855
+ const raw = neg ? s.slice(1) : s;
2856
+ const parts = raw.split(".");
2857
+ if (parts.length > 2)
2858
+ throw new Error(`invalid decimal: ${value}`);
2859
+ const whole = parts[0] ?? "0";
2860
+ const frac = parts[1] ?? "";
2861
+ if (!/^\d+$/.test(whole))
2862
+ throw new Error(`invalid decimal: ${value}`);
2863
+ if (frac && !/^\d+$/.test(frac))
2864
+ throw new Error(`invalid decimal: ${value}`);
2865
+ const fracPadded = (frac + "0".repeat(decimals)).slice(0, decimals);
2866
+ const n = BigInt(whole) * 10n ** BigInt(decimals) + (fracPadded ? BigInt(fracPadded) : 0n);
2867
+ return neg ? -n : n;
2868
+ }
2869
+ // src/utils/twap.ts
2870
+ function twapFromHistory(params) {
2871
+ const { history, startTs, endTs } = params;
2872
+ if (endTs <= startTs)
2873
+ return 0;
2874
+ if (!history.length)
2875
+ return 0;
2876
+ const xs = history.filter((x) => Number.isFinite(x.t) && Number.isFinite(x.p)).sort((a, b) => a.t - b.t);
2877
+ if (!xs.length)
2878
+ return 0;
2879
+ let curP = 0;
2880
+ for (let i = xs.length - 1;i >= 0; i--) {
2881
+ const x = xs[i];
2882
+ if (!x)
2883
+ continue;
2884
+ if (x.t <= startTs) {
2885
+ curP = x.p;
2886
+ break;
2887
+ }
2888
+ }
2889
+ if (curP === 0 && xs.length > 0) {
2890
+ const first = xs[0];
2891
+ if (first)
2892
+ curP = first.p;
2893
+ }
2894
+ let weighted = 0;
2895
+ let curT = startTs;
2896
+ for (const pt of xs) {
2897
+ const t = pt.t;
2898
+ if (t <= startTs) {
2899
+ curP = pt.p;
2900
+ continue;
2901
+ }
2902
+ if (t >= endTs)
2903
+ break;
2904
+ const dt = t - curT;
2905
+ if (dt > 0)
2906
+ weighted += curP * dt;
2907
+ curT = t;
2908
+ curP = pt.p;
2909
+ }
2910
+ const tail = endTs - curT;
2911
+ if (tail > 0)
2912
+ weighted += curP * tail;
2913
+ return weighted / (endTs - startTs);
2914
+ }
2915
+ // src/utils/pricing.ts
2916
+ function clamp(x, lo, hi) {
2917
+ return Math.min(Math.max(x, lo), hi);
2918
+ }
2919
+ function probToE8(p) {
2920
+ const clamped = clamp(p, 0, 1);
2921
+ return BigInt(Math.round(clamped * 1e8));
2922
+ }
2923
+ function collateralPriceFromMarkets(params) {
2924
+ const bestBid = clamp(params.bestBid, 0, 1);
2925
+ const twap = clamp(params.twap, 0, 1);
2926
+ if (bestBid <= 0 && twap <= 0)
2927
+ return 0;
2928
+ if (bestBid <= 0)
2929
+ return twap;
2930
+ if (twap <= 0)
2931
+ return bestBid;
2932
+ return Math.min(bestBid, twap);
2933
+ }
2843
2934
  // src/bridge-client.ts
2844
2935
  function buildQuery3(params) {
2845
2936
  const q = new URLSearchParams;
@@ -2966,6 +3057,7 @@ class BridgeClient {
2966
3057
  }
2967
3058
  export {
2968
3059
  validateOrderParams,
3060
+ twapFromHistory,
2969
3061
  toTokenUnits,
2970
3062
  roundUp,
2971
3063
  roundToTickSize,
@@ -2974,8 +3066,10 @@ export {
2974
3066
  resolveBinaryMarketsByConditionId,
2975
3067
  resolveBinaryMarketFromGammaMarket,
2976
3068
  resetClobClient,
3069
+ probToE8,
2977
3070
  priceValid,
2978
3071
  parseRetryAfter,
3072
+ parseDecimalToBigint,
2979
3073
  isValidPrice,
2980
3074
  isTickSizeSmaller,
2981
3075
  isTerminalStatus,
@@ -3006,6 +3100,7 @@ export {
3006
3100
  computePnlSummary,
3007
3101
  computeOpenPnl,
3008
3102
  computeClosedPnl,
3103
+ collateralPriceFromMarkets,
3009
3104
  classifyHttpStatus,
3010
3105
  classifyError,
3011
3106
  chunk,
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Deterministic decimal string → bigint parsing.
3
+ *
4
+ * General-purpose utility for converting human-readable decimal strings
5
+ * (e.g. CLOB prices "0.51", sizes "1000.123456") into fixed-point bigints
6
+ * with a specified number of fractional digits.
7
+ *
8
+ * No floating-point arithmetic is used — the conversion is purely string-based
9
+ * to avoid precision loss.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * parseDecimalToBigint("0.5", 8) // 50_000_000n
14
+ * parseDecimalToBigint("1", 8) // 100_000_000n
15
+ * parseDecimalToBigint("100.0", 6) // 100_000_000n
16
+ * ```
17
+ */
18
+ export declare function parseDecimalToBigint(value: string, decimals: number): bigint;
19
+ //# sourceMappingURL=decimal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decimal.d.ts","sourceRoot":"","sources":["../../src/utils/decimal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAuB5E"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Pure pricing utilities for prediction-market collateral valuation.
3
+ *
4
+ * These functions convert between probability space [0, 1] and the on-chain
5
+ * E8 fixed-point representation used by VarlaOracle (1e8 = 100%).
6
+ */
7
+ /**
8
+ * Convert a [0, 1] probability to oracle E8 units (1e8 = 100%).
9
+ *
10
+ * Clamps input to [0, 1] and rounds to nearest integer.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * probToE8(0.5) // 50_000_000n
15
+ * probToE8(1.0) // 100_000_000n
16
+ * probToE8(0) // 0n
17
+ * ```
18
+ */
19
+ export declare function probToE8(p: number): bigint;
20
+ /**
21
+ * Conservative lending collateral price for risk management.
22
+ *
23
+ * Returns `min(bestBid, twap)` clamped to [0, 1], with fallback logic
24
+ * when one side is unavailable (zero).
25
+ *
26
+ * **Why bestBid?** In a lending protocol, collateral must be valued at the
27
+ * price it can be liquidated at — the best bid (what buyers will pay NOW).
28
+ * Using mid or ask enables manipulation via thin ask orders.
29
+ *
30
+ * **TWAP smoothing** prevents flash bid spikes from inflating collateral value.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * collateralPriceFromMarkets({ bestBid: 0.60, twap: 0.65 }) // 0.60
35
+ * collateralPriceFromMarkets({ bestBid: 0.70, twap: 0.65 }) // 0.65
36
+ * collateralPriceFromMarkets({ bestBid: 0, twap: 0.50 }) // 0.50 (fallback)
37
+ * ```
38
+ */
39
+ export declare function collateralPriceFromMarkets(params: {
40
+ bestBid: number;
41
+ twap: number;
42
+ }): number;
43
+ //# sourceMappingURL=pricing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pricing.d.ts","sourceRoot":"","sources":["../../src/utils/pricing.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAG1C;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,0BAA0B,CAAC,MAAM,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAS5F"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Time-Weighted Average Price (TWAP) computation.
3
+ *
4
+ * Computes TWAP over a `{t, p}[]` history series using step-function semantics:
5
+ * price `p[i]` is assumed to hold constant until the next data point `p[i+1]`.
6
+ *
7
+ * Works with any Polymarket CLOB prices-history response shape.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const history = [
12
+ * { t: 1000, p: 0.5 },
13
+ * { t: 2000, p: 0.6 },
14
+ * { t: 3000, p: 0.55 },
15
+ * ];
16
+ * twapFromHistory({ history, startTs: 1000, endTs: 4000 });
17
+ * // → time-weighted average over the window
18
+ * ```
19
+ */
20
+ export declare function twapFromHistory(params: {
21
+ history: Array<{
22
+ t: number;
23
+ p: number;
24
+ }>;
25
+ startTs: number;
26
+ endTs: number;
27
+ }): number;
28
+ //# sourceMappingURL=twap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"twap.d.ts","sourceRoot":"","sources":["../../src/utils/twap.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE;IACtC,OAAO,EAAE,KAAK,CAAC;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACzC,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;CACf,GAAG,MAAM,CA+CT"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varla/polymarket",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "High-performance Polymarket SDK",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",