forked from fciaf420/MeteorShower
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
80 lines (69 loc) · 2.59 KB
/
cli.js
File metadata and controls
80 lines (69 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// ───────────────────────────────────────────────
// ~/cli.js
// ───────────────────────────────────────────────
import 'dotenv/config';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { main } from './main.js';
import { closeAllPositions } from './close-position.js';
function loadEnv() {
const cfg = {
RPC_URL : process.env.RPC_URL,
WALLET_PATH : process.env.WALLET_PATH,
LOG_LEVEL : process.env.LOG_LEVEL ?? 'info'
};
if (!cfg.RPC_URL) throw new Error('RPC_URL is not set');
if (!cfg.WALLET_PATH) throw new Error('WALLET_PATH is not set');
return cfg;
}
function parseArgs() {
const argv = yargs(hideBin(process.argv))
.command('run', 'start the liquidity bot', y =>
y.option('interval', {
alias : 'i',
type : 'number',
default : 5,
describe : 'Monitor tick interval in seconds'
})
)
.command('close', '⚡ EMERGENCY: close all positions and swap to SOL', () => {})
.example('$0 run', 'Start the bot with 5 second monitoring')
.example('$0 run -i 30', 'Start the bot with 30 second monitoring')
.example('$0 close', 'Emergency close all positions and swap to SOL')
.epilogue('💡 TIP: While bot is running, press Ctrl+C twice quickly for emergency exit')
.demandCommand(1)
.strict()
.help()
.parse();
return argv;
}
async function runCli() {
try {
// Parse args first to handle --help before loading env
const argv = parseArgs();
const { interval, _ } = argv;
const command = _[0]; // Get the command (run, close, etc.)
// Only load env if we're actually running a command (not just showing help)
const env = loadEnv();
if (command === 'close') {
// Handle close positions command
console.log('🔄 Closing all positions and swapping to SOL...');
await closeAllPositions();
return;
}
// Default to 'run' command behavior
await main({
...env,
MONITOR_INTERVAL_SECONDS : interval
});
} catch (err) {
// Always exit with non-zero so systemd / Kubernetes knows it failed
console.error('❌', err.message);
process.exit(1);
}
}
// Only run automatically if this file is invoked directly
if (import.meta.url === `file://${process.argv[1]}`) {
runCli();
}
export { loadEnv, parseArgs, runCli };