forked from EPLJVKHPSP/farming_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
142 lines (117 loc) · 6.13 KB
/
Copy pathcalculator.py
File metadata and controls
142 lines (117 loc) · 6.13 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
import os
from telegram import Update, ReplyKeyboardMarkup, KeyboardButton
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackContext
from pycoingecko import CoinGeckoAPI
from config import TELEGRAM_BOT_TOKEN as bot_token
def get_price(crypto_name):
cg_client = CoinGeckoAPI()
try:
data = cg_client.get_price(ids=crypto_name, vs_currencies='usd')
return data[crypto_name]['usd']
except Exception as e:
print(f"Failed to get price: {e}")
return None
CRYPTO1, CRYPTO1_BEFORE, CRYPTO1_AFTER, CRYPTO2, CRYPTO2_BEFORE, CRYPTO2_AFTER, CRYPTO2_FINAL_QTY, COMMISSION = range(8)
def send_menu(update: Update, context: CallbackContext) -> int:
keyboard = [['IL Calc'], ['Back']]
reply_markup = ReplyKeyboardMarkup(keyboard)
update.message.reply_text('Choose an option in menu, all the token names you can find on coingecko.com', reply_markup=reply_markup)
return CRYPTO1
def il_calc(update: Update, context: CallbackContext) -> int:
if update.message.text == 'IL Calc':
update.message.reply_text("Token1 Name?")
return CRYPTO1_BEFORE
elif update.message.text == 'Back':
return send_menu(update, context)
return send_menu(update, context)
def crypto1_before(update: Update, context: CallbackContext) -> int:
if update.message.text == 'Back':
return send_menu(update, context)
context.user_data['crypto1_name'] = update.message.text
update.message.reply_text(f"Quantity of {context.user_data['crypto1_name']} BEFORE?")
return CRYPTO1_AFTER
def crypto1_after(update: Update, context: CallbackContext) -> int:
try:
context.user_data['crypto1_qty_before'] = float(update.message.text)
update.message.reply_text(f"Quantity of {context.user_data['crypto1_name']} AFTER?:")
return CRYPTO2
except ValueError:
update.message.reply_text(f"Invalid input. Please enter a number for the quantity of {context.user_data['crypto1_name']} before:")
return CRYPTO1_AFTER
def crypto2(update: Update, context: CallbackContext) -> int:
if update.message.text == 'Back':
return send_menu(update, context)
context.user_data['crypto1_qty_after'] = float(update.message.text)
update.message.reply_text("Token2 Name?")
return CRYPTO2_BEFORE
def crypto2_before(update: Update, context: CallbackContext) -> int:
if update.message.text == 'Back':
return send_menu(update, context)
context.user_data['crypto2_name'] = update.message.text
update.message.reply_text(f"Quantity of {context.user_data['crypto2_name']} BEFORE?")
return CRYPTO2_AFTER
def crypto2_after(update: Update, context: CallbackContext) -> int:
try:
context.user_data['crypto2_qty_before'] = float(update.message.text)
update.message.reply_text(f"Quantity of {context.user_data['crypto2_name']} AFTER?")
return CRYPTO2_FINAL_QTY
except ValueError:
update.message.reply_text(f"Invalid input. Please enter a number for the quantity of {context.user_data['crypto2_name']} before:")
return CRYPTO2_AFTER
def crypto2_final_qty(update: Update, context: CallbackContext) -> int:
try:
context.user_data['crypto2_qty_after'] = float(update.message.text)
update.message.reply_text("Farmed comission?")
return COMMISSION
except ValueError:
update.message.reply_text(f"Invalid input. Please enter a number for the quantity of {context.user_data['crypto2_name']} after:")
return CRYPTO2_AFTER
def commission(update: Update, context: CallbackContext) -> int:
try:
context.user_data['commission'] = float(update.message.text)
return calculate_il(update, context) # move to calculate_il after setting the commission
except ValueError:
update.message.reply_text("Invalid input. Please enter a number for the commission:")
return COMMISSION
def calculate_il(update: Update, context: CallbackContext) -> int:
try:
crypto1_qty_before = context.user_data['crypto1_qty_before']
crypto1_qty_after = context.user_data['crypto1_qty_after']
crypto2_qty_before = context.user_data['crypto2_qty_before']
crypto2_qty_after = context.user_data['crypto2_qty_after']
commission = context.user_data['commission']
crypto1_name = context.user_data['crypto1_name']
crypto2_name = context.user_data['crypto2_name']
price1_after = get_price(crypto1_name)
price2_after = get_price(crypto2_name)
il = ((crypto2_qty_after - crypto2_qty_before - (crypto1_qty_before - crypto1_qty_after) * price1_after / price2_after)) * price2_after + commission
update.message.reply_text(f"The impermanent loss is: {round(il, 2)}$")
return send_menu(update, context)
except Exception as e:
print(f"An error occurred: {e}")
return ConversationHandler.END
def cancel(update: Update, context: CallbackContext) -> int:
update.message.reply_text("Operation cancelled.")
return ConversationHandler.END
def main():
bot_token = 'TELEGRAM-API'
updater = Updater(token=bot_token, use_context=True)
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', send_menu)],
states={
CRYPTO1: [MessageHandler(Filters.text & ~Filters.command, il_calc)],
CRYPTO1_BEFORE: [MessageHandler(Filters.text & ~Filters.command, crypto1_before)],
CRYPTO1_AFTER: [MessageHandler(Filters.text & ~Filters.command, crypto1_after)],
CRYPTO2: [MessageHandler(Filters.text & ~Filters.command, crypto2)],
CRYPTO2_BEFORE: [MessageHandler(Filters.text & ~Filters.command, crypto2_before)],
CRYPTO2_AFTER: [MessageHandler(Filters.text & ~Filters.command, crypto2_after)],
CRYPTO2_FINAL_QTY: [MessageHandler(Filters.text & ~Filters.command, crypto2_final_qty)],
COMMISSION: [MessageHandler(Filters.text & ~Filters.command, commission)],
},
fallbacks=[CommandHandler('cancel', cancel)],
)
updater.dispatcher.add_handler(conv_handler)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()