Example #1
0
async def choose_buy_gateway(message: types.Message, state: FSMContext):
    """Set gateway of buy currency and ask for sell currency."""
    if message.text.startswith(emojize(":fast_reverse_button:")):
        await OrderCreation.previous()
        await tg.send_message(
            message.chat.id,
            i18n("ask_buy_currency"),
            reply_markup=whitelist.currency_keyboard("buy"),
        )
        return
    elif message.text.startswith(emojize(":x:")):
        await cancel_order_creation(message.from_user.id, message.chat.id)
        return
    elif not message.text.startswith(emojize(":fast_forward:")):
        gateway_result = await get_currency_with_gateway("buy", message)
        if not gateway_result:
            return
        order, currency = gateway_result
        await database.creation.update_one({"_id": order["_id"]},
                                           {"$set": {
                                               "buy": currency
                                           }})

    await OrderCreation.sell.set()
    await tg.send_message(
        message.chat.id,
        i18n("ask_sell_currency"),
        reply_markup=whitelist.currency_keyboard("sell"),
    )
Example #2
0
async def choose_sell(message: types.Message, state: FSMContext):
    """Set currency user wants to sell and ask for price."""
    if message.text.startswith(emojize(":fast_reverse_button:")):
        await OrderCreation.buy.set()
        await tg.send_message(
            message.chat.id,
            i18n("ask_buy_currency"),
            reply_markup=whitelist.currency_keyboard("buy"),
        )
        return
    elif message.text.startswith(emojize(":x:")):
        await cancel_order_creation(message.from_user.id, message.chat.id)
        return

    match = await match_currency("sell", message)
    if not match:
        return

    order = await database.creation.find_one_and_update(
        {"user_id": message.from_user.id, "buy": {"$ne": match}},
        {"$set": {"sell": match, "price_currency": "sell"}},
        return_document=ReturnDocument.AFTER,
    )
    if not order:
        await tg.send_message(
            message.chat.id,
            i18n("same_currency_error"),
            reply_markup=whitelist.currency_keyboard("sell"),
        )
        return

    await set_price_state(message, order)
Example #3
0
async def handle_create(message: types.Message, state: FSMContext):
    """Start order creation by asking user for currency they want to buy."""
    current_time = time()
    user_orders = await database.orders.count_documents(
        {
            "user_id": message.from_user.id,
            "start_time": {"$gt": current_time - config.ORDERS_LIMIT_HOURS * 3600},
        }
    )
    if user_orders >= config.ORDERS_LIMIT_COUNT:
        await tg.send_message(
            message.chat.id,
            i18n("exceeded_order_creation_time_limit {orders} {hours}").format(
                orders=config.ORDERS_LIMIT_COUNT, hours=config.ORDERS_LIMIT_HOURS
            ),
        )
        return

    creation = {"user_id": message.from_user.id}
    await database.creation.find_one_and_replace(creation, creation, upsert=True)
    await states.OrderCreation.first()

    await tg.send_message(
        message.chat.id,
        i18n("ask_buy_currency"),
        reply_markup=whitelist.currency_keyboard("buy"),
    )
Example #4
0
async def choose_sell_gateway(message: types.Message, state: FSMContext):
    """Set gateway of sell currency and ask for price."""
    if message.text.startswith(emojize(":fast_reverse_button:")):
        await OrderCreation.previous()
        await tg.send_message(
            message.chat.id,
            i18n("ask_sell_currency"),
            reply_markup=whitelist.currency_keyboard("sell"),
        )
        return
    elif message.text.startswith(emojize(":x:")):
        await cancel_order_creation(message.from_user.id, message.chat.id)
        return
    elif not message.text.startswith(emojize(":fast_forward:")):
        order = await set_gateway("sell", message)
        if not order:
            return
    else:
        order = await database.creation.find_one_and_update(
            {"user_id": message.from_user.id},
            {"$set": {"price_currency": "sell"}},
            return_document=ReturnDocument.AFTER,
        )

    await set_price_state(message, order)
Example #5
0
async def choose_sell_gateway(message: types.Message, state: FSMContext):
    """Set gateway of sell currency and ask for price."""
    if message.text.startswith(emojize(":fast_reverse_button:")):
        await OrderCreation.previous()
        await tg.send_message(
            message.chat.id,
            i18n("ask_sell_currency"),
            reply_markup=whitelist.currency_keyboard("sell"),
        )
        return
    elif message.text.startswith(emojize(":x:")):
        await cancel_order_creation(message.from_user.id, message.chat.id)
        return

    same_gateway = False
    if not message.text.startswith(emojize(":fast_forward:")):
        gateway_result = await get_currency_with_gateway("sell", message)
        if not gateway_result:
            return
        order, currency = gateway_result
        if currency == order["buy"]:
            same_gateway = True
        else:
            await database.creation.update_one(
                {"_id": order["_id"]},
                {"$set": {
                    "sell": currency,
                    "price_currency": "sell"
                }},
            )
    else:
        order = await database.creation.find_one_and_update(
            {
                "user_id": message.from_user.id,
                "$expr": {
                    "$ne": ["$buy", "$sell"]
                }
            },
            {"$set": {
                "price_currency": "sell"
            }},
            return_document=ReturnDocument.AFTER,
        )
        if not order:
            order = await database.creation.find_one(
                {"user_id": message.from_user.id})
            same_gateway = True

    if same_gateway:
        await tg.send_message(
            message.chat.id,
            i18n("same_gateway_error"),
            reply_markup=whitelist.gateway_keyboard(order["sell"], "sell"),
        )
    else:
        await set_price_state(message, order)
Example #6
0
async def choose_buy_gateway(message: types.Message, state: FSMContext):
    """Set gateway of buy currency and ask for sell currency."""
    if message.text.startswith(emojize(":fast_reverse_button:")):
        await OrderCreation.previous()
        await tg.send_message(
            message.chat.id,
            i18n("ask_buy_currency"),
            reply_markup=whitelist.currency_keyboard("buy"),
        )
        return
    elif message.text.startswith(emojize(":x:")):
        await cancel_order_creation(message.from_user.id, message.chat.id)
        return
    elif not message.text.startswith(emojize(":fast_forward:")):
        order = await set_gateway("buy", message)
        if not order:
            return

    await OrderCreation.sell.set()
    await tg.send_message(
        message.chat.id,
        i18n("ask_sell_currency"),
        reply_markup=whitelist.currency_keyboard("sell"),
    )
Example #7
0
async def choose_buy(message: types.Message, state: FSMContext):
    """Set currency user wants to buy and ask for one they want to sell."""
    if message.text.startswith(emojize(":x:")):
        await cancel_order_creation(message.from_user.id, message.chat.id)
        return

    match = await match_currency("buy", message)
    if not match:
        return

    await database.creation.update_one(
        {"user_id": message.from_user.id}, {"$set": {"buy": match}}
    )
    await OrderCreation.sell.set()
    await tg.send_message(
        message.chat.id,
        i18n("ask_sell_currency"),
        reply_markup=whitelist.currency_keyboard("sell"),
    )