@polymarbot/shared 0.3.8 → 0.4.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/execution-queue/manager.cjs +123 -0
- package/execution-queue/manager.d.cts +12 -0
- package/execution-queue/manager.d.ts +12 -0
- package/execution-queue/manager.js +98 -0
- package/index.cjs +1 -0
- package/index.d.cts +1 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +11 -1
- package/trade-strategy/normalize.cjs +50 -2
- package/trade-strategy/normalize.d.cts +6 -2
- package/trade-strategy/normalize.d.ts +6 -2
- package/trade-strategy/normalize.js +47 -1
|
@@ -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,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
package/index.d.cts
CHANGED
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polymarbot/shared",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
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",
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
};
|