-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtransferLog.js
More file actions
371 lines (314 loc) · 9.34 KB
/
Copy pathtransferLog.js
File metadata and controls
371 lines (314 loc) · 9.34 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
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
368
369
370
371
import express from 'express';
import cors from 'cors';
import { ethers, formatEther, parseEther } from "ethers";
import axios from "axios";
import { db } from "./firebaseconfig.js";
import { collection, addDoc, getDocs, query, where, orderBy, limit } from "firebase/firestore";
import http from "http";
import https from "https";
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
const axiosInstance = axios.create({
httpAgent: new http.Agent({ keepAlive: false }),
httpsAgent: new https.Agent({ keepAlive: false }),
});
const provider = new ethers.JsonRpcProvider("https://sepolia.drpc.org/");
const wallet = new ethers.Wallet(
process.env.PRIVATE_KEY || "//add default key here, removed by us ",
provider
);
const DEFAULT_RECIPIENT = "0xBc5Fef730fEBc7432230f52868d30Fe07A3cf959";
const SUPPORTED_TOKENS = {
ETH: {
symbol: 'ETH',
name: 'Ethereum',
decimals: 18,
contractAddress: null,
network: 'Sepolia'
},
USDC: {
symbol: 'USDC',
name: 'USD Coin',
decimals: 6,
contractAddress: '0xA0b86a33E6417e88d9C4fE93c72D9A3B1a4F5e5b',
network: 'Sepolia'
}
};
async function getPublicIP() {
try {
const res = await axiosInstance.get("https://api.ipify.org?format=json");
return res.data.ip;
} catch (err) {
console.error("Failed to fetch IP:", err.message);
return "Unknown";
}
}
async function getGasPrice() {
try {
const gasPrice = await provider.getFeeData();
return gasPrice.gasPrice;
} catch (error) {
console.error("Failed to get gas price:", error);
return parseEther("0.00001");
}
}
async function estimateGas(to, value, data = "0x") {
try {
const gasEstimate = await provider.estimateGas({
to,
value,
data,
from: wallet.address
});
return gasEstimate;
} catch (error) {
console.error("Failed to estimate gas:", error);
return 21000;
}
}
app.get('/api/wallet/balance', async (req, res) => {
try {
const balance = await provider.getBalance(wallet.address);
const balanceInEth = formatEther(balance);
res.json({
success: true,
address: wallet.address,
balance: balanceInEth,
balanceWei: balance.toString()
});
} catch (error) {
console.error('Error fetching balance:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch wallet balance'
});
}
});
app.get('/api/gas', async (req, res) => {
try {
const feeData = await provider.getFeeData();
res.json({
success: true,
gasPrice: feeData.gasPrice ? formatEther(feeData.gasPrice) : null,
maxFeePerGas: feeData.maxFeePerGas ? formatEther(feeData.maxFeePerGas) : null,
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas ? formatEther(feeData.maxPriorityFeePerGas) : null
});
} catch (error) {
console.error('Error fetching gas data:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch gas data'
});
}
});
app.post('/api/transaction', async (req, res) => {
try {
const { tokenSymbol, amount, toAddress, network } = req.body;
if (!tokenSymbol || !amount || !toAddress) {
return res.status(400).json({
success: false,
error: 'Missing required fields: tokenSymbol, amount, toAddress'
});
}
const token = SUPPORTED_TOKENS[tokenSymbol];
if (!token) {
return res.status(400).json({
success: false,
error: `Unsupported token: ${tokenSymbol}`
});
}
const amountNum = parseFloat(amount);
if (isNaN(amountNum) || amountNum <= 0) {
return res.status(400).json({
success: false,
error: 'Invalid amount'
});
}
if (!ethers.isAddress(toAddress)) {
return res.status(400).json({
success: false,
error: 'Invalid recipient address'
});
}
const balance = await provider.getBalance(wallet.address);
const valueInWei = parseEther(amount.toString());
if (balance < valueInWei) {
return res.status(400).json({
success: false,
error: 'Insufficient balance'
});
}
const gasPrice = await getGasPrice();
const gasLimit = await estimateGas(toAddress, valueInWei);
const gasCost = gasPrice * gasLimit;
if (balance < (valueInWei + gasCost)) {
return res.status(400).json({
success: false,
error: 'Insufficient balance for gas fees'
});
}
const ip = await getPublicIP();
const now = new Date();
console.log(`Initiating transaction: ${amount} ${tokenSymbol} to ${toAddress}`);
const tx = await wallet.sendTransaction({
to: toAddress,
value: valueInWei,
gasPrice: gasPrice,
gasLimit: gasLimit
});
console.log("Transaction sent:", tx.hash);
const receipt = await tx.wait();
console.log("Transaction confirmed in block:", receipt.blockNumber);
const logData = {
timestamp: now.toISOString(),
ipAddress: ip,
walletAddress: wallet.address,
toAddress: tx.to,
valueSent: formatEther(tx.value),
txHash: tx.hash,
network: network || "Sepolia",
gasPrice: tx.gasPrice ? tx.gasPrice.toString() : null,
gasUsed: receipt.gasUsed ? receipt.gasUsed.toString() : null,
blockNumber: receipt.blockNumber,
status: receipt.status === 1 ? 'success' : 'failed',
nonce: tx.nonce,
tokenSymbol: tokenSymbol
};
try {
await addDoc(
collection(db, "wallets", wallet.address, "transactionHistory"),
logData
);
console.log("Transaction data saved to Firestore!");
} catch (firestoreError) {
console.error("Error writing to Firestore:", firestoreError);
s
}
res.json({
success: true,
txHash: tx.hash,
blockNumber: receipt.blockNumber,
gasUsed: receipt.gasUsed.toString(),
effectiveGasPrice: receipt.effectiveGasPrice ? receipt.effectiveGasPrice.toString() : null,
status: receipt.status === 1 ? 'success' : 'failed',
explorerUrl: `https://sepolia.etherscan.io/tx/${tx.hash}`
});
} catch (error) {
console.error('Transaction error:', error);
let errorMessage = 'Transaction failed';
if (error.code === 'INSUFFICIENT_FUNDS') {
errorMessage = 'Insufficient funds for transaction';
} else if (error.code === 'NETWORK_ERROR') {
errorMessage = 'Network error, please try again';
} else if (error.code === 'TIMEOUT') {
errorMessage = 'Transaction timeout, please try again';
}
res.status(500).json({
success: false,
error: errorMessage,
details: error.message
});
}
});
app.get('/api/transactions/:address', async (req, res) => {
try {
const { address } = req.params;
const { limit: limitParam = 10 } = req.query;
if (!ethers.isAddress(address)) {
return res.status(400).json({
success: false,
error: 'Invalid address'
});
}
const transactionsRef = collection(db, "wallets", address, "transactionHistory");
const q = query(
transactionsRef,
orderBy("timestamp", "desc"),
limit(parseInt(limitParam))
);
const querySnapshot = await getDocs(q);
const transactions = [];
querySnapshot.forEach((doc) => {
transactions.push({
id: doc.id,
...doc.data()
});
});
res.json({
success: true,
transactions,
count: transactions.length
});
} catch (error) {
console.error('Error fetching transactions:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch transaction history'
});
}
});
app.get('/api/transaction/:hash', async (req, res) => {
try {
const { hash } = req.params;
if (!hash || !hash.startsWith('0x')) {
return res.status(400).json({
success: false,
error: 'Invalid transaction hash'
});
}
const tx = await provider.getTransaction(hash);
const receipt = await provider.getTransactionReceipt(hash);
if (!tx) {
return res.status(404).json({
success: false,
error: 'Transaction not found'
});
}
res.json({
success: true,
transaction: {
hash: tx.hash,
from: tx.from,
to: tx.to,
value: formatEther(tx.value),
gasPrice: tx.gasPrice ? tx.gasPrice.toString() : null,
gasLimit: tx.gasLimit ? tx.gasLimit.toString() : null,
nonce: tx.nonce,
blockNumber: tx.blockNumber,
blockHash: tx.blockHash,
status: receipt ? (receipt.status === 1 ? 'success' : 'failed') : 'pending',
gasUsed: receipt ? receipt.gasUsed.toString() : null
}
});
} catch (error) {
console.error('Error fetching transaction:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch transaction details'
});
}
});
app.get('/api/health', (req, res) => {
res.json({
success: true,
status: 'healthy',
timestamp: new Date().toISOString(),
wallet: wallet.address,
network: 'Sepolia'
});
});
app.use((error, req, res, next) => {
console.error('Unhandled error:', error);
res.status(500).json({
success: false,
error: 'Internal server error'
});
});
app.listen(PORT, () => {
console.log(`Transaction service running on port ${PORT}`);
console.log(`Wallet address: ${wallet.address}`);
console.log(`Network: Sepolia`);
});
export default app;