-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieveFunds.js
83 lines (73 loc) · 3.41 KB
/
retrieveFunds.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
const { web3, BN, readWalletsWithPrivateKeys, calculateFee, performTransfer, writeDataToFile, getFormattedDateTime } = require("./utils")
async function retrieveFunds(walletFilePath, centralWalletAddress) {
const results = []
const wallets = await readWalletsWithPrivateKeys(walletFilePath)
for (const wallet of wallets) {
const balance = await web3.eth.getBalance(wallet.address)
if (new BN(balance).isZero()) {
console.log(`No funds to retrieve from ${wallet.address}`)
throw new Error(`No funds to retrieve from ${wallet.address}`)
}
console.log(`Balance of ${wallet.address} is ${web3.utils.fromWei(balance, "ether")} BNB`)
let transaction = {
from: wallet.address,
to: centralWalletAddress,
value: balance,
}
const estimatedGas = BigInt(await web3.eth.estimateGas({ ...transaction, value: "0x0" }))
const gasPrice = BigInt(await web3.eth.getGasPrice())
transaction.gas = estimatedGas > BigInt(21000) ? estimatedGas : BigInt(21000)
const fee = new BN(gasPrice).mul(new BN(transaction.gas))
transaction.value = new BN(transaction.value).sub(fee).toString()
if (new BN(transaction.value).lte(new BN(0))) {
console.log(`Insufficient funds to cover the fee from ${wallet.address}`)
throw new Error(`Insufficient funds to cover the fee from ${wallet.address}`)
}
const amountTransferred = web3.utils.fromWei(transaction.value, "ether")
const gasFee = web3.utils.fromWei(fee.toString(), "ether")
let nonce = await web3.eth.getTransactionCount(wallet.address, "pending") // Get the current nonce
// nonce++
const receipt = await performTransfer(wallet.address, wallet.privateKey, centralWalletAddress, amountTransferred, nonce)
if (!receipt.status) {
throw new Error(`Transaction failed at ${wallet.address}, transaction hash: ${receipt.transactionHash}`)
}
results.push({
timestamp: receipt.timestamp,
from: wallet.address,
to: centralWalletAddress,
transactionHash: receipt.transactionHash,
status: "Success",
amountTransferred: amountTransferred,
gasFee: gasFee,
})
console.log(`Transferred ${amountTransferred} BNB from ${wallet.address} to ${centralWalletAddress}, TxHash: ${receipt.transactionHash}, gasFee: ${gasFee}`)
}
return results
}
async function main() {
const [walletFilePath, centralWalletAddress] = process.argv.slice(2)
if (!walletFilePath || !centralWalletAddress) {
console.error("Please specify the wallet file path and the central wallet address.")
process.exit(1)
}
try {
const results = await retrieveFunds(walletFilePath, centralWalletAddress)
let format = "csv"
if (format === "csv") {
let content = "Timestamp,From,To,TransactionHash,Status,AmountTransferred,GasFee\n"
results.forEach(result => {
content += `"${result.timestamp}","${result.from}","${result.to}","${result.transactionHash}","${result.status}","${result.amountTransferred}","${result.gasFee}"\n`
})
writeDataToFile("retrieveFunds-transactions", content, "csv") // Ensure proper filename for CSV
} else {
writeDataToFile("retrieveFunds-transactions", results, "json") // Ensure proper filename for JSON
}
} catch (error) {
console.error("An unexpected error occurred:", error.message)
process.exit(1)
}
}
main().catch(error => {
console.error("An unexpected error occurred:", error.message)
process.exit(1)
})