forked from Bot80926/swap-arbitrage-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
268 lines (248 loc) · 10.3 KB
/
bot.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
/*
* @LastEditors: Bot80926
* @Description: Swap bot
* @LastEditTime: 2023-05-02 22:58:26
* Copyright (c) 2023 by Bot80926, All Rights Reserved.
* Buy me a coffee: 0xa1ebF7E97Cfd6939fb90b27567AEBa5904a66630
*/
const express = require("express");
const http = require('http');
const chalk = require('chalk');
const Web3 = require("web3")
const ethers = require("ethers");
const swapAbi = require('./swapAbi.json')
const abi = require('./routerAbi.json')
const app = express();
const PORT = process.env.PORT || 3888;
const isTestnet = true; // bot config flag. if true, use testnet, else use mainnet
const secretKey = "input your private key here"
const blackAddress = "input your wallet address here";
const MJT_ROUTER_ADDRESS = isTestnet ? "0x59a4210Dd69FDdE1457905098fF03E0617A548C5" : "0x8c8067ed3bC19ACcE28C1953bfC18DC85A2127F7";
const KCS_CONTRACT = isTestnet ? "0x6551358EDC7fee9ADAB1E2E49560E68a12E82d9e" : "0x4446Fc4eb47f2f6586f9fAAb68B3498F86C07521";
const wss = isTestnet ? "wss://rpc-ws-testnet.kcc.network" : "wss://rpc-ws-mainnet.kcc.network";
const explore = isTestnet ? "https://scan-testnet.kcc.network/tx/" : "https://explorer.kcc.io/en/tx/"
const web3 = new Web3(wss)
//Slippage refers to the difference between the expected price of a trade and the price at which the trade is executed
const slippage = 0.005
//buyAmount how much are we going to pay for example 0.1 KCS
const buyAmount = 3
function calculate_gas_price(action, amount) {
if (action === "buy") {
return amount.add(100000000) // 0.1 Gwei
} else {
return amount.sub(100000000) // 0.1 Gwei
}
}
function router(account) {
return new ethers.Contract(
MJT_ROUTER_ADDRESS,
[
'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
'function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)',
'function swapExactTokensForTokensSupportingFeeOnTransferTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)',
'function swapExactETHForTokensSupportingFeeOnTransferTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable',
'function swapExactTokensForETH (uint amountOutMin, address[] calldata path, address to, uint deadline) external payable'
],
account
);
}
function erc20(account, tokenAddress) {
return new ethers.Contract(
tokenAddress,
[{
"constant": true,
"inputs": [{
"name": "_owner",
"type": "address"
}],
"name": "balanceOf",
"outputs": [{
"name": "balance",
"type": "uint256"
}],
"payable": false,
"type": "function"
},
{
"inputs": [],
"name": "decimals",
"outputs": [{
"internalType": "uint256",
"name": "",
"type": "uint256"
}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "symbol",
"outputs": [{
"internalType": "string",
"name": "",
"type": "string"
}],
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_spender",
"type": "address"
}, {
"name": "_value",
"type": "uint256"
}],
"name": "approve",
"outputs": [{
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
],
account
);
}
const buyToken = async (account, tokenContract, gasLimit, gasPrice) => {
//amountOutMin how many token we are going to receive
let amountOutMin = 0;
const amountIn = ethers.utils.parseUnits(buyAmount.toString(), 'ether');
if (parseInt(slippage) !== 0) {
const amounts = await router(account).getAmountsOut(amountIn, [KCS_CONTRACT, tokenContract]);
amountOutMin = amounts[1].sub(amounts[1].div(100).mul(`${slippage}`));
}
const tx = await router(account).swapExactETHForTokensSupportingFeeOnTransferTokens(
amountOutMin,
[KCS_CONTRACT, tokenContract],
account.address,
(Date.now() + 1000 * 60 * 10), {
'value': amountIn,
'gasLimit': gasLimit,
'gasPrice': gasPrice,
}
);
const receipt = await tx.wait();
if (receipt && receipt.blockNumber && receipt.status === 1) { // 0 - failed, 1 - success
console.log(chalk.green(`Transaction ${explore}${receipt.transactionHash} mined, status success`));
} else if (receipt && receipt.blockNumber && receipt.status === 0) {
console.log(chalk.red(`Transaction ${explore}${receipt.transactionHash} mined, status failed`));
} else {
console.log(`Transaction ${explore}${receipt.transactionHash} not mined`);
}
}
const sellToken = async (account, tokenContract, gasLimit, gasPrice, value = 100) => {
const sellTokenContract = new ethers.Contract(tokenContract, swapAbi, account)
const contract = new ethers.Contract(MJT_ROUTER_ADDRESS, abi, account)
const accountAddress = account.address
const tokenBalance = await erc20(account, tokenContract).balanceOf(accountAddress);
let amountOutMin = 0;
const amountIn = tokenBalance.mul(value).div(100)
const amounts = await router(account).getAmountsOut(amountIn, [tokenContract, KCS_CONTRACT]);
if (parseInt(slippage) !== 0) {
amountOutMin = amounts[1].sub(amounts[1].mul(`${slippage}`).div(100));
} else {
amountOutMin = amounts[1]
}
// you can pre approve this contract to spend your tokens, it's can safe some tx time and improve success rate
// const approve = await sellTokenContract.approve(MJT_ROUTER_ADDRESS, amountIn)
// const receipt_approve = await approve.wait();
// if (receipt_approve && receipt_approve.blockNumber && receipt_approve.status === 1) {
console.log(`Approved ${explore}${receipt_approve.transactionHash}`);
const swap_txn = await contract.swapExactTokensForETHSupportingFeeOnTransferTokens(
amountIn, amountOutMin,
[tokenContract, KCS_CONTRACT],
accountAddress,
(Date.now() + 1000 * 60 * 10), {
'gasLimit': gasLimit,
'gasPrice': gasPrice,
}
)
const receipt = await swap_txn.wait();
if (receipt && receipt.blockNumber && receipt.status === 1) { // 0 - failed, 1 - success
console.log(chalk.green(`Transaction ${explore}${receipt.transactionHash} mined, status success`));
} else if (receipt && receipt.blockNumber && receipt.status === 0) {
console.log(chalk.red(`Transaction ${explore}${receipt.transactionHash} mined, status failed`));
} else {
console.log(`Transaction ${explore}${receipt.transactionHash} not mined`);
}
// }
}
const init = async function () {
const customWsProvider = new ethers.providers.WebSocketProvider(wss);
const wallet = new ethers.Wallet(secretKey);
const account = wallet.connect(customWsProvider)
const iface = new ethers.utils.Interface(['function swapExactETHForTokens(uint256 amountOutMin, address[] path, address to, uint256 deadline)',
'function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)',
'function swapExactETHForTokensSupportingFeeOnTransferTokens(uint amountOutMin,address[] calldata path,address to,uint deadline)'
])
customWsProvider.on("pending", (tx) => {
customWsProvider.getTransaction(tx).then(async function (transaction) {
// now we will only listen for pending transaction on mojitoSwap factory
if (transaction && transaction.to && transaction.to.toLowerCase() === MJT_ROUTER_ADDRESS.toLowerCase() && transaction.from !== blackAddress) {
const value = web3.utils.fromWei(transaction.value.toString())
const gasPrice = transaction.gasPrice.toString()
const gasLimit = transaction.gasLimit.toString()
// for example we will be only showing transaction that are higher than 30 KCS
if (value >= 3) {
console.log("value : ", value);
console.log("gasPrice : ", gasPrice);
console.log("gasLimit : ", gasLimit);
//we can print the sender of that transaction
console.log("from", transaction.from);
let result = []
//we will use try and catch to handle the error and decode the data of the function used to swap the token
try {
result = iface.decodeFunctionData('swapExactETHForTokens', transaction.data)
} catch (error) {
try {
result = iface.decodeFunctionData('swapExactETHForTokensSupportingFeeOnTransferTokens', transaction.data)
} catch (error) {
try {
result = iface.decodeFunctionData('swapETHForExactTokens', transaction.data)
} catch (error) {
console.log("final err : ", transaction);
}
}
}
if (result.length > 0) {
let tokenAddress = ""
if (result[1].length > 0) {
tokenAddress = result[1][1]
console.log("tokenAddress", tokenAddress);
const buyGasPrice = calculate_gas_price("buy", transaction.gasPrice)
const sellGasPrice = calculate_gas_price("sell", transaction.gasPrice)
// after calculating the gas price we buy the token
console.log("going to buy");
await buyToken(account, tokenAddress, transaction.gasLimit, buyGasPrice)
// after buying the token we sell it
console.log("going to sell the token");
await sellToken(account, tokenAddress, transaction.gasLimit, sellGasPrice)
}
}
}
}
});
});
customWsProvider._websocket.on("error", async (ep) => {
console.log(`Unable to connect to ${ep.subdomain} retrying in 3s...`);
setTimeout(init, 3000);
});
customWsProvider._websocket.on("close", async (code) => {
console.log(
`Connection lost with code ${code}! Attempting reconnect in 3s...`
);
customWsProvider._websocket.terminate();
setTimeout(init, 3000);
});
};
init();
//now we create the express server
const server = http.createServer(app);
// we launch the server
server.listen(PORT, () => {
console.log(`Listening on port ${PORT}`)
});