Skip to content

Commit

Permalink
HTML Emails supported
Browse files Browse the repository at this point in the history
  • Loading branch information
Hendrik Schicke committed Oct 16, 2021
1 parent 2723c9f commit 3bd91ec
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 76 deletions.
19 changes: 11 additions & 8 deletions C_Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from datetime import datetime, timedelta
from time import time

Transaction_class, Queries_class, Cooldown_class, Logger_class, Terra_class, Prettify_class = Transaction(), Queries(), Cooldown(), Logger(), Terra, Prettify()
Transaction_class, Queries_class, Cooldown_class, Logger_class, Terra_class, Prettify_class, Notifications_class = Transaction(), Queries(), Cooldown(), Logger(), Terra, Prettify(), Notifications
default_logger, report_logger, report_array = Logger_class.default_logger, Logger_class.report_logger, Logger_class.report_array

def keep_safe():
Expand Down Expand Up @@ -937,7 +937,7 @@ def keep_safe():
if cooldowns.get('Staus_Report_cooldown') is None or cooldowns['Staus_Report_cooldown'] <= datetime_now:
if datetime.strptime(f'{datetime_now:%H:%M}', '%H:%M') > datetime.strptime(config.Status_update_time, '%H:%M'):

status_update = Prettify_class.generate_status_report(Anchor_borrow_info, Mirror_position_info)
status_update = Notifications_class.generate_status_report(Anchor_borrow_info, Mirror_position_info)

# Cooldown: Write date of today into cooldown dictionary
cooldowns['Staus_Report_cooldown'] = datetime_now + timedelta(hours=config.Status_update_frequency)
Expand Down Expand Up @@ -966,26 +966,29 @@ def keep_safe():
# Write all from current report_logger to array
report_content = report_array.getvalue()

if config.Email_format.lower() == 'html':
report_content = Notifications_class.report_content_to_HTML(report_content)

# Notify user about something that has been done
# Will not send if Debug_mode enabled
if config.Send_me_a_report \
and len(report_content) > 0 \
and not config.Debug_mode:
if config.Notify_Slack:
Notifications.slack_webhook(report_content)
Notifications_class.slack_webhook(report_content)
if config.Notify_Telegram:
Notifications.telegram_notification(report_content)
Notifications_class.telegram_notification(report_content)
if config.Notify_Gmail:
Notifications.gmail_notification('TEXT', f'{config.EMAIL_SUBJECT} Report:', report_content)
Notifications_class.gmail_notification('TEXT', f'{config.EMAIL_SUBJECT} Report:', report_content)

# Notify user about status report
if status_update != False:
if config.Notify_Slack:
Notifications.slack_webhook(status_update)
Notifications_class.slack_webhook(status_update)
if config.Notify_Telegram:
Notifications.telegram_notification(status_update)
Notifications_class.telegram_notification(status_update)
if config.Notify_Gmail:
Notifications.gmail_notification(config.Email_format, f'{config.EMAIL_SUBJECT} Status:', status_update)
Notifications_class.gmail_notification(config.Email_format, f'{config.EMAIL_SUBJECT} Status:', status_update)

default_logger.debug(f'{datetime.now():%H:%M} [Script] Ran successful. Runtime: {(time() - begin_time):.0f}s')
print(f'[Script] At {datetime.now():%H:%M}, ran successfully. Runtime: {(time() - begin_time):.0f}s')
Expand Down
65 changes: 65 additions & 0 deletions assets/Notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,68 @@ def gmail_notification(format:str, subject:str, message:str):
msg['From'] = config.EMAIL_FROM
msg['To'] = config.EMAIL_TO
server.send_message(msg)

def generate_status_report(Anchor_borrow_info, Mirror_position_info):

status_update = ""

if config.Email_format.lower() == 'text' or config.Email_format.lower() == 'txt':
if Anchor_borrow_info["loan_amount"] > 0:
status_update += f'-----------------------------------\n'
status_update += f'------------- ANCHOR --------------\n'
status_update += f'-----------------------------------\n'
status_update += f'bETH collateral: {(Anchor_borrow_info["amount_bETH_collateral"].__float__()/1000000):.3f} bETH\n'
status_update += f'bLuna collateral: {(Anchor_borrow_info["amount_bLuna_collateral"].__float__()/1000000):.0f} bLuna\n'
status_update += f'Total collateral: {(Anchor_borrow_info["total_collateral_value"].__float__()/1000000):.0f} UST\n'
status_update += f'Loan amount: {(Anchor_borrow_info["loan_amount"].__float__()/1000000):.0f} UST\n'
status_update += f'Borrow limit: {(Anchor_borrow_info["borrow_limit"].__float__()/1000000):.0f} UST\n'
status_update += f'Current LTV: {Anchor_borrow_info["cur_col_ratio"].__float__()*100:.0f} %\n'
status_update += f'If your collateral would lose {Anchor_borrow_info["collateral_loss_to_liq"].__float__()*100:.0f}% you would get liquidated.\n'

