Exemplo n.º 1
0
def test_order_dto():
    order = OrderDTO(order_id=uuid4(), in_tx=TEST_TX)
    assert isinstance(order, OrderDTO)
    assert isinstance(order.in_tx, TransactionDTO)
    assert isinstance(order.in_tx.error, TxError)

    dump = order.Schema().dump(order)

    loads = order.Schema().load(dump)

    assert order.in_tx.amount == loads.in_tx.amount
    assert isinstance(loads.in_tx, TransactionDTO)
    assert type(loads.order_type) == type(order.order_type)
Exemplo n.º 2
0
    async def init_new_tx(self, request):
        order = OrderDTO.Schema().load(request.msg[1]["params"])

        # TODO Doing check and broadcast stuff

        out_tx = order.in_tx
        return self.jsonrpc_response(request, out_tx)
Exemplo n.º 3
0
def order_from_row(obj) -> OrderDTO:
    """Convert aiopg.sa.RowProxy object (result of select_orders_to_process() module) with Order row data
    to OrderDTO object"""
    in_tx_dto = TransactionDTO(
        coin=obj.in_tx_coin,
        tx_id=obj.in_tx_id,
        to_address=obj.in_tx_to_address,
        from_address=obj.in_tx_from_address,
        amount=obj.in_tx_amount,
        created_at=obj.in_tx_created_at,
        error=obj["in_tx_error"],
        confirmations=obj.in_tx_confirmations,
        max_confirmations=obj.in_tx_max_confirmations,
    )

    out_tx_dto = TransactionDTO(
        coin=obj.out_tx_coin,
        tx_id=obj.out_tx_id,
        to_address=obj.out_tx_to_address,
        from_address=obj.out_tx_from_address,
        amount=obj.out_tx_amount,
        created_at=obj.in_tx_created_at,
        error=obj.out_tx_error,
        confirmations=obj.out_tx_confirmations,
        max_confirmations=obj.out_tx_max_confirmations,
    )

    order_dto = OrderDTO(
        order_id=obj.id, in_tx=in_tx_dto, out_tx=out_tx_dto, order_type=obj.order_type
    )

    return order_dto
Exemplo n.º 4
0
    async def update_order(self, request):
        """Update existing transaction"""
        order_dto = OrderDTO.Schema().load(request.msg[1]["params"])

        if self.ctx:
            async with self.ctx.db_engine.acquire() as conn:
                update = await safe_update_order(conn, order_dto)
                log.info(f"Order {order_dto.order_id} updated: {update}")

        updated_tx = UpdateOrderDTO(is_updated=True)

        return self.jsonrpc_response(request, updated_tx)
Exemplo n.º 5
0
def test_jsonrpcresponse_order():

    o = OrderDTO(order_id=uuid4(), in_tx=TEST_TX, order_type=OrderType.DEPOSIT)

    resp = JSONRPCResponse(id=uuid4(), result=o, error=None)

    resp_dump = resp.Schema().dump(resp)

    resp_loads = JSONRPCResponse.Schema().load(resp_dump)
    result_loads = resp_loads.result
    assert isinstance(resp_loads, JSONRPCResponse)
    assert isinstance(result_loads, OrderDTO)
    assert resp_loads.result.in_tx == o.in_tx
    assert resp_loads.result.in_tx.amount == o.in_tx.amount == TEST_TX.amount
Exemplo n.º 6
0
    async def create_order(self, request):
        """Receiving new (IN) transaction, creating OUT transaction template and Order"""
        in_tx = TransactionDTO.Schema().load(request.msg[1]["params"])

        # TODO replace this mock
        out_tx = TransactionDTO(
            amount=in_tx.amount,
            tx_id=None,
            coin=in_tx.coin,
            to_address=in_tx.coin,
            from_address=in_tx.coin,
            confirmations=0,
            max_confirmations=1,
            created_at=datetime.datetime.now(),
        )

        if self.ctx:
            prefix = self.ctx.cfg.exchange_prefix
        else:
            prefix = Config.exchange_prefix

        if prefix in in_tx.coin:
            order_type = OrderType.WITHDRAWAL
        else:
            order_type = OrderType.DEPOSIT
        order = OrderDTO(order_id=uuid4(),
                         in_tx=in_tx,
                         out_tx=out_tx,
                         order_type=order_type)

        if self.ctx:
            async with self.ctx.db_engine.acquire() as conn:
                in_tx_model = Tx(id=uuid4(), **dataclasses.asdict(in_tx))
                out_tx_model = Tx(id=uuid4(), **dataclasses.asdict(out_tx))
                order_model = Order(
                    id=order.order_id,
                    in_tx=in_tx_model.id,
                    out_tx=out_tx_model.id,
                    order_type=order.order_type,
                )
                await safe_insert_order(conn, in_tx_model, out_tx_model,
                                        order_model)

        return self.jsonrpc_response(request, order)
