Example #1
0
def test_deserialize_timestamp_from_bittrex_date():
    """Test for deserialize_timestamp_from_bittrex_date() and regression for #1151

    In https://github.com/rotki/rotki/issues/1151 a user encountered an error with
    the following date: 2017-07-08T07:57:12
    """
    assert deserialize_timestamp_from_bittrex_date(
        '2014-02-13T00:00:00.00') == 1392249600
    assert deserialize_timestamp_from_bittrex_date(
        '2015-06-15T07:38:53.883') == 1434353933
    assert deserialize_timestamp_from_bittrex_date(
        '2015-08-19T04:24:47.217') == 1439958287
    assert deserialize_timestamp_from_bittrex_date(
        '2017-07-08T07:57:12') == 1499500632
Example #2
0
    def _deserialize_asset_movement(self, raw_data: Dict[str, Any]) -> Optional[AssetMovement]:
        """Processes a single deposit/withdrawal from bittrex and deserializes it

        Can log error/warning and return None if something went wrong at deserialization
        """
        try:
            if 'TxCost' in raw_data:
                category = AssetMovementCategory.WITHDRAWAL
                date_key = 'Opened'
                fee = deserialize_fee(raw_data['TxCost'])
            else:
                category = AssetMovementCategory.DEPOSIT
                date_key = 'LastUpdated'
                fee = Fee(ZERO)

            timestamp = deserialize_timestamp_from_bittrex_date(raw_data[date_key])
            asset = asset_from_bittrex(raw_data['Currency'])
            return AssetMovement(
                location=Location.BITTREX,
                category=category,
                timestamp=timestamp,
                asset=asset,
                amount=deserialize_asset_amount(raw_data['Amount']),
                fee_asset=asset,
                fee=fee,
                link=str(raw_data['TxId']),
            )
        except UnknownAsset as e:
            self.msg_aggregator.add_warning(
                f'Found bittrex deposit/withdrawal with unknown asset '
                f'{e.asset_name}. Ignoring it.',
            )
        except UnsupportedAsset as e:
            self.msg_aggregator.add_warning(
                f'Found bittrex deposit/withdrawal with unsupported asset '
                f'{e.asset_name}. Ignoring it.',
            )
        except (DeserializationError, KeyError) as e:
            msg = str(e)
            if isinstance(e, KeyError):
                msg = f'Missing key entry for {msg}.'
            self.msg_aggregator.add_error(
                f'Unexpected data encountered during deserialization of a bittrex '
                f'asset movement. Check logs for details and open a bug report.',
            )
            log.error(
                f'Unexpected data encountered during deserialization of bittrex '
                f'asset_movement {raw_data}. Error was: {str(e)}',
            )

        return None
Example #3
0
def trade_from_bittrex(bittrex_trade: Dict[str, Any]) -> Trade:
    """Turn a bittrex trade returned from bittrex trade history to our common trade
    history format

    Throws:
        - UnknownAsset/UnsupportedAsset due to bittrex_pair_to_world()
        - DeserializationError due to unexpected format of dict entries
        - KeyError due to dict entries missing an expected entry
    """
    amount = AssetAmount(
        deserialize_asset_amount(bittrex_trade['Quantity']) -
        deserialize_asset_amount(bittrex_trade['QuantityRemaining']), )
    timestamp = deserialize_timestamp_from_bittrex_date(
        bittrex_trade['TimeStamp'])
    rate = deserialize_price(bittrex_trade['PricePerUnit'])
    order_type = deserialize_trade_type(bittrex_trade['OrderType'])
    bittrex_price = deserialize_price(bittrex_trade['Price'])
    fee = deserialize_fee(bittrex_trade['Commission'])
    pair = bittrex_pair_to_world(bittrex_trade['Exchange'])
    quote_currency = get_pair_position_asset(pair, 'second')

    log.debug(
        'Processing bittrex Trade',
        sensitive_log=True,
        amount=amount,
        rate=rate,
        order_type=order_type,
        price=bittrex_price,
        fee=fee,
        bittrex_pair=bittrex_trade['Exchange'],
        pair=pair,
    )

    return Trade(
        timestamp=timestamp,
        location=Location.BITTREX,
        pair=pair,
        trade_type=order_type,
        amount=amount,
        rate=rate,
        fee=fee,
        fee_currency=quote_currency,
        link=str(bittrex_trade['OrderUuid']),
    )