polycopy 0.0.1 → 0.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/dist/cli.js +18 -35
- package/dist/config.js +1 -1
- package/dist/index.js +49 -1
- package/dist/{init-C8vHJzZn.js → init-C2C8sTcQ.js} +4 -4
- package/dist/paths-C5xeA_8E.js +13 -0
- package/package.json +3 -2
package/dist/cli.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const child_process = require("child_process");
|
|
4
4
|
const fs = require("fs");
|
|
5
5
|
const path = require("path");
|
|
6
|
+
const paths = require("./paths-C5xeA_8E.js");
|
|
6
7
|
function _interopNamespace(e) {
|
|
7
8
|
if (e && e.__esModule) return e;
|
|
8
9
|
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
@@ -22,34 +23,31 @@ function _interopNamespace(e) {
|
|
|
22
23
|
}
|
|
23
24
|
const fs__namespace = /* @__PURE__ */ _interopNamespace(fs);
|
|
24
25
|
const path__namespace = /* @__PURE__ */ _interopNamespace(path);
|
|
25
|
-
const PID_DIR = path__namespace.join(process.env.HOME || process.cwd(), ".polycopy");
|
|
26
|
-
const PID_FILE = path__namespace.join(PID_DIR, "polycopy.pid");
|
|
27
|
-
const LOGS_DIR = path__namespace.join(process.cwd(), "logs");
|
|
28
26
|
function ensurePidDir() {
|
|
29
|
-
if (!fs__namespace.existsSync(PID_DIR)) {
|
|
30
|
-
fs__namespace.mkdirSync(PID_DIR, { recursive: true });
|
|
27
|
+
if (!fs__namespace.existsSync(paths.PID_DIR)) {
|
|
28
|
+
fs__namespace.mkdirSync(paths.PID_DIR, { recursive: true });
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
31
|
function getRunningPid() {
|
|
34
|
-
if (!fs__namespace.existsSync(PID_FILE)) {
|
|
32
|
+
if (!fs__namespace.existsSync(paths.PID_FILE)) {
|
|
35
33
|
return null;
|
|
36
34
|
}
|
|
37
35
|
try {
|
|
38
|
-
const pid = parseInt(fs__namespace.readFileSync(PID_FILE, "utf-8").trim());
|
|
36
|
+
const pid = parseInt(fs__namespace.readFileSync(paths.PID_FILE, "utf-8").trim());
|
|
39
37
|
process.kill(pid, 0);
|
|
40
38
|
return pid;
|
|
41
39
|
} catch {
|
|
42
|
-
fs__namespace.unlinkSync(PID_FILE);
|
|
40
|
+
fs__namespace.unlinkSync(paths.PID_FILE);
|
|
43
41
|
return null;
|
|
44
42
|
}
|
|
45
43
|
}
|
|
46
44
|
function savePid(pid) {
|
|
47
45
|
ensurePidDir();
|
|
48
|
-
fs__namespace.writeFileSync(PID_FILE, pid.toString());
|
|
46
|
+
fs__namespace.writeFileSync(paths.PID_FILE, pid.toString());
|
|
49
47
|
}
|
|
50
48
|
function removePid() {
|
|
51
|
-
if (fs__namespace.existsSync(PID_FILE)) {
|
|
52
|
-
fs__namespace.unlinkSync(PID_FILE);
|
|
49
|
+
if (fs__namespace.existsSync(paths.PID_FILE)) {
|
|
50
|
+
fs__namespace.unlinkSync(paths.PID_FILE);
|
|
53
51
|
}
|
|
54
52
|
}
|
|
55
53
|
function showHelp() {
|
|
@@ -107,31 +105,16 @@ async function startForeground() {
|
|
|
107
105
|
const mainPath = path__namespace.join(__dirname, "index.js");
|
|
108
106
|
await import(mainPath);
|
|
109
107
|
}
|
|
110
|
-
function startDaemon() {
|
|
108
|
+
async function startDaemon() {
|
|
111
109
|
const runningPid = getRunningPid();
|
|
112
110
|
if (runningPid) {
|
|
113
111
|
console.log(`⚠️ 已有进程运行中 (PID: ${runningPid})`);
|
|
114
112
|
console.log(` 使用 'polycopy stop' 停止后再启动`);
|
|
115
113
|
process.exit(1);
|
|
116
114
|
}
|
|
117
|
-
|
|
115
|
+
process.env.POLYCOPY_DAEMON = "1";
|
|
118
116
|
const mainPath = path__namespace.join(__dirname, "index.js");
|
|
119
|
-
|
|
120
|
-
detached: true,
|
|
121
|
-
stdio: "ignore",
|
|
122
|
-
env: { ...process.env, POLYCOPY_DAEMON: "1" }
|
|
123
|
-
});
|
|
124
|
-
if (child.pid) {
|
|
125
|
-
savePid(child.pid);
|
|
126
|
-
console.log(`✅ 已在后台启动 (PID: ${child.pid})`);
|
|
127
|
-
console.log(` 日志目录: ${LOGS_DIR}`);
|
|
128
|
-
console.log(` 使用 'polycopy stop' 停止`);
|
|
129
|
-
child.unref();
|
|
130
|
-
process.exit(0);
|
|
131
|
-
} else {
|
|
132
|
-
console.error("❌ 启动失败");
|
|
133
|
-
process.exit(1);
|
|
134
|
-
}
|
|
117
|
+
await import(mainPath);
|
|
135
118
|
}
|
|
136
119
|
function stopDaemon() {
|
|
137
120
|
const pid = getRunningPid();
|
|
@@ -169,11 +152,11 @@ async function runConfig() {
|
|
|
169
152
|
await import(configPath);
|
|
170
153
|
}
|
|
171
154
|
function getLatestLogFile() {
|
|
172
|
-
if (!fs__namespace.existsSync(LOGS_DIR)) {
|
|
155
|
+
if (!fs__namespace.existsSync(paths.LOGS_DIR)) {
|
|
173
156
|
return null;
|
|
174
157
|
}
|
|
175
|
-
const files = fs__namespace.readdirSync(LOGS_DIR).filter((f) => f.endsWith(".log")).sort().reverse();
|
|
176
|
-
return files.length > 0 ? path__namespace.join(LOGS_DIR, files[0]) : null;
|
|
158
|
+
const files = fs__namespace.readdirSync(paths.LOGS_DIR).filter((f) => f.endsWith(".log")).sort().reverse();
|
|
159
|
+
return files.length > 0 ? path__namespace.join(paths.LOGS_DIR, files[0]) : null;
|
|
177
160
|
}
|
|
178
161
|
function showLogs() {
|
|
179
162
|
const runningPid = getRunningPid();
|
|
@@ -182,7 +165,7 @@ function showLogs() {
|
|
|
182
165
|
console.log(" 使用 'polycopy -d' 启动后台进程");
|
|
183
166
|
return;
|
|
184
167
|
}
|
|
185
|
-
if (!fs__namespace.existsSync(LOGS_DIR)) {
|
|
168
|
+
if (!fs__namespace.existsSync(paths.LOGS_DIR)) {
|
|
186
169
|
console.log("ℹ️ 日志目录不存在");
|
|
187
170
|
return;
|
|
188
171
|
}
|
|
@@ -199,7 +182,7 @@ function showLogs() {
|
|
|
199
182
|
let tail = child_process.spawn("tail", ["-f", "-n", "100", currentLogFile], {
|
|
200
183
|
stdio: "inherit"
|
|
201
184
|
});
|
|
202
|
-
const watcher = fs__namespace.watch(LOGS_DIR, (eventType, filename) => {
|
|
185
|
+
const watcher = fs__namespace.watch(paths.LOGS_DIR, (eventType, filename) => {
|
|
203
186
|
if (eventType === "rename" && filename?.endsWith(".log")) {
|
|
204
187
|
const newLogFile = getLatestLogFile();
|
|
205
188
|
if (newLogFile && newLogFile !== currentLogFile) {
|
|
@@ -252,7 +235,7 @@ async function main() {
|
|
|
252
235
|
break;
|
|
253
236
|
case "-d":
|
|
254
237
|
case "--daemon":
|
|
255
|
-
startDaemon();
|
|
238
|
+
await startDaemon();
|
|
256
239
|
break;
|
|
257
240
|
case void 0:
|
|
258
241
|
case "start":
|
package/dist/config.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
const init = require("./init-
|
|
2
|
+
const init = require("./init-C2C8sTcQ.js");
|
|
3
3
|
const grammy = require("grammy");
|
|
4
4
|
const wallet = require("@ethersproject/wallet");
|
|
5
5
|
const clobClient = require("@polymarket/clob-client");
|
|
@@ -9,8 +9,29 @@ const accounts = require("viem/accounts");
|
|
|
9
9
|
const chains = require("viem/chains");
|
|
10
10
|
const builderRelayerClient = require("@polymarket/builder-relayer-client");
|
|
11
11
|
const setPromiseInterval = require("set-promise-interval");
|
|
12
|
+
const child_process = require("child_process");
|
|
13
|
+
const fs = require("fs");
|
|
14
|
+
const paths = require("./paths-C5xeA_8E.js");
|
|
12
15
|
const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
|
|
16
|
+
function _interopNamespace(e) {
|
|
17
|
+
if (e && e.__esModule) return e;
|
|
18
|
+
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
19
|
+
if (e) {
|
|
20
|
+
for (const k in e) {
|
|
21
|
+
if (k !== "default") {
|
|
22
|
+
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
23
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
get: () => e[k]
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
n.default = e;
|
|
31
|
+
return Object.freeze(n);
|
|
32
|
+
}
|
|
13
33
|
const setPromiseInterval__default = /* @__PURE__ */ _interopDefault(setPromiseInterval);
|
|
34
|
+
const fs__namespace = /* @__PURE__ */ _interopNamespace(fs);
|
|
14
35
|
class Notifier {
|
|
15
36
|
bot = null;
|
|
16
37
|
adminChatId = null;
|
|
@@ -959,6 +980,29 @@ class Trader {
|
|
|
959
980
|
}
|
|
960
981
|
}
|
|
961
982
|
const trader = new Trader();
|
|
983
|
+
function enterDaemonMode() {
|
|
984
|
+
if (!fs__namespace.existsSync(paths.PID_DIR)) {
|
|
985
|
+
fs__namespace.mkdirSync(paths.PID_DIR, { recursive: true });
|
|
986
|
+
}
|
|
987
|
+
const child = child_process.spawn(process.execPath, [__filename], {
|
|
988
|
+
detached: true,
|
|
989
|
+
stdio: "ignore",
|
|
990
|
+
env: { ...process.env, POLYCOPY_DAEMON: void 0 }
|
|
991
|
+
});
|
|
992
|
+
if (child.pid) {
|
|
993
|
+
fs__namespace.writeFileSync(paths.PID_FILE, child.pid.toString());
|
|
994
|
+
init.logger.blank();
|
|
995
|
+
init.logger.success(`已进入后台模式 (PID: ${child.pid})`);
|
|
996
|
+
init.logger.line("", `日志目录: ${paths.LOGS_DIR}`);
|
|
997
|
+
init.logger.line("", `使用 'polycopy stop' 停止`);
|
|
998
|
+
init.logger.line("", `使用 'polycopy log' 查看日志`);
|
|
999
|
+
child.unref();
|
|
1000
|
+
process.exit(0);
|
|
1001
|
+
} else {
|
|
1002
|
+
init.logger.error("进入后台模式失败");
|
|
1003
|
+
process.exit(1);
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
962
1006
|
async function main() {
|
|
963
1007
|
init.logger.init();
|
|
964
1008
|
init.logger.section("PolyMarket 跟单机器人");
|
|
@@ -1036,6 +1080,10 @@ async function main() {
|
|
|
1036
1080
|
};
|
|
1037
1081
|
process.on("SIGINT", shutdown);
|
|
1038
1082
|
process.on("SIGTERM", shutdown);
|
|
1083
|
+
if (process.env.POLYCOPY_DAEMON === "1") {
|
|
1084
|
+
enterDaemonMode();
|
|
1085
|
+
return;
|
|
1086
|
+
}
|
|
1039
1087
|
await listener.start(() => {
|
|
1040
1088
|
if (trader.isInitialized()) {
|
|
1041
1089
|
trader.startRedeemer();
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
const readline = require("readline");
|
|
3
3
|
const fsExtra = require("fs-extra");
|
|
4
4
|
const path = require("path");
|
|
5
|
+
const paths = require("./paths-C5xeA_8E.js");
|
|
5
6
|
const grammy = require("grammy");
|
|
6
7
|
const winston = require("winston");
|
|
7
8
|
const DailyRotateFile = require("winston-daily-rotate-file");
|
|
@@ -32,7 +33,7 @@ class LocalStorage {
|
|
|
32
33
|
dbPath;
|
|
33
34
|
data;
|
|
34
35
|
constructor(dbFileName) {
|
|
35
|
-
this.dbPath = path__default.default.join(
|
|
36
|
+
this.dbPath = path__default.default.join(paths.LOCALSTORAGE_DIR, `${dbFileName}.json`);
|
|
36
37
|
if (fsExtra__default.default.existsSync(this.dbPath)) {
|
|
37
38
|
this.data = fsExtra__default.default.readJSONSync(this.dbPath);
|
|
38
39
|
} else {
|
|
@@ -69,7 +70,6 @@ function hasTradingConfig(config) {
|
|
|
69
70
|
function canAutoTrade(config) {
|
|
70
71
|
return hasPolyMarketConfig(config) && hasTradingConfig(config);
|
|
71
72
|
}
|
|
72
|
-
const LOG_DIR = path__default.default.resolve(process.cwd(), "logs");
|
|
73
73
|
const formatTimestamp = () => {
|
|
74
74
|
const now = /* @__PURE__ */ new Date();
|
|
75
75
|
const pad = (n) => n.toString().padStart(2, "0");
|
|
@@ -79,7 +79,7 @@ const customFormat = winston__default.default.format.printf(({ message }) => {
|
|
|
79
79
|
return `[${formatTimestamp()}] ${message}`;
|
|
80
80
|
});
|
|
81
81
|
const fileTransport = new DailyRotateFile__default.default({
|
|
82
|
-
dirname:
|
|
82
|
+
dirname: paths.LOGS_DIR,
|
|
83
83
|
filename: "%DATE%",
|
|
84
84
|
extension: ".log",
|
|
85
85
|
datePattern: "YYYY-MM-DD",
|
|
@@ -105,7 +105,7 @@ class Logger {
|
|
|
105
105
|
if (this.initialized) return;
|
|
106
106
|
this.initialized = true;
|
|
107
107
|
console.log(`
|
|
108
|
-
📁 日志目录: ${
|
|
108
|
+
📁 日志目录: ${paths.LOGS_DIR}`);
|
|
109
109
|
}
|
|
110
110
|
/**
|
|
111
111
|
* 获取带时间前缀的消息(用于控制台输出)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
|
|
4
|
+
const path__default = /* @__PURE__ */ _interopDefault(path);
|
|
5
|
+
const PROJECT_ROOT = path__default.default.join(__dirname, "..", "..");
|
|
6
|
+
const LOGS_DIR = path__default.default.join(PROJECT_ROOT, "logs");
|
|
7
|
+
const LOCALSTORAGE_DIR = path__default.default.join(PROJECT_ROOT, "localstorage");
|
|
8
|
+
const PID_DIR = path__default.default.join(process.env.HOME || PROJECT_ROOT, ".polycopy");
|
|
9
|
+
const PID_FILE = path__default.default.join(PID_DIR, "polycopy.pid");
|
|
10
|
+
exports.LOCALSTORAGE_DIR = LOCALSTORAGE_DIR;
|
|
11
|
+
exports.LOGS_DIR = LOGS_DIR;
|
|
12
|
+
exports.PID_DIR = PID_DIR;
|
|
13
|
+
exports.PID_FILE = PID_FILE;
|
package/package.json
CHANGED