forked from ELHARAKA/TrxAutoSweep
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto-sweep.js
More file actions
121 lines (105 loc) · 3.9 KB
/
auto-sweep.js
File metadata and controls
121 lines (105 loc) · 3.9 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
/**
* Auto-Sweep v3 Script for Tron (TRX)
* Developed by Fahd Elharaka
* Email: fahd@web3dev.ma / Telegram: @Thisiswhosthis
*
* DISCLAIMER:
*
* This script is provided for educational and informational purposes only.
* It is not intended for any illegal or unauthorized activities.
*
* The developer of this script shall not be responsible for any misuse or damage caused by the use of this script.
* Use this script at your own risk and responsibility.
*/
const TronWeb = require('tronweb').TronWeb
const tronWeb = new TronWeb({
fullHost: 'https://tron-rpc.publicnode.com',
privateKey: '21117889f96c85c63bb85f8cbe15671d62db6d5811b87be32f7d4090b141a0e2'
});
const sourceAddress = tronWeb.address.fromPrivateKey(tronWeb.defaultPrivateKey);
const destinationAddress = 'TGoi7MQq5WNGr9YVunea9Ptu6gjQ4qNuQa';
const FEE_RESERVE_TRX = 1;
async function getBalance(address) {
try {
const balanceInSun = await tronWeb.trx.getBalance(address);
const balanceInTRX = balanceInSun / 1_000_000;
return balanceInTRX;
} catch (error) {
console.error('Error retrieving balance:', error);
throw error;
}
}
async function checkEnergy(address) {
try {
const accountResources = await tronWeb.trx.getAccountResources(address);
return accountResources.EnergyLimit - accountResources.EnergyUsed;
} catch (error) {
console.error('Error checking energy:', error);
throw error;
}
}
async function sendTransaction(from, to, amountInTRX) {
try {
const amountInSun = amountInTRX * 1_000_000;
const transaction = await tronWeb.transactionBuilder.sendTrx(to, amountInSun, from);
const signedTransaction = await tronWeb.trx.sign(transaction);
const result = await tronWeb.trx.sendRawTransaction(signedTransaction);
if (result.result) {
//(`Transaction successfully broadcasted. TXID: ${result.txid}`);
return result;
} else {
throw new Error('Failed to broadcast the transaction.');
}
} catch (error) {
console.error('Error sending transaction:', error);
throw error;
}
}
async function autoSweep() {
try {
const currentBalance = await getBalance(sourceAddress);
//('balance: ' + currentBalance)
if (currentBalance > FEE_RESERVE_TRX) {
const energyAvailable = await checkEnergy(sourceAddress);
if (energyAvailable < 0) {
//('Warning: Low energy. Transaction fees will be paid with TRX.');
}
const transferAmount = currentBalance - FEE_RESERVE_TRX;
//(`Current balance: ${currentBalance.toFixed(6)} TRX. Sending ${transferAmount.toFixed(6)} TRX.`);
const result = await sendTransaction(sourceAddress, destinationAddress, transferAmount);
const txID = result.txid;
if (txID) {
let confirmed = false;
let retries = 0;
const maxRetries = 5;
while (!confirmed && retries < maxRetries) {
try {
const tx = await tronWeb.trx.getTransaction(txID);
if (tx && tx.ret && tx.ret[0] && tx.ret[0].contractRet === 'SUCCESS') {
confirmed = true;
//(`Successfully transferred ${transferAmount.toFixed(6)} TRX. Transaction ID: ${txID}`);
} else {
throw new Error('Transaction not yet confirmed.');
}
} catch (error) {
retries++;
//(`Checking transaction confirmation (${retries}/${maxRetries})...`);
await new Promise(resolve => setTimeout(resolve, 5000));
}
}
if (!confirmed) {
console.error(`Failed to confirm transaction after ${maxRetries} retries. Transaction ID: ${txID}`);
}
}
} else {
//(`Current balance: ${currentBalance.toFixed(6)} TRX. No action taken (balance ≤ reserve of ${FEE_RESERVE_TRX} TRX).`);
}
} catch (error) {
console.error('Auto-sweep error:', error);
}
}
async function runAutoSweep() {
await autoSweep();
setTimeout(runAutoSweep, 333);
}
runAutoSweep();