def withdrawal_amount(bot: Bot, update: Update, user, user_data): amount = to_float(update.message.text) currency = user_data['currency'] wallet = get_wallet(partner_id=user.partner.id, currency=currency) balance = get_wallet_balance(wallet) if balance < amount: bot.send_message(chat_id=update.message.chat_id, text=texts.area_insufficient_balance.format( currency=currency.upper(), amount=amount, balance=balance), reply_markup=keyboards.cancel(reply=True), parse_mode=ParseMode.HTML) return AMOUNT fee = get_fee(currency) user_data['amount'] = amount bot.send_message(chat_id=update.message.chat_id, text=texts.area_amount.format(currency=currency.upper(), amount=amount - fee, fee=fee), reply_markup=keyboards.cancel(reply=True), parse_mode=ParseMode.HTML) return ADDR
def registration_email(bot: Bot, update: Update, user_data): email = user_data['email'] = update.message.text partner = db.session.query(db.Partners).filter_by(email=email).first() if partner: bot.send_message( chat_id=update.message.chat_id, parse_mode=ParseMode.HTML, text=texts.area_email_already_exists.format(email=email), reply_markup=keyboards.authorization(registration=False)) user_data.clear() return ConversationHandler.END else: # TODO: Delete this after updating all accounts # TODO: Then delete 'telegram_id' from table 'partners' # Check out old account created by bot partner = db.session.query(db.Partners).filter_by( telegram_id=update.effective_user.id).first() if partner: if not partner.email and not partner.password: user_data['based_on'] = partner.id logger.debug("An old account was found. " "Additional registration.") bot.send_message(chat_id=update.message.chat_id, parse_mode=ParseMode.HTML, text=texts.area_request_password, reply_markup=keyboards.cancel(reply=True)) return PASSWORD
def withdrawal_invalid_addr(bot: Bot, update: Update, user_data): bot.send_message(chat_id=update.message.chat_id, text=texts.area_invalid_addr.format( currency=user_data['currency'].upper()), reply_markup=keyboards.cancel(reply=True)) return ADDR
def withdrawal_addr(bot: Bot, update: Update, user_data): addr = update.message.text if not validate_address(addr, user_data['currency']): bot.send_message(chat_id=update.message.chat_id, text=texts.area_invalid_addr.format( currency=user_data['currency'].upper()), reply_markup=keyboards.cancel(reply=True)) return ADDR user_data['addr'] = addr fee = get_fee(user_data['currency']) bot.send_message(chat_id=update.message.chat_id, text=texts.area_addr.format( currency=user_data['currency'].upper(), amount=user_data['amount'] - fee, addr=addr, fee=fee), reply_markup=keyboards.confirmation(), parse_mode=ParseMode.HTML) return OK
def sell_token_amount(bot: Bot, update: Update, user, user_data): amount = to_float(update.message.text) base = user_data['base'] wallet = get_wallet(partner_id=user.partner.id, currency=base) balance = get_wallet_balance(wallet) if balance < amount: bot.send_message(chat_id=update.message.chat_id, text=texts.area_insufficient_balance.format( currency=base.upper(), amount=amount, balance=balance), reply_markup=keyboards.cancel(reply=True), parse_mode=ParseMode.HTML) return AMOUNT user_data['amount'] = amount bot.send_message(chat_id=update.message.chat_id, text=texts.area_sell_token_amount.format( currency=base.upper(), amount=amount), reply_markup=keyboards.token_quoted(), parse_mode=ParseMode.HTML) return QUOTED
def sell_token(bot: Bot, update: Update, user, user_data): if not user.partner: return base = get_currency(update.message.text) user_data['base'] = base bot.send_message(chat_id=user.telegram_id, parse_mode=ParseMode.HTML, text=texts.area_sell_token.format(currency=base.upper()), reply_markup=keyboards.cancel(reply=True)) return AMOUNT
def buy_token_quoted(bot: Bot, update: Update, user, user_data): quoted = update.message.text.lower() base = user_data['base'] user_data['quoted'] = quoted rate = get_rate(base, quoted) bot.send_message(chat_id=user.telegram_id, parse_mode=ParseMode.HTML, text=texts.area_token_buy_quoted.format( base=base.upper(), quoted=quoted.upper(), rate=rate), reply_markup=keyboards.cancel(reply=True)) return BUY_AMOUNT
def withdrawal(bot: Bot, update: Update, user, user_data): if not user.partner: return currency = get_currency(update.message.text) user_data['currency'] = currency bot.send_message( chat_id=update.message.chat_id, text=texts.area_withdrawal.format(currency=currency.upper()), reply_markup=keyboards.cancel(reply=True), parse_mode=ParseMode.HTML) return AMOUNT
def buy_token_amount(bot: Bot, update: Update, user, user_data): amount = to_float(update.message.text) base = user_data['base'] quoted = user_data['quoted'] wallet = get_wallet(partner_id=user.partner.id, currency=base) balance = get_wallet_balance(wallet) if balance < amount: bot.send_message(chat_id=update.message.chat_id, text=texts.area_insufficient_balance.format( currency=quoted.upper(), amount=amount, balance=balance), reply_markup=keyboards.cancel(reply=True), parse_mode=ParseMode.HTML) return BUY_AMOUNT user_data['amount'] = amount rate = get_rate(base, quoted) if not rate: bot.send_message(chat_id=update.message.chat_id, text=texts.area_maintenance, reply_markup=keyboards.main(), parse_mode=ParseMode.HTML) user_data.clear() return ConversationHandler.END amount_received = round(amount / rate, 8) fee = round(amount_received * 0.05, 8) amount_received *= 0.95 bot.send_message(chat_id=update.message.chat_id, text=texts.area_token_buy_amount.format( base=base.upper(), quoted=quoted.upper(), fee=fee, amount=amount, amount_received=amount_received, rate=rate), reply_markup=keyboards.confirmation(), parse_mode=ParseMode.HTML) return BUY_CONFIRMATION
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
def invalid_password(bot: Bot, update: Update): bot.send_message(chat_id=update.message.chat_id, parse_mode=ParseMode.HTML, text=texts.area_invalid_password, reply_markup=keyboards.cancel(reply=True)) return PASSWORD
def registration(bot: Bot, update: Update): bot.send_message(chat_id=update.message.chat_id, parse_mode=ParseMode.HTML, text=texts.area_request_email, reply_markup=keyboards.cancel(reply=True)) return EMAIL
def withdrawal_confirmation(bot: Bot, update: Update, user, user_data): currency = user_data['currency'] amount = user_data['amount'] address = user_data['addr'] server = rpc_servers.get(currency) wallet = get_wallet(partner_id=user.partner.id, currency=currency) wallet_balance = get_wallet_balance(wallet) blockchain_balance = server.getbalance() if blockchain_balance < amount: # Нам недостаточно средств на ноде, сообщение об этом # придет админу, а эвент запишется в gateway_events, # без id транзакции и подтверждения. bot.send_message(chat_id=update.message.chat_id, text=texts.area_withdrawal_error, reply_markup=keyboards.main(), parse_mode=ParseMode.HTML) gateway_event = add_gateway_event(currency=currency, amount=amount, address=address) add_wallet_event(partner_id=user.partner.id, wallet_id=wallet.id, amount=-amount, source_id=gateway_event.id, status='moderation') error_msg = texts.area_withdrawal_error_admin.format( id=user.partner.id, amount=amount, currency=currency.upper(), balance=blockchain_balance, event_id=gateway_event.id, addr=address) send_to_admin(bot, error_msg) user_data.clear() return ConversationHandler.END if wallet_balance < amount: bot.send_message(chat_id=update.message.chat_id, text=texts.area_insufficient_balance.format( currency=currency.upper(), amount=amount, balance=wallet_balance), reply_markup=keyboards.cancel(reply=True), parse_mode=ParseMode.HTML) return AMOUNT txid = server.sendtoaddress(address, amount, "bot withdrawal", "", True) logger.debug(f"txid: {txid}") txinfo = server.gettransaction(txid) logger.debug(f"txinfo: {txinfo}") gateway_event = add_gateway_event(currency=currency, amount=amount, address=address, txid=txid, confirmed=True) add_wallet_event(partner_id=user.partner.id, wallet_id=wallet.id, amount=-amount, source_id=gateway_event.id) text = texts.area_withdrawal_info.format(id=txid, currency=currency.upper(), amount=abs(txinfo['amount']), fee=abs(txinfo['fee'])) user_data.clear() bot.send_message(chat_id=update.message.chat_id, text=text, reply_markup=keyboards.main(), parse_mode=ParseMode.HTML) return ConversationHandler.END
def buy_token_invalid_amount(bot: Bot, update: Update): bot.send_message(chat_id=update.message.chat_id, text=texts.area_token_invalid_amount, reply_markup=keyboards.cancel(reply=True)) return BUY_AMOUNT
def buy_token_confirmation(bot: Bot, update: Update, user, user_data): amount = user_data['amount'] base = user_data['base'] quoted = user_data['quoted'] wallet = get_wallet(partner_id=user.partner.id, currency=quoted) balance = get_wallet_balance(wallet) if balance < amount: bot.send_message(chat_id=update.message.chat_id, text=texts.area_insufficient_balance.format( currency=quoted.upper(), amount=amount, balance=balance), reply_markup=keyboards.cancel(reply=True), parse_mode=ParseMode.HTML) return BUY_AMOUNT rate = get_rate(base, quoted) if not rate: bot.send_message(chat_id=update.message.chat_id, text=texts.area_maintenance, reply_markup=keyboards.main(), parse_mode=ParseMode.HTML) user_data.clear() return ConversationHandler.END amount_received = round(amount / rate, 8) fee = round(amount_received * 0.05, 8) amount_received *= 0.95 exchange_event = add_exchange_event(partner_id=user.partner.id, src=quoted, dst=base, amount=amount, status='pending') # 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) bot.send_message(chat_id=update.message.chat_id, text=texts.area_token_buy_confirmation.format( base=base.upper(), quoted=quoted.upper(), fee=fee, amount=amount, amount_received=amount_received, rate=rate), reply_markup=keyboards.main(), parse_mode=ParseMode.HTML) user_data.clear() return ConversationHandler.END