Initial commit
This commit is contained in:
commit
9638f5a31b
45 files changed
+2421
No files matched your search
@@ -0,0 +1,70 @@
|
||||
import traceback
|
||||
from asyncio import sleep
|
||||
from datetime import datetime
|
||||
|
||||
from aiogram import Bot
|
||||
|
||||
from app.api import app
|
||||
from app.bot import dp
|
||||
from app.core._config import SANIC_PORT, MYSQL_URI, TELEGRAM_API_KEY
|
||||
from app.core._utils.create_maria_tables import create_maria_tables
|
||||
from app.core.logger import make_log
|
||||
from app.core.models import Memory
|
||||
from app.core.storage import engine
|
||||
|
||||
|
||||
async def queue_daemon(app):
|
||||
await sleep(3)
|
||||
|
||||
while True:
|
||||
delayed_list = {k: v for k, v in app.ctx.memory._delayed_queue.items()}
|
||||
for _execute_ts in delayed_list:
|
||||
if _execute_ts <= datetime.now().timestamp():
|
||||
del app.ctx.memory._delayed_queue[_execute_ts]
|
||||
app.ctx.memory._execute_queue.append(delayed_list[_execute_ts])
|
||||
|
||||
await sleep(.7)
|
||||
|
||||
|
||||
async def execute_queue(app):
|
||||
await create_maria_tables(engine)
|
||||
|
||||
telegram_bot_username = (await app.ctx.memory._telegram_bot.get_me()).username
|
||||
make_log(None, f"Application normally started. HTTP port: {SANIC_PORT}")
|
||||
make_log(None, f"Telegram bot: https://t.me/{telegram_bot_username}")
|
||||
make_log(None, f"MariaDB host: {MYSQL_URI.split('@')[1].split('/')[0].replace('/', '')}")
|
||||
while True:
|
||||
try:
|
||||
_cmd = app.ctx.memory._execute_queue.pop(0)
|
||||
except IndexError:
|
||||
await sleep(.05)
|
||||
continue
|
||||
|
||||
_fn = _cmd.pop(0)
|
||||
assert _fn
|
||||
_args = _cmd.pop(0)
|
||||
assert type(_args) is tuple
|
||||
try:
|
||||
_kwargs = _cmd.pop(0)
|
||||
assert type(_kwargs) is dict
|
||||
except IndexError:
|
||||
_kwargs = {}
|
||||
|
||||
try:
|
||||
make_log("Queue.execute", f"{_fn} {_args} {_kwargs}", level='debug')
|
||||
await _fn(*_args, **_kwargs)
|
||||
except BaseException as e:
|
||||
make_log("Queue.execute", f"{_fn} {_args} {_kwargs} => Error: {e}" + '\n' + str(traceback.format_exc()))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.ctx.memory = Memory()
|
||||
app.ctx.memory._telegram_bot = Bot(TELEGRAM_API_KEY)
|
||||
dp._s_memory = app.ctx.memory
|
||||
app.ctx.memory._app = app
|
||||
|
||||
app.add_task(execute_queue(app))
|
||||
app.add_task(queue_daemon(app))
|
||||
app.add_task(dp.start_polling(app.ctx.memory._telegram_bot))
|
||||
|
||||
app.run(host='0.0.0.0', port=SANIC_PORT)
|
||||
@@ -0,0 +1,9 @@
|
||||
from sanic import Sanic
|
||||
|
||||
app = Sanic(__name__)
|
||||
|
||||
from app.api.routes._index import s_index
|
||||
from app.api.routes.tonconnect import s_api_tonconnect_manifest
|
||||
|
||||
app.add_route(s_index, "/")
|
||||
app.add_route(s_api_tonconnect_manifest, "/api/tonconnect-manifest.json")
|
||||
@@ -0,0 +1,7 @@
|
||||
from sanic import response
|
||||
|
||||
|
||||
async def s_index(request):
|
||||
return response.text("OK")
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
from sanic import response
|
||||
|
||||
from app.core._config import PROJECT_HOST
|
||||
|
||||
|
||||
async def s_api_tonconnect_manifest(request):
|
||||
return response.json({
|
||||
"url": f"{PROJECT_HOST}/#from=tonconnect",
|
||||
"name": f"{PROJECT_HOST}", # TODO: maybe edit
|
||||
"iconUrl": "https://github.com/projscale/assets/blob/main/ton-connect.png?raw=true",
|
||||
})
|
||||
@@ -0,0 +1,78 @@
|
||||
from datetime import datetime
|
||||
|
||||
from aiogram import BaseMiddleware, Dispatcher
|
||||
from aiogram.fsm.storage.memory import MemoryStorage
|
||||
|
||||
from app.bot.routers.index import main_router
|
||||
from app.core.logger import logger
|
||||
from app.core.models._telegram import Wrapped_CBotChat
|
||||
from app.core.models.user import User
|
||||
from app.core.storage import db_session
|
||||
|
||||
dp = Dispatcher(storage=MemoryStorage())
|
||||
|
||||
|
||||
class UserDataMiddleware(BaseMiddleware):
|
||||
async def __call__(self, handler, event, data):
|
||||
update_body = event.message or event.callback_query
|
||||
if not update_body:
|
||||
return
|
||||
|
||||
if update_body.from_user.is_bot is True:
|
||||
return
|
||||
|
||||
user_id = update_body.from_user.id
|
||||
assert user_id >= 1
|
||||
# TODO: maybe make users cache
|
||||
|
||||
with db_session(auto_commit=False) as session:
|
||||
try:
|
||||
user = session.query(User).filter_by(telegram_id=user_id).first()
|
||||
except BaseException as e:
|
||||
logger.error(f"Error when middleware getting user: {e}")
|
||||
user = None
|
||||
|
||||
if user is None:
|
||||
logger.debug(f"User {user_id} not found. Creating new user")
|
||||
user = User(
|
||||
telegram_id=user_id,
|
||||
username=update_body.from_user.username,
|
||||
lang_code='en',
|
||||
last_use=datetime.now(),
|
||||
meta=dict(first_name=update_body.from_user.first_name,
|
||||
last_name=update_body.from_user.last_name, username=update_body.from_user.username,
|
||||
language_code=update_body.from_user.language_code,
|
||||
is_premium=update_body.from_user.is_premium),
|
||||
created=datetime.now()
|
||||
)
|
||||
session.add(user)
|
||||
session.commit()
|
||||
else:
|
||||
if user.username != update_body.from_user.username:
|
||||
user.username = update_body.from_user.username
|
||||
|
||||
updated_meta_fields = {}
|
||||
if user.meta.get('first_name') != update_body.from_user.first_name:
|
||||
updated_meta_fields['first_name'] = update_body.from_user.first_name
|
||||
|
||||
if user.meta.get('last_name') != update_body.from_user.last_name:
|
||||
updated_meta_fields['last_name'] = update_body.from_user.last_name
|
||||
|
||||
user.meta = {
|
||||
**user.meta,
|
||||
**updated_meta_fields
|
||||
}
|
||||
|
||||
user.last_use = datetime.now()
|
||||
session.commit()
|
||||
|
||||
data['user'] = user
|
||||
data['db_session'] = session
|
||||
data['chat_wrap'] = Wrapped_CBotChat(data['bot'], chat_id=user_id)
|
||||
data['memory'] = dp._s_memory
|
||||
result = await handler(event, data)
|
||||
return result
|
||||
|
||||
|
||||
dp.update.outer_middleware(UserDataMiddleware())
|
||||
dp.include_router(main_router)
|
||||
@@ -0,0 +1,54 @@
|
||||
import os
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
from aiogram import types, Router, F
|
||||
from aiogram.filters import Command
|
||||
|
||||
from app.bot.routers.tonconnect import router as tonconnect_router
|
||||
from app.core._utils.tg_process_template import tg_process_template
|
||||
from app.core.logger import logger
|
||||
|
||||
main_router = Router()
|
||||
|
||||
|
||||
async def t_home_menu(__msg, **extra):
|
||||
memory, user, db_session, chat_wrap = extra['memory'], extra['user'], extra['db_session'], extra['chat_wrap']
|
||||
if extra.get('state'):
|
||||
await extra['state'].clear()
|
||||
|
||||
return await tg_process_template(
|
||||
chat_wrap, user.translated('home_menu'), message_id=__msg.message.message_id if isinstance(__msg, types.CallbackQuery) else None
|
||||
)
|
||||
|
||||
|
||||
main_router.message.register(t_home_menu, Command('start'))
|
||||
main_router.callback_query.register(t_home_menu, F.data == 'home')
|
||||
|
||||
main_router.include_routers(tonconnect_router)
|
||||
|
||||
closing_router = Router()
|
||||
|
||||
|
||||
@closing_router.message()
|
||||
async def t_index(message: types.Message, **extra):
|
||||
return await message.answer(extra['user'].translated('error_unknownCommand'), parse_mode='html')
|
||||
|
||||
main_router.include_routers(closing_router)
|
||||
|
||||
|
||||
@main_router.error()
|
||||
async def t_index_error(err_event: types.ErrorEvent, **extra):
|
||||
try:
|
||||
raise err_event.exception
|
||||
except BaseException as e:
|
||||
exc_type, exc_obj, exc_tb = sys.exc_info()
|
||||
|
||||
try:
|
||||
filename = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
|
||||
except:
|
||||
filename = None
|
||||
|
||||
logger.error(f"""Error: {e}
|
||||
-/ {exc_type} {exc_obj} {filename} {exc_tb.tb_frame.f_lineno}""")
|
||||
traceback.print_tb(exc_tb)
|
||||
@@ -0,0 +1,66 @@
|
||||
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'))
|
||||
Whitespace-only changes.
@@ -0,0 +1,23 @@
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv(dotenv_path='.env')
|
||||
|
||||
PROJECT_HOST = os.getenv('PROJECT_HOST', 'http://127.0.0.1:8080')
|
||||
SANIC_PORT = int(os.getenv('SANIC_PORT', '8080'))
|
||||
|
||||
TELEGRAM_API_KEY = os.environ.get('TELEGRAM_API_KEY')
|
||||
assert TELEGRAM_API_KEY, "Telegram API_KEY required"
|
||||
|
||||
MYSQL_URI = os.environ['MYSQL_URI']
|
||||
MYSQL_DATABASE = os.environ['MYSQL_DATABASE']
|
||||
|
||||
LOG_LEVEL = os.getenv('LOG_LEVEL', 'DEBUG')
|
||||
LOG_DIR = os.getenv('LOG_DIR', 'logs')
|
||||
if not os.path.exists(LOG_DIR):
|
||||
os.mkdir(LOG_DIR)
|
||||
|
||||
_now_str = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||
LOG_FILEPATH = f"{LOG_DIR}/{_now_str}.log"
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
DEFAULT_ASSET_INITOBJ = {
|
||||
'symbol': 'USD',
|
||||
'name': 'US Dollar',
|
||||
'decimals': 6,
|
||||
'network': None,
|
||||
'address': None,
|
||||
'meta': {},
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
from aiogram import types
|
||||
|
||||
|
||||
def get_keyboard(rows, one_time_keyboard=True, **kwargs) -> types.KeyboardButton:
|
||||
keyboard = types.ReplyKeyboardMarkup(
|
||||
resize_keyboard=True, one_time_keyboard=one_time_keyboard, **kwargs
|
||||
)
|
||||
for row in rows:
|
||||
keyboard.add(
|
||||
*[
|
||||
types.KeyboardButton(button)
|
||||
for button in row
|
||||
]
|
||||
)
|
||||
return keyboard
|
||||
|
||||
|
||||
def get_inline_keyboard(rows, *args, **kwargs):
|
||||
if not rows: return None
|
||||
|
||||
keyboard = []
|
||||
for row in rows:
|
||||
if not row:
|
||||
continue
|
||||
|
||||
result_row = []
|
||||
for _item in row:
|
||||
if not _item:
|
||||
continue
|
||||
|
||||
if isinstance(_item, dict):
|
||||
result_row.append(
|
||||
types.InlineKeyboardButton(
|
||||
**{
|
||||
_key: _value for _key, _value in _item.items()
|
||||
if _value
|
||||
}
|
||||
)
|
||||
)
|
||||
else:
|
||||
result_row.append(_item)
|
||||
|
||||
keyboard.append(result_row)
|
||||
|
||||
return types.InlineKeyboardMarkup(inline_keyboard=keyboard)
|
||||
|
||||
|
||||
def get_inline_query(items: list):
|
||||
inline_items = []
|
||||
for item in items:
|
||||
inline_items.append(
|
||||
types.InlineQueryResultArticle(
|
||||
id=str(item.get('id', '0')),
|
||||
title=item.get('title', ''),
|
||||
input_message_content=types.InputTextMessageContent(
|
||||
item.get('input_message_content', ''),
|
||||
parse_mode='html',
|
||||
disable_web_page_preview=True
|
||||
),
|
||||
reply_markup=get_inline_keyboard(item.get('reply_markup')),
|
||||
url=item.get('url'),
|
||||
hide_url=item.get('hide_url'),
|
||||
description=item.get('description'),
|
||||
thumb_url=item.get('thumb_url'),
|
||||
)
|
||||
)
|
||||
|
||||
return inline_items
|
||||
@@ -0,0 +1 @@
|
||||
from app.core._utils.tg_process_template import tg_process_template
|
||||
@@ -0,0 +1,10 @@
|
||||
from app.core.models import Asset
|
||||
from app.core.models.base import AlchemyBase
|
||||
|
||||
|
||||
async def create_maria_tables(engine):
|
||||
"""Create all tables in the database."""
|
||||
Asset()
|
||||
AlchemyBase.metadata.create_all(engine)
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
async def tg_process_template(
|
||||
chat_wrap: 'Wrapped_CBot',
|
||||
text, keyboard=None, message_id=None,
|
||||
photo=None, video=None, document=None, **kwargs
|
||||
):
|
||||
if (photo or video or document) and message_id:
|
||||
await chat_wrap.delete_message(message_id)
|
||||
message_id = None
|
||||
|
||||
if message_id:
|
||||
m = await chat_wrap.edit_message(
|
||||
message_id,
|
||||
text,
|
||||
reply_markup=keyboard,
|
||||
**kwargs
|
||||
)
|
||||
if not (m is None):
|
||||
return m
|
||||
|
||||
await chat_wrap.delete_message(message_id)
|
||||
|
||||
if photo:
|
||||
return await chat_wrap.send_photo(
|
||||
photo,
|
||||
caption=text,
|
||||
reply_markup=keyboard,
|
||||
**kwargs
|
||||
)
|
||||
elif video:
|
||||
return await chat_wrap.send_video(
|
||||
video,
|
||||
caption=text,
|
||||
reply_markup=keyboard,
|
||||
**kwargs
|
||||
)
|
||||
elif document:
|
||||
return await chat_wrap.send_document(
|
||||
document,
|
||||
caption=text,
|
||||
reply_markup=keyboard,
|
||||
**kwargs
|
||||
)
|
||||
else:
|
||||
return await chat_wrap.send_message(
|
||||
text,
|
||||
reply_markup=keyboard,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from app.core._config import LOG_LEVEL, LOG_FILEPATH
|
||||
|
||||
LOG_LEVELS = {
|
||||
'DEBUG': logging.DEBUG,
|
||||
'INFO': logging.INFO,
|
||||
'WARNING': logging.WARNING,
|
||||
'ERROR': logging.ERROR
|
||||
}
|
||||
LOG_LEVEL = LOG_LEVELS[LOG_LEVEL]
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
handler2 = logging.StreamHandler(sys.stdout)
|
||||
handler2.setLevel(LOG_LEVEL)
|
||||
handler2.setFormatter(
|
||||
logging.Formatter('%(asctime)s | %(levelname)s | %(message)s')
|
||||
)
|
||||
logger.addHandler(handler2)
|
||||
|
||||
handler3 = logging.FileHandler(LOG_FILEPATH)
|
||||
handler3.setLevel(logging.DEBUG)
|
||||
handler3.setFormatter(
|
||||
logging.Formatter('%(asctime)s | %(levelname)s | %(message)s')
|
||||
)
|
||||
logger.addHandler(handler3)
|
||||
|
||||
|
||||
def make_log(issuer, message, *args, level='INFO', **kwargs):
|
||||
assert level.upper() in LOG_LEVELS.keys(), f"Unknown log level"
|
||||
_log = getattr(logger, level.lower())
|
||||
log_buffer = f"[{issuer if not (issuer is None) else 'System'}] {message}"
|
||||
if args:
|
||||
log_buffer += f" | {args}"
|
||||
if kwargs:
|
||||
log_buffer += f" | {kwargs}"
|
||||
_log(log_buffer)
|
||||
@@ -0,0 +1,6 @@
|
||||
from app.core.models.asset import Asset
|
||||
from app.core.models.memory import Memory
|
||||
from app.core.models.transaction import UserBalance, InternalTransaction
|
||||
from app.core.models.user import User
|
||||
from app.core.models.wallet_connection import WalletConnection
|
||||
from app.core.models.base import AlchemyBase
|
||||
Whitespace-only changes.
Whitespace-only changes.
@@ -0,0 +1,152 @@
|
||||
import os
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import datetime
|
||||
from hashlib import sha256
|
||||
|
||||
from pytonconnect import TonConnect as ExternalLib_TonConnect
|
||||
from pytonconnect.storage import DefaultStorage
|
||||
|
||||
from app.core._config import PROJECT_HOST
|
||||
from app.core.logger import make_log
|
||||
from app.core.models.wallet_connection import WalletConnection
|
||||
|
||||
TON_CONNECT_MANIFEST_URI = os.getenv("TON_CONNECT_MANIFEST_URI")
|
||||
|
||||
|
||||
def unpack_wallet_info(wallet_info) -> dict:
|
||||
return {
|
||||
'provider': wallet_info.provider,
|
||||
'device': {
|
||||
'platform': wallet_info.device.platform,
|
||||
'app_name': wallet_info.device.app_name,
|
||||
'app_version': wallet_info.device.app_version,
|
||||
'max_protocol_version': wallet_info.device.max_protocol_version,
|
||||
'features': wallet_info.device.features
|
||||
} if wallet_info.device else None,
|
||||
'account': {
|
||||
'address': wallet_info.account.address,
|
||||
'chain': wallet_info.account.chain,
|
||||
'wallet_state_init': wallet_info.account.wallet_state_init,
|
||||
'public_key': wallet_info.account.public_key,
|
||||
} if wallet_info.account else None,
|
||||
'ton_proof': {
|
||||
'timestamp': wallet_info.ton_proof.timestamp,
|
||||
'domain_len': wallet_info.ton_proof.domain_len,
|
||||
'domain_val': wallet_info.ton_proof.domain_val,
|
||||
'payload': wallet_info.ton_proof.payload,
|
||||
'signature': wallet_info.ton_proof.signature,
|
||||
} if wallet_info.ton_proof else None
|
||||
}
|
||||
|
||||
|
||||
class TonConnect:
|
||||
def __init__(self, callback_fn: tuple = None):
|
||||
self._manifest_uri = TON_CONNECT_MANIFEST_URI
|
||||
if not self._manifest_uri:
|
||||
self._manifest_uri = f"{PROJECT_HOST}/api/tonconnect-manifest.json"
|
||||
|
||||
self._sdk_client = ExternalLib_TonConnect(
|
||||
manifest_url=self._manifest_uri, storage=DefaultStorage()
|
||||
)
|
||||
|
||||
def status_change_callback(status):
|
||||
status = unpack_wallet_info(status)
|
||||
make_log("TonConnect", f"Changed status (connected={self.connected}): {status}", level='debug')
|
||||
|
||||
if callback_fn:
|
||||
callback_fn[0](self, status, *callback_fn[1:])
|
||||
|
||||
self.set_status_change_callback(status_change_callback)
|
||||
|
||||
async def new_connection(self, app_name: str):
|
||||
wallets = self._sdk_client.get_wallets()
|
||||
for wallet in wallets:
|
||||
if wallet["app_name"] == app_name:
|
||||
return await self._sdk_client.connect(wallet)
|
||||
|
||||
async def restore_connection(self):
|
||||
return await self._sdk_client.restore_connection()
|
||||
|
||||
def pause_connection(self):
|
||||
try:
|
||||
self._sdk_client.pause_connection()
|
||||
except BaseException as e:
|
||||
make_log("pause_connection", e, level='error')
|
||||
|
||||
@asynccontextmanager
|
||||
async def connection(self):
|
||||
try:
|
||||
yield None
|
||||
finally:
|
||||
self.pause_connection()
|
||||
|
||||
@property
|
||||
def connected(self):
|
||||
# make_log("is_connected", self._sdk_client._storage._cache)
|
||||
return self._sdk_client.connected
|
||||
|
||||
@property
|
||||
def connection_key(self):
|
||||
return self._sdk_client._storage._cache.get(DefaultStorage.KEY_CONNECTION, None)
|
||||
# return self._sdk_client._storage.get_item(DefaultStorage.KEY_CONNECTION, None)
|
||||
|
||||
def set_status_change_callback(self, callback):
|
||||
self._sdk_client.on_status_change(callback)
|
||||
|
||||
@classmethod
|
||||
def by_key(cls, connection_key: str, callback_fn: tuple = None):
|
||||
ton_connect = cls(callback_fn=callback_fn)
|
||||
ton_connect._sdk_client._storage._cache[DefaultStorage.KEY_CONNECTION] = connection_key
|
||||
# ton_connect._sdk_client._storage.set_item(DefaultStorage.KEY_CONNECTION, connection_key)
|
||||
# Immediately restore connection
|
||||
return ton_connect
|
||||
|
||||
@classmethod
|
||||
def by_user(cls, session, user, callback_fn: tuple = None):
|
||||
def new_callback_fn(self, status, _callback_fn):
|
||||
try:
|
||||
if not session.query(
|
||||
WalletConnection
|
||||
).filter(WalletConnection.connection_id == sha256(self.connection_key.encode()).hexdigest()) \
|
||||
.count():
|
||||
new_connection = WalletConnection(
|
||||
user_id=user.id,
|
||||
network='ton',
|
||||
wallet_key=f"{status['device'].get('app_name', 'UNKNOWN_NAME')}=={status['device'].get('app_version', '1.0')}",
|
||||
connection_id=sha256(self.connection_key.encode()).hexdigest(),
|
||||
wallet_address=status['account']['address'],
|
||||
keys={
|
||||
'connection_key': self.connection_key,
|
||||
},
|
||||
meta={
|
||||
key: status[key] for key in status if status[key]
|
||||
},
|
||||
created=datetime.now(),
|
||||
updated=datetime.now(),
|
||||
invalidated=False
|
||||
)
|
||||
session.add(new_connection)
|
||||
session.commit()
|
||||
except BaseException as e:
|
||||
make_log("TonConnect.save_connection", e, level='error')
|
||||
|
||||
if _callback_fn:
|
||||
_callback_fn[0](self, status, *_callback_fn[1:])
|
||||
|
||||
ton_connect = cls(callback_fn=(new_callback_fn, callback_fn))
|
||||
connections = (
|
||||
session.query(WalletConnection).filter(
|
||||
WalletConnection.user_id == user.id,
|
||||
WalletConnection.invalidated == False,
|
||||
WalletConnection.network == 'ton'
|
||||
)
|
||||
)
|
||||
if connections.count() == 0:
|
||||
return ton_connect, None
|
||||
|
||||
connection = connections.first()
|
||||
ton_connect._sdk_client._storage._cache[DefaultStorage.KEY_CONNECTION] = connection.keys["connection_key"]
|
||||
# ton_connect._sdk_client._storage.set_item(DefaultStorage.KEY_CONNECTION, bytes.fromhex(connection.keys["connection_key"]))
|
||||
# Immediately restore connection
|
||||
return ton_connect, connection
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
from .wrapped_bot import Wrapped_CBotChat
|
||||
@@ -0,0 +1,129 @@
|
||||
from aiogram import Bot
|
||||
|
||||
from app.core.logger import make_log
|
||||
|
||||
|
||||
class Wrapped_CBotChat:
|
||||
def __init__(self, api_key: str, chat_id: int = None, **kwargs):
|
||||
if isinstance(api_key, Bot):
|
||||
self._bot_key = api_key.token
|
||||
self._bot = api_key
|
||||
elif type(api_key) is str:
|
||||
self._bot_key = api_key
|
||||
self._bot = Bot(token=api_key)
|
||||
else:
|
||||
raise TypeError(f'api_key must be Bot or str, not {type(api_key)}')
|
||||
|
||||
self._chat_id = chat_id
|
||||
self.options = kwargs
|
||||
|
||||
def __repr__(self):
|
||||
if self.options.get('cbot_id'):
|
||||
return f'Bot, chat_id={self.options["cbot_id"]} ' + '\\'
|
||||
|
||||
return "Bot"
|
||||
|
||||
async def send_message(self, text: str, **kwargs):
|
||||
try:
|
||||
make_log(self, f"Send message to {self._chat_id}. Text len: {len(text)}", level='debug')
|
||||
return await self._bot.send_message(
|
||||
self._chat_id,
|
||||
text,
|
||||
parse_mode='html',
|
||||
disable_web_page_preview=True,
|
||||
**kwargs
|
||||
)
|
||||
except BaseException as e:
|
||||
make_log(self, f"Error sending message to {self._chat_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
|
||||
async def edit_message(self, message_id, text, **kwargs):
|
||||
try:
|
||||
make_log(self, f"Edit message {self._chat_id}/{message_id}. Text len: {len(text)}", level='debug')
|
||||
return await self._bot.edit_message_text(
|
||||
text,
|
||||
chat_id=self._chat_id,
|
||||
message_id=message_id,
|
||||
parse_mode='html',
|
||||
disable_web_page_preview=True,
|
||||
**kwargs
|
||||
)
|
||||
except Exception as e:
|
||||
make_log(self, f"Error editing message {self._chat_id}/{message_id}. Error: {e}", level='warning')
|
||||
if 'exactly the same as a current content' in f'{e}':
|
||||
return True
|
||||
|
||||
return None
|
||||
|
||||
async def delete_message(self, message_id):
|
||||
try:
|
||||
make_log(self, f"Delete message {self._chat_id}/{message_id}", level='debug')
|
||||
return await self._bot.delete_message(
|
||||
self._chat_id,
|
||||
message_id
|
||||
)
|
||||
except Exception as e:
|
||||
make_log(self, f"Error deleting message {self._chat_id}/{message_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
|
||||
async def send_photo(self, file_id, **kwargs):
|
||||
try:
|
||||
make_log(self, f"Send photo to {self._chat_id}. File: {file_id}", level='debug')
|
||||
return await self._bot.send_photo(
|
||||
self._chat_id,
|
||||
file_id,
|
||||
**kwargs
|
||||
)
|
||||
except Exception as e:
|
||||
make_log(self, f"Error sending photo to {self._chat_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
|
||||
async def send_document(self, file_id, **kwargs):
|
||||
try:
|
||||
make_log(self, f"Send document to {self._chat_id}. File: {file_id}", level='debug')
|
||||
return await self._bot.send_document(
|
||||
self._chat_id,
|
||||
file_id,
|
||||
**kwargs
|
||||
)
|
||||
except Exception as e:
|
||||
make_log(self, f"Error sending document to {self._chat_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
|
||||
async def send_video(self, file_id, **kwargs):
|
||||
try:
|
||||
make_log(self, f"Send video to {self._chat_id}. File: {file_id}", level='debug')
|
||||
return await self._bot.send_video(
|
||||
self._chat_id,
|
||||
file_id,
|
||||
**kwargs
|
||||
)
|
||||
except Exception as e:
|
||||
make_log(self, f"Error sending video to {self._chat_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
|
||||
async def copy_message(self, from_chat_id, message_id, **kwargs):
|
||||
try:
|
||||
make_log(self, f"Copy message from {from_chat_id}/{message_id} to {self._chat_id}", level='debug')
|
||||
return await self._bot.copy_message(
|
||||
self._chat_id,
|
||||
from_chat_id,
|
||||
message_id,
|
||||
**kwargs
|
||||
)
|
||||
except Exception as e:
|
||||
make_log(self, f"Error copying message from {from_chat_id}/{message_id} to {self._chat_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
|
||||
async def forward_message(self, from_chat_id, message_id, **kwargs):
|
||||
try:
|
||||
make_log(self, f"Forward message from {from_chat_id}/{message_id} to {self._chat_id}", level='debug')
|
||||
return await self._bot.forward_message(
|
||||
self._chat_id,
|
||||
from_chat_id,
|
||||
message_id,
|
||||
**kwargs
|
||||
)
|
||||
except Exception as e:
|
||||
make_log(self, f"Error forwarding message from {from_chat_id}/{message_id} to {self._chat_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
@@ -0,0 +1,50 @@
|
||||
from sqlalchemy import Column, Integer, String, DateTime, JSON, Boolean
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from app.core._defaults import DEFAULT_ASSET_INITOBJ
|
||||
from app.core.models.base import AlchemyBase
|
||||
|
||||
|
||||
class Asset(AlchemyBase):
|
||||
__tablename__ = 'assets'
|
||||
|
||||
id = Column(Integer, autoincrement=True, primary_key=True)
|
||||
symbol = Column(String(32), nullable=False)
|
||||
name = Column(String(256), nullable=False)
|
||||
decimals = Column(Integer, nullable=False)
|
||||
|
||||
network = Column(String(32), nullable=True)
|
||||
address = Column(String(1024), nullable=True)
|
||||
meta = Column(JSON, nullable=False, default={})
|
||||
rates = Column(JSON, nullable=False, default={})
|
||||
|
||||
created = Column(DateTime, nullable=False, default=0)
|
||||
is_active = Column(Boolean, nullable=False, default=True)
|
||||
|
||||
balances = relationship('UserBalance', back_populates='asset')
|
||||
internal_transactions = relationship('InternalTransaction', back_populates='asset')
|
||||
|
||||
@classmethod
|
||||
def init_table(cls, engine):
|
||||
AlchemyBase.metadata.create_all(engine)
|
||||
|
||||
@classmethod
|
||||
def find(cls, session, **kwargs):
|
||||
if 'symbol' in kwargs:
|
||||
kwargs['symbol'] = kwargs['symbol'].upper()
|
||||
|
||||
result = session.query(cls).filter_by(**kwargs)
|
||||
results_count = result.count()
|
||||
if results_count == 0:
|
||||
any_count = session.query(cls).count()
|
||||
if any_count == 0:
|
||||
init_asset = cls(**DEFAULT_ASSET_INITOBJ)
|
||||
session.add(init_asset)
|
||||
session.commit()
|
||||
return cls.find(session, **kwargs)
|
||||
|
||||
raise Exception(f"Asset not found: {kwargs}")
|
||||
elif results_count == 1:
|
||||
return result.first()
|
||||
else:
|
||||
raise Exception(f"Multiple assets found: {results_count}")
|
||||
@@ -0,0 +1,3 @@
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
AlchemyBase = declarative_base()
|
||||
@@ -0,0 +1,50 @@
|
||||
import asyncio
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from app.core.logger import make_log
|
||||
|
||||
|
||||
class Memory:
|
||||
def __init__(self):
|
||||
self._execute_queue = []
|
||||
self._delayed_queue = {}
|
||||
|
||||
self.transactions_disabled = False
|
||||
|
||||
@asynccontextmanager
|
||||
async def transaction(self, desc=""):
|
||||
make_log("Memory.transaction", f"Starting transaction; {desc}", level='debug')
|
||||
while self.transactions_disabled:
|
||||
await asyncio.sleep(.2)
|
||||
|
||||
self.transactions_disabled = True
|
||||
try:
|
||||
yield None
|
||||
except BaseException as e:
|
||||
self.transactions_disabled = False
|
||||
make_log("Memory.transaction", f"Transaction error: {e}", level='error')
|
||||
raise e
|
||||
|
||||
self.transactions_disabled = False
|
||||
make_log("Memory.transaction", f"Transaction finished; {desc}", level='debug')
|
||||
|
||||
def add_task(self, _fn, *args, **kwargs):
|
||||
try:
|
||||
if type(kwargs.get("delay_s")) in [int, float]:
|
||||
kwargs['execute_ts'] = (datetime.now() + timedelta(seconds=kwargs.pop('delay_s'))).timestamp()
|
||||
|
||||
_execute_ts = kwargs.pop('execute_ts')
|
||||
_execute_ts = int(_execute_ts)
|
||||
assert _execute_ts > 0
|
||||
while _execute_ts in self._delayed_queue:
|
||||
_execute_ts += .01
|
||||
|
||||
self._delayed_queue[_execute_ts] = [_fn, args, kwargs]
|
||||
except (KeyError, ValueError, AssertionError) as e:
|
||||
if not ("execute_ts" in str(e)):
|
||||
make_log("Queue.add_task", f"Error when adding task to memory: {e}", level='error')
|
||||
|
||||
self._execute_queue.append([_fn, args, kwargs])
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Boolean, Float
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from .base import AlchemyBase
|
||||
|
||||
|
||||
class UserBalance(AlchemyBase):
|
||||
__tablename__ = 'user_balances'
|
||||
|
||||
id = Column(Integer, autoincrement=True, primary_key=True)
|
||||
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
|
||||
asset_id = Column(Integer, ForeignKey('assets.id'), nullable=False)
|
||||
balance = Column(Float, nullable=False, default=0)
|
||||
|
||||
updated = Column(DateTime, nullable=False, default=0)
|
||||
created = Column(DateTime, nullable=False, default=0)
|
||||
|
||||
user = relationship('User', uselist=False, foreign_keys=[user_id], back_populates='balances')
|
||||
asset = relationship('Asset', uselist=False, foreign_keys=[asset_id], back_populates='balances')
|
||||
|
||||
|
||||
class InternalTransaction(AlchemyBase):
|
||||
__tablename__ = 'internal_transactions'
|
||||
|
||||
id = Column(Integer, autoincrement=True, primary_key=True)
|
||||
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
|
||||
asset_id = Column(Integer, ForeignKey('assets.id'), nullable=False)
|
||||
amount = Column(Float, nullable=False)
|
||||
|
||||
is_spent = Column(Boolean, nullable=False) # transaction type
|
||||
spent_transaction_id = Column(Integer, ForeignKey('internal_transactions.id'), nullable=True)
|
||||
type = Column(String(256), nullable=False, default="NOT_SPECIFIED")
|
||||
|
||||
created = Column(DateTime, nullable=False, default=0)
|
||||
|
||||
user = relationship('User', uselist=False, back_populates='internal_transactions', foreign_keys=[user_id])
|
||||
asset = relationship('Asset', uselist=False, foreign_keys=[asset_id])
|
||||
spent_transaction = relationship('InternalTransaction', uselist=False, foreign_keys=[spent_transaction_id])
|
||||
@@ -0,0 +1,29 @@
|
||||
from sqlalchemy import Column, Integer, String, BigInteger, DateTime, JSON
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from app.core.translation import TranslationCore
|
||||
from ..base import AlchemyBase
|
||||
|
||||
|
||||
class User(AlchemyBase, TranslationCore):
|
||||
LOCALE_DOMAIN = 'sanic_telegram_bot'
|
||||
|
||||
__tablename__ = 'users'
|
||||
id = Column(Integer, autoincrement=True, primary_key=True)
|
||||
telegram_id = Column(BigInteger, nullable=False)
|
||||
|
||||
username = Column(String(512), nullable=True)
|
||||
lang_code = Column(String(8), nullable=False, default="en")
|
||||
meta = Column(JSON, nullable=False, default={})
|
||||
|
||||
last_use = Column(DateTime, nullable=False, default=0)
|
||||
created = Column(DateTime, nullable=False, default=0)
|
||||
|
||||
balances = relationship('UserBalance', back_populates='user')
|
||||
internal_transactions = relationship('InternalTransaction', back_populates='user')
|
||||
wallet_connections = relationship('WalletConnection', back_populates='user')
|
||||
|
||||
def __str__(self):
|
||||
return f"User, {self.id}_{self.telegram_id} | Username: {self.username} " + '\\'
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, JSON, Boolean
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from .base import AlchemyBase
|
||||
|
||||
|
||||
class WalletConnection(AlchemyBase):
|
||||
__tablename__ = 'wallet_connections'
|
||||
|
||||
id = Column(Integer, autoincrement=True, primary_key=True)
|
||||
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
|
||||
network = Column(String(32), nullable=False)
|
||||
wallet_key = Column(String(256), nullable=False)
|
||||
connection_id = Column(String(2048), nullable=False, unique=True)
|
||||
|
||||
wallet_address = Column(String(1024), nullable=False)
|
||||
|
||||
keys = Column(JSON, nullable=False, default={})
|
||||
meta = Column(JSON, nullable=False, default={})
|
||||
|
||||
created = Column(DateTime, nullable=False, default=0)
|
||||
updated = Column(DateTime, nullable=False, default=0)
|
||||
invalidated = Column(Boolean, nullable=False, default=True)
|
||||
without_pk = Column(Boolean, nullable=False, default=False)
|
||||
|
||||
user = relationship('User', uselist=False, back_populates='wallet_connections', foreign_keys=[user_id])
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import time
|
||||
from contextlib import contextmanager
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.sql import text
|
||||
|
||||
from app.core._config import MYSQL_URI, MYSQL_DATABASE
|
||||
from app.core.logger import make_log
|
||||
|
||||
engine = create_engine(MYSQL_URI, echo=True)
|
||||
Session = sessionmaker(bind=engine)
|
||||
|
||||
|
||||
database_initialized = False
|
||||
while not database_initialized:
|
||||
try:
|
||||
with Session() as session:
|
||||
databases_list = session.execute(text("SHOW DATABASES;"))
|
||||
databases_list = [row[0] for row in databases_list]
|
||||
make_log("SQL", 'Database list: ' + str(databases_list), level='debug')
|
||||
assert MYSQL_DATABASE in databases_list, 'Database not found'
|
||||
database_initialized = True
|
||||
except Exception as e:
|
||||
make_log("SQL", 'MariaDB is not ready yet: ' + str(e), level='debug')
|
||||
time.sleep(1)
|
||||
|
||||
engine = create_engine(f"{MYSQL_URI}/{MYSQL_DATABASE}")
|
||||
Session = sessionmaker(bind=engine)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def db_session(auto_commit=False):
|
||||
_session = Session()
|
||||
try:
|
||||
yield _session
|
||||
if auto_commit is True:
|
||||
_session.commit()
|
||||
except BaseException as e:
|
||||
_session.rollback()
|
||||
raise e
|
||||
finally:
|
||||
_session.close()
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
from datetime import datetime
|
||||
|
||||
from app.core.logger import make_log
|
||||
from app.core.models import Memory, User, UserBalance, Asset, InternalTransaction
|
||||
from app.core.storage import db_session
|
||||
|
||||
|
||||
def get_user_balance(session, user: User, asset: Asset) -> UserBalance:
|
||||
assert user, "No user"
|
||||
assert asset, "No asset"
|
||||
result = session.query(UserBalance).filter(
|
||||
UserBalance.user_id == user.id,
|
||||
UserBalance.asset_id == asset.id
|
||||
)
|
||||
results_count = result.count()
|
||||
if results_count == 0:
|
||||
user_balance = UserBalance(
|
||||
user_id=user.id,
|
||||
asset_id=asset.id,
|
||||
balance=0,
|
||||
created=datetime.now(),
|
||||
)
|
||||
session.add(user_balance)
|
||||
session.commit()
|
||||
return get_user_balance(session, user, asset)
|
||||
elif results_count == 1:
|
||||
return result.first()
|
||||
else:
|
||||
raise Exception(f"Multiple user balances found: {results_count}")
|
||||
|
||||
|
||||
async def make_internal_transaction(
|
||||
memory: Memory, user_id: int, asset_id: int, amount: float,
|
||||
is_spent: bool, type: str = "NOT_SPECIFIED", spent_transaction_id: int = None
|
||||
) -> InternalTransaction:
|
||||
amount = float(amount)
|
||||
is_spent = bool(is_spent)
|
||||
type = str(type)[:256]
|
||||
if amount < 0:
|
||||
if not is_spent:
|
||||
raise Exception(f"Invalid is_spent: {is_spent}, amount: {amount}")
|
||||
elif amount > 0:
|
||||
if is_spent:
|
||||
raise Exception(f"Invalid is_spent: {is_spent}, amount: {amount}")
|
||||
else:
|
||||
raise Exception(f"Invalid amount: {amount}")
|
||||
|
||||
abs_amount = abs(amount)
|
||||
with db_session(auto_commit=False) as session:
|
||||
async with memory.transaction():
|
||||
user = session.query(User).filter_by(id=user_id).first()
|
||||
assert user, "No user"
|
||||
asset = session.query(Asset).filter_by(id=asset_id).first()
|
||||
assert asset, "No asset"
|
||||
user_balance = get_user_balance(session, user, asset)
|
||||
assert user_balance, "No user balance"
|
||||
if is_spent is True:
|
||||
if abs_amount > user_balance.balance:
|
||||
raise Exception(f"Insufficient balance: {user_balance.balance} < {abs_amount}")
|
||||
|
||||
user_balance.balance = float(user_balance.balance) + amount
|
||||
user_balance.updated = datetime.now()
|
||||
|
||||
internal_transaction = InternalTransaction(
|
||||
user_id=user.id,
|
||||
asset_id=asset.id,
|
||||
amount=abs_amount,
|
||||
is_spent=is_spent,
|
||||
type=type,
|
||||
spent_transaction_id=spent_transaction_id,
|
||||
created=datetime.now(),
|
||||
)
|
||||
session.add(internal_transaction)
|
||||
session.commit()
|
||||
|
||||
make_log(user, f"Made internal transaction: {'-' if is_spent else ''}{abs_amount} {asset.symbol}, type: {type}")
|
||||
@@ -0,0 +1,28 @@
|
||||
import gettext
|
||||
|
||||
from app.core.logger import make_log
|
||||
|
||||
|
||||
def get_translator(lang_code: str, domain='sanic_telegram_bot'):
|
||||
localedir = 'locale'
|
||||
|
||||
try:
|
||||
return gettext.translation(domain, localedir, languages=[lang_code])
|
||||
except FileNotFoundError:
|
||||
return get_translator('en', domain=domain)
|
||||
|
||||
|
||||
class TranslationCore:
|
||||
@property
|
||||
def translator(self):
|
||||
return get_translator(self.lang_code or 'en', domain=self.LOCALE_DOMAIN)
|
||||
|
||||
def translated(self, message, **format_kwargs):
|
||||
try:
|
||||
message = self.translator.gettext(message)
|
||||
if format_kwargs:
|
||||
message = message.format(**format_kwargs)
|
||||
except BaseException as e:
|
||||
make_log("User.translated", f"Error translating message ({self.lang_code}): {e}")
|
||||
|
||||
return message
|
||||
Reference in new issue
Block a user