Repository: rotki/rotki

Issue:
Querying REP token of an account which holds exactly same amount of token from previous REP does not work.
## Problem Definition

Let's say we got an account in Rotki. This one https://etherscan.io/address/0xf70b08043373bf0e3bfa0bf98371066deb346dc2. Took it out of the top REP holders.

It has 33,000 REP but also 33,000 REP-old. In the same account. In order to get token balances we query alethio. The alethio query is this: https://api.aleth.io/v1/accounts/0xF70B08043373bf0e3bfa0bf98371066deb346Dc2/tokenBalances?page[limit]=100

and it returns the following:

```
{"data":[{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc20da345128eb3337e091942f4ff57a5dd199854bf","attributes":{"balance":"2000000000000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc20da345128eb3337e091942f4ff57a5dd199854bf/account"}},"token":{"data":{"type":"Token","id":"0x0da345128eb3337e091942f4ff57a5dd199854bf"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc20da345128eb3337e091942f4ff57a5dd199854bf/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc20da345128eb3337e091942f4ff57a5dd199854bf"}},{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc20e3129b3fde4a458b7910a2602e92ac533b9400e","attributes":{"balance":"9000000000000000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc20e3129b3fde4a458b7910a2602e92ac533b9400e/account"}},"token":{"data":{"type":"Token","id":"0x0e3129b3fde4a458b7910a2602e92ac533b9400e"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc20e3129b3fde4a458b7910a2602e92ac533b9400e/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc20e3129b3fde4a458b7910a2602e92ac533b9400e"}},{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc248c80f1f4d53d5951e5d5438b54cba84f29f32a5","attributes":{"balance":"33000000000000000000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc248c80f1f4d53d5951e5d5438b54cba84f29f32a5/account"}},"token":{"data":{"type":"Token","id":"0x48c80f1f4d53d5951e5d5438b54cba84f29f32a5"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc248c80f1f4d53d5951e5d5438b54cba84f29f32a5/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc248c80f1f4d53d5951e5d5438b54cba84f29f32a5"}},{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc24e0603e2a27a30480e5e3a4fe548e29ef12f64be","attributes":{"balance":"1000000000000000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc24e0603e2a27a30480e5e3a4fe548e29ef12f64be/account"}},"token":{"data":{"type":"Token","id":"0x4e0603e2a27a30480e5e3a4fe548e29ef12f64be"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc24e0603e2a27a30480e5e3a4fe548e29ef12f64be/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc24e0603e2a27a30480e5e3a4fe548e29ef12f64be"}},{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc25c406d99e04b8494dc253fcc52943ef82bca7d75","attributes":{"balance":"1000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc25c406d99e04b8494dc253fcc52943ef82bca7d75/account"}},"token":{"data":{"type":"Token","id":"0x5c406d99e04b8494dc253fcc52943ef82bca7d75"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc25c406d99e04b8494dc253fcc52943ef82bca7d75/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc25c406d99e04b8494dc253fcc52943ef82bca7d75"}},{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc26ff313fb38d53d7a458860b1bf7512f54a03e968","attributes":{"balance":"49950000000000000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc26ff313fb38d53d7a458860b1bf7512f54a03e968/account"}},"token":{"data":{"type":"Token","id":"0x6ff313fb38d53d7a458860b1bf7512f54a03e968"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc26ff313fb38d53d7a458860b1bf7512f54a03e968/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc26ff313fb38d53d7a458860b1bf7512f54a03e968"}},{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc275efc1111f98f2d5dcec9851c8abc77cd5e6ced8","attributes":{"balance":"223000000000000000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc275efc1111f98f2d5dcec9851c8abc77cd5e6ced8/account"}},"token":{"data":{"type":"Token","id":"0x75efc1111f98f2d5dcec9851c8abc77cd5e6ced8"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc275efc1111f98f2d5dcec9851c8abc77cd5e6ced8/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc275efc1111f98f2d5dcec9851c8abc77cd5e6ced8"}},{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc27d3e7d41da367b4fdce7cbe06502b13294deb758","attributes":{"balance":"1000000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc27d3e7d41da367b4fdce7cbe06502b13294deb758/account"}},"token":{"data":{"type":"Token","id":"0x7d3e7d41da367b4fdce7cbe06502b13294deb758"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc27d3e7d41da367b4fdce7cbe06502b13294deb758/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc27d3e7d41da367b4fdce7cbe06502b13294deb758"}},{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc28c4e7f814d40f8929f9112c5d09016f923d34472","attributes":{"balance":"200000000000000000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc28c4e7f814d40f8929f9112c5d09016f923d34472/account"}},"token":{"data":{"type":"Token","id":"0x8c4e7f814d40f8929f9112c5d09016f923d34472"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc28c4e7f814d40f8929f9112c5d09016f923d34472/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc28c4e7f814d40f8929f9112c5d09016f923d34472"}},{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2c1f5ba8bab3ca299f9817876a6715627f9e2b11a","attributes":{"balance":"20180614000000000000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2c1f5ba8bab3ca299f9817876a6715627f9e2b11a/account"}},"token":{"data":{"type":"Token","id":"0xc1f5ba8bab3ca299f9817876a6715627f9e2b11a"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2c1f5ba8bab3ca299f9817876a6715627f9e2b11a/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2c1f5ba8bab3ca299f9817876a6715627f9e2b11a"}},{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2cdb7ecfd3403eef3882c65b761ef9b5054890a47","attributes":{"balance":"3162500000000000000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2cdb7ecfd3403eef3882c65b761ef9b5054890a47/account"}},"token":{"data":{"type":"Token","id":"0xcdb7ecfd3403eef3882c65b761ef9b5054890a47"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2cdb7ecfd3403eef3882c65b761ef9b5054890a47/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2cdb7ecfd3403eef3882c65b761ef9b5054890a47"}},{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2dff3718cd0fda48e045bac6ad66950fa259f51b5","attributes":{"balance":"13000000000000000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2dff3718cd0fda48e045bac6ad66950fa259f51b5/account"}},"token":{"data":{"type":"Token","id":"0xdff3718cd0fda48e045bac6ad66950fa259f51b5"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2dff3718cd0fda48e045bac6ad66950fa259f51b5/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2dff3718cd0fda48e045bac6ad66950fa259f51b5"}},{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2e0b7927c4af23765cb51314a0e0521a9645f0e2a","attributes":{"balance":"0"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2e0b7927c4af23765cb51314a0e0521a9645f0e2a/account"}},"token":{"data":{"type":"Token","id":"0xe0b7927c4af23765cb51314a0e0521a9645f0e2a"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2e0b7927c4af23765cb51314a0e0521a9645f0e2a/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2e0b7927c4af23765cb51314a0e0521a9645f0e2a"}},{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2f18432ef894ef4b2a5726f933718f5a8cf9ff831","attributes":{"balance":"50000000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2f18432ef894ef4b2a5726f933718f5a8cf9ff831/account"}},"token":{"data":{"type":"Token","id":"0xf18432ef894ef4b2a5726f933718f5a8cf9ff831"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2f18432ef894ef4b2a5726f933718f5a8cf9ff831/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2f18432ef894ef4b2a5726f933718f5a8cf9ff831"}},{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2f3e014fe81267870624132ef3a646b8e83853a96","attributes":{"balance":"7770000000000000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2f3e014fe81267870624132ef3a646b8e83853a96/account"}},"token":{"data":{"type":"Token","id":"0xf3e014fe81267870624132ef3a646b8e83853a96"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2f3e014fe81267870624132ef3a646b8e83853a96/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2f3e014fe81267870624132ef3a646b8e83853a96"}},{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2f6276830c265a779a2225b9d2fcbab790cbeb92b","attributes":{"balance":"100000000000000000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2f6276830c265a779a2225b9d2fcbab790cbeb92b/account"}},"token":{"data":{"type":"Token","id":"0xf6276830c265a779a2225b9d2fcbab790cbeb92b"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2f6276830c265a779a2225b9d2fcbab790cbeb92b/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc2f6276830c265a779a2225b9d2fcbab790cbeb92b"}}],"links":{"next":"https://api.aleth.io/v1/token-balances?filter%5Baccount%5D=0xf70b08043373bf0e3bfa0bf98371066deb346dc2\u0026page%5Blimit%5D=100\u0026page%5Bnext%5D=0xf70b08043373bf0e3bfa0bf98371066deb346dc2f6276830c265a779a2225b9d2fcbab790cbeb92b","prev":"https://api.aleth.io/v1/token-balances?filter%5Baccount%5D=0xf70b08043373bf0e3bfa0bf98371066deb346dc2\u0026page%5Blimit%5D=100\u0026page%5Bprev%5D=0xf70b08043373bf0e3bfa0bf98371066deb346dc20da345128eb3337e091942f4ff57a5dd199854bf"},"meta":{"confirmedBlock":{"number":9895359,"blockCreationTime":1587197935,"blockHash":"0x93ff3e95774c386aa6275e3f38f12a8d96b71fe561de9aacc1b95462f91e88fb"},"count":16,"latestBlock":{"number":9895359,"blockCreationTime":1587197935,"blockHash":"0x93ff3e95774c386aa6275e3f38f12a8d96b71fe561de9aacc1b95462f91e88fb"},"page":{"hasNext":false,"hasPrev":false},"query":{"block":{"data":{"id":"0x1503e691b06a276d2d5af117e0d59063d9f3abf363b86ec8704dc4b0239288b8","type":"Block"},"links":{"self":"https://api.aleth.io/v1/blocks/0x1503e691b06a276d2d5af117e0d59063d9f3abf363b86ec8704dc4b0239288b8"}}}}}
```

