Ejemplo n.º 1
0
def test_add_funds(patch_blockchain: None):
    buy_amount = 1000
    buy_currency = DiemCurrency.XUS
    inventory_id, account_id, order_id = AddFundsSeeder.run(
        db_session,
        buy_amount=buy_amount,
        buy_currency=buy_currency,
        pay_currency=FiatCurrency.EUR,
        pay_price=900,
    )

    payment_method = "4580 2601 0743 7443"

    order_service.execute_order(order_id, payment_method)

    order = get_order(order_id)
    assert order.order_status == OrderStatus.Executed.value
    assert order.cover_status == CoverStatus.Covered.value

    add_funds_transaction = storage.get_transaction(order.internal_ledger_tx)
    assert add_funds_transaction
    assert add_funds_transaction.currency == buy_currency
    assert add_funds_transaction.amount == buy_amount
    assert add_funds_transaction.source_id == inventory_id
    assert add_funds_transaction.destination_id == account_id
Ejemplo n.º 2
0
def cover_order(order_id: OrderId):
    order = get_order(order_id)

    if order.order_type == OrderType.DirectConvert.value:
        update_order(order_id=order_id, cover_status=CoverStatus.Covered)
        return

    inventory.cover_order(order)
Ejemplo n.º 3
0
def test_update_order(clean_db: None) -> None:
    user_id, order_id = OneUserWithOneOrder().run(db_session)

    update_order(OrderId(UUID(order_id)),
                 order_status=OrderStatus.FailedCredit)

    order = get_order(order_id)

    assert order.order_status == OrderStatus.FailedCredit.value
Ejemplo n.º 4
0
def order_expired(order_id: OrderId):
    order = get_order(order_id)

    is_expired = datetime.utcnow() > order.order_expiration

    if is_expired:
        update_order(order_id=order_id, order_status=OrderStatus.Expired)
        return True

    return False
Ejemplo n.º 5
0
def execute_order(order_id: OrderId, payment_method: Optional[str] = None):
    if order_expired(order_id):
        return

    order = get_order(order_id)
    if payment_method:
        process_payment_method(
            payment_method=payment_method,
            amount=order.amount,
            action=PaymentMethodAction.Charge,
        )

    if order.order_type == OrderType.Trade:
        if execute_trade(order):
            if services.run_bg_tasks():
                from ..background_tasks.background import async_cover_order

                async_cover_order.send(order_id)
            else:
                cover_order(order_id=order_id)
    else:
        execute_convert(order)
Ejemplo n.º 6
0
def test_withdraw_funds(patch_blockchain: None):
    inventory_id, account_id, order_id = WithdrawFundsSeeder.run(
        db_session,
        account_amount=1000,
        account_currency=DiemCurrency.XUS,
        withdraw_amount=500,
        withdraw_to_currency=FiatCurrency.USD,
        price=550,
    )
    payment_method = "4580 2601 0743 7443"

    order_service.execute_order(order_id, payment_method)

    order = get_order(order_id)
    assert order.order_status == OrderStatus.Executed.value
    assert order.cover_status == CoverStatus.Covered.value

    withdraw_transaction = storage.get_transaction(order.internal_ledger_tx)
    assert withdraw_transaction
    assert withdraw_transaction.currency == DiemCurrency.XUS
    assert withdraw_transaction.amount == 500
    assert withdraw_transaction.source_id == account_id
    assert withdraw_transaction.destination_id == inventory_id
Ejemplo n.º 7
0
def process_order_payment(order_id, payment_method,
                          action: PaymentMethodAction):
    order = get_order(order_id)

    time.sleep(PAYMENT_PROCESSING_DUMMY_SLEEP_TIME)
    charge_token = process_payment_method(payment_method,
                                          order.exchange_amount, action)

    if charge_token:
        update_order(
            order_id=order_id,
            charge_token=charge_token,
            order_status=OrderStatus.Charged.value,
            payment_method=payment_method,
        )
    else:
        update_order(
            order_id=order_id,
            order_status=OrderStatus.FailedCharge.value if action
            == PaymentMethodAction.Charge else OrderStatus.FailedCredit,
            payment_method=payment_method,
        )

    return charge_token
Ejemplo n.º 8
0
def is_executed(order_id: OrderId):
    order = get_order(order_id)

    return OrderStatus[order.order_status] == OrderStatus.Executed
Ejemplo n.º 9
0
def test_get_order(clean_db: None) -> None:
    user_id, order_id = OneUserWithOneOrder().run(db_session)

    order = get_order(OrderId(UUID(order_id)))

    assert order