Repository: rotki/rotki

Issue:
NANO cryptocurrency: no price for a given timestamp exception
## Problem Definition

Using Rotkehlchen, i was able to import my binance/bittrex accounts and then tried generating a tax report. Apparently, the issue was because NANO was rebranding at some point from Raiblocks (XRB symbol) and cryptocompare does not provide the price of NANO before the rebrand has happened. 

The process failed with the following exception:

```
28/01/2019 12:56:56 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1445587200
28/01/2019 12:56:56 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1452787200
28/01/2019 12:56:57 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1459987200
28/01/2019 12:56:57 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1467187200
28/01/2019 12:56:57 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1474387200
28/01/2019 12:56:58 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1481587200
28/01/2019 12:56:58 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1488787200
28/01/2019 12:56:58 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1495987200
28/01/2019 12:56:59 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1503187200
28/01/2019 12:56:59 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1510387200
28/01/2019 12:56:59 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1517587200
28/01/2019 12:56:59 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1524787200
28/01/2019 12:57:00 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1531987200
28/01/2019 12:57:01 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1539187200
28/01/2019 12:57:01 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1546387200
28/01/2019 12:57:01 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for hourly historical price from_asset=NANO, to_asset=BTC, cryptocompare_hourquerylimit=2000, end_date=1553587200
28/01/2019 12:57:02 CET -- INFO:rotkehlchen.history:Updating price history cache filename=/home/rakan/.rotkehlchen/price_history_NANO_BTC.json, from_asset=NANO, to_asset=BTC
28/01/2019 12:57:02 CET -- INFO:rotkehlchen.history:Writting history file filepath=/home/rakan/.rotkehlchen/price_history_NANO_BTC.json, start_time=1438387200, end_time=1548676616
28/01/2019 12:57:02 CET -- DEBUG:rotkehlchen.history:Coudn't find historical price from NANO to BTC. Attempting to get daily price...
28/01/2019 12:57:02 CET -- DEBUG:rotkehlchen.history:Querying cryptocompare for daily historical price from_asset=NANO, to_asset=BTC, timestamp=1524605511
28/01/2019 12:57:02 CET -- ERROR:rotkehlchen.server:Greenlet for task 4 dies with exception: Unable to query a historical price for "NANO" to "BTC" at 24/04/2018, 21:31:51.
Exception Name: <class 'rotkehlchen.history.NoPriceForGivenTimestamp'>
Exception Info: Unable to query a historical price for "NANO" to "BTC" at 24/04/2018, 21:31:51
Traceback:
   File "src/gevent/greenlet.py", line 716, in gevent._greenlet.Greenlet.run
  File "/home/rakan/Code/Python/rotkehlchen/rotkehlchen/server.py", line 118, in _query_async
    result = getattr(self, command)(**kwargs)
  File "/home/rakan/Code/Python/rotkehlchen/rotkehlchen/server.py", line 231, in process_trade_history
    result, error_or_empty = self.rotkehlchen.process_history(start_ts, end_ts)
  File "/home/rakan/Code/Python/rotkehlchen/rotkehlchen/rotkehlchen.py", line 494, in process_history
    eth_transactions,
  File "/home/rakan/Code/Python/rotkehlchen/rotkehlchen/accounting/accountant.py", line 348, in process_history
    ) = self.process_action(action, end_ts, prev_time, count)
  File "/home/rakan/Code/Python/rotkehlchen/rotkehlchen/accounting/accountant.py", line 486, in process_action
    fee_in_profit_currency=self.get_fee_in_profit_currency(trade),
  File "/home/rakan/Code/Python/rotkehlchen/rotkehlchen/accounting/accountant.py", line 186, in get_fee_in_profit_currency
    trade.timestamp,
  File "/home/rakan/Code/Python/rotkehlchen/rotkehlchen/accounting/accountant.py", line 166, in query_historical_price
    price = self.price_historian.query_historical_price(from_asset, to_asset, timestamp)
  File "/home/rakan/Code/Python/rotkehlchen/rotkehlchen/history.py", line 510, in query_historical_price
    asset_btc_price = self.query_historical_price(from_asset, 'BTC', timestamp)
  File "/home/rakan/Code/Python/rotkehlchen/rotkehlchen/history.py", line 567, in query_historical_price
    tsToDate(timestamp, formatstr='%d/%m/%Y, %H:%M:%S'),
```

Potential solution is to fallback to requesting XRB's price before the time of the rebrand.

Rotkehlchen also logs debug information to the `rotkehlchen.log` file. Please attach it to the
issue as it may help us find the source of the issue faster.

### System Description

Running Rotkehlchen from source. Commit: c0b8d42fce8a8833014867db3df8487c5e7d15c3



Discussion:
As we discussed offline this is due to cryptocompare not having a price for the requested timestamp for `NANO`. It seems that `NANO` used to be `XRB` and that cryptocompare treats them as 2 different coins.

There is prices for a historical period for `XRB` and then when it rebranded there are only prices for `NANO` and not for `XRB`.

So the solution to this is to handle known rebrandings like this in a way that will guarantee a correct price will always be returned.
Was fixed by https://github.com/rotkehlchenio/rotkehlchen/pull/281. Thank you @rakanalh 

Resolving patch:
diff --git a/rotkehlchen/constants.py b/rotkehlchen/constants.py
--- a/rotkehlchen/constants.py
+++ b/rotkehlchen/constants.py
@@ -5,7 +5,6 @@
 
 ETH_DAO_FORK_TS = 1469020840  # 2016-07-20 13:20:40 UTC
 BTC_BCH_FORK_TS = 1501593374  # 2017-08-01 13:16:14 UTC
-XRB_NANO_REBRAND_TS = 1525132800  # 2018-05-01 2:00 AM UTC
 
 SUPPORTED_EXCHANGES = ['kraken', 'poloniex', 'bittrex', 'bitmex', 'binance']
 ROTKEHLCHEN_SERVER_TIMEOUT = 5
diff --git a/rotkehlchen/history.py b/rotkehlchen/history.py
--- a/rotkehlchen/history.py
+++ b/rotkehlchen/history.py
@@ -508,8 +508,8 @@ def query_historical_price(self, from_asset, to_asset, timestamp):
         if price == 0:
             if from_asset != 'BTC' and to_asset != 'BTC':
                 log.debug(
-                    f"Coudn't find historical price from {from_asset} to "
-                    f"{to_asset}. Comparing with BTC...",
+                    f"Couldn't find historical price from {from_asset} to "
+                    f"{to_asset} at timestamp {timestamp}. Comparing with BTC...",
                 )
                 # Just get the BTC price
                 asset_btc_price = self.query_historical_price(from_asset, 'BTC', timestamp)
@@ -517,8 +517,8 @@ def query_historical_price(self, from_asset, to_asset, timestamp):
                 price = asset_btc_price * btc_to_asset_price
             else:
                 log.debug(
-                    f"Coudn't find historical price from {from_asset} to "
-                    f"{to_asset}. Attempting to get daily price...",
+                    f"Couldn't find historical price from {from_asset} to "
+                    f"{to_asset} at timestamp {timestamp}. Attempting to get daily price...",
                 )
                 # attempt to get the daily price by timestamp
                 cc_from_asset = world_to_cryptocompare(from_asset, timestamp)
diff --git a/rotkehlchen/inquirer.py b/rotkehlchen/inquirer.py
--- a/rotkehlchen/inquirer.py
+++ b/rotkehlchen/inquirer.py
@@ -6,7 +6,6 @@
 from typing import Dict, Iterable, Optional, cast
 
 import requests
-
 from rotkehlchen.constants import (
     CURRENCYCONVERTER_API_KEY,
     FIAT_CURRENCIES,
@@ -18,7 +17,6 @@
     S_RAIBLOCKS,
     S_RDN,
     S_USD,
-    XRB_NANO_REBRAND_TS,
 )
 from rotkehlchen.errors import RemoteError
 from rotkehlchen.fval import FVal
@@ -57,9 +55,7 @@ def world_to_cryptocompare(asset: Asset, timestamp: Timestamp = None) -> Asset:
         asset = cast(NonEthTokenBlockchainAsset, 'BSV')
     elif asset == S_BQX:
         asset = cast(EthToken, 'ETHOS')
-    elif asset == S_NANO and timestamp and timestamp < XRB_NANO_REBRAND_TS:
-        return S_RAIBLOCKS
-    elif asset == S_RAIBLOCKS and timestamp and timestamp >= XRB_NANO_REBRAND_TS:
+    elif asset == S_RAIBLOCKS:
         return S_NANO
 
     return asset


Test patch: