@polymarbot/shared 0.1.0 → 0.1.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.
package/basic.cjs CHANGED
@@ -33,7 +33,6 @@ function safeJsonParse(str, defaultValue) {
33
33
  try {
34
34
  return JSON.parse(str);
35
35
  } catch (e) {
36
- console.error(e);
37
36
  return defaultValue ?? {};
38
37
  }
39
38
  }
package/basic.d.cts CHANGED
@@ -1,34 +1,9 @@
1
- /**
2
- * 等待指定毫秒
3
- * @param ms
4
- * @returns {Promise<any>}
5
- */
6
1
  declare function sleep(ms: number): Promise<void>;
7
- /**
8
- * 安全解析Json
9
- * @param str - 需要解析的字符串
10
- * @param defaultValue - 解析失败时的默认值
11
- */
2
+
12
3
  declare function safeJsonParse<T extends object | unknown[]>(str?: string, defaultValue?: T): T;
13
- /**
14
- * 判断字符串是否为 URL
15
- * @param str - 需要判断的字符串
16
- * @returns 是否为 URL
17
- */
4
+
18
5
  declare function isUrl(str: string): boolean;
19
- /**
20
- * 将对象序列化为稳定的 JSON 字符串
21
- * 通过深度遍历对象,对所有 key 进行排序,确保相同值的对象序列化结果一致
22
- *
23
- * 处理策略:
24
- * - 对象:按 key 字母序排序
25
- * - 数组:当 sortArray 为 true 时,对基本类型直接排序,对对象类型先序列化再排序;为 false 时保持原顺序
26
- * - 从底层向上递归处理,确保嵌套结构都被正确处理
27
- *
28
- * @param value - 需要序列化的值
29
- * @param sortArray - 是否对数组进行排序,默认为 false
30
- * @returns 稳定的 JSON 字符串
31
- */
6
+
32
7
  declare function stableStringify(value: unknown, sortArray?: boolean): string;
33
8
 
34
9
  export { isUrl, safeJsonParse, sleep, stableStringify };
package/basic.d.ts CHANGED
@@ -1,34 +1,9 @@
1
- /**
2
- * 等待指定毫秒
3
- * @param ms
4
- * @returns {Promise<any>}
5
- */
6
1
  declare function sleep(ms: number): Promise<void>;
7
- /**
8
- * 安全解析Json
9
- * @param str - 需要解析的字符串
10
- * @param defaultValue - 解析失败时的默认值
11
- */
2
+
12
3
  declare function safeJsonParse<T extends object | unknown[]>(str?: string, defaultValue?: T): T;
13
- /**
14
- * 判断字符串是否为 URL
15
- * @param str - 需要判断的字符串
16
- * @returns 是否为 URL
17
- */
4
+
18
5
  declare function isUrl(str: string): boolean;
19
- /**
20
- * 将对象序列化为稳定的 JSON 字符串
21
- * 通过深度遍历对象,对所有 key 进行排序,确保相同值的对象序列化结果一致
22
- *
23
- * 处理策略:
24
- * - 对象:按 key 字母序排序
25
- * - 数组:当 sortArray 为 true 时,对基本类型直接排序,对对象类型先序列化再排序;为 false 时保持原顺序
26
- * - 从底层向上递归处理,确保嵌套结构都被正确处理
27
- *
28
- * @param value - 需要序列化的值
29
- * @param sortArray - 是否对数组进行排序,默认为 false
30
- * @returns 稳定的 JSON 字符串
31
- */
6
+
32
7
  declare function stableStringify(value: unknown, sortArray?: boolean): string;
33
8
 
34
9
  export { isUrl, safeJsonParse, sleep, stableStringify };
package/basic.js CHANGED
@@ -6,7 +6,6 @@ function safeJsonParse(str, defaultValue) {
6
6
  try {
7
7
  return JSON.parse(str);
8
8
  } catch (e) {
9
- console.error(e);
10
9
  return defaultValue ?? {};
11
10
  }
12
11
  }
