@polymarbot/shared 0.3.8 → 0.4.1

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.
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/utils/execution-queue/manager.ts
21
+ var manager_exports = {};
22
+ __export(manager_exports, {
23
+ QueueManager: () => QueueManager
24
+ });
25
+ module.exports = __toCommonJS(manager_exports);
26
+
27
+ // src/utils/execution-queue/queue.ts
28
+ var ExecutionQueue = class {
29
+ queue = [];
30
+ processing = false;
31
+ /**
32
+ * Enqueue an async function to be executed
33
+ * @param fn - Async function to execute
34
+ * @returns Promise that resolves with the function's result
35
+ */
36
+ async enqueue(fn) {
37
+ return new Promise((resolve, reject) => {
38
+ this.queue.push({ fn, resolve, reject });
39
+ this.processQueue();
40
+ });
41
+ }
42
+ /**
43
+ * Process the queue sequentially
44
+ */
45
+ async processQueue() {
46
+ if (this.processing) return;
47
+ this.processing = true;
48
+ while (this.queue.length > 0) {
49
+ const task = this.queue.shift();
50
+ try {
51
+ const result = await task.fn();
52
+ task.resolve(result);
53
+ } catch (error) {
54
+ task.reject(error);
55
+ }
56
+ }
57
+ this.processing = false;
58
+ }
59
+ /**
60
+ * Get current queue length
61
+ */
62
+ get length() {
63
+ return this.queue.length;
64
+ }
65
+ /**
66
+ * Check if queue is currently processing
67
+ */
68
+ get isProcessing() {
69
+ return this.processing;
70
+ }
71
+ };
72
+
73
+ // src/utils/execution-queue/manager.ts
74
+ var queues = /* @__PURE__ */ new Map();
75
+ function cleanupInactiveQueues() {
76
+ for (const [id, queue] of queues.entries()) {
77
+ if (queue.length === 0 && !queue.isProcessing) {
78
+ queues.delete(id);
79
+ }
80
+ }
81
+ }
82
+ function getOrCreateQueue(id) {
83
+ cleanupInactiveQueues();
84
+ if (!queues.has(id)) {
85
+ queues.set(id, new ExecutionQueue());
86
+ }
87
+ return queues.get(id);
88
+ }
89
+ var QueueManager = {
90
+ /**
91
+ * Execute an operation in the queue identified by ID
92
+ * @param id - Unique identifier for the queue
93
+ * @param operation - Async operation to execute
94
+ * @returns Promise that resolves with the operation result
95
+ */
96
+ async execute(id, operation) {
97
+ const queue = getOrCreateQueue(id);
98
+ return queue.enqueue(operation);
99
+ },
100
+ /**
101
+ * Get current active queue count (for monitoring/debugging)
102
+ */
103
+ getActiveQueueCount() {
104
+ return queues.size;
105
+ },
106
+ /**
107
+ * Clear a specific queue by ID
108
+ * @param id - Queue ID to clear
109
+ */
110
+ clear(id) {
111
+ queues.delete(id);
112
+ },
113
+ /**
114
+ * Clear all queues (mainly for testing)
115
+ */
116
+ clearAll() {
117
+ queues.clear();
118
+ }
119
+ };
120
+ // Annotate the CommonJS export names for ESM import in node:
121
+ 0 && (module.exports = {
122
+ QueueManager
123
+ });
@@ -0,0 +1,12 @@
1
+ declare const QueueManager: {
2
+
3
+ execute<T>(id: string, operation: () => Promise<T>): Promise<T>;
4
+
5
+ getActiveQueueCount(): number;
6
+
7
+ clear(id: string): void;
8
+
9
+ clearAll(): void;
10
+ };
11
+
12
+ export { QueueManager };
@@ -0,0 +1,12 @@
1
+ declare const QueueManager: {
2
+
3
+ execute<T>(id: string, operation: () => Promise<T>): Promise<T>;
4
+
5
+ getActiveQueueCount(): number;
6
+
7
+ clear(id: string): void;
8
+
9
+ clearAll(): void;
10
+ };
11
+
12
+ export { QueueManager };
@@ -0,0 +1,98 @@
1
+ import "../chunk-JSBRDJBE.js";
2
+
3
+ // src/utils/execution-queue/queue.ts
4
+ var ExecutionQueue = class {
5
+ queue = [];
6
+ processing = false;
7
+ /**
8
+ * Enqueue an async function to be executed
9
+ * @param fn - Async function to execute
10
+ * @returns Promise that resolves with the function's result
11
+ */
12
+ async enqueue(fn) {
13
+ return new Promise((resolve, reject) => {
14
+ this.queue.push({ fn, resolve, reject });
15
+ this.processQueue();
16
+ });
17
+ }
18
+ /**
19
+ * Process the queue sequentially
20
+ */
21
+ async processQueue() {
22
+ if (this.processing) return;
23
+ this.processing = true;
24
+ while (this.queue.length > 0) {
25
+ const task = this.queue.shift();
26
+ try {
27
+ const result = await task.fn();
28
+ task.resolve(result);
29
+ } catch (error) {
30
+ task.reject(error);
31
+ }
32
+ }
33
+ this.processing = false;
34
+ }
35
+ /**
36
+ * Get current queue length
37
+ */
38
+ get length() {
39
+ return this.queue.length;
40
+ }
41
+ /**
42
+ * Check if queue is currently processing
43
+ */
44
+ get isProcessing() {
45
+ return this.processing;
46
+ }
47
+ };
48
+
49
+ // src/utils/execution-queue/manager.ts
50
+ var queues = /* @__PURE__ */ new Map();
51
+ function cleanupInactiveQueues() {
52
+ for (const [id, queue] of queues.entries()) {
53
+ if (queue.length === 0 && !queue.isProcessing) {
54
+ queues.delete(id);
55
+ }
56
+ }
57
+ }
58
+ function getOrCreateQueue(id) {
59
+ cleanupInactiveQueues();
60
+ if (!queues.has(id)) {
61
+ queues.set(id, new ExecutionQueue());
62
+ }
63
+ return queues.get(id);
64
+ }
65
+ var QueueManager = {
66
+ /**
67
+ * Execute an operation in the queue identified by ID
68
+ * @param id - Unique identifier for the queue
69
+ * @param operation - Async operation to execute
70
+ * @returns Promise that resolves with the operation result
71
+ */
72
+ async execute(id, operation) {
73
+ const queue = getOrCreateQueue(id);
74
+ return queue.enqueue(operation);
75
+ },
76
+ /**
77
+ * Get current active queue count (for monitoring/debugging)
78
+ */
79
+ getActiveQueueCount() {
80
+ return queues.size;
81
+ },
82
+ /**
83
+ * Clear a specific queue by ID
84
+ * @param id - Queue ID to clear
85
+ */
86
+ clear(id) {
87
+ queues.delete(id);
88
+ },
89
+ /**
90
+ * Clear all queues (mainly for testing)
91
+ */
92
+ clearAll() {
93
+ queues.clear();
94
+ }
95
+ };
96
+ export {
97
+ QueueManager
98
+ };
package/index.cjs CHANGED
@@ -1,8 +1,9 @@
1
1
  module.exports = {
2
2
  ...require('./basic.cjs'),
3
3
  ...require('./crypto.cjs'),
4
+ ...require('./execution-queue/manager.cjs'),
4
5
  ...require('./hash.cjs'),
5
- ...require('./loadEnv.cjs'),
6
+ ...require('./load-env.cjs'),
6
7
  ...require('./markets/types.cjs'),
7
8
  ...require('./markets/utils.cjs'),
8
9
  ...require('./relayer-client.cjs'),
package/index.d.cts CHANGED
@@ -1,7 +1,8 @@
1
1
  export * from './basic.js'
2
2
  export * from './crypto.js'
3
+ export * from './execution-queue/manager.js'
3
4
  export * from './hash.js'
4
- export * from './loadEnv.js'
5
+ export * from './load-env.js'
5
6
  export * from './markets/types.js'
6
7
  export * from './markets/utils.js'
7
8
  export * from './relayer-client.js'
package/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  export * from './basic.js'
2
2
  export * from './crypto.js'
3
+ export * from './execution-queue/manager.js'
3
4
  export * from './hash.js'
4
- export * from './loadEnv.js'
5
+ export * from './load-env.js'
5
6
  export * from './markets/types.js'
6
7
  export * from './markets/utils.js'
7
8
  export * from './relayer-client.js'
package/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  export * from './basic.js'
2
2
  export * from './crypto.js'
3
+ export * from './execution-queue/manager.js'
3
4
  export * from './hash.js'
4
- export * from './loadEnv.js'
5
+ export * from './load-env.js'
5
6
  export * from './markets/types.js'
6
7
  export * from './markets/utils.js'
7
8
  export * from './relayer-client.js'
@@ -17,12 +17,12 @@ var __copyProps = (to, from, except, desc) => {
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
- // src/utils/loadEnv.ts
21
- var loadEnv_exports = {};
22
- __export(loadEnv_exports, {
20
+ // src/utils/load-env.ts
21
+ var load_env_exports = {};
22
+ __export(load_env_exports, {
23
23
  loadEnv: () => loadEnv
24
24
  });
25
- module.exports = __toCommonJS(loadEnv_exports);
25
+ module.exports = __toCommonJS(load_env_exports);
26
26
  var import_node_fs = require("fs");
27
27
  var import_dotenv = require("dotenv");
28
28
  function loadEnv(cwd = process.cwd(), extraPaths = []) {
@@ -1,6 +1,6 @@
1
1
  import "./chunk-JSBRDJBE.js";
2
2
 
3
- // src/utils/loadEnv.ts
3
+ // src/utils/load-env.ts
4
4
  import { existsSync } from "fs";
5
5
  import { config as dotenvConfig } from "dotenv";
6
6
  function loadEnv(cwd = process.cwd(), extraPaths = []) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polymarbot/shared",
3
- "version": "0.3.8",
3
+ "version": "0.4.1",
4
4
  "type": "module",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",
@@ -36,6 +36,16 @@
36
36
  "default": "./crypto.cjs"
37
37
  }
38
38
  },
39
+ "./execution-queue/manager": {
40
+ "import": {
41
+ "types": "./execution-queue/manager.d.ts",
42
+ "default": "./execution-queue/manager.js"
43
+ },
44
+ "require": {
45
+ "types": "./execution-queue/manager.d.cts",
46
+ "default": "./execution-queue/manager.cjs"
47
+ }
48
+ },
39
49
  "./hash": {
40
50
  "import": {
41
51
  "types": "./hash.d.ts",
@@ -46,14 +56,14 @@
46
56
  "default": "./hash.cjs"
47
57
  }
48
58
  },
49
- "./loadEnv": {
59
+ "./load-env": {
50
60
  "import": {
51
- "types": "./loadEnv.d.ts",
52
- "default": "./loadEnv.js"
61
+ "types": "./load-env.d.ts",
62
+ "default": "./load-env.js"
53
63
  },
54
64
  "require": {
55
- "types": "./loadEnv.d.cts",
56
- "default": "./loadEnv.cjs"
65
+ "types": "./load-env.d.cts",
66
+ "default": "./load-env.cjs"
57
67
  }
58
68
  },
59
69
  "./markets/types": {
@@ -21,7 +21,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var normalize_exports = {};
22
22
  __export(normalize_exports, {
23
23
  hasTradeStepsBuy: () => hasTradeStepsBuy,
24
- normalizeStrategy: () => normalizeStrategy
24
+ normalizeStrategy: () => normalizeStrategy,
25
+ stringifyTradeStep: () => stringifyTradeStep,
26
+ stringifyTradeSteps: () => stringifyTradeSteps
25
27
  });
26
28
  module.exports = __toCommonJS(normalize_exports);
27
29
  function normalizeTradeStepBuy(step, options) {
@@ -141,8 +143,54 @@ function normalizeStrategy(config, options = {}) {
141
143
  function hasTradeStepsBuy(config) {
142
144
  return config.some((step) => step.side === "BUY" /* BUY */);
143
145
  }
146
+ function sortObjectKeys(obj) {
147
+ if (obj === null || typeof obj !== "object") {
148
+ return obj;
149
+ }
150
+ if (Array.isArray(obj)) {
151
+ return obj.map((item) => {
152
+ if (item !== null && typeof item === "object") {
153
+ return sortObjectKeys(item);
154
+ }
155
+ return item;
156
+ });
157
+ }
158
+ const sortedKeys = Object.keys(obj).sort();
159
+ const sortedObj = {};
160
+ for (const key of sortedKeys) {
161
+ const val = obj[key];
162
+ if (val !== null && typeof val === "object") {
163
+ sortedObj[key] = sortObjectKeys(val);
164
+ } else {
165
+ sortedObj[key] = val;
166
+ }
167
+ }
168
+ return sortedObj;
169
+ }
170
+ function stringifyTradeStep(step) {
171
+ return JSON.stringify(sortObjectKeys(step));
172
+ }
173
+ function stringifyTradeSteps(config) {
174
+ const processedSteps = config.map(
175
+ (step) => sortObjectKeys(step)
176
+ );
177
+ const sortedSteps = [...processedSteps].sort((a, b) => {
178
+ const sideA = a.side;
179
+ const sideB = b.side;
180
+ if (sideA === "BUY" /* BUY */ && sideB === "SELL" /* SELL */) {
181
+ return -1;
182
+ }
183
+ if (sideA === "SELL" /* SELL */ && sideB === "BUY" /* BUY */) {
184
+ return 1;
185
+ }
186
+ return JSON.stringify(a).localeCompare(JSON.stringify(b));
187
+ });
188
+ return JSON.stringify(sortedSteps);
189
+ }
144
190
  // Annotate the CommonJS export names for ESM import in node:
145
191
  0 && (module.exports = {
146
192
  hasTradeStepsBuy,
147
- normalizeStrategy
193
+ normalizeStrategy,
194
+ stringifyTradeStep,
195
+ stringifyTradeSteps
148
196
  });
@@ -1,4 +1,4 @@
1
- import { StrategyConfig } from './types.cjs';
1
+ import { StrategyConfig, TradeStep } from './types.cjs';
2
2
  import '@polymarket/clob-client';
3
3
  import '../markets/types.cjs';
4
4
 
@@ -13,4 +13,8 @@ declare function normalizeStrategy(config: StrategyConfig, options?: NormalizeSt
13
13
 
14
14
  declare function hasTradeStepsBuy(config: StrategyConfig): boolean;
15
15
 
16
- export { type NormalizeStrategyOptions, hasTradeStepsBuy, normalizeStrategy };
16
+ declare function stringifyTradeStep(step: TradeStep): string;
17
+
18
+ declare function stringifyTradeSteps(config: StrategyConfig): string;
19
+
20
+ export { type NormalizeStrategyOptions, hasTradeStepsBuy, normalizeStrategy, stringifyTradeStep, stringifyTradeSteps };
@@ -1,4 +1,4 @@
1
- import { StrategyConfig } from './types.js';
1
+ import { StrategyConfig, TradeStep } from './types.js';
2
2
  import '@polymarket/clob-client';
3
3
  import '../markets/types.js';
4
4
 
@@ -13,4 +13,8 @@ declare function normalizeStrategy(config: StrategyConfig, options?: NormalizeSt
13
13
 
14
14
  declare function hasTradeStepsBuy(config: StrategyConfig): boolean;
15
15
 
16
- export { type NormalizeStrategyOptions, hasTradeStepsBuy, normalizeStrategy };
16
+ declare function stringifyTradeStep(step: TradeStep): string;
17
+
18
+ declare function stringifyTradeSteps(config: StrategyConfig): string;
19
+
20
+ export { type NormalizeStrategyOptions, hasTradeStepsBuy, normalizeStrategy, stringifyTradeStep, stringifyTradeSteps };
@@ -119,7 +119,53 @@ function normalizeStrategy(config, options = {}) {
119
119
  function hasTradeStepsBuy(config) {
120
120
  return config.some((step) => step.side === "BUY" /* BUY */);
121
121
  }
122
+ function sortObjectKeys(obj) {
123
+ if (obj === null || typeof obj !== "object") {
124
+ return obj;
125
+ }
126
+ if (Array.isArray(obj)) {
127
+ return obj.map((item) => {
128
+ if (item !== null && typeof item === "object") {
129
+ return sortObjectKeys(item);
130
+ }
131
+ return item;
132
+ });
133
+ }
134
+ const sortedKeys = Object.keys(obj).sort();
135
+ const sortedObj = {};
136
+ for (const key of sortedKeys) {
137
+ const val = obj[key];
138
+ if (val !== null && typeof val === "object") {
139
+ sortedObj[key] = sortObjectKeys(val);
140
+ } else {
141
+ sortedObj[key] = val;
142
+ }
143
+ }
144
+ return sortedObj;
145
+ }
146
+ function stringifyTradeStep(step) {
147
+ return JSON.stringify(sortObjectKeys(step));
148
+ }
149
+ function stringifyTradeSteps(config) {
150
+ const processedSteps = config.map(
151
+ (step) => sortObjectKeys(step)
152
+ );
153
+ const sortedSteps = [...processedSteps].sort((a, b) => {
154
+ const sideA = a.side;
155
+ const sideB = b.side;
156
+ if (sideA === "BUY" /* BUY */ && sideB === "SELL" /* SELL */) {
157
+ return -1;
158
+ }
159
+ if (sideA === "SELL" /* SELL */ && sideB === "BUY" /* BUY */) {
160
+ return 1;
161
+ }
162
+ return JSON.stringify(a).localeCompare(JSON.stringify(b));
163
+ });
164
+ return JSON.stringify(sortedSteps);
165
+ }
122
166
  export {
123
167
  hasTradeStepsBuy,
124
- normalizeStrategy
168
+ normalizeStrategy,
169
+ stringifyTradeStep,
170
+ stringifyTradeSteps
125
171
  };
package/wallet.cjs CHANGED
@@ -65,7 +65,7 @@ async function getUSDCBalancesBatch(addresses) {
65
65
  return [];
66
66
  }
67
67
  const publicClient = getPublicClient();
68
- const multicallResults = await publicClient.multicall({
68
+ return await publicClient.multicall({
69
69
  contracts: addresses.map((address) => ({
70
70
  address: USDC_ADDRESS,
71
71
  abi: USDC_ABI,
@@ -74,9 +74,6 @@ async function getUSDCBalancesBatch(addresses) {
74
74
  })),
75
75
  allowFailure: true
76
76
  });
77
- return multicallResults.map(
78
- (result) => result.status === "success" ? result.result : 0n
79
- );
80
77
  }
81
78
  function isAddressEqual(a, b) {
82
79
  if (!a) return false;
package/wallet.d.cts CHANGED
@@ -20,7 +20,15 @@ declare function formatBalance(rawBalance: string | bigint, decimals?: number):
20
20
 
21
21
  declare function getUSDCBalance(address: string): Promise<bigint>;
22
22
 
23
- declare function getUSDCBalancesBatch(addresses: string[]): Promise<bigint[]>;
23
+ declare function getUSDCBalancesBatch(addresses: string[]): Promise<({
24
+ error?: undefined;
25
+ result: bigint;
26
+ status: "success";
27
+ } | {
28
+ error: Error;
29
+ result?: undefined;
30
+ status: "failure";
31
+ })[]>;
24
32
 
25
33
  declare function isAddressEqual(a?: Address | string | null, b?: Address | string | null): boolean;
26
34
 
package/wallet.d.ts CHANGED
@@ -20,7 +20,15 @@ declare function formatBalance(rawBalance: string | bigint, decimals?: number):
20
20
 
21
21
  declare function getUSDCBalance(address: string): Promise<bigint>;
22
22
 
23
- declare function getUSDCBalancesBatch(addresses: string[]): Promise<bigint[]>;
23
+ declare function getUSDCBalancesBatch(addresses: string[]): Promise<({
24
+ error?: undefined;
25
+ result: bigint;
26
+ status: "success";
27
+ } | {
28
+ error: Error;
29
+ result?: undefined;
30
+ status: "failure";
31
+ })[]>;
24
32
 
25
33
  declare function isAddressEqual(a?: Address | string | null, b?: Address | string | null): boolean;
26
34
 
package/wallet.js CHANGED
@@ -36,7 +36,7 @@ async function getUSDCBalancesBatch(addresses) {
36
36
  return [];
37
37
  }
38
38
  const publicClient = getPublicClient();
39
- const multicallResults = await publicClient.multicall({
39
+ return await publicClient.multicall({
40
40
  contracts: addresses.map((address) => ({
41
41
  address: USDC_ADDRESS,
42
42
  abi: USDC_ABI,
@@ -45,9 +45,6 @@ async function getUSDCBalancesBatch(addresses) {
45
45
  })),
46
46
  allowFailure: true
47
47
  });
48
- return multicallResults.map(
49
- (result) => result.status === "success" ? result.result : 0n
50
- );
51
48
  }
52
49
  function isAddressEqual(a, b) {
53
50
  if (!a) return false;
File without changes
File without changes