-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.js
More file actions
166 lines (146 loc) · 7.81 KB
/
tools.js
File metadata and controls
166 lines (146 loc) · 7.81 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// tools.js
// ─────────────────────────────────────────────────────────────
// Handles all external API calls and side effects the agent can trigger.
// This is the "hands" of the agent — Claude decides what to do,
// this file actually does it.
// Responsible for:
// - Calling LI.FI API to get swap quotes
// - Calling LI.FI API to check transfer status
// - Saving/loading the DCA config to config.json
// - Executing on-chain transactions via wallet.js
// ─────────────────────────────────────────────────────────────
const fs = require("fs");
const path = require("path");
const { sendTransaction, getWalletAddress } = require("./wallet");
require("dotenv").config();
// Path to the config file where the DCA rule is stored
const CONFIG_PATH = path.join(__dirname, "config.json");
// Base URL for LI.FI's public API (no API key needed under 200 req/2hr)
const LIFI_BASE_URL = "https://li.quest/v1";
// ─────────────────────────────────────────────────────────────
// getQuote()
// Calls LI.FI's /quote endpoint to get the best available route
// for a token swap or bridge.
// Returns a trimmed result — Claude only sees the summary and key numbers,
// not the raw hex data field (which is too large and causes max_tokens issues).
// The full transactionRequest is preserved for execute_swap to use.
// ─────────────────────────────────────────────────────────────
async function getQuote({ fromChain, toChain, fromToken, toToken, fromAmount, fromAddress }) {
const params = new URLSearchParams({
fromChain,
toChain,
fromToken,
toToken,
fromAmount,
fromAddress,
slippage: "0.005"
});
const response = await fetch(`${LIFI_BASE_URL}/quote?${params}`);
const data = await response.json();
if (data.message) {
throw new Error(`LI.FI quote error: ${data.message}`);
}
const transactionRequest = data.transactionRequest;
const toAmountReadable = (data.estimate?.toAmount / 1e6).toFixed(2);
const feeCostUSD = data.estimate?.feeCosts?.[0]?.amountUSD ?? "unknown";
const gasCostUSD = data.estimate?.gasCosts?.[0]?.amountUSD ?? "unknown";
return {
tool: data.tool,
toAmount: data.estimate?.toAmount,
toAmountMin: data.estimate?.toAmountMin,
feeCostUSD,
gasCostUSD,
// Full transactionRequest preserved so execute_swap can use it
// but Claude sees a clean summary instead of raw hex
transactionRequest: {
to: transactionRequest.to,
value: transactionRequest.value,
gasLimit: transactionRequest.gasLimit,
chainId: transactionRequest.chainId,
data: transactionRequest.data
},
// Human-readable summary — this is what Claude reasons on
summary: `Swap via ${data.tool}: 0.001 ETH → ~${toAmountReadable} USDC. Fee: $${feeCostUSD}, Gas: $${gasCostUSD}`
};
}
// ─────────────────────────────────────────────────────────────
// getStatus()
// Polls LI.FI's /status endpoint to check if a cross-chain
// transfer has completed, is still pending, or has failed.
// txHash — the transaction hash returned after sending a tx
// ─────────────────────────────────────────────────────────────
async function getStatus({ txHash, fromChain, toChain }) {
const params = new URLSearchParams({ txHash });
// fromChain and toChain are optional but speed up the response
if (fromChain) params.append("fromChain", fromChain);
if (toChain) params.append("toChain", toChain);
const response = await fetch(`${LIFI_BASE_URL}/status?${params}`);
const data = await response.json();
return {
status: data.status, // NOT_FOUND | PENDING | DONE | FAILED
substatus: data.substatus, // e.g. COMPLETED, PARTIAL, REFUNDED
txLink: data.txLink // block explorer link for the tx
};
}
// ─────────────────────────────────────────────────────────────
// saveConfig()
// Writes the DCA rule to config.json so the agent remembers it
// across loop iterations.
// Called by Claude when the user gives a new instruction.
// ─────────────────────────────────────────────────────────────
async function saveConfig({ instruction, interval_minutes, from_token, to_token, amount_wei }) {
const config = {
active: true,
instruction,
interval_minutes,
from_token,
to_token,
amount_wei,
updated_at: new Date().toISOString()
};
// Write config as formatted JSON so it's human-readable
fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2));
console.log(`💾 Config saved: ${JSON.stringify(config)}`);
return { success: true, config };
}
// ─────────────────────────────────────────────────────────────
// loadConfig()
// Reads the current DCA rule from config.json.
// Returns null if no config exists yet.
// ─────────────────────────────────────────────────────────────
function loadConfig() {
if (!fs.existsSync(CONFIG_PATH)) return null;
const raw = fs.readFileSync(CONFIG_PATH, "utf-8");
return JSON.parse(raw);
}
// ─────────────────────────────────────────────────────────────
// executeSwap()
// Takes the transactionRequest from a LI.FI quote and sends it
// on-chain via our wallet.
// This is the point of no return — once called, the tx is submitted.
// ─────────────────────────────────────────────────────────────
async function executeSwap({ to, data, value, gasLimit }) {
const txHash = await sendTransaction({ to, data, value, gasLimit });
return { success: true, txHash };
}
// ─────────────────────────────────────────────────────────────
// executeTool()
// Router function — Claude returns a tool name and input object,
// this function maps that to the correct implementation above.
// This is what agent.js calls after Claude decides to use a tool.
// ─────────────────────────────────────────────────────────────
async function executeTool(toolName, toolInput) {
switch (toolName) {
case "get_quote":
return await getQuote(toolInput);
case "get_status":
return await getStatus(toolInput);
case "save_config":
return await saveConfig(toolInput);
case "execute_swap":
return await executeSwap(toolInput);
default:
throw new Error(`Unknown tool: ${toolName}`);
}
}
module.exports = { executeTool, loadConfig };