-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsell.js
60 lines (50 loc) · 1.92 KB
/
sell.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
"use strict";
const Env = require("./env.json");
Object.assign(process.env, Env);
const ethers = require("ethers");
const sellToken = process.env.SELL_TOKEN;
const sellAmount = Ethers.utils.parseUnits(process.env.SELL_AMOUNT, "ether"); // sell in a previously purchased token
const slippage = process.env.SLIPPAGE;
const wbnb = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c";
const pcs = "0x10ED43C718714eb63d5aA57B78B54704E256024E";
const provider = new ethers.providers.WebSocketProvider(
process.env.BSC_NODE_WSS
);
const wallet = ethers.Wallet.fromMnemonic(process.env.MNEMONIC);
const account = wallet.connect(provider);
const router = new ethers.Contract(
pcs,
[
"function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)",
"function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)",
"function swapExactTokensForETHSupportingFeeOnTransferTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external",
],
account
);
async function sell() {
const amounts = await router.getAmountsOut(sellAmount, [sellToken, wbnb]);
const amountOutMin = amounts[1].sub(amounts[1].div(slippage));
console.log(`
Selling Token
=================
tokenIn: ${sellAmount.toString()}
tokenOut: ${amountOutMin.toString()} ${sellToken}
`);
// For "taxed" coins - swap out next line
//const txt = await router.swapExactTokensForETHSupportingFeeOnTransferTokens(
const tx = await router.swapTokensForExactETH(
amountOutMin,
[sellToken, wbnb],
process.env.RECIPIENT,
Date.now() + 1000 * 60 * 5, //5 minutes
{
gasLimit: 345684,
gasPrice: ethers.utils.parseUnits("6", "gwei"),
}
);
const receipt = await tx.wait();
console.log("Transaction receipt");
console.log(receipt);
process.exit();
}
sell();