Repository navigation
Expand file tree
/
Copy pathagent.mjs
More file actions
59 lines (52 loc) · 2.26 KB
/
Copy pathagent.mjs
File metadata and controls
59 lines (52 loc) · 2.26 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
#!/usr/bin/env node
// 04-mcap-trigger: limit buy when token mcap drops to target.
//
// Usage: node agent.mjs
import { spawn } from 'node:child_process';
const BONK_MINT = 'DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263';
const TARGET_MCAP_USD = 500_000_000; // $500M
const SOL_LAMPORTS = 1_000_000_000;
const BUY_AMOUNT_SOL = 0.5;
const SLIPPAGE_BPS = 1500;
const EXPIRY_HOURS = 168;
async function callMcp(toolName, args) {
return new Promise((resolve, reject) => {
const proc = spawn('npx', ['-y', '@traderouter/trade-router-mcp'], { env: process.env, stdio: ['pipe', 'pipe', 'inherit'] });
let buf = '';
proc.stdout.on('data', (c) => {
buf += c.toString();
for (const line of buf.split('\n')) {
try {
const msg = JSON.parse(line);
if (msg.id === 2 && msg.result) {
proc.kill();
resolve(JSON.parse(msg.result.content[0].text));
return;
}
} catch {}
}
});
proc.on('error', reject);
proc.stdin.write(JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'initialize', params: { protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 'cookbook-04', version: '1.0' } } }) + '\n');
proc.stdin.write(JSON.stringify({ jsonrpc: '2.0', id: 2, method: 'tools/call', params: { name: toolName, arguments: args } }) + '\n');
});
}
async function main() {
const wallet = await callMcp('get_wallet_address', {});
if (!wallet.configured) { console.error('TRADEROUTER_PRIVATE_KEY missing'); process.exit(1); }
console.log('Wallet:', wallet.wallet_address);
console.log('DRY_RUN:', process.env.TRADEROUTER_DRY_RUN === 'true' ? 'YES' : 'NO (LIVE)');
const order = await callMcp('place_limit_order', {
wallet_address: wallet.wallet_address,
token_address: BONK_MINT,
action: 'buy',
target: TARGET_MCAP_USD,
amount: Math.round(BUY_AMOUNT_SOL * SOL_LAMPORTS),
slippage: SLIPPAGE_BPS,
expiry_hours: EXPIRY_HOURS,
});
console.log('Limit-buy order placed:');
console.log(JSON.stringify(order, null, 2));
console.log('\nServer monitors mcap ~every 5s. The order_created event is signed; the params_hash includes target mcap so the server cannot trigger at a different price.');
}
main().catch((e) => { console.error(e); process.exit(1); });