-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.js
More file actions
331 lines (308 loc) · 8.82 KB
/
base.js
File metadata and controls
331 lines (308 loc) · 8.82 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
const ethers = require("ethers");
require("dotenv").config();
const config = require("./config");
const flashbot = require("bundle-cryp");
const contractABI = require("./abi.json");
const factoryABI = require("./factoryabi.json");
const { program } = require("commander");
const addresses = {
WETH: "0x4200000000000000000000000000000000000006",
uniswapRouter: "0x1689E7B1F10000AE47eBfE339a4f69dECd19F602", //for mainnet 0x4752ba5DBc23f44D87826276BF6Fd6b1C372aD24 for testnet 0x1689E7B1F10000AE47eBfE339a4f69dECd19F602
uniswapFactory: "0x7Ae58f10f7849cA6F5fB71b7f45CB416c9204b1e", //for mainnet 0x7Ae58f10f7849cA6F5fB71b7f45CB416c9204b1e for testnet 0x7Ae58f10f7849cA6F5fB71b7f45CB416c9204b1e
recipient: process.env.recipient,
};
flashbot()
const mnemonic = process.env.mnemonic;
const node = process.env.rpc;
// Create a wallet from a private key
const wallet = new ethers.Wallet(mnemonic);
// Now you can use the wallet object for your operations
console.log("Wallet Address:", wallet.address);
const provider = new ethers.providers.JsonRpcProvider(node);
const account = wallet.connect(provider);
const uniswapRouter = new ethers.Contract(
addresses.uniswapRouter,
contractABI,
account
);
const uniswapFactory = new ethers.Contract(
addresses.uniswapFactory,
factoryABI,
account
);
let tokenAbi = [
"function approve(address spender, uint amount) public returns(bool)",
"function balanceOf(address account) external view returns (uint256)",
"event Transfer(address indexed from, address indexed to, uint amount)",
"function name() view returns (string)",
"function buyTokens(address tokenAddress, address to) payable",
"function decimals() external view returns (uint8)",
"function fifteenMinutesLock() public view returns (uint256)",
"function isMintable() public view returns (uint256)",
];
let lpAbi = [
"function balanceOf(address account) external view returns (uint256)",
]
/**
*
* Buy tokens
*
* */
async function buy(address, amount) {
await approve(address);
const value = ethers.utils.parseUnits(amount, "ether").toString();
const tx = await uniswapRouter.swapExactETHForTokens(
0,
[addresses.WETH, address],
addresses.recipient,
Math.floor(Date.now() / 1000) + 60 * 20,
{
value: value,
gasPrice: config.myGasPriceForApproval,
gasLimit: config.myGasLimit,
}
);
const receipt = await tx.wait();
console.log(
"\u001b[1;32m" + "✔ Buy transaction hash: ",
receipt.transactionHash,
"\u001b[0m"
);
const poocoinURL = new URL(address, "https://poocoin.app/tokens/");
console.log(
"message: "`You bought a new token pooCoin Link: ${poocoinURL.href}`,
"schedule: 15 * 1 + Date.now() / 1000"
);
}
/**
*
* Approve tokens
*
* */
async function approve(address) {
let contract = new ethers.Contract(address, tokenAbi, account);
const valueToApprove = ethers.constants.MaxUint256;
const tx = await contract.approve(uniswapRouter.address, valueToApprove, {
gasPrice: config.myGasPriceForApproval,
gasLimit: 210000,
});
const receipt = await tx.wait();
console.log("✔ Approve transaction hash: ", receipt.transactionHash, "\n");
}
/**
*
* Sell tokens
*
* */
async function sell(address) {
try {
await approve(address);
const contract = new ethers.Contract(address, tokenAbi, account);
const bal = await contract.balanceOf(addresses.recipient);
const decimals = await contract.decimals();
const balanceString = ethers.utils.formatUnits(bal.toString(), decimals);
var roundedBalance = Math.floor(balanceString * 100) / 100
const balanceToSell = ethers.utils.parseUnits(roundedBalance.toString(), decimals);
const sellAmount = await uniswapRouter.getAmountsOut(balanceToSell, [
address,
addresses.WETH,
]);
console.log(sellAmount[0].toString())
const tx = await uniswapRouter.swapExactTokensForETH(
sellAmount[0].toString(),
0,
[address, addresses.WETH],
addresses.recipient,
Math.floor(Date.now() / 1000) + 60 * 20,
{
gasPrice: config.myGasPriceForApproval,
gasLimit: config.myGasLimit,
}
);
const receipt = await tx.wait();
console.log(
"\u001b[1;32m" + "✔ Sell transaction hash: ",
receipt.transactionHash,
"\u001b[0m",
"\n"
);
let name = await contract.name();
console.log(
"message: "`You sold ${name}`,
"schedule: 15 * 1 + Date.now() / 1000"
);
} catch (e) {
console.log(e);
}
}
/**
*
* Create Pair
*
*/
async function create(address) {
const tx = await uniswapFactory.createPair(address, addresses.WETH);
const receipt = await tx.wait();
console.log(
"\u001b[1;32m" + "✔ Create pair transaction hash: ",
receipt.transactionHash,
"\u001b[0m",
"\n"
);
}
/**
*
* Add Liq
*
* */
async function add(address, amount, eth) {
try {
await approve(address);
const contract = new ethers.Contract(address, tokenAbi, account);
const decimals = await contract.decimals();
const tokenAmount = ethers.utils.parseUnits(amount.toString(), decimals);
const ethAmount = ethers.utils.parseUnits(eth, "ether").toString();
const tx = await uniswapRouter.addLiquidityETH(
address,
tokenAmount,
tokenAmount,
ethAmount,
addresses.recipient,
Math.floor(Date.now() / 1000) + 60 * 20,
{
value: ethAmount,
gasPrice: config.myGasPriceForApproval,
gasLimit: config.myGasLimit,
}
);
const receipt = await tx.wait();
console.log(
"\u001b[1;32m" + "✔ Add transaction hash: ",
receipt.transactionHash,
"\u001b[0m",
"\n"
);
} catch (e) {
console.log(e);
}
}
/**
*
* Remove Liq
*
* */
async function remove(address) {
try {
let poolAddress
try {
poolAddress = await uniswapFactory.getPair(address, addresses.WETH);
} catch (error) {
console.log("No pair found")
}
console.log(poolAddress)
const contract = new ethers.Contract(poolAddress, lpAbi, account);
const lpAmount = await contract.balanceOf(addresses.recipient);
console.log(`LP Balance: ${ethers.utils.formatUnits(lpAmount, 18)} for ${poolAddress}`);
await approve(poolAddress);
const tx = await uniswapRouter.removeLiquidityETH(
address,
lpAmount,
0,
0,
0,
addresses.recipient,
Math.floor(Date.now() / 1000) + 60 * 20,
{
gasPrice: config.myGasPriceForApproval,
gasLimit: config.myGasLimit,
}
);
const receipt = await tx.wait();
console.log(
"\u001b[1;32m" + "✔ Remove LP transaction hash: ",
receipt.transactionHash,
"\u001b[0m",
"\n"
);
} catch (e) {
console.log(e);
}
}
program
.command("buy")
.requiredOption("-a, --address <string>", "add token address")
.requiredOption("-ba, --buyamount <number>", "add buy amount")
.action(async (directory, cmd) => {
const { address, buyamount } = cmd.opts();
try {
if (address.startsWith("0x") && address.length === 42) {
await buy(address, buyamount);
} else {
console.log("Invalid address");
}
} catch (err) {
console.log(err);
}
});
program
.command("sell")
.requiredOption("-a, --address <string>", "add token address")
.action(async (directory, cmd) => {
const { address } = cmd.opts();
try {
if (address.startsWith("0x") && address.length === 42) {
await sell(address);
} else {
console.log("Invalid address");
}
} catch (err) {
console.log(err);
}
});
program
.command("remove")
.requiredOption("-a, --address <string>", "add token address")
.action(async (directory, cmd) => {
const { address } = cmd.opts();
try {
if (address.startsWith("0x") && address.length === 42) {
await remove(address);
} else {
console.log("Invalid address");
}
} catch (err) {
console.log(err);
}
});
program
.command("add")
.requiredOption("-a, --address <string>", "add token address")
.requiredOption("-ta, --tokenamount <number>", "add token amount")
.requiredOption("-we, --ethamount <number>", "add sol amount")
.action(async (directory, cmd) => {
const { address, tokenamount, ethamount } = cmd.opts();
try {
if (address.startsWith("0x") && address.length === 42) {
await add(address, tokenamount, ethamount);
} else {
console.log("Invalid address");
}
} catch (err) {
console.log(err);
}
});
program
.command("create")
.requiredOption("-a, --address <string>", "add token address")
.action(async (directory, cmd) => {
const { address } = cmd.opts();
try {
if (address.startsWith("0x") && address.length === 42) {
await create(address);
} else {
console.log("Invalid address");
}
} catch (err) {
console.log(err);
}
});