-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
74 lines (61 loc) · 2.09 KB
/
app.js
File metadata and controls
74 lines (61 loc) · 2.09 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
const express = require('express');
const ethers = require('ethers');
const app = express();
const port = 3000;
// Middleware to parse JSON data
app.use(express.json());
// Route 1: Validate Ethereum Wallet Address
app.post('/validate', (req, res) => {
const { address } = req.body;
try {
// Attempt to create an Ethereum address instance
ethers.utils.getAddress(address);
// If getAddress does not throw an error, the address is valid
res.json({ isValid: true });
} catch (error) {
// getAddress will throw an error for an invalid address
res.json({ isValid: false });
}
});
// Route 2: Create a new Ethereum Wallet
app.get('/create-wallet', (req, res) => {
const wallet = ethers.Wallet.createRandom();
res.json({
address: wallet.address,
privateKey: wallet.privateKey
});
});
// Route 3: Get Latest 1000 Ethereum Transactions
app.get('/latest-transactions', async (req, res) => {
try {
const provider = ethers.getDefaultProvider(); // Use default Ethereum provider
// Get latest block number
const blockNumber = await provider.getBlockNumber();
// Get transactions from the latest 1000 blocks
const transactions = [];
for (let i = blockNumber; i > blockNumber - 1000; i--) {
const block = await provider.getBlockWithTransactions(i);
transactions.push(...block.transactions);
}
// Sort transactions by ether amount
transactions.sort((a, b) => {
return ethers.utils.bigNumberify(b.value).sub(ethers.utils.bigNumberify(a.value));
});
// Extract relevant data from transactions
const formattedTransactions = transactions.map(tx => ({
hash: tx.hash,
from: tx.from,
to: tx.to,
value: ethers.utils.formatEther(tx.value),
blockNumber: tx.blockNumber
}));
res.json(formattedTransactions);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal server error' });
}
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});