Exemplo n.º 7
0
    async def create_order(self, request):
        """Receiving Order with new (IN) transaction, creating OUT transaction template and Order_id"""
        order_dto = OrderDTO.Schema().load(request.msg[1]["params"])

        order_dto.out_tx.amount = order_dto.in_tx.amount

        if self.ctx:
            prefix = self.ctx.cfg.exchange_prefix
        else:
            prefix = Config.exchange_prefix

        if prefix in order_dto.in_tx.coin:
            order_dto.order_type = OrderType.WITHDRAWAL
            order_dto.out_tx.coin = str(order_dto.in_tx.coin).replace(
                f"{prefix}.", "")
        else:
            order_dto.order_type = OrderType.DEPOSIT
            order_dto.out_tx.coin = f"{prefix}.{order_dto.in_tx.coin}"

        order_dto.order_id = uuid4()

        if self.ctx:
            async with self.ctx.db_engine.acquire() as conn:
                in_tx_model = Tx(id=uuid4(),
                                 **dataclasses.asdict(order_dto.in_tx))
                out_tx_model = Tx(id=uuid4(),
                                  **dataclasses.asdict(order_dto.out_tx))
                order_model = Order(
                    id=order_dto.order_id,
                    in_tx=in_tx_model.id,
                    out_tx=out_tx_model.id,
                    order_type=order_dto.order_type,
                )
                insert = await safe_insert_order(conn, in_tx_model,
                                                 out_tx_model, order_model)

                log.info(f"Order {order_model.id} created: {insert}")

        return self.jsonrpc_response(request, order_dto)
Exemplo n.º 8
0
async def test_update_order():
    from finteh_proto.dto import OrderDTO, TransactionDTO

    engine = await _get_db_engine()

    async with engine.acquire() as conn:
        in_tx1 = Tx(
            id=uuid4(),
            coin="USDT",
            tx_id="some_id11",
            from_address="some_sender",
            to_address="some_receiver",
            amount=10.1,
            created_at=datetime.datetime.now(),
            error=TxError.NO_ERROR,
            confirmations=4,
            max_confirmations=3,
        )

        out_tx1 = Tx(
            id=uuid4(),
            coin="FINTEH.USDT",
            tx_id="some_id2",
            from_address="some_sender",
            to_address="some_receiver",
            amount=9.99,
            created_at=datetime.datetime.now(),
            error=TxError.NO_ERROR,
            confirmations=3,
            max_confirmations=3,
        )

        order1 = Order(id=uuid4(),
                       in_tx=in_tx1.id,
                       out_tx=out_tx1.id,
                       order_type=OrderType.DEPOSIT)

        await safe_insert_order(conn, in_tx1, out_tx1, order1)

        out_tx1_dto = TransactionDTO(
            coin=out_tx1.coin,
            from_address=out_tx1.from_address,
            to_address=out_tx1.to_address,
            amount=out_tx1.amount,
            created_at=out_tx1.created_at,
            error=out_tx1.error,
            confirmations=out_tx1.confirmations,
            max_confirmations=out_tx1.max_confirmations,
        )

        order_dto = OrderDTO(
            order_id=order1.id,
            in_tx=None,
            out_tx=out_tx1_dto,
            order_type=order1.order_type,
        )

        order_dto.out_tx.confirmations = 6

        await safe_update_order(conn, order_dto)

        order_db_instance = await select_order_by_id(conn, order1.id)

        assert order_db_instance.out_tx_confirmations == 6
        assert order_db_instance.in_tx_hash != None

        await delete_order(conn, order1.id)

        await delete_tx(conn, in_tx1.id)
        await delete_tx(conn, out_tx1.id)
Exemplo n.º 9
0
from booker_api.booker_side_client import BookerSideClient

import pytest

TEST_TX = TransactionDTO(
    amount=Decimal("10.1"),
    tx_id="some:hash",
    coin="USDT",
    to_address="one",
    from_address="two",
    confirmations=0,
    max_confirmations=1,
    created_at=datetime.datetime.now(),
)

TEST_ORDER = OrderDTO(order_id=str(uuid4()), in_tx=TEST_TX)


# @pytest.mark.skip
@pytest.mark.asyncio
async def test_booker_client_create_empty_order():
    test_empty_in_tx = EmptyTransactionDTO(coin="USDT", to_address="kwaskoff")
    test_empty_out_tx = EmptyTransactionDTO(
        coin="FINTEH.USDT",
        to_address="0x3d354a13bb077f15e31e02bf169c103aa60af90a")

    test_empty_order = EmptyOrderDTO(order_id=uuid4(),
                                     in_tx=test_empty_in_tx,
                                     out_tx=test_empty_out_tx)

    server = GatewayServer()