def bittrex_pair_to_world(given_pair: str) -> TradePair: """ Turns a pair written in the bittrex way to Rotkehlchen way Throws: - UnsupportedAsset due to asset_from_bittrex() - UnprocessableTradePair if the pair can't be split into its parts """ if not isinstance(given_pair, str): raise DeserializationError( f'Could not deserialize bittrex trade pair. Expected a string ' f'but found {type(given_pair)}', ) pair = TradePair(given_pair.replace('-', '_')) base_currency = asset_from_bittrex(get_pair_position_str(pair, 'first')) quote_currency = asset_from_bittrex(get_pair_position_str(pair, 'second')) # Since in Bittrex the base currency is the cost currency, iow in Bittrex # for BTC_ETH we buy ETH with BTC and sell ETH for BTC, we need to turn it # into the Rotkehlchen way which is following the base/quote approach. pair = trade_pair_from_assets(quote_currency, base_currency) return pair
def trade_from_poloniex(poloniex_trade: Dict[str, Any], pair: TradePair) -> Trade: """Turn a poloniex trade returned from poloniex trade history to our common trade history format Throws: - UnsupportedAsset due to asset_from_poloniex() - DeserializationError due to the data being in unexpected format - UnprocessableTradePair due to the pair data being in an unexpected format """ try: trade_type = deserialize_trade_type(poloniex_trade['type']) amount = deserialize_asset_amount(poloniex_trade['amount']) rate = deserialize_price(poloniex_trade['rate']) perc_fee = deserialize_fee(poloniex_trade['fee']) base_currency = asset_from_poloniex( get_pair_position_str(pair, 'first')) quote_currency = asset_from_poloniex( get_pair_position_str(pair, 'second')) timestamp = deserialize_timestamp_from_poloniex_date( poloniex_trade['date']) except KeyError as e: raise DeserializationError( f'Poloniex trade deserialization error. Missing key entry for {str(e)} in trade dict', ) cost = rate * amount if trade_type == TradeType.BUY: fee = amount * perc_fee fee_currency = quote_currency elif trade_type == TradeType.SELL: fee = cost * perc_fee fee_currency = base_currency else: raise DeserializationError( f'Got unexpected trade type "{trade_type}" for poloniex trade') if poloniex_trade['category'] == 'settlement': if trade_type == TradeType.BUY: trade_type = TradeType.SETTLEMENT_BUY else: trade_type = TradeType.SETTLEMENT_SELL log.debug( 'Processing poloniex Trade', sensitive_log=True, timestamp=timestamp, order_type=trade_type, pair=pair, base_currency=base_currency, quote_currency=quote_currency, amount=amount, fee=fee, rate=rate, ) # Use the converted assets in our pair pair = trade_pair_from_assets(base_currency, quote_currency) # Since in Poloniex the base currency is the cost currency, iow in poloniex # for BTC_ETH we buy ETH with BTC and sell ETH for BTC, we need to turn it # into the Rotkehlchen way which is following the base/quote approach. pair = invert_pair(pair) return Trade( timestamp=timestamp, location='poloniex', pair=pair, trade_type=trade_type, amount=amount, rate=rate, fee=fee, fee_currency=fee_currency, )
def test_get_pair_position_str(): assert get_pair_position_str('ETH_BTC', 'first') == 'ETH' assert get_pair_position_str('ETH_BTC', 'second') == 'BTC' with pytest.raises(AssertionError): get_pair_position_str('ETH_BTC', 'third') with pytest.raises(AssertionError): get_pair_position_str('ETH_BTC', 'sdsadsad') with pytest.raises(UnprocessableTradePair): get_pair_position_str('_', 'first') with pytest.raises(UnprocessableTradePair): get_pair_position_str('ETH_', 'first') with pytest.raises(UnprocessableTradePair): get_pair_position_str('_BTC', 'second') with pytest.raises(UnprocessableTradePair): get_pair_position_str('ETH_BTC_USD', 'first') # This function does not checks for known assets assert get_pair_position_str('ETH_FDFSFDSFDSF', 'first') == 'ETH' assert get_pair_position_str('FDFSFDSFDSF_BTC', 'first') == 'FDFSFDSFDSF' assert get_pair_position_str('ETH_RDN', 'first') == 'ETH' assert get_pair_position_str('ETH_RDN', 'second') == 'RDN'