Skip to content

Commit

Permalink
feat: bot 通知支撑显示用于交易的 SOL 数量
Browse files Browse the repository at this point in the history
  • Loading branch information
mkdir700 committed Aug 19, 2024
1 parent aad02f6 commit ec03b4b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 36 deletions.
60 changes: 26 additions & 34 deletions src/tgbot/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import json
import os
from datetime import datetime, timedelta

Expand Down Expand Up @@ -44,10 +43,10 @@ def shorten_address(s, max_length=16):

def extend_data(data: dict):
transaction_type = data["transaction_type"]
address = data["address"]
token_mint = data["token_mint"]
pre_token_balance = data["pre_token_balance"]
post_token_balance = data["post_token_balance"]
address = data["owner"]
token_mint = data["token"]["mint"]
pre_token_balance = data["token"]["pre_balance"]
post_token_balance = data["token"]["post_balance"]

token_amount_change = post_token_balance - pre_token_balance
if pre_token_balance == 0:
Expand Down Expand Up @@ -75,13 +74,9 @@ def extend_data(data: dict):

def build_message(data: dict):
transaction_id = data["transaction_id"]
address = data["address"]
token_mint = data["token_mint"]
pre_token_balance = data["pre_token_balance"]
post_token_balance = data["post_token_balance"]
platform = data["platform"]
signature = data["signature"]
token_amount_change = post_token_balance - pre_token_balance
change_rate = data["change_rate"]
owner = data["owner"]
transaction_direction = data["transaction_direction"]
timestamp = float(data["timestamp"])
seconds = timestamp // 1000
Expand All @@ -90,15 +85,23 @@ def build_message(data: dict):
milliseconds=milliseconds
)

logger.info(data)
# Token
token_mint = data["token"]["mint"]
pre_token_balance = data["token"]["pre_balance"]
post_token_balance = data["token"]["post_balance"]
token_amount_change = post_token_balance - pre_token_balance
change_rate = data["change_rate"]

# SOL
sol_change_amount = abs(data["sol"]["change_amount"])

tmpl = Template(message_tmpl)
rendered = tmpl.render(
transaction_id=transaction_id,
transaction_direction=transaction_direction,
smart_money_address=address,
smart_money_alias=shorten_address(address),
smart_money_address=owner,
smart_money_alias=shorten_address(owner),
token_mint=token_mint,
token_amount=data["token_amount"],
change_rate=f"{change_rate:.2%}",
change_icon="📈" if token_amount_change > 0 else "📉",
before_amount=pre_token_balance,
Expand All @@ -108,34 +111,23 @@ def build_message(data: dict):
signature=signature,
transaction_time=transaction_time,
processed_time=datetime.now(),
token_name=data["token_name"],
token_symbol=data["token_symbol"],
token_name=data["token"]["name"],
token_symbol=data["token"]["symbol"],
sol_change_amount=sol_change_amount,
platform=platform,
)
logger.info("\n" + rendered)
return rendered


async def get_latest_message() -> dict | None:
message = await tg_bot.pop_message(my_chat_id)
"""
(b'tgbot:5049063827', b'{"address": "EARFf4ZxBRBuPJc1DyhNwXG5GJNJYSEZHNUJwTSGhzyQ", "token_mint": "Gc2yDSR1rUZ4QuWc4yxQ3y7cvAA2AE2QmrjP3a8mpump", "token_amount": 12.85346999997273, "pre_token_balance": 283068.032564, "post_token_balance": 283055.179094, "transaction_type": "reduce", "transaction_id": "EARFf4ZxBRBuPJc1DyhNwXG5GJNJYSEZHNUJwTSGhzyQ:Gc2yDSR1rUZ4QuWc4yxQ3y7cvAA2AE2QmrjP3a8mpump:12.85346999997273:reduce", "signature": "42GPWM2XNtAdJBmuyX6owbjRK9Ej2MnkqDKP1K4mSaMgeqYnQ8S11T2MmszkHHNSx9ifnXiPCVgSkc44qVZRbotz"}')
"""
if message is None:
return
_, content = message
try:
data = json.loads(content.decode("utf-8"))
except json.JSONDecodeError:
logger.error(f"Failed to decode message: {content}")
return
return data
return await tg_bot.pop_message(my_chat_id)


async def send_message(bot: Bot, chat_id: str, data: dict) -> None:
address = data["address"]
token_mint = data["token_mint"]
address = data["owner"]
token_mint = data["token"]["mint"]
data = extend_data(data)
# change_rate = f"{data['change_rate']:.2%}"
# tx_direction = data["transaction_direction"]
text = build_message(data)

keyboards = [
Expand Down
4 changes: 2 additions & 2 deletions src/tgbot/templates/message.jinja
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% if token_amount_change > 0 %}
🟢<a href="https://gmgn.ai/sol/address/{{smart_money_address}}">{{smart_money_alias}}</a> {{transaction_direction}} 🟢🟢
🟢<a href="https://gmgn.ai/sol/address/{{smart_money_address}}">{{smart_money_alias}}</a> 买入 {{sol_change_amount}} SOL {{token_symbol}} 在 {{platform}} 🟢🟢
{% else %}
🔴<a href="https://gmgn.ai/sol/address/{{smart_money_address}}">{{smart_money_alias}}</a> {{transaction_direction}} 🔴🔴
🔴<a href="https://gmgn.ai/sol/address/{{smart_money_address}}">{{smart_money_alias}}</a> 卖出 {{sol_change_amount}} SOL {{token_symbol}} 在 {{platform}} 🔴🔴
{% endif %}
【{{transaction_direction}}】 <a href="https://solscan.io/tx/{{signature}}">点击查看</a>
🪙{{transaction_direction}}金额: {{amount_change}}
Expand Down

0 comments on commit ec03b4b

Please sign in to comment.