67 lines
2.4 KiB
Python
67 lines
2.4 KiB
Python
import json
|
|
|
|
from aiogram import types, Router
|
|
from aiogram.filters import Command
|
|
|
|
from app.core._keyboards import get_inline_keyboard
|
|
from app.core._utils.tg_process_template import tg_process_template
|
|
from app.core.logger import make_log
|
|
from app.core.models._blockchain.ton.connect import TonConnect, unpack_wallet_info
|
|
|
|
router = Router()
|
|
|
|
|
|
async def pause_ton_connection(ton_connect: TonConnect):
|
|
if ton_connect.connected:
|
|
ton_connect._sdk_client.pause_connection()
|
|
|
|
|
|
async def t_tonconnect_dev_menu(message: types.Message, memory=None, user=None, db_session=None, chat_wrap=None, **extra):
|
|
try:
|
|
command_args = message.text.split(" ")[1:]
|
|
except BaseException as e:
|
|
command_args = []
|
|
|
|
make_log("TonConnect_DevMenu", f"Command args: {command_args}", level='info')
|
|
wallet_app_name = 'tonkeeper'
|
|
if len(command_args) > 0:
|
|
wallet_app_name = command_args[0].lower()
|
|
|
|
keyboard = []
|
|
|
|
ton_connect, ton_connection = TonConnect.by_user(db_session, user, callback_fn=())
|
|
await ton_connect.restore_connection()
|
|
make_log("TonConnect_DevMenu", f"SDK connected?: {ton_connect.connected}", level='info')
|
|
if not ton_connect.connected:
|
|
if ton_connection:
|
|
make_log("TonConnect_DevMenu", f"Invalidating old connection", level='debug')
|
|
ton_connection.invalidated = True
|
|
db_session.commit()
|
|
|
|
message_text = f"""<b>Wallet is not connected</b>
|
|
|
|
Use /dev_tonconnect <code>{wallet_app_name}</code> for connect to wallet."""
|
|
connection_link = await ton_connect.new_connection(wallet_app_name)
|
|
ton_connect.connected
|
|
make_log("TonConnect_DevMenu", f"New connection link for {wallet_app_name}: {connection_link}", level='debug')
|
|
keyboard.append([
|
|
{
|
|
'text': 'Connect',
|
|
'url': connection_link
|
|
}
|
|
])
|
|
else:
|
|
wallet_info_text = json.dumps(unpack_wallet_info(ton_connect._sdk_client._wallet), indent=4, ensure_ascii=False)
|
|
message_text = f"""<b>Wallet is connected</b>
|
|
|
|
<pre>{wallet_info_text}</pre>"""
|
|
|
|
memory.add_task(pause_ton_connection, ton_connect, delay_s=60 * 3)
|
|
|
|
return await tg_process_template(
|
|
chat_wrap, message_text,
|
|
keyboard=get_inline_keyboard(keyboard) if keyboard else None
|
|
)
|
|
|
|
router.message.register(t_tonconnect_dev_menu, Command('dev_tonconnect'))
|