-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
281 lines (243 loc) · 9.17 KB
/
index.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
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
import axios from 'axios';
import fs from 'fs';
import readline from 'readline';
import Web3 from 'web3';
function printBanner() {
console.log('\x1b[34m', '██ ██ ██ ███ ██ ███████ ███ ██ ██ ██████ ');
console.log('\x1b[34m', '██ ██ ██ ████ ██ ██ ████ ██ ██ ██ ██ ');
console.log('\x1b[34m', '██ █ ██ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██████ ');
console.log('\x1b[34m', '██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ');
console.log('\x1b[34m', ' ███ ███ ██ ██ ████ ███████ ██ ████ ██ ██ ');
console.log('\x1b[0m');
console.log("Hanafuda Bot Auto Deposit");
console.log("Join our Telegram channel: https://t.me/winsnip");
}
function consolewithTime(word) {
const now = new Date().toISOString().split('.')[0].replace('T', ' ');
console.log(`[${now}] ${word}`);
}
const RPC_URL = "https://mainnet.base.org";
const CONTRACT_ADDRESS = "0xC5bf05cD32a14BFfb705Fb37a9d218895187376c";
const CONFIG = './config.json';
const REQUEST_URL = 'https://hanafuda-backend-app-520478841386.us-central1.run.app/graphql';
const REFRESH_URL = 'https://securetoken.googleapis.com/v1/token?key=AIzaSyDipzN0VRfTPnMGhQ5PSzO27Cxm3DohJGY';
const FEE_THRESHOLD = 0.00000030;
const web3 = new Web3(new Web3.providers.HttpProvider(RPC_URL));
const ABI = [
{
"constant": false,
"inputs": [],
"name": "depositETH",
"outputs": [],
"payable": true,
"stateMutability": "payable",
"type": "function"
}
];
const contract = new web3.eth.Contract(ABI, CONTRACT_ADDRESS);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let accounts = [];
function getAccounts() {
if (fs.existsSync(CONFIG)) {
try {
const data = fs.readFileSync(CONFIG);
const tokensData = JSON.parse(data);
if (tokensData.refreshToken) {
accounts = [{
refreshToken: tokensData.refreshToken,
authToken: tokensData.authToken
}];
} else {
accounts = Object.values(tokensData);
}
consolewithTime(`Mendapatkan ${accounts.length} Akun didalam config`);
return JSON.parse(data);
} catch (error) {
consolewithTime(`Error Load Token: ${error.message}`);
process.exit(1);
}
} else {
consolewithTime('Token tidak ditemukan.');
process.exit(1);
}
}
function saveTokens(tokens) {
try {
fs.writeFileSync(CONFIG, JSON.stringify(tokens, null, 2));
consolewithTime('Tokens berhasil di update.');
} catch (error) {
consolewithTime(`Gagal update token: ${error.message}`);
process.exit(1);
}
}
async function refreshTokenHandler(tokenData) {
consolewithTime('Mencoba merefresh token...')
try {
const response = await axios.post(REFRESH_URL, null, {
params: {
grant_type: 'refresh_token',
refresh_token: tokenData.refreshToken,
},
});
const updatedTokens = {
...tokenData,
authToken: `Bearer ${response.data.access_token}`, // Update auth token
refreshToken: response.data.refresh_token, // Update refresh token
};
const existingTokens = JSON.parse(fs.readFileSync(CONFIG, 'utf-8'));
const index = existingTokens.findIndex(token => token.privateKey === tokenData.privateKey);
if (index !== -1) {
existingTokens[index] = updatedTokens;
} else {
consolewithTime('Token dengan unique private key tidak ditemukan!');
return false;
}
saveTokens(existingTokens);
consolewithTime('Token refreshed and saved successfully.');
return updatedTokens.authToken;
} catch (error) {
consolewithTime(`Failed to refresh token: ${error.message}`);
return false;
}
}
async function syncTransaction(txHash, tokenData) {
const maxRetries = 3;
const retryDelay = 5000;
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const response = await axios.post(
REQUEST_URL,
{
query: `
mutation SyncEthereumTx($chainId: Int!, $txHash: String!) {
syncEthereumTx(chainId: $chainId, txHash: $txHash)
}`,
variables: {
chainId: 8453,
txHash: txHash
},
operationName: "SyncEthereumTx"
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': tokenData.authToken
}
}
);
if (response.data && response.data.data && response.data.data.syncEthereumTx) {
consolewithTime(`Transaksi ${txHash} Sukses sync.`);
break;
} else {
throw new Error(`Sync gagal.`);
}
} catch (error) {
consolewithTime(`Mencoba ${attempt} - Gagal sync transaksi ${txHash}:`, error.message);
if (attempt === 3) {
consolewithTime('Mencoba refresh token...');
const refreshedToken = await refreshTokenHandler(tokenData);
if (refreshedToken) {
tokenData.authToken = refreshedToken;
consolewithTime('Token berhasil di refresh...');
attempt--;
continue;
} else {
consolewithTime('Token gagal di refresh...');
break;
}
}
consolewithTime(`Mencoba retry dalam ${retryDelay / 1000} detik...`);
await new Promise(resolve => setTimeout(resolve, retryDelay));
}
}
}
async function waitForLowerFee(gasLimit) {
let gasPrice, txnFeeInEther;
do {
gasPrice = await web3.eth.getGasPrice();
const txnFee = gasPrice * gasLimit;
txnFeeInEther = web3.utils.fromWei(txnFee.toString(), 'ether');
if (parseFloat(txnFeeInEther) > FEE_THRESHOLD) {
consolewithTime(`Transaksi fee sekitar: ${txnFeeInEther} ETH, menunggu...`);
await new Promise(resolve => setTimeout(resolve, 5000));
}
} while (parseFloat(txnFeeInEther) > FEE_THRESHOLD);
return gasPrice;
}
async function executeTransactions(tokenData, numTx, amountInEther) {
const { privateKey } = tokenData;
if (!/^(0x)?[0-9a-f]{64}$/i.test(privateKey)) {
consolewithTime('Invalid format private key.');
return;
}
try {
const amountInWei = web3.utils.toWei(amountInEther, 'ether');
const account = web3.eth.accounts.privateKeyToAccount('0x' + privateKey);
web3.eth.accounts.wallet.add(account);
const fromAddress = account.address;
for (let i = 0; i < numTx; i++) {
try {
const currentNonce = await web3.eth.getTransactionCount(fromAddress, 'pending');
const gasLimit = await contract.methods.depositETH().estimateGas({ from: fromAddress, value: amountInWei });
const gasPrice = await waitForLowerFee(gasLimit);
const tx = {
from: fromAddress,
to: CONTRACT_ADDRESS,
value: amountInWei,
gas: gasLimit,
gasPrice: gasPrice,
nonce: currentNonce,
data: contract.methods.depositETH().encodeABI()
};
const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
consolewithTime(`Transaksi ${i + 1} untuk wallet ${fromAddress} sukses dengan hash: ${receipt.transactionHash}`);
await syncTransaction(receipt.transactionHash, tokenData);
} catch (txError) {
consolewithTime(`Transaksi failed ${i + 1} untuk wallet ${fromAddress}:`, txError.message);
i--;
await new Promise(resolve => setTimeout(resolve, 5000));
}
}
consolewithTime(`Transaksi untuk wallet ${fromAddress} selesai.`);
} catch (error) {
consolewithTime(`Failed execute transaksi: ${error.message}`);
}
}
async function main() {
const accounts = getAccounts();
rl.question('Enter number of transactions: ', async (txCount) => {
const numTx = parseInt(txCount);
if (isNaN(numTx) || numTx <= 0) {
consolewithTime('Invalid jumlah transaksi.');
rl.close();
return;
}
rl.question('Do you want to use the default amount of 0.0000000000001 ETH? (y/n): ', async (useDefault) => {
let amountInEther = '0.0000000000001';
if (useDefault.toLowerCase() !== 'y') {
rl.question('Enter amount to deposit (in ETH): ', (amount) => {
if (!isNaN(parseFloat(amount)) && parseFloat(amount) > 0) {
amountInEther = amount;
} else {
consolewithTime('Invalid amount entered. Using the default amount.');
}
rl.close();
for (const account of accounts) {
executeTransactions(account, numTx, amountInEther);
}
});
} else {
rl.close();
for (const account of accounts) {
executeTransactions(account, numTx, amountInEther);
}
}
});
});
}
printBanner();
main();