-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathformat.js
More file actions
328 lines (291 loc) · 12.7 KB
/
Copy pathformat.js
File metadata and controls
328 lines (291 loc) · 12.7 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* Pretty-print formatters — human-readable output when --pretty is used.
* No external deps — ANSI escape codes + string padding.
*/
const BOLD = "\x1b[1m";
const DIM = "\x1b[2m";
const GREEN = "\x1b[32m";
const RED = "\x1b[31m";
const YELLOW = "\x1b[33m";
const CYAN = "\x1b[36m";
const RESET = "\x1b[0m";
function pad(str, len) {
return String(str).padEnd(len);
}
function padStart(str, len) {
return String(str).padStart(len);
}
function usd(value) {
if (value == null) return "-";
return `$${Number(value).toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
}
function pct(value) {
if (value == null) return "-";
const n = Number(value);
const color = n >= 0 ? GREEN : RED;
return `${color}${n >= 0 ? "+" : ""}${n.toFixed(2)}%${RESET}`;
}
// --- Policy display helpers (shared by list/show/create policy commands) ---
import { fromCaip2 } from "../chain/registry.js";
export function formatPolicyRules(rules) {
return (rules || []).map((r) => {
if (r.type === "allowed_chains") {
return { type: r.type, chains: r.chain_ids.map(fromCaip2) };
}
return r;
});
}
export function shortenScriptPaths(scripts) {
return (scripts || []).map((s) => s.split("/").pop());
}
// --- Pretty-print formatters ---
export function formatWalletList(data) {
const showing = data.total !== data.count
? `showing ${data.offset + 1}–${data.offset + data.count} of ${data.total}`
: `${data.total}`;
const lines = [`${BOLD}Wallets${RESET} (${showing})\n`];
for (const w of data.wallets) {
const def = w.isDefault ? ` ${CYAN}(default)${RESET}` : "";
lines.push(` ${BOLD}${w.name}${RESET}${def}`);
if (w.evmAddress) lines.push(` ${DIM}EVM:${RESET} ${w.evmAddress}`);
if (w.solAddress) lines.push(` ${DIM}SOL:${RESET} ${w.solAddress}`);
if (w.policies?.length) {
for (const p of w.policies) {
const detail = p.summary ? ` ${DIM}(${p.summary})${RESET}` : "";
lines.push(` ${DIM}Policy:${RESET} ${p.name}${detail}`);
}
}
lines.push("");
}
if (data.hasMore) {
lines.push(` ${DIM}Use --offset ${data.offset + data.limit} to see more${RESET}\n`);
}
return lines.join("\n");
}
export function formatSearch(data) {
const lines = [`${BOLD}Search: "${data.query}"${RESET} — ${data.count} results\n`];
lines.push(` ${DIM}${pad("Token", 20)} ${padStart("Price", 12)} ${padStart("24h", 10)} ${padStart("MCap", 14)}${RESET}`);
lines.push(` ${DIM}${"─".repeat(58)}${RESET}`);
for (const r of data.results) {
const verified = r.verified ? "✓" : " ";
lines.push(
` ${verified} ${pad(`${r.symbol} (${r.name})`, 18)} ${padStart(usd(r.price), 12)} ${padStart(pct(r.change_24h), 20)} ${padStart(usd(r.market_cap), 14)}`
);
}
return lines.join("\n");
}
export function formatPortfolio(data) {
const lines = [
`${BOLD}Portfolio${RESET} — ${data.wallet.name} ${DIM}(${data.wallet.address.slice(0, 8)}...)${RESET}\n`,
` Total: ${BOLD}${usd(data.portfolio.total)}${RESET} 24h: ${pct(data.portfolio.change_24h)}\n`,
];
if (data.positions.length > 0) {
lines.push(` ${DIM}${pad("Token", 16)} ${pad("Chain", 12)} ${padStart("Value", 12)} ${padStart("Amount", 16)}${RESET}`);
lines.push(` ${DIM}${"─".repeat(58)}${RESET}`);
for (const p of data.positions) {
lines.push(
` ${pad(p.symbol || "?", 16)} ${pad(p.chain || "?", 12)} ${padStart(usd(p.value), 12)} ${padStart(p.quantity?.toFixed(4) || "-", 16)}`
);
}
}
return lines.join("\n");
}
export function formatPositions(data) {
const walletLabel = data.wallet.name || data.wallet.address.slice(0, 10) + "...";
const lines = [
`${BOLD}Positions${RESET} — ${walletLabel} (${data.count})\n`,
` ${DIM}${pad("Token", 16)} ${pad("Chain", 12)} ${padStart("Value", 12)} ${padStart("24h", 18)} ${padStart("Amount", 16)}${RESET}`,
` ${DIM}${"─".repeat(76)}${RESET}`,
];
for (const p of data.positions) {
const change = formatChange(p);
lines.push(
` ${pad(p.symbol || "?", 16)} ${pad(p.chain || "?", 12)} ${padStart(usd(p.value), 12)} ${padStart(change, 28)} ${padStart(p.quantity?.toFixed(4) || "-", 16)}`
);
}
return lines.join("\n");
}
function formatChange(position) {
if (position.change_percent_1d == null) {
return `${DIM}-${RESET}`;
}
const percent = pct(position.change_percent_1d);
if (position.change_absolute_1d == null) {
return percent;
}
const sign = position.change_absolute_1d >= 0 ? "+" : "";
return `${percent} (${sign}${usd(position.change_absolute_1d)})`;
}
function resolveTradeType(data) {
if (data.swap) return { label: "Swap", detail: data.swap };
if (data.bridge) return { label: "Bridge", detail: data.bridge };
if (data.buy) return { label: "Buy", detail: data.buy };
if (data.send) return { label: "Send", detail: data.send };
return { label: "Sell", detail: data.sell };
}
export function formatSwapQuote(data) {
const { label: type, detail: swap } = resolveTradeType(data);
const lines = [`${BOLD}${type} Quote${RESET}\n`];
if (swap.input) lines.push(` ${DIM}Input:${RESET} ${swap.input}`);
if (swap.output) lines.push(` ${DIM}Output:${RESET} ~${swap.output}`);
if (swap.spending) lines.push(` ${DIM}Spending:${RESET} ${swap.spending}`);
if (swap.receiving) lines.push(` ${DIM}Receive:${RESET} ${swap.receiving}`);
if (swap.selling) lines.push(` ${DIM}Selling:${RESET} ${swap.selling}`);
if (swap.token) lines.push(` ${DIM}Token:${RESET} ${swap.amount} ${swap.token}`);
if (swap.from) lines.push(` ${DIM}From:${RESET} ${swap.from}`);
if (swap.to) lines.push(` ${DIM}To:${RESET} ${swap.to}`);
if (swap.chain) lines.push(` ${DIM}Chain:${RESET} ${swap.chain}`);
if (swap.fee?.protocolPercent != null) {
lines.push(` ${DIM}Fee:${RESET} ${swap.fee.protocolPercent}%`);
}
if (swap.source) lines.push(` ${DIM}Source:${RESET} ${swap.source}`);
if (swap.estimatedTime) lines.push(` ${DIM}Time:${RESET} ${swap.estimatedTime}`);
if (data.tx) {
lines.push("");
const status = data.tx.status === "success" ? `${GREEN}✓ Success${RESET}` : `${RED}✗ Failed${RESET}`;
lines.push(` ${status}`);
lines.push(` ${DIM}Hash:${RESET} ${data.tx.hash}`);
lines.push(` ${DIM}Block:${RESET} ${data.tx.blockNumber}`);
lines.push(` ${DIM}Gas:${RESET} ${data.tx.gasUsed}`);
} else if (data.action) {
lines.push(`\n ${YELLOW}${data.action}${RESET}`);
}
return lines.join("\n");
}
export function formatHistory(data) {
const lines = [`${BOLD}Transactions${RESET} — ${data.wallet.name} (${data.count})\n`];
for (const tx of data.transactions) {
const status = tx.status === "confirmed" ? `${GREEN}✓${RESET}` : `${YELLOW}⏳${RESET}`;
lines.push(` ${status} ${DIM}${tx.timestamp || "?"}${RESET} ${tx.type || "unknown"} ${DIM}${tx.chain || ""}${RESET}`);
for (const t of tx.transfers || []) {
const dir = t.direction === "in" ? `${GREEN}+${RESET}` : `${RED}-${RESET}`;
lines.push(` ${dir} ${t.quantity} ${t.fungible || "?"} ${DIM}(${usd(t.value)})${RESET}`);
}
}
return lines.join("\n");
}
export function formatChains(data) {
const lines = [`${BOLD}Supported Chains${RESET} (${data.count})\n`];
for (const c of data.chains) {
lines.push(` ${pad(c.id, 22)} ${pad(c.name, 14)} ${DIM}(${c.nativeCurrency})${RESET}`);
}
return lines.join("\n");
}
export function formatAnalysis(data) {
const label = data.label ? `${data.label} ` : "";
const lines = [
`${BOLD}Analysis${RESET} — ${label}${DIM}(${data.address.slice(0, 8)}...)${RESET} Period: ${data.period}\n`,
` Portfolio: ${BOLD}${usd(data.portfolio.total)}${RESET}`,
"",
` ${BOLD}Activity${RESET}`,
` Transactions: ${data.activity.transactions}`,
` Swaps: ${data.activity.swaps}`,
` Transfers: ${data.activity.transfers}`,
` Volume: ${usd(data.activity.volumeUsd)}`,
` Chains: ${data.activity.chains.join(", ") || "none"}`,
];
if (data.pnl.totalGain != null) {
lines.push("");
lines.push(` ${BOLD}PnL${RESET}`);
lines.push(` Total Gain: ${usd(data.pnl.totalGain)} ${pct(data.pnl.totalGainPercent)}`);
if (data.pnl.realizedGain != null) lines.push(` Realized: ${usd(data.pnl.realizedGain)}`);
if (data.pnl.unrealizedGain != null) lines.push(` Unrealized: ${usd(data.pnl.unrealizedGain)}`);
}
return lines.join("\n");
}
export function formatPnl(data) {
const p = data.pnl;
const lines = [`${BOLD}PnL${RESET} — ${data.wallet.name}\n`];
if (p.totalGain != null) lines.push(` Total Gain: ${usd(p.totalGain)} ${pct(p.totalGainPercent)}`);
if (p.realizedGain != null) lines.push(` Realized: ${usd(p.realizedGain)}`);
if (p.unrealizedGain != null) lines.push(` Unrealized: ${usd(p.unrealizedGain)}`);
if (p.totalInvested != null) lines.push(` Total Invested: ${usd(p.totalInvested)}`);
if (p.netInvested != null) lines.push(` Net Invested: ${usd(p.netInvested)}`);
if (p.totalFees != null) lines.push(` Fees Paid: ${usd(p.totalFees)}`);
return lines.join("\n");
}
export function formatOverview(data) {
const walletLabel = data.label || data.wallet?.query || "Unknown";
const addr = data.wallet?.query || "";
const shortAddr = addr.length > 12 ? `${addr.slice(0, 6)}...${addr.slice(-4)}` : addr;
const lines = [];
// Header
lines.push(`${BOLD}━━━ Wallet Analysis ━━━${RESET}`);
lines.push(` ${BOLD}${walletLabel}${RESET} ${DIM}${shortAddr}${RESET}`);
lines.push("");
// Portfolio
if (data.portfolio) {
const total = data.portfolio.total;
lines.push(` ${BOLD}💰 Portfolio${RESET} ${BOLD}${usd(total)}${RESET}`);
const ch = data.portfolio.change_1d;
if (ch) {
const absChange = usd(ch.absolute_1d);
const pctChange = pct(ch.percent_1d);
lines.push(` ${DIM}24h:${RESET} ${pctChange} (${absChange})`);
}
// Top chains by value
if (data.portfolio.chains) {
const chainEntries = Object.entries(data.portfolio.chains)
.sort((a, b) => b[1] - a[1])
.slice(0, 5);
if (chainEntries.length > 0) {
lines.push("");
lines.push(` ${DIM}Top chains:${RESET}`);
for (const [chain, val] of chainEntries) {
lines.push(` ${pad(chain, 22)} ${padStart(usd(val), 14)}`);
}
}
}
lines.push("");
}
// Top Positions
if (data.positions && data.positions.count > 0) {
lines.push(` ${BOLD}📊 Top Positions${RESET} (${data.positions.count} total)`);
lines.push(` ${DIM}${pad("Token", 20)} ${pad("Chain", 14)} ${padStart("Value", 14)} ${padStart("Amount", 16)}${RESET}`);
lines.push(` ${DIM}${"─".repeat(66)}${RESET}`);
for (const p of data.positions.top) {
const sym = p.symbol ? `${p.name} (${p.symbol})` : p.name;
const qty = p.quantity != null ? p.quantity.toFixed(4) : "-";
lines.push(` ${pad(sym, 20)} ${pad(p.chain || "?", 14)} ${padStart(usd(p.value), 14)} ${padStart(qty, 16)}`);
}
lines.push("");
} else {
lines.push(` ${DIM}📊 No positions data${RESET}`);
lines.push("");
}
// Recent Transactions
if (data.transactions && data.transactions.sampled > 0) {
lines.push(` ${BOLD}📝 Recent Transactions${RESET} (${data.transactions.sampled} sampled)`);
for (const tx of data.transactions.recent) {
const status = tx.status === "confirmed" ? `${GREEN}✓${RESET}` : tx.status === "pending" ? `${YELLOW}⏳${RESET}` : `${DIM}${tx.status || "?"}${RESET}`;
const type = tx.operation_type || "unknown";
const time = tx.mined_at ? new Date(tx.mined_at * 1000).toISOString().replace("T", " ").slice(0, 16) : "?";
lines.push(` ${status} ${DIM}${time}${RESET} ${type}`);
for (const t of tx.transfers || []) {
const dir = t.direction === "in" ? `${GREEN}+${RESET}` : `${RED}-${RESET}`;
const name = t.fungible_info?.symbol || t.fungible_info?.name || "?";
lines.push(` ${dir} ${t.quantity || "?"} ${name} ${DIM}${usd(t.value)}${RESET}`);
}
}
lines.push("");
} else {
lines.push(` ${DIM}📝 No recent transactions${RESET}`);
lines.push("");
}
// PnL
if (data.pnl && data.pnl.available && data.pnl.summary) {
const s = data.pnl.summary;
lines.push(` ${BOLD}📈 PnL${RESET}`);
if (s.total_gain != null) lines.push(` Total Gain: ${usd(s.total_gain)}`);
if (s.realized_gain != null) lines.push(` Realized: ${usd(s.realized_gain)}`);
if (s.unrealized_gain != null) lines.push(` Unrealized: ${usd(s.unrealized_gain)}`);
} else {
lines.push(` ${DIM}📈 PnL not available${RESET}`);
}
// Failures
if (data.failures && data.failures.length > 0) {
lines.push("");
lines.push(` ${YELLOW}⚠ Some data unavailable: ${data.failures.join(", ")}${RESET}`);
}
return lines.join("\n");
}