-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.ts
More file actions
135 lines (117 loc) · 4.84 KB
/
index.ts
File metadata and controls
135 lines (117 loc) · 4.84 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
122
123
124
125
126
127
128
129
130
131
132
import {
get_erc20_abi,
get_router_abi,
} from './fetchAbi';
import {
BASE_WALLET_ADDRESS,
BASE_WALLET_PRIVATE_KEY,
TARGET_TOKEN_ADDRESS,
WrappedNative
} from './constants'
import { ChainId, Wallet } from './types';
import { delay, gather, generateWallets, getRandomDelay, getRouterAddress, getRpc, readingWallets, saveWallet, sendEther } from './utils';
import { ethers } from 'ethers'
import fs from "fs";
import { CHAINID, fee, subWalletNum } from './config';
const baseWallet = {
privateKey: BASE_WALLET_PRIVATE_KEY,
address: BASE_WALLET_ADDRESS,
}
let fileName = "";
export const buyToken = async (tokenAddress: string, wallet: Wallet, chainId: ChainId) => {
const routerAbi = get_router_abi();
const routerAddress = getRouterAddress(chainId);
const rpc = getRpc(chainId);
const provider = new ethers.JsonRpcProvider(rpc);
const block = await provider.getBlock("latest");
const currentTimestamp = block?.timestamp || 9999999999999;
try {
const signer = new ethers.Wallet(wallet.privateKey, provider)
const contract = new ethers.Contract(routerAddress, routerAbi, signer);
console.log("=================================== Buying ===================================")
console.log(`Token Address: ${tokenAddress}`)
await delay(5000);
const tx = await contract
.swapExactETHForTokensSupportingFeeOnTransferTokens(0,
[WrappedNative[chainId], tokenAddress],
wallet.address,
currentTimestamp + 1000000000,
{
value: ethers.parseEther(wallet.amount.toString())
});
await tx.wait();
console.log(`Buy : ${tx.hash}`);
return tx.hash;
} catch (error) {
console.log(error);
await gather(wallet, provider);
return "";
}
}
export const sellToken = async (tokenAddress: string, wallet: Wallet, chainId: ChainId) => {
const routerAbi = get_router_abi();
const routerAddress = getRouterAddress(chainId);
const rpc = getRpc(chainId);
const provider = new ethers.JsonRpcProvider(rpc);
const block = await provider.getBlock("latest");
const currentTimestamp = block?.timestamp || 9999999999999;
const erc20Abi = get_erc20_abi();
try {
const signer = new ethers.Wallet(wallet.privateKey, provider)
const tokenContract = new ethers.Contract(tokenAddress, erc20Abi, signer);
const tokenBalance = await tokenContract.balanceOf(wallet.address);
console.log("=================================== Approving ===================================")
console.log(`Approving ${tokenAddress} to sell from ${wallet.address}`);
await delay(5000);
const approval = await tokenContract.approve(routerAddress, tokenBalance);
console.log(`Approval : ${approval.hash}`);
console.log("=================================== Selling ===================================")
const contract = new ethers.Contract(routerAddress, routerAbi, signer);
await delay(5000);
const tx = await contract.swapExactTokensForETHSupportingFeeOnTransferTokens(tokenBalance, 0, [tokenAddress, WrappedNative[chainId]], wallet.address, currentTimestamp + 1000000000);
await tx.wait();
console.log(`Sell : ${tx.hash}`);
await gather(wallet, provider);
return tx.hash;
} catch (error) {
console.log(error);
return "Transaction Failed";
}
}
export const processTransaction = async (wallet: Wallet, token_addr: string, chainId: number) => {
const rpc = getRpc(chainId);
const provider = new ethers.JsonRpcProvider(rpc);
try {
const transferAmount = (+wallet.amount + +fee).toFixed(6);
const isTransferred = await sendEther(baseWallet.privateKey, wallet.address, transferAmount, provider);
if (isTransferred) {
await saveWallet({...wallet, funded: transferAmount}, fileName);
}
const hash = await buyToken(token_addr, wallet, chainId);
if(hash == "") {
console.log("Trading with the next wallet.");
return;
}
const delayTime = getRandomDelay();
console.log(`=================================== Delaying ${delayTime / 1000}s ===================================`)
await delay(delayTime);
await sellToken(token_addr, wallet, chainId)
} catch (error) {
console.error(`Error processing transaction for user`, error);
}
const delayTime = getRandomDelay();
console.log(`=================================== Delaying ${delayTime / 1000}s ===================================`)
await delay(delayTime);
}
const runBot = async () => {
console.log(`Generating ${subWalletNum} subwallets`);
fileName = await generateWallets(subWalletNum);
const wallets = await readingWallets(fileName);
for (let i = 0; i < wallets.length; i++) {
await processTransaction(wallets[i], TARGET_TOKEN_ADDRESS, CHAINID);
const delayTime = getRandomDelay();
console.log(`=================================== Delaying ${delayTime / 1000}s ===================================`)
await delay(delayTime);
}
}
runBot()