package/package.json CHANGED
@@ -1,35 +1,60 @@
1
1
  {
2
2
  "name": "@polymarbot/shared",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "main": "./basic.cjs",
6
6
  "module": "./basic.js",
7
7
  "types": "./basic.d.ts",
8
8
  "exports": {
9
9
  "./basic": {
10
- "import": "./basic.js",
11
- "require": "./basic.cjs",
12
- "types": "./basic.d.ts"
10
+ "import": {
11
+ "types": "./basic.d.ts",
12
+ "default": "./basic.js"
13
+ },
14
+ "require": {
15
+ "types": "./basic.d.cts",
16
+ "default": "./basic.cjs"
17
+ }
13
18
  },
14
19
  "./relayer-client": {
15
- "import": "./relayer-client.js",
16
- "require": "./relayer-client.cjs",
17
- "types": "./relayer-client.d.ts"
20
+ "import": {
21
+ "types": "./relayer-client.d.ts",
22
+ "default": "./relayer-client.js"
23
+ },
24
+ "require": {
25
+ "types": "./relayer-client.d.cts",
26
+ "default": "./relayer-client.cjs"
27
+ }
18
28
  },
19
29
  "./trade-strategy/normalize": {
20
- "import": "./trade-strategy/normalize.js",
21
- "require": "./trade-strategy/normalize.cjs",
22
- "types": "./trade-strategy/normalize.d.ts"
30
+ "import": {
31
+ "types": "./trade-strategy/normalize.d.ts",
32
+ "default": "./trade-strategy/normalize.js"
33
+ },
34
+ "require": {
35
+ "types": "./trade-strategy/normalize.d.cts",
36
+ "default": "./trade-strategy/normalize.cjs"
37
+ }
23
38
  },
24
39
  "./trade-strategy/types": {
25
- "import": "./trade-strategy/types.js",
26
- "require": "./trade-strategy/types.cjs",
27
- "types": "./trade-strategy/types.d.ts"
40
+ "import": {
41
+ "types": "./trade-strategy/types.d.ts",
42
+ "default": "./trade-strategy/types.js"
43
+ },
44
+ "require": {
45
+ "types": "./trade-strategy/types.d.cts",
46
+ "default": "./trade-strategy/types.cjs"
47
+ }
28
48
  },
29
49
  "./wallet": {
30
- "import": "./wallet.js",
31
- "require": "./wallet.cjs",
32
- "types": "./wallet.d.ts"
50
+ "import": {
51
+ "types": "./wallet.d.ts",
52
+ "default": "./wallet.js"
53
+ },
54
+ "require": {
55
+ "types": "./wallet.d.cts",
56
+ "default": "./wallet.cjs"
57
+ }
33
58
  }
34
59
  },
35
60
  "files": [
@@ -2,26 +2,8 @@ import { Hex } from 'viem';
2
2
  import { RelayClient } from '@polymarket/builder-relayer-client';
3
3
  import { BuilderApiKeyCreds } from '@polymarket/builder-signing-sdk';
4
4
 
5
- /**
6
- * Polymarket Relayer Client Utility
7
- *
8
- * Features: Create Relayer Client, manage Builder API credentials
9
- * Documentation: https://docs.polymarket.com/developers/builders/relayer-client
10
- */
11
-
12
- /**
13
- * Read Builder API credentials from environment variables
14
- *
15
- * @returns Builder API credentials
16
- * @throws Throws an error when environment variables are missing
17
- */
18
5
  declare function getBuilderCredsFromEnv(): BuilderApiKeyCreds;
19
- /**
20
- * Create a Relayer Client instance
21
- *
22
- * @param privateKey - User wallet private key
23
- * @returns RelayClient instance
24
- */
6
+
25
7
  declare function createRelayerClient(privateKey: Hex): RelayClient;
26
8
 
27
9
  export { createRelayerClient, getBuilderCredsFromEnv };
@@ -2,26 +2,8 @@ import { Hex } from 'viem';
2
2
  import { RelayClient } from '@polymarket/builder-relayer-client';
3
3
  import { BuilderApiKeyCreds } from '@polymarket/builder-signing-sdk';
4
4
 
5
- /**
6
- * Polymarket Relayer Client Utility
7
- *
8
- * Features: Create Relayer Client, manage Builder API credentials
9
- * Documentation: https://docs.polymarket.com/developers/builders/relayer-client
10
- */
11
-
12
- /**
13
- * Read Builder API credentials from environment variables
14
- *
15
- * @returns Builder API credentials
16
- * @throws Throws an error when environment variables are missing
17
- */
18
5
  declare function getBuilderCredsFromEnv(): BuilderApiKeyCreds;
19
- /**
20
- * Create a Relayer Client instance
21
- *
22
- * @param privateKey - User wallet private key
23
- * @returns RelayClient instance
24
- */
6
+
25
7
  declare function createRelayerClient(privateKey: Hex): RelayClient;
26
8
 
27
9
  export { createRelayerClient, getBuilderCredsFromEnv };
@@ -29,23 +29,18 @@ function normalizeBuyStep(step, options) {
29
29
  const { start, end, price, size } = step;
30
30
  const { minPrice, minSize } = options;
31
31
  if (start === void 0 || start === null || start < 0) {
32
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u4E70\u5165\u6B65\u9AA4: start \u5FC5\u987B >= 0\uFF0C\u5F53\u524D\u503C: ${start}`);
33
32
  return null;
34
33
  }
35
34
  if (end === void 0 || end === null || end <= 0) {
36
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u4E70\u5165\u6B65\u9AA4: end \u5FC5\u987B > 0\uFF0C\u5F53\u524D\u503C: ${end}`);
37
35
  return null;
38
36
  }
39
37
  if (start >= end) {
40
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u4E70\u5165\u6B65\u9AA4: start \u5FC5\u987B\u5C0F\u4E8E end\uFF0C\u5F53\u524D start: ${start}, end: ${end}`);
41
38
  return null;
42
39
  }
43
40
  if (price === void 0 || price === null || price < minPrice || price >= 1) {
44
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u4E70\u5165\u6B65\u9AA4: price \u5FC5\u987B >= ${minPrice} \u4E14 < 1\uFF0C\u5F53\u524D\u503C: ${price}`);
45
41
  return null;
46
42
  }
47
43
  if (size === void 0 || size === null || size < minSize) {
48
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u4E70\u5165\u6B65\u9AA4: size \u5FC5\u987B >= ${minSize}\uFF0C\u5F53\u524D\u503C: ${size}`);
49
44
  return null;
50
45
  }
51
46
  const normalized = {
@@ -68,11 +63,9 @@ function normalizeBuyStep(step, options) {
68
63
  if (step.maxUnderlyingDiff >= 1 && step.maxUnderlyingDiff <= 1e3) {
69
64
  normalized.maxUnderlyingDiff = step.maxUnderlyingDiff;
70
65
  } else {
71
- console.warn(` \u2514\u2500 \u5DF2\u79FB\u9664\u65E0\u6548\u5B57\u6BB5 maxUnderlyingDiff: \u503C\u5FC5\u987B\u5728 1-1000 \u8303\u56F4\u5185\uFF0C\u5F53\u524D\u503C: ${step.maxUnderlyingDiff}`);
72
66
  }
73
67
  }
74
68
  if (step.minUnderlyingDiff !== void 0) {
75
- console.warn(` \u2514\u2500 \u5DF2\u79FB\u9664\u65E0\u6548\u5B57\u6BB5 minUnderlyingDiff: \u4F4E\u4F4D\u4E70\u5165\u6A21\u5F0F(price=${price})\u4E0D\u652F\u6301\u6B64\u5B57\u6BB5`);
76
69
  }
77
70
  }
78
71
  if (isChaseMode) {
@@ -80,11 +73,9 @@ function normalizeBuyStep(step, options) {
80
73
  if (step.minUnderlyingDiff >= 1 && step.minUnderlyingDiff <= 1e3) {
81
74
  normalized.minUnderlyingDiff = step.minUnderlyingDiff;
82
75
  } else {
83
- console.warn(` \u2514\u2500 \u5DF2\u79FB\u9664\u65E0\u6548\u5B57\u6BB5 minUnderlyingDiff: \u503C\u5FC5\u987B\u5728 1-1000 \u8303\u56F4\u5185\uFF0C\u5F53\u524D\u503C: ${step.minUnderlyingDiff}`);
84
76
  }
85
77
  }
86
78
  if (step.maxUnderlyingDiff !== void 0) {
87
- console.warn(` \u2514\u2500 \u5DF2\u79FB\u9664\u65E0\u6548\u5B57\u6BB5 maxUnderlyingDiff: \u8FFD\u6DA8\u4E70\u5165\u6A21\u5F0F(price=${price})\u4E0D\u652F\u6301\u6B64\u5B57\u6BB5`);
88
79
  }
89
80
  }
90
81
  return normalized;
@@ -93,23 +84,18 @@ function normalizeSellStep(step, options) {
93
84
  const { start, end, price, size } = step;
94
85
  const { minPrice, minSize } = options;
95
86
  if (start === void 0 || start === null || start < 0) {
96
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u5356\u51FA\u6B65\u9AA4: start \u5FC5\u987B >= 0\uFF0C\u5F53\u524D\u503C: ${start}`);
97
87
  return null;
98
88
  }
