-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.js
executable file
·367 lines (347 loc) · 12 KB
/
repl.js
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import chalk from 'chalk';
import fs from 'fs';
import os from 'os';
import path from 'path';
import {
createPublicClient,
createWalletClient,
formatEther,
hexToString,
http,
isAddress,
keccak256,
parseEther,
stringToHex,
webSocket
} from 'viem';
import { ipc } from 'viem/node';
// Update configuration options
const config = {
rpcUrl: process.env.ETH_RPC_URL || 'http://127.0.0.1:8545', // Use IPv4 address
wsUrl: process.env.ETH_WS_URL || 'ws://127.0.0.1:8546',
ipcPath: process.env.ETH_IPC_PATH || null,
};
// Function to create the appropriate transport based on the configuration
const createTransport = () => {
const transportOptions = {
timeout: 30000, // 30 seconds
retryCount: 3,
retryDelay: 1000, // 1 second
};
if (config.ipcPath) {
return ipc(config.ipcPath, transportOptions);
} else if (config.rpcUrl.startsWith('http')) {
return http(config.rpcUrl, transportOptions);
} else {
return webSocket(config.wsUrl, transportOptions);
}
};
// Create viem public client with configurable transport
const publicClient = createPublicClient({
transport: createTransport(),
batch: {
multicall: true,
},
pollingInterval: 4000,
});
// Create a wallet client with configurable transport
const walletClient = createWalletClient({
transport: createTransport(),
});
// TODO: Implement proper account management
// For now, we'll use a simplified version that doesn't actually manage accounts
const accountsDir = path.join(os.homedir(), '.ethereum', 'keystore');
if (!fs.existsSync(accountsDir)) {
fs.mkdirSync(accountsDir, { recursive: true });
}
function listAccounts() {
console.log('TODO: Implement proper account listing');
return [];
}
function createAccount(password) {
console.log('TODO: Implement proper account creation');
return '0x0000000000000000000000000000000000000000';
}
// Create context object
const context = {
eth: {
get accounts() {
return listAccounts();
},
coinbase: '0x0000000000000000000000000000000000000000',
defaultAccount: '0x0000000000000000000000000000000000000000',
get protocolVersion() {
return publicClient.request({ method: 'eth_protocolVersion' });
},
get syncing() {
return publicClient.request({ method: 'eth_syncing' });
},
get mining() {
return publicClient.request({ method: 'eth_mining' });
},
gasPrice: async () => {
return formatEther(await publicClient.getGasPrice());
},
getBalance: async (address, blockTag = 'latest') => {
const balance = await publicClient.getBalance({ address, blockTag });
return formatEther(balance);
},
getBlock: async (blockHashOrNumber, fullTransactions = false) => {
return await publicClient.getBlock({ blockHashOrNumber, includeTransactions: fullTransactions });
},
getBlockByNumber: async (blockNumber = 'latest', fullTransactions = false) => {
return await publicClient.getBlock({
blockNumber: blockNumber === 'latest' ? blockNumber : BigInt(blockNumber),
includeTransactions: fullTransactions
});
},
getBlockNumber: async () => {
return await publicClient.getBlockNumber();
},
getCode: async (address, blockTag = 'latest') => {
return await publicClient.getBytecode({ address, blockTag });
},
getGasPrice: async () => {
return formatEther(await publicClient.getGasPrice());
},
getTransactionCount: async (address, blockTag = 'latest') => {
return await publicClient.getTransactionCount({ address, blockTag });
},
sendTransaction: async (tx) => {
const hash = await walletClient.sendTransaction(tx);
return hash;
},
sign: (data, address) => {
console.log('TODO: Implement proper message signing');
return '0x0000000000000000000000000000000000000000000000000000000000000000';
},
call: async (tx, blockTag = 'latest') => {
return await publicClient.call({ ...tx, blockTag });
},
contract: (abi, address) => {
console.log("contracts not supported yet");
return null;
},
getTransactionReceipt: async (txHash) => {
return await publicClient.getTransactionReceipt({ hash: txHash });
},
estimateGas: async (tx) => {
return await publicClient.estimateGas(tx);
},
getStorageAt: async (address, position, blockTag = 'latest') => {
return await publicClient.getStorageAt({ address, slot: position, blockTag });
},
sendRawTransaction: async (signedTx) => {
return await publicClient.sendRawTransaction({ serializedTransaction: signedTx });
},
getPendingTransactions: async () => {
// Note: This method might not be available on all RPC providers
return await publicClient.request({ method: 'eth_pendingTransactions' });
},
subscribe: async (type, ...args) => {
let unsubscribe;
try {
switch (type) {
case 'newHeads':
unsubscribe = await wsClient.watchBlocks(
{ onBlock: (block) => console.log('New block:', block) }
);
break;
case 'logs':
const filter = args[0] || {};
unsubscribe = await wsClient.watchContractEvent({
address: filter.address,
event: filter.topics ? filter.topics[0] : undefined,
args: filter.topics ? filter.topics.slice(1) : undefined,
onLogs: (logs) => console.log('New logs:', logs),
});
break;
case 'newPendingTransactions':
unsubscribe = await wsClient.watchPendingTransactions({
onTransactions: (hashes) => console.log('New pending transactions:', hashes),
});
break;
case 'syncing':
unsubscribe = await wsClient.watchBlockNumber({
onBlockNumber: (blockNumber) => console.log('New block number:', blockNumber),
});
break;
default:
console.error(`Unsupported subscription type: ${type}`);
return;
}
console.log(`Subscribed to ${type}`);
return () => {
unsubscribe();
console.log(`Unsubscribed from ${type}`);
};
} catch (error) {
console.error(`Error subscribing to ${type}:`, error);
}
},
getChainId: async () => {
return await publicClient.getChainId();
},
},
net: {
listening: async () => {
return await publicClient.request({ method: 'net_listening' });
},
peerCount: async () => {
const peerCount = await publicClient.request({ method: 'net_peerCount' });
return parseInt(peerCount, 16);
},
version: async () => {
return await publicClient.request({ method: 'net_version' });
},
},
admin: {
nodeInfo: async () => {
return await publicClient.request({ method: 'admin_nodeInfo' });
},
peers: async () => {
return await publicClient.request({ method: 'admin_peers' });
},
datadir: () => {
return process.cwd();
},
addPeer: async (enode) => {
return await publicClient.request({ method: 'admin_addPeer', params: [enode] });
},
removePeer: async (enode) => {
return await publicClient.request({ method: 'admin_removePeer', params: [enode] });
},
},
web3: {
fromWei: (value, unit = 'ether') => formatEther(BigInt(value)),
toWei: (value, unit = 'ether') => parseEther(value).toString(),
hexToAscii: hexToString,
asciiToHex: stringToHex,
sha3: (data) => keccak256(stringToHex(data)),
isAddress: isAddress,
},
personal: {
newAccount: (password) => {
console.log('TODO: Implement proper account creation');
return '0x0000000000000000000000000000000000000000';
},
unlockAccount: (address, password, duration) => {
console.log('TODO: Implement proper account unlocking');
return false;
},
},
loadScript: (filename) => {
const filePath = path.resolve(filename);
if (fs.existsSync(filePath)) {
const script = fs.readFileSync(filePath, 'utf8');
try {
eval(script);
console.log(`Loaded and executed script: ${filename}`);
} catch (error) {
console.error(`Error executing script ${filename}:`, error);
}
} else {
console.error(`Script not found: ${filename}`);
}
},
txpool: {
status: async () => {
// Note: This method might not be available on all RPC providers
return await publicClient.request({ method: 'txpool_status' });
},
inspect: async () => {
// Note: This method might not be available on all RPC providers
return await publicClient.request({ method: 'txpool_inspect' });
},
content: async () => {
// Note: This method might not be available on all RPC providers
return await publicClient.request({ method: 'txpool_content' });
},
},
debug: {
traceTransaction: async (txHash, options) => {
return await publicClient.request({ method: 'debug_traceTransaction', params: [txHash, options] });
},
getBlockRlp: async (blockNumber) => {
return await publicClient.request({ method: 'debug_getBlockRlp', params: [blockNumber] });
},
printBlock: async (blockNumber) => {
return await publicClient.request({ method: 'debug_printBlock', params: [blockNumber] });
},
},
miner: {
start: async (threads) => {
return await publicClient.request({ method: 'miner_start', params: [threads] });
},
stop: async () => {
return await publicClient.request({ method: 'miner_stop' });
},
setEtherbase: async (address) => {
return await publicClient.request({ method: 'miner_setEtherbase', params: [address] });
},
setGasPrice: async (gasPrice) => {
return await publicClient.request({ method: 'miner_setGasPrice', params: [gasPrice] });
},
},
};
// Add some utility functions
context.getBalance = context.eth.getBalance;
context.getBlock = context.eth.getBlock;
context.getTransaction = async (txHash) => await publicClient.getTransaction({ hash: txHash });
async function startRepl() {
const repl = await import('node:repl');
// Create a custom eval function that handles Promises and ignores empty lines
const evalWithPromiseResolution = async (cmd, context, filename, callback) => {
cmd = cmd.trim();
if (!cmd) {
callback(null);
return;
}
try {
let result = eval(cmd);
if (result instanceof Promise) {
result = await result;
}
callback(null, result);
} catch (err) {
callback(err);
}
};
try {
console.log(chalk.yellow('Connecting to Ethereum node...'));
const chainId = await publicClient.getChainId();
const blockNumber = await publicClient.getBlockNumber();
console.log(chalk.green('Successfully connected to Ethereum node'));
console.log(chalk.yellow(`Chain ID: ${chainId}`));
console.log(chalk.yellow(`Latest block: ${blockNumber}`));
} catch (error) {
console.error(chalk.red('Failed to connect to Ethereum node:'), error);
console.log(chalk.yellow('Please check your node configuration and try again.'));
process.exit(1);
}
// Display welcome message before starting REPL
console.log(chalk.blue('Eth JavaScript console'));
console.log(chalk.yellow(`Connected to ${config.ipcPath ? 'IPC' : (config.rpcUrl.startsWith('http') ? 'HTTP' : 'WebSocket')}`));
console.log(chalk.yellow(`Endpoint: ${config.ipcPath || config.rpcUrl || config.wsUrl}`));
console.log(chalk.magenta('Available modules:'), chalk.cyan(Object.keys(context).join(', ')));
console.log(chalk.red('\nTo exit, press ctrl-d or type .exit'));
// Start the REPL server
const replServer = repl.start({
prompt: chalk.green('eth> '),
useGlobal: true,
eval: evalWithPromiseResolution
});
// Extend the REPL context with our custom context
Object.assign(replServer.context, context);
replServer.on('exit', () => {
console.log(chalk.blue('Exiting ETH REPL'));
process.exit();
});
}
if (require.main === module) {
startRepl().catch(error => {
console.error('Failed to start REPL:', error);
process.exit(1);
});
}
module.exports = { startRepl };