def registration_password(bot: Bot, update: Update, user, user_data): user_data['password'] = update.message.text user_data['referrer'] = user.referrer_id response = requests.post("http://localhost:6000/v1/user/signup", data=user_data).json() output = {'email': user_data['email']} user_data.clear() if response['success']: logger.debug(f"Api registration response: {response}") user.partner_id = response['data']['id'] user.ts_update = int(time()) db.commit() bot.send_message(chat_id=update.message.chat_id, parse_mode=ParseMode.HTML, text=texts.area_success_registration.format(**output), reply_markup=keyboards.main()) else: logger.warning(f'Invalid response: {response}') bot.send_message( chat_id=update.message.chat_id, parse_mode=ParseMode.HTML, text=texts.area_email_already_exists.format(**output), reply_markup=keyboards.authorization(registration=False)) return ConversationHandler.END
def sell_token_confirmation(bot: Bot, update: Update, user, user_data): addr = user_data['addr'] quoted = user_data['quoted'] base = user_data['base'] amount = user_data['amount'] rate = get_rate(base, quoted) wallet = get_wallet(user.partner.id, base) amount_received = round(amount / rate, 8) fee = 0 # TODO: добавить вычисление комиссии exchange_event = add_exchange_event(partner_id=user.partner.id, src=base, dst=quoted, amount=amount, status='done') # fee add_wallet_event(partner_id=32, wallet_id=1, amount=fee, status='done', source_name='exchange', source_id=exchange_event.id) add_wallet_event(partner_id=user.partner.id, wallet_id=wallet.id, amount=-amount, status='done', source_name='exchange', source_id=exchange_event.id) ticket = db.WithdrawalTicket(partner_id=user.partner.id, src=base, dst=quoted, src_count=amount, dst_count=amount_received, rate=rate, address=addr, status='pending', ts_create=int(time()), ts_update=int(time())) db.session.add(ticket) db.commit() bot.send_message(chat_id=update.message.chat_id, text=texts.area_sell_token_confirmation.format( amount=amount, base=base.upper(), quoted=quoted.upper(), amount_received=amount_received, address=addr, rate=rate, fee=fee), reply_markup=keyboards.main(), parse_mode=ParseMode.HTML) user_data.clear() return ConversationHandler.END
def create_token_wallet(partner_id, currency): wallet = db.PartnersWallet(partner_id=partner_id, currency=currency, ts_add=int(time())) db.session.add(wallet) db.commit() return wallet
def logout(bot: Bot, update: Update, user): """ """ user.partner_id = None db.commit() bot.edit_message_text(chat_id=user.telegram_id, parse_mode=ParseMode.HTML, message_id=update.callback_query.message.message_id, text='Вы вышли из аккаунта!')
def add_exchange_event(partner_id, src, dst, amount, status): event = db.ExchangeEvents(partner_id=partner_id, src=src, dst=dst, amount=amount, status=status, ts_add=int(time())) db.session.add(event) db.commit() return event
def create_db_user(tg_user, referrer_id=None): db_user = db.TelegramUsers(ts_add=int(time()), ts_update=int(time()), telegram_id=tg_user.id, referrer_id=referrer_id, username=tg_user.username, first_name=tg_user.first_name, last_name=tg_user.last_name) db.session.add(db_user) db.commit() return db_user
def create_wallet(partner_id, currency, autopay=None): server = jsonrpclib.Server( "http://*****:*****@127.0.0.1:{port}".format(**cfg.RPC[currency])) addr = server.getnewaddress() wallet = db.PartnersWallet(partner_id=partner_id, currency=currency, addr=addr, autopay=autopay, ts_add=int(time())) logger.info(f'Create new {currency}-address for partner {partner_id}: ' f'{addr}. Autopay = {autopay}.') db.session.add(wallet) db.commit() return wallet
def add_gateway_event(currency, amount, address, event_name='out', txid=None, confirmed=None): event = db.GatewayEvents(currency=currency, event_name=event_name, amount=amount, address=address, txid=txid, confirmed=confirmed, ts_add=int(time())) db.session.add(event) db.commit() return event
def add_wallet_event(partner_id, wallet_id, amount, status='done', source_name='gateway', source_id=None): event = db.WalletsEvent(partner_id=partner_id, wallet_id=wallet_id, amount=amount, status=status, source_name=source_name, source_id=source_id, ts_add=int(time())) db.session.add(event) db.commit() return event
def get_db_user(tg_user, logger): db_user = db.session.query(db.TelegramUsers).\ filter_by(telegram_id=tg_user.id).first() if db_user: if (db_user.username != tg_user.username or db_user.first_name != tg_user.first_name or db_user.last_name != tg_user.last_name): db_user.ts_update = int(time()) db_user.username = tg_user.username db_user.first_name = tg_user.first_name db_user.last_name = tg_user.last_name db.commit() logger.info(f"User updated: {db_user}") else: db_user = create_db_user(tg_user) logger.info(f"New user added: {db_user}") return db_user
def get_personal_address(partner_id, type_): partner_addr = db.session.query(db.PartnersAddress).\ filter_by(partner_id=partner_id, type=type_, testnet=False).first() if partner_addr: addr = partner_addr.addr else: server = jsonrpclib.Server( "http://*****:*****@127.0.0.1:{port}".format( **cfg.RPC[type_])) addr = server.getnewaddress() entry = db.PartnersAddress(partner_id=partner_id, type=type_, addr=addr, testnet=False) db.session.add(entry) db.commit() return addr
def login_password(bot: Bot, update: Update, user, user_data): password = update.message.text email = user_data['email'] partner = db.session.query(db.Partners).filter_by( email=email, password=password).first() if not partner: bot.send_message(chat_id=update.message.chat_id, parse_mode=ParseMode.HTML, text=texts.area_unsuitable_password, reply_markup=keyboards.cancel(reply=True)) return PASSWORD else: user.partner_id = partner.id user.ts_update = int(time()) db.commit() bot.send_message(chat_id=update.message.chat_id, parse_mode=ParseMode.HTML, text=texts.area_success_login, reply_markup=keyboards.main()) user_data.clear() return ConversationHandler.END