Skip to content

Commit

Permalink
refactor: dont send a notification for every fee change (#5)
Browse files Browse the repository at this point in the history
* refactor: dont send a notification for every fee change

* Apply suggestions from code review

Co-authored-by: michael1011 <[email protected]>

---------

Co-authored-by: michael1011 <[email protected]>
  • Loading branch information
jackstar12 and michael1011 authored Jan 23, 2025
1 parent 2891c5c commit 5c501a9
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,19 @@ async def notify_subscribers(
from_currency: str,
to_currency: str,
fees: float,
threshold: float,
):
subscribers = await get_subscribers(session)
logging.info(
f"Notifying {len(subscribers)} subscribers about {from_currency} -> {to_currency} {swap_type} fees"
)

message = f"Fees for {swap_type} {from_currency} -> {to_currency} at {encode_url_params(swap_type, from_currency, to_currency)}: {fees}%"
threshold_msg = (
f"went below {threshold}%"
if fees < threshold
else f"are above {threshold}% again"
)
message = f"Fees for {swap_type} {from_currency} -> {to_currency} {threshold_msg}: {encode_url_params(swap_type, from_currency, to_currency)}"

for chat_id in subscribers:
try:
Expand Down Expand Up @@ -106,17 +112,18 @@ async def check_fees(
):
for from_currency, pairs in current.items():
for to_currency, fee in pairs.items():
if fee == previous.get(from_currency, {}).get(to_currency, 0):
continue

if fee < fee_threshold:
previous_fee = previous.get(from_currency, {}).get(to_currency, 0)
below = fee < fee_threshold and previous_fee > fee_threshold
above = fee > fee_threshold and previous_fee < fee_threshold
if below or above:
await notify_subscribers(
bot,
session,
swap_type,
from_currency,
to_currency,
fee,
fee_threshold,
)


Expand Down

0 comments on commit 5c501a9

Please sign in to comment.