-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwithdraw_ETH.js
164 lines (115 loc) · 5.07 KB
/
withdraw_ETH.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
const Web3 = require('web3');
const swap_ABI = require('./abi/weth_abi');
require('dotenv').config();
const rpcUrl = process.env.RPC_URL;
const privateKey = process.env.WALLET_PRIVATEKEY;
const withdrawCA = process.env.WETH_CA;
const web3 = new Web3(rpcUrl);
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
web3.eth.accounts.wallet.add(account);
const wethContract = new web3.eth.Contract(
swap_ABI,
withdrawCA
);
let totalAmountWithdrew = 0;
let totalGasSpent = 0;
let totalWETHBalance = 0;
let jumpIndex = 0;
//====================================//
async function withdrawETH(sendIndex) {
const wethBalance = await wethContract.methods.balanceOf(account.address).call();
const initialWETHBalance = parseInt(wethBalance);
const gasLimit = parseInt(process.env.GAS_LIMIT);
const randomAmount = getRandomAmount();
const amountToWithdraw = randomAmount + '0000000000';
let valueToWithdraw = parseInt(amountToWithdraw);
if (initialWETHBalance <= valueToWithdraw){
valueToWithdraw = initialWETHBalance;
jumpIndex = 50;
}
if (initialWETHBalance == 0.000000000000000000) {
finalizeTransaction();
return;
}
const withETH = wethContract.methods.withdraw(valueToWithdraw).encodeABI();
const transactionObject = {
from: account.address,
to: withdrawCA,
value: 0,
maxPriorityFeePerGas: web3.utils.toHex(web3.utils.toWei(process.env.MAX_PRIORITY_FEE_PER_GAS, "gwei")),
maxFeePerGas: web3.utils.toHex(web3.utils.toWei(process.env.MAX_FEE_PER_GAS, "gwei")),
type: 2,
chainId: 167000,
data: withETH,
gasLimit: web3.utils.toHex(gasLimit)
};
const amountSent = web3.utils.fromWei(valueToWithdraw.toString(), 'ether');
totalAmountWithdrew += parseFloat(amountSent);
try {
const transactionReceipt = await web3.eth.sendTransaction(transactionObject);
totalGasSpent += parseFloat(web3.utils.fromWei((transactionReceipt.gasUsed * transactionReceipt.effectiveGasPrice).toString(), 'ether'));
const blockNumber = transactionReceipt.blockNumber;
const blockDetails = await web3.eth.getBlock(blockNumber);
const timestamp = blockDetails.timestamp;
const date = new Date(timestamp * 1000);
const formattedDate = formatDateToTimezone(date, 'Asia/Manila');
console.log(`\n${sendIndex + 1}. \x1b[91m${amountSent}\x1b[0m ETH unwrap (withdrawal) success @ \x1b[93m${formattedDate}\x1b[0m with Block # \x1b[32m${blockNumber}\x1b[0m`);
console.log(` Transaction hash: \x1b[96m${transactionReceipt.transactionHash}\x1b[0m`);
console.log(` Transaction details -> \x1b[1;94mhttps://taikoscan.network/tx/${transactionReceipt.transactionHash}\x1b[0m`);
const wethBalance = await wethContract.methods.balanceOf(account.address).call();
totalWETHBalance = parseFloat(web3.utils.fromWei((wethBalance).toString(), 'ether'));
console.log('Total WETH balance: \x1b[95m' + totalWETHBalance.toFixed(8) + '\x1b[0m WETH');
} catch (withdrawError) {
console.error(`Error withdrawing ETH:`, withdrawError);
}
let sendIndexNew = sendIndex + jumpIndex + 1;
let txCount = parseInt(process.env.WITHDRAW_TX_COUNT);
if (sendIndexNew >= txCount) {
finalizeTransaction();
return;
}
setTimeout(() => {
withdrawETH(sendIndexNew);
}, 20000);
}
//===================================//
async function finalizeTransaction() {
console.log('\x1b[94mAll Unwrap ETH (Withdraw) Transactions Completed.\x1b[0m');
console.log('Overall ETH withdrawal: \x1b[91m' + totalAmountWithdrew.toFixed(8) + '\x1b[0m ETH');
console.log('Overall txn fee spent: \x1b[93m' + totalGasSpent.toFixed(10) + '\x1b[0m ETH');
const wethBalance = await wethContract.methods.balanceOf(account.address).call();
totalWETHBalance = parseFloat(web3.utils.fromWei((wethBalance).toString(), 'ether'));
console.log('Overall WETH balance: \x1b[95m' + totalWETHBalance.toFixed(8) + '\x1b[0m WETH');
const queryRemainingBalance = await web3.eth.getBalance(account.address);
const remainingBalance = parseFloat(web3.utils.fromWei(queryRemainingBalance, 'ether')).toFixed(8);
console.log('Overall ETH balance: \x1b[92m' + remainingBalance + '\x1b[0m ETH');
}
//===================================//
function getRandomAmount() {
const min = parseFloat(process.env.WITHDRAW_RANDOM_AMOUNT_MIN);
const max = parseFloat(process.env.WITHDRAW_RANDOM_AMOUNT_MAX);
const randomValue = Math.random() * (max - min) + min;
const roundedValue = Math.floor(randomValue);
return roundedValue.toString();
}
//===================================//
function formatDateToTimezone(date, timeZone) {
const timeOptions = {
hour: 'numeric',
minute: 'numeric',
hour12: true,
timeZone: timeZone
};
const timeFormatter = new Intl.DateTimeFormat('en-US', timeOptions);
const formattedTime = timeFormatter.format(date);
const dateOptions = {
month: 'short',
day: 'numeric',
year: 'numeric',
timeZone: timeZone
};
const dateFormatter = new Intl.DateTimeFormat('en-US', dateOptions);
const formattedDate = dateFormatter.format(date);
return `${formattedTime} · ${formattedDate}`;
}
withdrawETH(0);