if len(Mirror_position_info) > 0:

status_update += f'-----------------------------------\n'
status_update += f'------------- MIRROR --------------\n'
status_update += f'-----------------------------------\n'

for position in Mirror_position_info:

status_update += f'Position: {position["position_idx"]} - {position["mAsset_symbol"]}\n'
status_update += f'Collateral value: {(position["collateral_amount_in_kind"].__float__()/1000000):.0f} {position["collateral_token_denom"]}\n'
status_update += f'Collateral value: {(position["collateral_amount_in_ust"].__float__()/1000000):.0f} UST\n'
status_update += f'Shorted Value in UST: {(position["shorted_asset_amount"].__float__()/1000000):.0f} UST\n'
status_update += f'Current LTV: {position["cur_col_ratio"].__float__():.0f}00 %\n'
status_update += f'If all your collateral loses {(position["collateral_loss_to_liq"].__float__()*100):.0f}%\n'
status_update += f'or if {position["mAsset_symbol"]} raises by {(position["shorted_mAsset_gain_to_liq"].__float__()*100):.0f}% you would get liquidated.\n'
status_update += f'\n'

elif config.Email_format == 'html' or config.Email_format =='HTML':
if Anchor_borrow_info["loan_amount"] > 0:
status_update += f'<h2>Anchor</h2>'
status_update += f'bETH collateral: {(Anchor_borrow_info["amount_bETH_collateral"].__float__()/1000000):.3f} bETH<br>'
status_update += f'bLuna collateral: {(Anchor_borrow_info["amount_bLuna_collateral"].__float__()/1000000):.0f} bLuna<br>'
status_update += f'Total collateral: {(Anchor_borrow_info["total_collateral_value"].__float__()/1000000):.0f} UST<br>'
status_update += f'Loan amount: {(Anchor_borrow_info["loan_amount"].__float__()/1000000):.0f} UST<br>'
status_update += f'Borrow limit: {(Anchor_borrow_info["borrow_limit"].__float__()/1000000):.0f} UST<br>'
status_update += f'Current LTV: {Anchor_borrow_info["cur_col_ratio"].__float__()*100:.0f} %<br>'
status_update += f'If your collateral would lose {Anchor_borrow_info["collateral_loss_to_liq"].__float__()*100:.0f}% you would get liquidated.<br>'

if len(Mirror_position_info) > 0:

status_update += f'<h2>Mirror</h2>'

for position in Mirror_position_info:

status_update += f'<h3>Position: {position["position_idx"]} - {position["mAsset_symbol"]}</h3>'
status_update += f'Collateral value: {(position["collateral_amount_in_kind"].__float__()/1000000):.0f} {position["collateral_token_denom"]}<br>'
status_update += f'Collateral value: {(position["collateral_amount_in_ust"].__float__()/1000000):.0f} UST<br>'
status_update += f'Shorted Value in UST: {(position["shorted_asset_amount"].__float__()/1000000):.0f} UST<br>'
status_update += f'Current LTV: {position["cur_col_ratio"].__float__():.0f}00 %<br>'
status_update += f'If your collateral would lose {(position["collateral_loss_to_liq"].__float__()*100):.0f}%<br>'
status_update += f'or if {position["mAsset_symbol"]} would raise by {(position["shorted_mAsset_gain_to_liq"].__float__()*100):.0f}% you would get liquidated.<br>'
status_update += f'<br>'

return status_update

def report_content_to_HTML(report_content):
return report_content.replace('\n','<br>')
69 changes: 2 additions & 67 deletions assets/Other.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ def read_cooldown(self):
return {}

class Prettify:
if config.Debug_mode: print(f'Prettify Class loaded.')
def email_text_to_html(self):
# Todo
pass
if config.Debug_mode: print(f'Prettify Class loaded.')

def value_convert_dec_to_float(self, input_value, human):

Expand Down Expand Up @@ -83,66 +80,4 @@ def dict_value_convert_dec_to_float(self, input_value, human=False):

# If it is just a plain dict
elif type(input_value) is dict:
return dict((k, self.value_convert_dec_to_float(v, human)) for k, v in input_value.items())

def generate_status_report(self, Anchor_borrow_info, Mirror_position_info):

status_update = ""