99
89
  if (end === void 0 || end === null || end <= 0) {
100
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u5356\u51FA\u6B65\u9AA4: end \u5FC5\u987B > 0\uFF0C\u5F53\u524D\u503C: ${end}`);
101
90
  return null;
102
91
  }
103
92
  if (start >= end) {
104
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u5356\u51FA\u6B65\u9AA4: start \u5FC5\u987B\u5C0F\u4E8E end\uFF0C\u5F53\u524D start: ${start}, end: ${end}`);
105
93
  return null;
106
94
  }
107
95
  if (price === void 0 || price === null || price < minPrice || price >= 1) {
108
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u5356\u51FA\u6B65\u9AA4: price \u5FC5\u987B >= ${minPrice} \u4E14 < 1\uFF0C\u5F53\u524D\u503C: ${price}`);
109
96
  return null;
110
97
  }
111
98
  if (size === void 0 || size === null || size < minSize) {
112
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u5356\u51FA\u6B65\u9AA4: size \u5FC5\u987B >= ${minSize}\uFF0C\u5F53\u524D\u503C: ${size}`);
113
99
  return null;
114
100
  }
115
101
  const normalized = {
@@ -126,13 +112,11 @@ function normalizeSellStep(step, options) {
126
112
  if ("maxUnderlyingDiff" in stepAsTradeStep && stepAsTradeStep.maxUnderlyingDiff !== void 0) invalidFields.push("maxUnderlyingDiff");
127
113
  if ("minUnderlyingDiff" in stepAsTradeStep && stepAsTradeStep.minUnderlyingDiff !== void 0) invalidFields.push("minUnderlyingDiff");
128
114
  if (invalidFields.length > 0) {
129
- console.warn(` \u2514\u2500 \u5DF2\u79FB\u9664\u5356\u51FA\u6B65\u9AA4\u4E2D\u7684\u65E0\u6548\u5B57\u6BB5: ${invalidFields.join(", ")}`);
130
115
  }
131
116
  return normalized;
132
117
  }
133
118
  function normalizeStrategy(config, options = {}) {
134
119
  if (!config || !Array.isArray(config) || config.length === 0) {
135
- console.warn("\u26A0\uFE0F \u7B56\u7565\u914D\u7F6E\u4E3A\u7A7A\u6216\u683C\u5F0F\u9519\u8BEF");
136
120
  return [];
137
121
  }
138
122
  const normalizeOptions = {
@@ -141,7 +125,6 @@ function normalizeStrategy(config, options = {}) {
141
125
  };
142
126
  const normalized = [];
143
127
  config.forEach((step, index) => {
144
- console.debug(`\u9A8C\u8BC1\u6B65\u9AA4 ${index + 1}/${config.length}: ${step.side}`);
145
128
  let normalizedStep = null;
146
129
  if (step.side === import_clob_client.Side.BUY) {
147
130
  normalizedStep = normalizeBuyStep(step, normalizeOptions);
@@ -149,13 +132,11 @@ function normalizeStrategy(config, options = {}) {
149
132
  normalizedStep = normalizeSellStep(step, normalizeOptions);
150
133
  } else {
151
134
  const unknownStep = step;
152
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u6B65\u9AA4: \u672A\u77E5\u7684 side \u7C7B\u578B: ${unknownStep.side}`);
153
135
  }
154
136
  if (normalizedStep) {
155
137
  normalized.push(normalizedStep);
156
138
  }
157
139
  });
158
- console.info(`\u2705 \u7B56\u7565\u4FEE\u6B63\u5B8C\u6210: \u539F\u59CB\u6B65\u9AA4 ${config.length} \u4E2A\uFF0C\u6709\u6548\u6B65\u9AA4 ${normalized.length} \u4E2A`);
159
140
  return normalized;
160
141
  }
161
142
  function hasBuySteps(config) {
@@ -1,29 +1,15 @@
1
1
  import { StrategyConfig } from './types.cjs';
2
2
  import '@polymarket/clob-client';
3
3
 
4
- /**
5
- * 策略修正选项
6
- */
7
4
  interface NormalizeStrategyOptions {
8
- /** 最小价格,默认 0.01 */
5
+
9
6
  minPrice?: number;
10
- /** 最小数量,默认 5 */
7
+
11
8
  minSize?: number;
12
9
  }
13
- /**
14
- * 修正策略配置
15
- * 对输入的策略进行验证、修正和过滤
16
- *
17
- * @param config - 原始策略配置
18
- * @param options - 修正选项
19
- * @returns 修正后的策略配置
20
- */
10
+
21
11
  declare function normalizeStrategy(config: StrategyConfig, options?: NormalizeStrategyOptions): StrategyConfig;
22
- /**
23
- * 检查策略是否包含买入步骤
24
- * @param config - 策略配置
25
- * @returns 是否包含买入步骤
26
- */
12
+
27
13
  declare function hasBuySteps(config: StrategyConfig): boolean;
28
14
 
29
15
  export { type NormalizeStrategyOptions, hasBuySteps, normalizeStrategy };
@@ -1,29 +1,15 @@
1
1
  import { StrategyConfig } from './types.js';
2
2
  import '@polymarket/clob-client';
3
3
 
4
- /**
5
- * 策略修正选项
6
- */
7
4
  interface NormalizeStrategyOptions {
8
- /** 最小价格,默认 0.01 */
5
+
9
6
  minPrice?: number;
10
- /** 最小数量,默认 5 */
7
+
11
8
  minSize?: number;
12
9
  }
13
- /**
14
- * 修正策略配置
15
- * 对输入的策略进行验证、修正和过滤
16
- *
17
- * @param config - 原始策略配置
18
- * @param options - 修正选项
19
- * @returns 修正后的策略配置
20
- */
10
+
21
11
  declare function normalizeStrategy(config: StrategyConfig, options?: NormalizeStrategyOptions): StrategyConfig;
22
- /**
23
- * 检查策略是否包含买入步骤
24
- * @param config - 策略配置
25
- * @returns 是否包含买入步骤
26
- */
12
+
27
13
  declare function hasBuySteps(config: StrategyConfig): boolean;
28
14
 
29
15
  export { type NormalizeStrategyOptions, hasBuySteps, normalizeStrategy };
@@ -4,23 +4,18 @@ function normalizeBuyStep(step, options) {
4
4
  const { start, end, price, size } = step;
5
5
  const { minPrice, minSize } = options;
6
6
  if (start === void 0 || start === null || start < 0) {
7
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u4E70\u5165\u6B65\u9AA4: start \u5FC5\u987B >= 0\uFF0C\u5F53\u524D\u503C: ${start}`);
8
7
  return null;
9
8
  }
10
9
  if (end === void 0 || end === null || end <= 0) {
11
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u4E70\u5165\u6B65\u9AA4: end \u5FC5\u987B > 0\uFF0C\u5F53\u524D\u503C: ${end}`);
12
10
  return null;
13
11
  }
14
12
  if (start >= end) {
15
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u4E70\u5165\u6B65\u9AA4: start \u5FC5\u987B\u5C0F\u4E8E end\uFF0C\u5F53\u524D start: ${start}, end: ${end}`);
16
13
  return null;
17
14
  }
18
15
  if (price === void 0 || price === null || price < minPrice || price >= 1) {
19
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u4E70\u5165\u6B65\u9AA4: price \u5FC5\u987B >= ${minPrice} \u4E14 < 1\uFF0C\u5F53\u524D\u503C: ${price}`);
20
16
  return null;
21
17
  }
22
18
  if (size === void 0 || size === null || size < minSize) {
23
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u4E70\u5165\u6B65\u9AA4: size \u5FC5\u987B >= ${minSize}\uFF0C\u5F53\u524D\u503C: ${size}`);
24
19
  return null;
25
20
  }
26
21
  const normalized = {
@@ -43,11 +38,9 @@ function normalizeBuyStep(step, options) {
43
38
  if (step.maxUnderlyingDiff >= 1 && step.maxUnderlyingDiff <= 1e3) {
44
39
  normalized.maxUnderlyingDiff = step.maxUnderlyingDiff;
45
40
  } else {
46
- console.warn(` \u2514\u2500 \u5DF2\u79FB\u9664\u65E0\u6548\u5B57\u6BB5 maxUnderlyingDiff: \u503C\u5FC5\u987B\u5728 1-1000 \u8303\u56F4\u5185\uFF0C\u5F53\u524D\u503C: ${step.maxUnderlyingDiff}`);
47
41
  }
48
42
  }
49
43
  if (step.minUnderlyingDiff !== void 0) {
50
- console.warn(` \u2514\u2500 \u5DF2\u79FB\u9664\u65E0\u6548\u5B57\u6BB5 minUnderlyingDiff: \u4F4E\u4F4D\u4E70\u5165\u6A21\u5F0F(price=${price})\u4E0D\u652F\u6301\u6B64\u5B57\u6BB5`);
51
44
  }
52
45
  }
53
46
  if (isChaseMode) {
@@ -55,11 +48,9 @@ function normalizeBuyStep(step, options) {
55
48
  if (step.minUnderlyingDiff >= 1 && step.minUnderlyingDiff <= 1e3) {
56
49
  normalized.minUnderlyingDiff = step.minUnderlyingDiff;
57
50
  } else {
58
- console.warn(` \u2514\u2500 \u5DF2\u79FB\u9664\u65E0\u6548\u5B57\u6BB5 minUnderlyingDiff: \u503C\u5FC5\u987B\u5728 1-1000 \u8303\u56F4\u5185\uFF0C\u5F53\u524D\u503C: ${step.minUnderlyingDiff}`);
59
51
  }
60
52
  }
61
53
  if (step.maxUnderlyingDiff !== void 0) {
62
- console.warn(` \u2514\u2500 \u5DF2\u79FB\u9664\u65E0\u6548\u5B57\u6BB5 maxUnderlyingDiff: \u8FFD\u6DA8\u4E70\u5165\u6A21\u5F0F(price=${price})\u4E0D\u652F\u6301\u6B64\u5B57\u6BB5`);
63
54
  }
64
55
  }
65
56
  return normalized;
@@ -68,23 +59,18 @@ function normalizeSellStep(step, options) {
68
59
  const { start, end, price, size } = step;
69
60
  const { minPrice, minSize } = options;
70
61
  if (start === void 0 || start === null || start < 0) {
71
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u5356\u51FA\u6B65\u9AA4: start \u5FC5\u987B >= 0\uFF0C\u5F53\u524D\u503C: ${start}`);
72
62
  return null;
73
63
  }
74
64
  if (end === void 0 || end === null || end <= 0) {
75
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u5356\u51FA\u6B65\u9AA4: end \u5FC5\u987B > 0\uFF0C\u5F53\u524D\u503C: ${end}`);
76
65
  return null;
77
66
  }
78
67
  if (start >= end) {
79
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u5356\u51FA\u6B65\u9AA4: start \u5FC5\u987B\u5C0F\u4E8E end\uFF0C\u5F53\u524D start: ${start}, end: ${end}`);
80
68
  return null;
81
69
  }
82
70
  if (price === void 0 || price === null || price < minPrice || price >= 1) {
83
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u5356\u51FA\u6B65\u9AA4: price \u5FC5\u987B >= ${minPrice} \u4E14 < 1\uFF0C\u5F53\u524D\u503C: ${price}`);
84
71
  return null;
85
72
  }
86
73
  if (size === void 0 || size === null || size < minSize) {
87
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u5356\u51FA\u6B65\u9AA4: size \u5FC5\u987B >= ${minSize}\uFF0C\u5F53\u524D\u503C: ${size}`);
88
74
  return null;
89
75
  }
90
76
  const normalized = {
@@ -101,13 +87,11 @@ function normalizeSellStep(step, options) {
101
87
  if ("maxUnderlyingDiff" in stepAsTradeStep && stepAsTradeStep.maxUnderlyingDiff !== void 0) invalidFields.push("maxUnderlyingDiff");
102
88
  if ("minUnderlyingDiff" in stepAsTradeStep && stepAsTradeStep.minUnderlyingDiff !== void 0) invalidFields.push("minUnderlyingDiff");
103
89
  if (invalidFields.length > 0) {
104
- console.warn(` \u2514\u2500 \u5DF2\u79FB\u9664\u5356\u51FA\u6B65\u9AA4\u4E2D\u7684\u65E0\u6548\u5B57\u6BB5: ${invalidFields.join(", ")}`);
105
90
  }
106
91
  return normalized;
107
92
  }
108
93
  function normalizeStrategy(config, options = {}) {
109
94
  if (!config || !Array.isArray(config) || config.length === 0) {
110
- console.warn("\u26A0\uFE0F \u7B56\u7565\u914D\u7F6E\u4E3A\u7A7A\u6216\u683C\u5F0F\u9519\u8BEF");
111
95
  return [];
112
96
  }
113
97
  const normalizeOptions = {
@@ -116,7 +100,6 @@ function normalizeStrategy(config, options = {}) {
116
100
  };
117
101
  const normalized = [];
118
102
  config.forEach((step, index) => {
119
- console.debug(`\u9A8C\u8BC1\u6B65\u9AA4 ${index + 1}/${config.length}: ${step.side}`);
120
103
  let normalizedStep = null;
121
104
  if (step.side === Side.BUY) {
122
105
  normalizedStep = normalizeBuyStep(step, normalizeOptions);
@@ -124,13 +107,11 @@ function normalizeStrategy(config, options = {}) {
124
107
  normalizedStep = normalizeSellStep(step, normalizeOptions);
125
108
  } else {
126
109
  const unknownStep = step;
127
- console.warn(`\u26A0\uFE0F \u5DF2\u8FC7\u6EE4\u65E0\u6548\u6B65\u9AA4: \u672A\u77E5\u7684 side \u7C7B\u578B: ${unknownStep.side}`);
128
110
  }
129
111
  if (normalizedStep) {
130
112
  normalized.push(normalizedStep);
131
113
  }
132
114
  });
133
- console.info(`\u2705 \u7B56\u7565\u4FEE\u6B63\u5B8C\u6210: \u539F\u59CB\u6B65\u9AA4 ${config.length} \u4E2A\uFF0C\u6709\u6548\u6B65\u9AA4 ${normalized.length} \u4E2A`);
134
115
  return normalized;
135
116
  }
136
117
  function hasBuySteps(config) {
@@ -1,8 +1,5 @@
1
1
  import { Side, ClobClient } from '@polymarket/clob-client';
2
2
 
3
- /**
4
- * 市场相关类型定义
5
- */
6
3
  declare enum SupportedSymbol {
7
4
  ETH = "eth",
8
5
  BTC = "btc",
@@ -61,55 +58,34 @@ interface MarketToken {
61
58
  name: string;
62
59
  }
63
60
 
64
- /**
65
- * Polymarket 交易基础工具方法
66
- */
67
- /**
68
- * 仓位信息
69
- */
70
61
  interface PositionInfo {
71
- /** 仓位数量(格式化后的数字) */
62
+
72
63
  size: number;
73
- /** 原始余额(字符串格式) */
64
+
74
65
  balance: string;
75
66
  }
76
67
 
77
- /**
78
- * 加密货币价格查询模块
79
- * 支持通过 Binance API 查询 ETH, BTC, SOL, XRP 的价格
80
- */
81
-
82
- /**
83
- * 价格差信息类型
84
- */
85
68
  interface PriceDifferenceInfo {
86
- /** 开始价格 */
69
+
87
70
  start: number;
88
- /** 结束价格 */
71
+
89
72
  end: number;
90
- /** 价格差 */
73
+
91
74
  difference: number;
92
- /** 价格差绝对值 */
75
+
93
76
  differenceAbs: number;
94
- /** 涨跌幅百分比,100 表示 100%,-100 表示 -100% */
77
+
95
78
  percent: number;
96
- /** 涨跌幅百分比绝对值 */
79
+
97
80
  percentAbs: number;
98
- /** 是否上涨 */
81
+
99
82
  isUp: boolean;
100
- /** 是否下跌 */
83
+
101
84
  isDown: boolean;
102
- /** 是否持平 */
85
+
103
86
  isFlat: boolean;
104
87
  }
105
88
 
106
- /**
107
- * 交易策略类型定义
108
- */
109
-
110
- /**
111
- * 买入步骤配置
112
- */
113
89
  interface BuyStep {
114
90
  side: Side.BUY;
115
91
  price: number;
@@ -121,9 +97,7 @@ interface BuyStep {
121
97
  maxUnderlyingDiff?: number;
122
98
  minUnderlyingDiff?: number;
123
99
  }
124
- /**
125
- * 卖出步骤配置
126
- */
100
+
127
101
  interface SellStep {
128
102
  side: Side.SELL;
129
103
  price: number;
@@ -131,50 +105,20 @@ interface SellStep {
131
105
  start: number;
132
106
  end: number;
133
107
  }
134
- /**
135
- * 交易步骤类型
136
- */
108
+
137
109
  type TradeStep = BuyStep | SellStep;
138
- /**
139
- * 策略配置类型
140
- * 由多个交易步骤组成的数组
141
- */
110
+
142
111
  type StrategyConfig = TradeStep[];
143
- /**
144
- * 策略回调函数类型
145
- * @param prevResult - 上一个策略回调的返回值(首个回调为 undefined)
146
- * @returns 返回值说明:
147
- * - 返回 false:终止后续所有策略的执行
148
- * - 返回 true、null、undefined、void:继续执行后续策略,不传递值
149
- * - 返回其他非 boolean 值:继续执行后续策略,并将该值作为下一个策略的入参
150
- */
112
+
151
113
  type StrategyCallback = (prevResult?: any) => any | Promise<any>;
152
- /**
153
- * 策略配置数组
154
- * StrategyCondition 格式:
155
- * - "<10": 从开始到10分钟(不包含10分钟)
156
- * - ">=10": 从10分钟开始(包含10分钟)到结束
157
- * - ">=20,<40": 从20分钟开始(包含20分钟)到40分钟(不包含40分钟)
158
- * - "=30" 或 "30": 等于30分钟
159
- * - ">10,<=20": 从10分钟后(不包含10分钟)到20分钟(包含20分钟)
160
- *
161
- * 任务严格按照数组顺序执行
162
- */
114
+
163
115
  type StrategyCondition = string;
164
116
  type StrategyExecQueue = Array<[StrategyCondition, StrategyCallback]>;
165
- /**
166
- * 策略执行错误回调函数类型
167
- * @param error - 错误对象
168
- */
117
+
169
118
  type StrategyErrorCallback = (error: Error) => void | Promise<void>;
170
- /**
171
- * 策略函数类型
172
- * @param onError - 策略执行失败时的错误回调(可选)
173
- */
119
+
174
120
  type StrategyFn = (onError?: StrategyErrorCallback) => Promise<void>;
175
- /**
176
- * Token 交易步骤订单信息
177
- */
121
+
178
122
  interface TokenTradeStepOrder {
179
123
  id: string;
180
124
  stepJson: string;
@@ -182,9 +126,7 @@ interface TokenTradeStepOrder {
182
126
  status: string;
183
127
  updatedAt: number;
184
128
  }
185
- /**
186
- * 市场交易记录类型
187
- */
129
+
188
130
  interface MarketTradeRecord {
189
131
  marketId: string;
190
132
  strategyJson: string;
@@ -192,40 +134,31 @@ interface MarketTradeRecord {
192
134
  [tokenId: string]: TokenTradeStepOrder[];
193
135
  };
194
136
  }
195
- /**
196
- * 市场交易上下文
197
- * 包含执行交易策略所需的市场信息和客户端实例
198
- */
137
+
199
138
  interface MarketTradeContext {
200
- /** CLOB 客户端实例 */
139
+
201
140
  clobClient: ClobClient;
202
- /** Funder 地址或标识 */
141
+
203
142
  funder: string;
204
- /** 市场信息(包含开始/结束时间戳等) */
143
+
205
144
  market: Market;
206
- /** Token 列表 (包含 id 和 name) */
145
+
207
146
  tokens: MarketToken[];
208
- /** 交易记录 */
147
+
209
148
  tradeRecord: MarketTradeRecord;
210
- /** 标的物价格信息(可能为 null,如果价格查询失败) */
149
+
211
150
  underlyingPrice: PriceDifferenceInfo | null;
212
151
  }
213
- /**
214
- * 市场 Token 数据
215
- */
152
+
216
153
  interface MarketTokenData extends MarketToken {
217
- /** 是否有持仓 */
154
+
218
155
  hasPosition: boolean;
219
- /** 仓位信息,null 表示未查询 */
156
+
220
157
  positionInfo: PositionInfo | null;
221
- /** 订单记录 */
158
+
222
159
  orderRecords: TokenTradeStepOrder[];
223
160
  }
224
- /**
225
- * 市场 Token 上下文
226
- * 记录市场中每个 Token 的持仓和订单记录数据
227
- * 使用 tokenId 作为 key
228
- */
161
+
229
162
  type MarketTokenContext = Record<string, MarketTokenData>;
230
163
 
231
164
  export type { BuyStep, MarketTokenContext, MarketTokenData, MarketTradeContext, MarketTradeRecord, SellStep, StrategyCallback, StrategyCondition, StrategyConfig, StrategyErrorCallback, StrategyExecQueue, StrategyFn, TokenTradeStepOrder, TradeStep };
@@ -1,8 +1,5 @@
1
1
  import { Side, ClobClient } from '@polymarket/clob-client';
2
2
 
3
- /**
4
- * 市场相关类型定义
5
- */
6
3
  declare enum SupportedSymbol {
7
4
  ETH = "eth",
8
5
  BTC = "btc",
@@ -61,55 +58,34 @@ interface MarketToken {
61
58
  name: string;
62
59
  }
63
60
 
64
- /**
65
- * Polymarket 交易基础工具方法
66
- */
67
- /**
68
- * 仓位信息
69
- */
70
61
  interface PositionInfo {
71
- /** 仓位数量(格式化后的数字) */
62
+
72
63
  size: number;
73
- /** 原始余额(字符串格式) */
64
+
74
65
  balance: string;
75
66
  }
76
67
 
77
- /**
78
- * 加密货币价格查询模块
79
- * 支持通过 Binance API 查询 ETH, BTC, SOL, XRP 的价格
80
- */
81
-
82
- /**
83
- * 价格差信息类型
84
- */
85
68
  interface PriceDifferenceInfo {
86
- /** 开始价格 */
69
+
87
70
  start: number;
88
- /** 结束价格 */
71
+
89
72
  end: number;
90
- /** 价格差 */
73
+
91
74
  difference: number;
92
- /** 价格差绝对值 */
75
+
93
76
  differenceAbs: number;
94
- /** 涨跌幅百分比,100 表示 100%,-100 表示 -100% */
77
+
95
78
  percent: number;
96
- /** 涨跌幅百分比绝对值 */
79
+
97
80
  percentAbs: number;
98
- /** 是否上涨 */
81
+
99
82
  isUp: boolean;
100
- /** 是否下跌 */
83
+
101
84
  isDown: boolean;
102
- /** 是否持平 */
85
+
103
86
  isFlat: boolean;
104
87
  }
105
88
 
106
- /**
107
- * 交易策略类型定义
108
- */
109
-
110
- /**
111
- * 买入步骤配置
112
- */
113
89
  interface BuyStep {
114
90
  side: Side.BUY;
115
91
  price: number;
@@ -121,9 +97,7 @@ interface BuyStep {
121
97
  maxUnderlyingDiff?: number;
122
98
  minUnderlyingDiff?: number;
123
99
  }
124
- /**
125
- * 卖出步骤配置
126
- */
100
+
127
101
  interface SellStep {
128
102
  side: Side.SELL;
129
103
  price: number;
@@ -131,50 +105,20 @@ interface SellStep {
131
105
  start: number;
132
106
  end: number;
133
107
  }
134
- /**
135
- * 交易步骤类型
136
- */
108
+
137
109
  type TradeStep = BuyStep | SellStep;
138
- /**
139
- * 策略配置类型
140
- * 由多个交易步骤组成的数组
141
- */
110
+
142
111
  type StrategyConfig = TradeStep[];
143
- /**
144
- * 策略回调函数类型
145
- * @param prevResult - 上一个策略回调的返回值(首个回调为 undefined)
146
- * @returns 返回值说明:
147
- * - 返回 false:终止后续所有策略的执行
148
- * - 返回 true、null、undefined、void:继续执行后续策略,不传递值
149
- * - 返回其他非 boolean 值:继续执行后续策略,并将该值作为下一个策略的入参
150
- */
112
+
151
113
  type StrategyCallback = (prevResult?: any) => any | Promise<any>;
152
- /**
153
- * 策略配置数组
154
- * StrategyCondition 格式:
155
- * - "<10": 从开始到10分钟(不包含10分钟)
156
- * - ">=10": 从10分钟开始(包含10分钟)到结束
157
- * - ">=20,<40": 从20分钟开始(包含20分钟)到40分钟(不包含40分钟)
158
- * - "=30" 或 "30": 等于30分钟
159
- * - ">10,<=20": 从10分钟后(不包含10分钟)到20分钟(包含20分钟)
160
- *
161
- * 任务严格按照数组顺序执行
162
- */
114
+
163
115
  type StrategyCondition = string;
164
116
  type StrategyExecQueue = Array<[StrategyCondition, StrategyCallback]>;
165
- /**
166
- * 策略执行错误回调函数类型
167
- * @param error - 错误对象
168
- */
117
+
169
118
  type StrategyErrorCallback = (error: Error) => void | Promise<void>;
170
- /**
171
- * 策略函数类型
172
- * @param onError - 策略执行失败时的错误回调(可选)
173
- */
119
+
174
120
  type StrategyFn = (onError?: StrategyErrorCallback) => Promise<void>;
175
- /**
176
- * Token 交易步骤订单信息
177
- */
121
+
178
122
  interface TokenTradeStepOrder {
179
123
  id: string;
180
124
  stepJson: string;
@@ -182,9 +126,7 @@ interface TokenTradeStepOrder {
182
126
  status: string;
183
127
  updatedAt: number;
184
128
  }
185
- /**
186
- * 市场交易记录类型
187
- */
129
+
188
130
  interface MarketTradeRecord {
189
131
  marketId: string;
190
132
  strategyJson: string;
@@ -192,40 +134,31 @@ interface MarketTradeRecord {
192
134
  [tokenId: string]: TokenTradeStepOrder[];
193
135
  };
194
136
  }
195
- /**
196
- * 市场交易上下文
197
- * 包含执行交易策略所需的市场信息和客户端实例
198
- */
137
+
199
138
  interface MarketTradeContext {
200
- /** CLOB 客户端实例 */
139
+
201
140
  clobClient: ClobClient;
202
- /** Funder 地址或标识 */
141
+
203
142
  funder: string;
204
- /** 市场信息(包含开始/结束时间戳等) */
143
+
205
144
  market: Market;
206
- /** Token 列表 (包含 id 和 name) */
145
+
207
146
  tokens: MarketToken[];
208
- /** 交易记录 */
147
+
209
148
  tradeRecord: MarketTradeRecord;
210
- /** 标的物价格信息(可能为 null,如果价格查询失败) */
149
+
211
150
  underlyingPrice: PriceDifferenceInfo | null;
212
151
  }
213
- /**
214
- * 市场 Token 数据
215
- */
152
+
216
153
  interface MarketTokenData extends MarketToken {
217
- /** 是否有持仓 */
154
+
218
155
  hasPosition: boolean;
219
- /** 仓位信息,null 表示未查询 */
156
+
220
157
  positionInfo: PositionInfo | null;
221
- /** 订单记录 */
158
+
222
159
  orderRecords: TokenTradeStepOrder[];
223
160
  }
224
- /**
225
- * 市场 Token 上下文
226
- * 记录市场中每个 Token 的持仓和订单记录数据
227
- * 使用 tokenId 作为 key
228
- */
161
+
229
162
  type MarketTokenContext = Record<string, MarketTokenData>;
230
163
 
231
164
  export type { BuyStep, MarketTokenContext, MarketTokenData, MarketTradeContext, MarketTradeRecord, SellStep, StrategyCallback, StrategyCondition, StrategyConfig, StrategyErrorCallback, StrategyExecQueue, StrategyFn, TokenTradeStepOrder, TradeStep };
package/wallet.d.cts CHANGED
@@ -13,27 +13,13 @@ declare const USDC_ABI: readonly [{
13
13
  readonly type: "uint256";
14
14
  }];
15
15
  }];
16
- /**
17
- * 创建单例 publicClient,避免重复实例化
18
- * @returns viem PublicClient 实例
19
- */
16
+
20
17
  declare const getPublicClient: () => PublicClient;
21
- /**
22
- * 格式化原始余额为可读格式
23
- * @param rawBalance - 原始余额
24
- * @param decimals - 小数位数
25
- * @returns 格式化后的余额
26
- */
18
+
27
19
  declare function formatBalance(rawBalance: string | bigint, decimals?: number): string;
28
- /**
29
- * 查询指定地址的 USDC 余额
30
- * @param address - 要查询的地址
31
- * @returns USDC 余额 (原始值,需要用 formatBalance 格式化)
32
- */
20
+
33
21
  declare function getUSDCBalance(address: string): Promise<bigint>;
34
- /**
35
- * 比较两个以太坊地址是否相等
36
- */
22
+
37
23
  declare function isAddressEqual(a?: Address | string | null, b?: Address | string | null): boolean;
38
24
 
39
25
  export { USDC_ABI, USDC_ADDRESS, USDC_DECIMALS, formatBalance, getPublicClient, getUSDCBalance, isAddressEqual };
package/wallet.d.ts CHANGED
@@ -13,27 +13,13 @@ declare const USDC_ABI: readonly [{
13
13
  readonly type: "uint256";
14
14
  }];
15
15
  }];
16
- /**
17
- * 创建单例 publicClient,避免重复实例化
18
- * @returns viem PublicClient 实例
19
- */
16
+
20
17
  declare const getPublicClient: () => PublicClient;
21
- /**
22
- * 格式化原始余额为可读格式
23
- * @param rawBalance - 原始余额
24
- * @param decimals - 小数位数
25
- * @returns 格式化后的余额
26
- */
18
+
27
19
  declare function formatBalance(rawBalance: string | bigint, decimals?: number): string;
28
- /**
29
- * 查询指定地址的 USDC 余额
30
- * @param address - 要查询的地址
31
- * @returns USDC 余额 (原始值,需要用 formatBalance 格式化)
32
- */
20
+
33
21
  declare function getUSDCBalance(address: string): Promise<bigint>;
34
- /**
35
- * 比较两个以太坊地址是否相等
36
- */
22
+
37
23
  declare function isAddressEqual(a?: Address | string | null, b?: Address | string | null): boolean;
38
24
 
39
25
  export { USDC_ABI, USDC_ADDRESS, USDC_DECIMALS, formatBalance, getPublicClient, getUSDCBalance, isAddressEqual };