-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui.jsx
More file actions
executable file
·194 lines (178 loc) · 5.2 KB
/
tui.jsx
File metadata and controls
executable file
·194 lines (178 loc) · 5.2 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
#!/usr/bin/env tsx
import React from 'react';
import { render, Box, Text, useInput } from 'ink';
import { spawn } from 'node:child_process';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import meow from 'meow';
import { replayTxFlow } from './replayTxFlow.mjs';
const cli = meow(`
Usage
$ comet-txflow
Options
--from-block, -f Starting block
--to-block, -t Ending block
--limit, -l Max transactions
--json, -j JSON output (disables TUI)
Examples
$ comet-txflow --limit 5
`, {
importMeta: import.meta,
flags: {
fromBlock: {
type: 'string',
shortFlag: 'f',
},
toBlock: {
type: 'string',
shortFlag: 't',
},
limit: {
type: 'number',
shortFlag: 'l',
default: 5
},
json: {
type: 'boolean',
shortFlag: 'j',
default: false
},
noDashboard: {
type: 'boolean',
shortFlag: 'd',
default: false
},
watch: {
type: 'boolean',
shortFlag: 'w',
default: false
},
interval: {
type: 'number',
default: 10000
},
view: {
type: 'string',
default: 'expanded'
},
web: {
type: 'boolean',
default: false
}
}
});
// If JSON mode, print results and exit
if (cli.flags.json) {
const run = async () => {
const results = await replayTxFlow(cli.flags);
// eslint-disable-next-line no-console
console.log(JSON.stringify(results, null, 2));
process.exit(0);
};
run();
}
// Start web dashboard server unless disabled, or when --web is requested
if (!cli.flags.json && (!cli.flags.noDashboard || cli.flags.web)) {
import('./server.js').catch(() => {});
}
// If --web, start Next.js dev server and open browser
if (!cli.flags.json && cli.flags.web) {
const projectRoot = path.dirname(fileURLToPath(import.meta.url));
const dashboardDir = path.join(projectRoot, 'web-dashboard');
try {
const nextProc = spawn('npm', ['run', 'dev', '--', '--port', '3000'], {
cwd: dashboardDir,
stdio: 'ignore',
detached: true,
});
nextProc.unref();
} catch {}
try {
// macOS open
spawn('open', ['http://localhost:3000'], { stdio: 'ignore', detached: true }).unref();
} catch {}
}
const TxDisplay = ({ tx }) => (
<Box flexDirection="column" borderStyle="round" padding={1} marginBottom={1}>
<Text>🔁 Tx Hash: {tx.txHash}</Text>
<Text>📦 Block: {tx.block} | ⏱ {new Date(tx.timestamp).toLocaleString()}</Text>
<Text>👤 From: {tx.from}</Text>
<Text>➡️ To: {tx.to}</Text>
<Text>⛽ Gas: {tx.gasUsed} | ✅ Status: {tx.status}</Text>
<Text>🧠 Function: {tx.function}</Text>
<Text>📥 Args: {JSON.stringify(tx.args)}</Text>
</Box>
);
const formatNumber = (n) => new Intl.NumberFormat().format(Number(n));
const App = () => {
const [txs, setTxs] = React.useState([]);
const [viewMode, setViewMode] = React.useState(
(cli.flags.view === 'minimal' || cli.flags.view === 'expanded') ? cli.flags.view : 'expanded'
);
const [showJson, setShowJson] = React.useState(false);
React.useEffect(() => {
const load = async () => {
const results = await replayTxFlow(cli.flags);
setTxs(results);
};
load();
if (cli.flags.watch) {
const id = setInterval(load, Number(cli.flags.interval) || 10000);
return () => clearInterval(id);
}
}, []);
useInput((input) => {
const key = input.toLowerCase();
if (key === 'm') setViewMode('minimal');
if (key === 'e') setViewMode('expanded');
if (key === 'r') {
(async () => {
const results = await replayTxFlow(cli.flags);
setTxs(results);
})();
}
if (key === 'j') setShowJson((v) => !v);
if (key === 'q') process.exit(0);
});
const stats = React.useMemo(() => {
const total = txs.length;
const failed = txs.filter((t) => t.status !== 1).length;
const totalGas = txs.reduce((acc, t) => {
try { return acc + BigInt(t.gasUsed ?? 0); } catch { return acc; }
}, 0n);
return { total, failed, totalGas: totalGas.toString() };
}, [txs]);
return (
<Box flexDirection="column">
<Text color="cyanBright">📊 Comet TxFlow Replayer - TUI Mode</Text>
<Text>
Controls: [m]inimal [e]xpanded [r]efresh [j]SON toggle [q]uit | View: {viewMode} |
Stats: total {stats.total}, failed {stats.failed}, gas {formatNumber(stats.totalGas)}
</Text>
{txs.length === 0 && <Text>⏳ Loading transactions...</Text>}
{showJson ? (
<Box borderStyle="round" padding={1} marginTop={1}>
<Text>{JSON.stringify(txs, null, 2)}</Text>
</Box>
) : (
<>
{viewMode === 'minimal' && (
<Box flexDirection="column" marginTop={1}>
{txs.map((tx, idx) => (
<Text key={idx}>#{idx + 1} {tx.txHash.slice(0, 10)}… | {tx.function || 'unknown'} | blk {tx.block} | {new Date(tx.timestamp).toLocaleTimeString()}</Text>
))}
</Box>
)}
{viewMode === 'expanded' && (
<>
{txs.map((tx, idx) => <TxDisplay key={idx} tx={tx} />)}
</>
)}
</>
)}
</Box>
);
};
if (!cli.flags.json) {
render(<App />);
}