dev@locazia: improve connect wallet

This commit is contained in:
user 2024-02-29 00:14:34 +03:00
parent 9ea251ec01
commit 031dbab6a6
3 changed files with 53 additions and 8 deletions

View File

@ -0,0 +1,35 @@
from aiogram import types, Router, F
from aiogram.filters import Command
from app.core._utils.tg_process_template import tg_process_template
from app.core._keyboards import get_inline_keyboard
from app.core.logger import logger
from app.core.models.wallet_connection import WalletConnection
from app.core.models._blockchain.ton.connect import TonConnect, unpack_wallet_info
from app.core._config import WEB_APP_URLS
router = Router()
async def t_callback_owned_content(query: types.CallbackQuery, memory=None, user=None, db_session=None, chat_wrap=None, **extra):
message_text = user.translated("ownedContent_menu")
return await tg_process_template(
chat_wrap, message_text,
keyboard=get_inline_keyboard([
[{
'text': user.translated('webApp_uploadContent_button'),
'web_app': types.WebAppInfo(
url=WEB_APP_URLS['uploadContent']
)
}],
[{
'text': user.translated('back_button'),
'callback_data': 'home'
}]
])
)
router.callback_query.register(t_callback_owned_content, F.data == 'ownedContent')

View File

@ -6,6 +6,8 @@ from aiogram import types, Router, F
from aiogram.filters import Command from aiogram.filters import Command
from app.bot.routers.tonconnect import router as tonconnect_router from app.bot.routers.tonconnect import router as tonconnect_router
from app.bot.routers.content import router as content_router
from app.core._utils.tg_process_template import tg_process_template from app.core._utils.tg_process_template import tg_process_template
from app.core._keyboards import get_inline_keyboard from app.core._keyboards import get_inline_keyboard
from app.core.logger import logger from app.core.logger import logger
@ -21,10 +23,12 @@ async def t_home_menu(__msg, **extra):
if extra.get('state'): if extra.get('state'):
await extra['state'].clear() await extra['state'].clear()
if not db_session.query(WalletConnection).filter( wallet_connection = db_session.query(WalletConnection).filter(
WalletConnection.user_id == user.id, WalletConnection.user_id == user.id,
WalletConnection.invalidated == False WalletConnection.invalidated == False
).first(): ).first()
if not wallet_connection:
ton_connect, ton_connection = TonConnect.by_user(db_session, user, callback_fn=()) ton_connect, ton_connection = TonConnect.by_user(db_session, user, callback_fn=())
await ton_connect.restore_connection() await ton_connect.restore_connection()
wallets = ton_connect._sdk_client.get_wallets() wallets = ton_connect._sdk_client.get_wallets()
@ -47,13 +51,17 @@ async def t_home_menu(__msg, **extra):
) )
return await tg_process_template( return await tg_process_template(
chat_wrap, user.translated('home_menu'), message_id=__msg.message.message_id if isinstance(__msg, types.CallbackQuery) else None, chat_wrap, user.translated('home_menu').format(
wallet_address=wallet_connection.wallet_address
), message_id=__msg.message.message_id if isinstance(__msg, types.CallbackQuery) else None,
keyboard=get_inline_keyboard([ keyboard=get_inline_keyboard([
[{ [{
'text': user.translated('webApp_uploadContent_button'), 'text': user.translated('ownedContent_button'),
'web_app': types.WebAppInfo( 'callback_data': 'ownedContent'
url=WEB_APP_URLS['uploadContent'] }]
) [{
'text': user.translated('disconnectWallet_button'),
'callback_data': 'disconnectWallet'
}] }]
]) ])
) )
@ -63,6 +71,7 @@ main_router.message.register(t_home_menu, Command('start'))
main_router.callback_query.register(t_home_menu, F.data == 'home') main_router.callback_query.register(t_home_menu, F.data == 'home')
main_router.include_routers(tonconnect_router) main_router.include_routers(tonconnect_router)
main_router.include_routers(content_router)
closing_router = Router() closing_router = Router()

View File

@ -67,10 +67,11 @@ Use /dev_tonconnect <code>{wallet_app_name}</code> for connect to wallet."""
async def t_callback_init_tonconnect(query: types.CallbackQuery, memory=None, user=None, db_session=None, chat_wrap=None, **extra): async def t_callback_init_tonconnect(query: types.CallbackQuery, memory=None, user=None, db_session=None, chat_wrap=None, **extra):
wallet_app_name = query.data.split("_")[1] wallet_app_name = query.data.split("_")[1]
ton_connect, ton_connection = TonConnect.by_user(db_session, user, callback_fn=()) ton_connect, ton_connection = TonConnect.by_user(db_session, user)
await ton_connect.restore_connection() await ton_connect.restore_connection()
connection_link = await ton_connect.new_connection(wallet_app_name) connection_link = await ton_connect.new_connection(wallet_app_name)
ton_connect.connected ton_connect.connected
memory.add_task(pause_ton_connection, ton_connect, delay_s=60 * 3)
make_log("TonConnect_Init", f"New connection link for {wallet_app_name}: {connection_link}", level='debug') make_log("TonConnect_Init", f"New connection link for {wallet_app_name}: {connection_link}", level='debug')
message_text = user.translated("tonconnectInit_menu") message_text = user.translated("tonconnectInit_menu")
return await tg_process_template( return await tg_process_template(