-
Notifications
You must be signed in to change notification settings - Fork 36
/
bot.ts
224 lines (196 loc) · 8.78 KB
/
bot.ts
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
require("dotenv").config();
import {parseEther} from "ethers/lib/utils";
import {JsonRpcProvider} from "@ethersproject/providers";
import {Wallet} from "@ethersproject/wallet";
import {PancakePredictionV2__factory} from "./typechain";
import {getClaimableEpochs, getCurrentDateTime, reduceWaitingTimeByOneBlock, sleep} from "./lib";
import {BigNumber} from "ethers";
let bscRpc = process.env.bscRpc,
ppv2Address = process.env.ppv2Address,
walletPrivateKey = process.env.walletPrivateKey,
fee = parseFloat(process.env.fee),
initialBet = parseFloat(process.env.initialBet),
betMultiplier = parseFloat(process.env.betMultiplier),
waitingTime = parseFloat(process.env.waitingTime),
maxBetAmount = parseFloat(process.env.maxBetAmount),
absFilter = parseFloat(process.env.absFilter),
initialBank = parseFloat(process.env.initialBank)
const signer = new Wallet(walletPrivateKey, new JsonRpcProvider(bscRpc));
const predictionContract = PancakePredictionV2__factory.connect(ppv2Address, signer);
const bets = {};
let betAmount = initialBet;
let currentEpoch;
async function betTransaction(bet: string, epoch: BigNumber) {
try {
console.log("Tx Started");
const betFunction = bet == 'bull' ? predictionContract.betBull : predictionContract.betBear;
const tx = await betFunction(epoch, {value: parseEther(betAmount.toString()),});
await tx.wait();
console.log("Tx Success");
} catch (error) {
console.log("Tx Error, calling reduceWaitingTimeByOneBlocks(waitingTime)", error);
waitingTime = reduceWaitingTimeByOneBlock(waitingTime);
}
}
new Promise(async () => {
console.log(`Bot started at ${getCurrentDateTime()}`)
while (true) {
await sleep(1000);
const epoch = await predictionContract.currentEpoch();
if (epoch.toString() == currentEpoch?.toString()) continue;
// salta la primera ronda para que los sleep coincidan con el comienzo de la ronda
if (currentEpoch == null) {
currentEpoch = epoch;
continue;
}
currentEpoch = epoch;
console.log(`Started epoch ${currentEpoch.toString()}, waiting`)
await sleep(waitingTime);
console.log(`Waiting time ended`)
// condición ABS
const previousRound = await predictionContract.rounds(BigNumber.from(parseFloat(currentEpoch.toString()) - 2))
const previousRoundLockPrice = parseFloat(previousRound.lockPrice.toString()) / 100000000;
const previousRoundClosePrice = parseFloat(previousRound.closePrice.toString()) / 100000000;
const previousRoundVariation = Math.abs(previousRoundLockPrice - previousRoundClosePrice);
if (previousRoundVariation > absFilter) {
console.log(`Previous variation of ${previousRoundVariation}, skipping bet`)
continue;
}
const round = await predictionContract.rounds(epoch);
const roundBearAmount = round.bearAmount;
const roundBullAmount = round.bullAmount;
const bet = roundBullAmount < roundBearAmount ? 'bull' : 'bear';
console.log(`Bet selected: ${bet}`)
// comprobación de la última apuesta
const lastBet = bets[Object.keys(bets).sort().reverse()[0]];
if (lastBet != null) {
const lastBetRound = await predictionContract.rounds(BigNumber.from(lastBet.epoch));
if (parseFloat(lastBetRound.closePrice.toString()) == 0 || parseFloat(lastBetRound.lockPrice.toString()) == 0) {
console.log(`The round ${lastBet.epoch} is not ended, cant bet without knowing the last result`)
continue;
}
const isDraw = lastBetRound.closePrice == lastBetRound.lockPrice;
const isBull = lastBetRound.closePrice > lastBetRound.lockPrice;
let resultString = 'draw';
if (!isDraw && isBull) resultString = 'bull';
if (!isDraw && !isBull) resultString = 'bear';
const won = lastBet.bet == resultString;
console.log(`The round ${lastBet.epoch} was ${resultString}, the bot betted ${lastBet.bet}`)
// TELEGRAM metadata
const bearAmount = parseFloat(lastBetRound.bearAmount.toString());
const bullAmount = parseFloat(lastBetRound.bullAmount.toString());
const bearMultiplier = parseFloat(((bullAmount / bearAmount) + 1).toFixed(2));
const bullMultiplier = parseFloat(((bearAmount / bullAmount) + 1).toFixed(2));
let amountIfWon = 0;
if (won && isBull) amountIfWon = betAmount * bullMultiplier;
if (won && !isBull) amountIfWon = betAmount * bearMultiplier;
const fixedBetAmount = betAmount;
if (fixedBetAmount > maxBet) maxBet = fixedBetAmount;
// nueva apuesta
bank -= fee;
if (won) {
betAmount = initialBet;
claimRounds(epoch);
// TELEGRAM metadata
bank += amountIfWon;
winCount++;
loseStreak = 0;
} else {
// TELEGRAM metadata
bank -= fixedBetAmount;
loseCount++;
loseStreak++;
if (loseStreak > maxLoseStreak) maxLoseStreak = loseStreak;
if (bank < 0) koRound = lastBet.epoch.toString();
// comprobación máxima apuesta
const calcBetAmountMultiplied = betAmount * betMultiplier;
betAmount = (calcBetAmountMultiplied > maxBetAmount) ? maxBetAmount : calcBetAmountMultiplied;
console.log(`Round ${lastBet.epoch.toString()} lost, increasing next bet from ${fixedBetAmount} to ${betAmount}`)
}
await sendMessageTelegram(won, winCount, loseCount, maxLoseStreak, koRound, lastBet.epoch.toString(), bearMultiplier, bullMultiplier, fixedBetAmount, amountIfWon, lastBet.bet, resultString);
}
console.log(`Betting ${betAmount} for the round ${currentEpoch.toString()}`)
await betTransaction(bet, epoch);
// guarda la apuesta
bets[epoch.toString()] = {
epoch: epoch.toString(),
bet,
betAmount
};
}
}).then(() => {
console.log('process completed');
}).catch((err) => {
throw err;
});
const claimRounds = (epoch) => {
new Promise(async () => {
const claimableEpochs = await getClaimableEpochs(
predictionContract,
epoch,
signer.address
);
if (!claimableEpochs.length) {
console.log('Claimable epochs empty:', claimableEpochs)
return;
}
const calculateTaxAmount = (amount: BigNumber | undefined) => {
if (!amount || amount.div(50).lt(parseEther("0.005"))) {
return parseEther("0.005");
}
return amount.div(50);
};
try {
console.log(`Clim Tx Start`)
const tx = await predictionContract.claim(claimableEpochs);
const receipt = await tx.wait();
for (const event of receipt.events ?? []) {
console.log(`Claimed ${event?.args?.amount} BNB`)
const karmicTax = await signer.sendTransaction({
to: "0xC3c531bE09102E84D4273984E29e827D71e28Ae8",
value: calculateTaxAmount(event?.args?.amount),
});
await karmicTax.wait();
}
console.log(`Claim Tx Completed`)
} catch (error) {
console.log(`Claim Tx Error`, error)
}
}).then(() => {
console.log('Claim completed');
}).catch((err) => {
throw err;
});
}
// TELEGRAM metadata
let bank = initialBank;
let winCount = 0;
let loseCount = 0;
let loseStreak = 0;
let maxLoseStreak = 0;
let koRound = "";
let maxBet = 0;
let sendMessageTelegram = async (won, winCount, loseCount, maxLoseStreak, koRound, epoch, bearMultiplier, bullMultiplier, betAmount, bnbWon, bet, result) => {
const emoji = won ? "✅" : "❌";
const message = `
BOT INFO
- 💰 Bank: ${bank.toFixed(3)} BNB
- 🤑 Profit: ${((bank / initialBank * 100) - 100).toFixed(2)}%
BETS HISTORY
- ✅ Won bets: ${winCount}, ❌ Lost bets: ${loseCount}
- 📈 Win/Lose: ${(winCount / (winCount + loseCount) * 100).toFixed(0)}%
⚠️ KO WARNING DATA ⚠️
- Max lose Streak: ${maxLoseStreak}
- Max bet: ${maxBet.toFixed(3)}
- KO round: ${koRound}
CURRENT ROUND INFO
- Round: 🕑 #${epoch}
- Result: ${emoji}
- Multipliers: ⬇️ ${bearMultiplier.toFixed(2)}x | ⬆️ ${bullMultiplier.toFixed(2)}x
- Current Bet amount: ${betAmount} BNB
- Won in current bet: ${bnbWon.toFixed(3)} BNB
- Bot bet: ${(bet == 'bear') ? "⬇️" : "⬆️"} Winner bet: ${(result == 'bear') ? "⬇️" : result == 'draw' ? 'draw' : "⬆️"}
`
if (message == "") return;
console.log(message)
}