-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathapp.js
273 lines (212 loc) · 10.9 KB
/
app.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
269
270
271
272
273
const express = require('express')
const app = express()
const config = require('./config');
const port = config.port;
const fs = require('fs');
const { ethers } = require('ethers');
const provider = new ethers.providers.JsonRpcProvider(config.network);
const pk = config.privateKey;
const wallet = new ethers.Wallet(pk, provider);
const farmAbi = JSON.parse(fs.readFileSync(config.farmAbi, 'utf8'));
const farmAddress = config.farmAddress;
const farmContract = new ethers.Contract(farmAddress, farmAbi, provider);
const pendingRewardFnName = config.pendingRewardFnName;
const millisecondToCheck = config.millisecondToCheck;
const amountOfPool = config.numberOfPool;
const harvestNumber = config.harvestNumber;
const reinvestPool = config.reinvestPool;
const farmTokenAddress = config.farmTokenAddress;
const farmTokenAbi = JSON.parse(fs.readFileSync(config.farmTokenAbi, 'utf8'));;
const swapAddress = config.swapAddress;
const swapAbi = JSON.parse(fs.readFileSync(config.swapAbi, 'utf8'));
const swapContract = new ethers.Contract(swapAddress, swapAbi, provider);
const slippagePercent = config.slippagePercent;
const swapCutOffPercent = config.swapCutOffPercent;
const firstReinvestTokenAddress = config.firstReinvestTokenAddress;
const firstReinvestTokenAbi = JSON.parse(fs.readFileSync(config.firstReinvestTokenAbi, 'utf8'));;
const secoundReinvestTokenAddress = config.secoundReinvestTokenAddress;
const lpTokenAddress = config.lpTokenAddress;
const lpTokenAbi = JSON.parse(fs.readFileSync(config.lpTokenAbi, 'utf8'));
let tokenName = '';
app.listen(port, async () => {
console.log(`Auto-Reinvest on BSC app listening at http://localhost:${port}`)
console.log(`Recheck every : ${millisecondToCheck / 60000} mins`);
tokenName = await getTokenName(farmTokenAddress, farmTokenAbi);
checkToReinvest(amountOfPool, reinvestPool, wallet);
setInterval(async () => {
await checkToReinvest(amountOfPool, reinvestPool, wallet);
}, millisecondToCheck);
})
const getBalance = async (address) => {
const balance = await provider.getBalance(address);
return balance;
}
const getPendingRewards = async (poolNumber, address, fnName) => {
const balance = await farmContract[fnName](poolNumber, address);
return balance;
}
const toReadableNumber = (number) => ethers.utils.formatEther(number.toString());
const harvestReward = async (poolNumber, signer) => {
const contractWithSigner = farmContract.connect(signer);
const options = getTransactionOptions();
const transaction = await contractWithSigner.deposit(poolNumber, 0, options);
const receipt = await getTransactionReceipt(transaction);
if (receipt.status !== 1) {
console.log("Transaction failed!!! re-do it");
await harvestReward(poolNumber, signer)
}
const harvestBalance = await getTokenBalance(farmTokenAddress, farmTokenAbi, wallet.address);
console.log(`Harvested Reward : ${toReadableNumber(harvestBalance)} `);
}
const deposit = async (poolNumber, amount, signer) => {
const contractWithSigner = farmContract.connect(signer);
const options = getTransactionOptions();
const transaction = await contractWithSigner.deposit(poolNumber, amount, options);
const receipt = await getTransactionReceipt(transaction);
if (receipt.status !== 1) {
console.log("Transaction failed!!! re-do it");
await deposit(poolNumber, amount, signer);
}
console.log(`Deposit amount: ${toReadableNumber(amount)} `);
}
const checkToReinvest = async (amountOfPool, reinvestPool, wallet) => {
let harvestBalance = 0;
harvestBalance = await getTokenBalance(farmTokenAddress, farmTokenAbi, wallet.address);
console.log(`---------------------------------------------------------------`);
console.log(`******${tokenName}******`);
console.log(`Current native token amount in wallet : ${toReadableNumber(await getBalance(wallet.address))}`);
console.log(`Current ${tokenName} token amount in wallet : ${toReadableNumber(harvestBalance)}`);
for (let i = 0; i <= amountOfPool; i++) {
const currentStaking = await getStakingBalance(i, wallet.address)
if (currentStaking <= 0) continue;
console.log(`Current staking amount in Pool ${i} : ${toReadableNumber(currentStaking)}`);
}
if (toReadableNumber(harvestBalance) > harvestNumber) {
console.log(`Found token reward ${toReadableNumber(harvestBalance)} left in wallet, start reinvest`);
await reinvest(getSwapBalance(harvestBalance, swapCutOffPercent), reinvestPool, wallet);
}
let pendingRewards = 0
// SKIP 0 POOL
for (let i = 1; i <= amountOfPool; i++) {
const reward = await getPendingRewards(i, wallet.address, pendingRewardFnName);
if (reward <= 0) continue;
console.log(`Pending reward in Pool ${i} : ${toReadableNumber(reward)}`);
pendingRewards += parseFloat(toReadableNumber(reward));
}
console.log(`All pending rewards : ${pendingRewards}`);
console.log(`Will reinvest in Pool ${reinvestPool} when pending rewards ${pendingRewards} > ${harvestNumber} `);
console.log(`---------------------------------------------------------------`);
if (pendingRewards > harvestNumber) {
await harvestAllReward(amountOfPool, wallet);
harvestBalance = await getTokenBalance(farmTokenAddress, farmTokenAbi, wallet.address)
console.log(`Harvest Token amount : ${toReadableNumber(harvestBalance)}`);
await reinvest(harvestBalance, reinvestPool, wallet);
console.log(`Current staking amount : ${toReadableNumber(await getStakingBalance(reinvestPool, wallet.address))}`);
}
}
const harvestAllReward = async (amountOfPool, wallet) => {
console.log(`Start harvest`);
for (let i = 1; i <= amountOfPool; i++) {
const reward = await getPendingRewards(i, wallet.address, pendingRewardFnName);
if (reward <= 0) continue;
await harvestReward(i, wallet);
}
}
const reinvest = async (harvestBalance, poolNumber, wallet) => {
console.log(`Start reinvest in Pool ${poolNumber} with ${tokenName}`);
const deadline = getDeadlineTime();
const swapBalance = getSwapBalance(harvestBalance, swapCutOffPercent);
await swapToken(swapBalance, getSlippagePercentage(slippagePercent), [farmTokenAddress, secoundReinvestTokenAddress], deadline, wallet);
const reinvestBalance = await getTokenBalance(farmTokenAddress, farmTokenAbi, wallet.address);
await addLiquidityETH(farmTokenAddress, reinvestBalance, getSlippagePercentage(slippagePercent), wallet);
const lpBalance = await getTokenBalance(lpTokenAddress, lpTokenAbi, wallet.address);
await deposit(poolNumber, lpBalance.toString(), wallet);
}
const getAmountOut = async (amountIn, paths) => {
const amountOutMin = await swapContract.getAmountsOut(amountIn.toString(), paths);
return amountOutMin;
}
const swapToken = async (amountIn, slippagePercentage, paths, deadline, signer) => {
const getAmountRes = await getAmountOut(amountIn.toString(), paths);
const amountOutMin = getAmountRes[1] - (getAmountRes[1] * slippagePercentage);
const contractWithSigner = swapContract.connect(signer);
const options = getTransactionOptions();
const transaction = await contractWithSigner.swapExactTokensForETH(getAmountRes[0], amountOutMin.toString(), paths, signer.address, deadline, options);
const receipt = await getTransactionReceipt(transaction);
if (receipt.status !== 1) {
console.log("Transaction failed!!! re-do it");
await swapToken(amountIn, slippagePercentage, paths, deadline, signer);
}
console.log(`Swap ${tokenName} : ${toReadableNumber(amountIn)} for ${toReadableNumber(amountOutMin)} native token`);
}
const addLiquidityETH = async (tokenAddress, amountIn, slippagePercentage, signer) => {
const contractWithSigner = swapContract.connect(signer);
const amountTokenMin = amountIn * slippagePercentage;
const getAmountOutRes = await getAmountOut(amountIn, [tokenAddress, secoundReinvestTokenAddress]);
const amountETHMin = getAmountOutRes[1];
const deadline = getDeadlineTime();
const options = { gasLimit: 450000, value: amountETHMin };
const transaction = await contractWithSigner.addLiquidityETH(tokenAddress, amountIn.toString(), amountTokenMin.toString(), amountETHMin, signer.address, deadline, options);
const receipt = await getTransactionReceipt(transaction);
if (receipt.status !== 1) {
console.log("Transaction failed!!! re-do it");
await addLiquidityETH(tokenAddress, amountIn, slippagePercentage, signer);
}
console.log(`Add liquidity : [${tokenName} : ${toReadableNumber(amountIn)}, Native : ${toReadableNumber(amountETHMin)}]`);
}
const getSlippagePercentage = (slippage) => (100 - slippage) / 100;
const enterStaking = async (amount, signer) => {
const contractWithSigner = farmContract.connect(signer);
const options = getTransactionOptions();
const transaction = await contractWithSigner.enterStaking(amount, options);
const receipt = await getTransactionReceipt(transaction);
if (receipt.status !== 1) {
console.log("Transaction failed!!! re-do it");
await enterStaking(amount, signer);
}
}
const getStakingBalance = async (poolNumber, address) => {
const balance = await farmContract.userInfo(poolNumber, address);
return balance.amount;
}
const getTokenBalance = async (tokenAddress, abi, address) => {
const contract = new ethers.Contract(tokenAddress, abi, provider);
const balance = await contract.balanceOf(address);
return balance;
}
const getToken0FromLP = async (lpAddress, abi) => {
const contract = new ethers.Contract(lpAddress, abi, provider);
return await contract.token0();
}
const getToken1FromLP = async (lpAddress, abi) => {
const contract = new ethers.Contract(lpAddress, abi, provider);
return await contract.token1();
}
const getTokenName = async (tokenAddress, abi) => {
const contract = new ethers.Contract(tokenAddress, abi, provider);
return await contract.name();
}
const getTransactionOptions = () => {
return { gasLimit: 450000 };
}
const getDeadlineTime = () => {
const date = new Date();
date.setHours(date.getHours() + 1);
return date.getTime();
}
const getSwapBalance = (allBalance, cutOffPercent) => {
console.log(`Balance : ${toReadableNumber(allBalance)}`)
const divBalance = (allBalance / 2);
console.log(`DivBalance : ${toReadableNumber(divBalance)}`)
const percentage = cutOffPercent / 100;
console.log(`Cut off Percentage : ${cutOffPercent}%`)
const cutOffBalance = divBalance * percentage;
console.log(`Cut off Balance : ${toReadableNumber(cutOffBalance)}`)
const swapBalance = divBalance + cutOffBalance;
console.log(`SwapBalance : ${toReadableNumber(swapBalance)}`)
return swapBalance;
}
const getTransactionReceipt = async (transaction) => {
await provider.waitForTransaction(transaction.hash);
return await provider.getTransactionReceipt(transaction.hash);
}