You will notice that it contains an entry for the old REP:

```
{"type":"TokenBalance","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc248c80f1f4d53d5951e5d5438b54cba84f29f32a5","attributes":{"balance":"33000000000000000000000"},"relationships":{"account":{"data":{"type":"Account","id":"0xf70b08043373bf0e3bfa0bf98371066deb346dc2"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc248c80f1f4d53d5951e5d5438b54cba84f29f32a5/account"}},"token":{"data":{"type":"Token","id":"0x48c80f1f4d53d5951e5d5438b54cba84f29f32a5"},"links":{"related":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc248c80f1f4d53d5951e5d5438b54cba84f29f32a5/token"}}},"links":{"self":"https://api.aleth.io/v1/token-balances/0xf70b08043373bf0e3bfa0bf98371066deb346dc248c80f1f4d53d5951e5d5438b54cba84f29f32a5"}},
```

with contract address `0x48c80f1f4d53d5951e5d5438b54cba84f29f32a5`.

But there is no entry in the response for the actual new REP `0x1985365e9f78359a9B6AD760e32412f4a445E862` even though the address holds it.

This seems to be a bug in Alethio querying.

It's quite possible that this problem generalizes to any contract with the same symbol that has had its address upgraded.

## Task

1. Wait for them to fix it
2. Possibly add a temporary rule that if an account is shown to have the old REP token to make a token balance query for the new one.


