-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMain.js
83 lines (71 loc) · 2.66 KB
/
Main.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 fs = require('fs');
const csv = require('csv-parser');
const { ethers } = require('ethers');
const ca = require('./ca.json');
const rpcs = require('./rpc.json');
const tokenAbi = [
"function balanceOf(address owner) view returns (uint256)",
"function decimals() view returns (uint8)",
"function transfer(address to, uint amount) returns (bool)"
];
async function getWalletsFromCSV(filePath) {
const wallets = [];
return new Promise((resolve, reject) => {
fs.createReadStream(filePath)
.pipe(csv())
.on('data', (data) => wallets.push(data))
.on('end', () => {
resolve(wallets);
})
.on('error', reject);
});
}
async function checkAndTransferTokens(pk, toAddress, chain) {
const provider = new ethers.providers.JsonRpcProvider(rpcs[chain]);
const wallet = new ethers.Wallet(pk, provider);
console.log(`Checking wallet balance ${wallet.address}...`);
const gasPrice = await provider.getGasPrice();
console.log(`Gas price: ${ethers.utils.formatUnits(gasPrice, 'gwei')} gwei`);
const tokens = Object.keys(ca[chain]);
for (const tokenName of tokens.filter(name => name !== 'ETH')) {
const tokenContract = new ethers.Contract(ca[chain][tokenName], tokenAbi, wallet);
const balance = await tokenContract.balanceOf(wallet.address);
if (!balance.isZero()) {
console.log(`${tokenName} balance: ${ethers.utils.formatUnits(balance, await tokenContract.decimals())}`);
const estimatedGasLimit = await tokenContract.estimateGas.transfer(toAddress, balance);
const tx = await tokenContract.transfer(toAddress, balance, {
gasPrice: gasPrice,
gasLimit: estimatedGasLimit
});
await tx.wait();
console.log(`✅${tokenName} transfer to ${toAddress} successful.`);
}
}
if (tokens.includes('ETH')) {
const balance = await provider.getBalance(wallet.address);
if (!balance.isZero()) {
console.log(`ETH balance: ${ethers.utils.formatEther(balance)}`);
const transferAmount = balance.mul(90).div(100);
const txData = {
to: toAddress,
value: transferAmount,
gasPrice: gasPrice
};
const estimatedGasLimit = await wallet.estimateGas(txData);
const tx = await wallet.sendTransaction({
...txData,
gasLimit: estimatedGasLimit
});
await tx.wait();
console.log(`✅ETH transfer of 90% balance to ${toAddress} successful.`);
}
}
}
async function main() {
const filePath = './wallet.csv';
const wallets = await getWalletsFromCSV(filePath);
for (const {pk, toAddress, chain} of wallets) {
await checkAndTransferTokens(pk, toAddress, chain);
}
}
main().catch(console.error);