if config.Email_format == 'text' or 'TEXT' or 'TXT':
if Anchor_borrow_info["loan_amount"] > 0:
status_update += f'-----------------------------------\n'
status_update += f'------------- ANCHOR --------------\n'
status_update += f'-----------------------------------\n'
status_update += f'bETH collateral: {(Anchor_borrow_info["amount_bETH_collateral"].__float__()/1000000):.3f} bETH\n'
status_update += f'bLuna collateral: {(Anchor_borrow_info["amount_bLuna_collateral"].__float__()/1000000):.0f} bLuna\n'
status_update += f'Total collateral: {(Anchor_borrow_info["total_collateral_value"].__float__()/1000000):.0f} UST\n'
status_update += f'Loan amount: {(Anchor_borrow_info["loan_amount"].__float__()/1000000):.0f} UST\n'
status_update += f'Borrow limit: {(Anchor_borrow_info["borrow_limit"].__float__()/1000000):.0f} UST\n'
status_update += f'Current LTV: {Anchor_borrow_info["cur_col_ratio"].__float__()*100:.0f} %\n'
status_update += f'If all your collateral loses {Anchor_borrow_info["collateral_loss_to_liq"].__float__()*100:.0f}% you would get liquidated.\n'

if len(Mirror_position_info) > 0:

status_update += f'-----------------------------------\n'
status_update += f'------------- MIRROR --------------\n'
status_update += f'-----------------------------------\n'

for position in Mirror_position_info:

status_update += f'Position: {position["position_idx"]} - {position["mAsset_symbol"]}\n'
status_update += f'Collateral value: {(position["collateral_amount_in_kind"].__float__()/1000000):.0f} {position["collateral_token_denom"]}\n'
status_update += f'Collateral value: {(position["collateral_amount_in_ust"].__float__()/1000000):.0f} UST\n'
status_update += f'Shorted Value in UST: {(position["shorted_asset_amount"].__float__()/1000000):.0f} UST\n'
status_update += f'Current LTV: {position["cur_col_ratio"].__float__():.0f}00 %\n'
status_update += f'If all your collateral loses {(position["collateral_loss_to_liq"].__float__()*100):.0f}%\n'
status_update += f'or if {position["mAsset_symbol"]} raises by {(position["shorted_mAsset_gain_to_liq"].__float__()*100):.0f}% you would get liquidated.\n'
status_update += f'\n'

elif config.Email_format == 'html' or 'HTML':
if Anchor_borrow_info["loan_amount"] > 0:
status_update += f'<h2>Anchor</h2>'
status_update += f'bETH collateral: {(Anchor_borrow_info["amount_bETH_collateral"].__float__()/1000000):.3f} bETH</br>'
status_update += f'bLuna collateral: {(Anchor_borrow_info["amount_bLuna_collateral"].__float__()/1000000):.0f} bLuna</br>'
status_update += f'Total collateral: {(Anchor_borrow_info["total_collateral_value"].__float__()/1000000):.0f} UST</br>'
status_update += f'Loan amount: {(Anchor_borrow_info["loan_amount"].__float__()/1000000):.0f} UST</br>'
status_update += f'Borrow limit: {(Anchor_borrow_info["borrow_limit"].__float__()/1000000):.0f} UST</br>'
status_update += f'Current LTV: {Anchor_borrow_info["cur_col_ratio"].__float__()*100:.0f} %</br>'
status_update += f'If all your collateral loses {Anchor_borrow_info["collateral_loss_to_liq"].__float__()*100:.0f}% you would get liquidated.</br>'

if len(Mirror_position_info) > 0:

status_update += f'<h2>Mirror</h2>'

for position in Mirror_position_info:

status_update += f'<h3>Position: {position["position_idx"]} - {position["mAsset_symbol"]}</h3>'
status_update += f'Collateral value: {(position["collateral_amount_in_kind"].__float__()/1000000):.0f} {position["collateral_token_denom"]}</br>'
status_update += f'Collateral value: {(position["collateral_amount_in_ust"].__float__()/1000000):.0f} UST</br>'
status_update += f'Shorted Value in UST: {(position["shorted_asset_amount"].__float__()/1000000):.0f} UST</br>'
status_update += f'Current LTV: {position["cur_col_ratio"].__float__():.0f}00 %</br>'
status_update += f'If all your collateral loses {(position["collateral_loss_to_liq"].__float__()*100):.0f}%</br>'
status_update += f'or if {position["mAsset_symbol"]} raises by {(position["shorted_mAsset_gain_to_liq"].__float__()*100):.0f}% you would get liquidated.</br>'
status_update += f'</br>'

return status_update
return dict((k, self.value_convert_dec_to_float(v, human)) for k, v in input_value.items())
1 change: 0 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ To send emails from your Google account you need to get a `GMAIL_APP_PASSWORD`.
5. Copy the app password, it will be in a yellow box and looks like: "cjut fanq prdo diby".

## Under development (in desc priority)
- HTML Status Report (function is there, just not formatted properly).
- Run Mirror withdrawals before repay of Anchor Borrow to make use of that available aUST
- Build a front end.

Expand Down

0 comments on commit 3bd91ec

Please sign in to comment.