Discussion:

Resolving patch:
diff --git a/rotkehlchen/chain/manager.py b/rotkehlchen/chain/manager.py
--- a/rotkehlchen/chain/manager.py
+++ b/rotkehlchen/chain/manager.py
@@ -10,7 +10,7 @@
 
 from rotkehlchen.assets.asset import Asset, EthereumToken
 from rotkehlchen.chain.ethereum.makerdao import MakerDAO
-from rotkehlchen.constants.assets import A_BTC, A_DAI, A_ETH
+from rotkehlchen.constants.assets import A_BTC, A_DAI, A_ETH, A_REP
 from rotkehlchen.constants.misc import ZERO
 from rotkehlchen.db.utils import BlockchainAccounts
 from rotkehlchen.errors import EthSyncError, InputError, RemoteError, UnableToDecryptRemoteData
@@ -647,6 +647,16 @@ def _query_ethereum_tokens_alethio(
         for account in accounts:
             balances = self.alethio.get_token_balances(account)
             for token, balance in balances.items():
+                if token.identifier == 'REP-old':
+                    # Handle the special case for the Alethio old REP bug
+                    # https://github.com/rotki/rotki/issues/899
+                    balance = ChainManager._query_token_balances(
+                        token_asset=A_REP,
+                        query_callback=self.ethereum.get_token_balance,
+                        argument=account,
+                    )
+                    token = A_REP
+
                 if balance != ZERO:
                     try:
                         usd_price = token_usd_price.get(
diff --git a/rotkehlchen/constants/assets.py b/rotkehlchen/constants/assets.py
--- a/rotkehlchen/constants/assets.py
+++ b/rotkehlchen/constants/assets.py
@@ -21,5 +21,6 @@
 A_BSV = Asset('BSV')
 A_ETH = Asset(S_ETH)
 A_ETC = Asset('ETC')
+A_REP = EthereumToken('REP')
 A_DAI = EthereumToken('DAI')
 A_SAI = EthereumToken('SAI')


Test patch: