Repository: rotki/rotki

Issue:
Crypto.com operation error
## Problem Definition

Some data in crypto.com importer causes a division error in the code. In the user case it was a amount of 0.0000003 ETH that
the CSV assigned 0 as EUR value

## Logs

```
[28/01/2022 01:22:58 GMT Standard Time] INFO rotkehlchen.api.server.pywsgi 127.0.0.1 - - "OPTIONS /api/1/import HTTP/1.1" 200 314 0.000000
[28/01/2022 01:22:58 GMT Standard Time] ERROR rotkehlchen.api.server Exception on /api/1/import [PUT]
Traceback (most recent call last):
  File "site-packages\flask\app.py", line 1516, in full_dispatch_request
  File "site-packages\flask\app.py", line 1502, in dispatch_request
  File "site-packages\flask_restful\__init__.py", line 467, in wrapper
  File "site-packages\flask\views.py", line 84, in view
  File "site-packages\flask_restful\__init__.py", line 582, in dispatch_request
  File "site-packages\webargs\core.py", line 450, in wrapper
  File "rotkehlchen\api\v1\resources.py", line 1223, in put
  File "rotkehlchen\api\rest.py", line 194, in wrapper
  File "rotkehlchen\api\rest.py", line 2083, in import_data
  File "rotkehlchen\data\importer.py", line 688, in import_cryptocom_csv
  File "rotkehlchen\data\importer.py", line 589, in _import_cryptocom_associated_entries
  File "rotkehlchen\fval.py", line 88, in __truediv__
decimal.InvalidOperation: [<class 'decimal.DivisionUndefined'>]

[28/01/2022 01:22:58 GMT Standard Time] INFO rotkehlchen.api.server.pywsgi 127.0.0.1 - - "PUT /api/1/import HTTP/1.1" 500 216 0.041305
```


### System Description
Operating system: 
Rotki version: 1.23



Discussion:

Resolving patch:
diff --git a/rotkehlchen/assets/asset.py b/rotkehlchen/assets/asset.py
--- a/rotkehlchen/assets/asset.py
+++ b/rotkehlchen/assets/asset.py
@@ -552,6 +552,7 @@ def deserialize_from_db(cls, entry: UnderlyingTokenDBTuple) -> 'UnderlyingToken'
 
 WORLD_TO_CRYPTOCOM = {
     strethaddress_to_identifier('0xBB0E17EF65F82Ab018d8EDd776e8DD940327B28b'): 'AXS',
+    'LUNA-2': 'LUNA',
 }
 
 # Create a generic variable that can be 'Asset', or any subclass.
diff --git a/rotkehlchen/data/importer.py b/rotkehlchen/data/importer.py
--- a/rotkehlchen/data/importer.py
+++ b/rotkehlchen/data/importer.py
@@ -249,6 +249,7 @@ def _consume_cryptocom_entry(self, csv_row: Dict[str, Any], **kwargs: Any) -> No
             'viban_purchase',
             'crypto_viban_exchange',
             'recurring_buy_order',
+            'card_top_up',
         ):
             # variable mapping to raw data
             currency = csv_row['Currency']
@@ -273,6 +274,11 @@ def _consume_cryptocom_entry(self, csv_row: Dict[str, Any], **kwargs: Any) -> No
                     raise DeserializationError('Got a trade entry with an empty quote asset')
                 base_amount_bought = deserialize_asset_amount(to_amount)
                 quote_amount_sold = deserialize_asset_amount(amount)
+            elif row_type == 'card_top_up':
+                quote_asset = asset_from_cryptocom(currency)
+                base_asset = asset_from_cryptocom(native_currency)
+                base_amount_bought = deserialize_asset_amount_force_positive(native_amount)
+                quote_amount_sold = deserialize_asset_amount_force_positive(amount)
             else:
                 base_asset = asset_from_cryptocom(currency)
                 quote_asset = asset_from_cryptocom(native_currency)
@@ -482,6 +488,7 @@ def _import_cryptocom_associated_entries(
         formatstr = kwargs.get('timestamp_format')
         timestamp_format = formatstr if formatstr is not None else '%Y-%m-%d %H:%M:%S'
         for row in data:
+            log.debug(f'Processing cryptocom row at {row["Timestamp (UTC)"]} and type {tx_kind}')
             # If we don't have the corresponding debited entry ignore them
             # and warn the user
             if (
@@ -571,8 +578,8 @@ def _import_cryptocom_associated_entries(
                     fee = Fee(ZERO)
                     fee_currency = A_USD
 
-                    base_asset = symbol_to_asset_or_token(credited_row['Currency'])
-                    quote_asset = symbol_to_asset_or_token(debited_row['Currency'])
+                    base_asset = asset_from_cryptocom(credited_row['Currency'])
+                    quote_asset = asset_from_cryptocom(debited_row['Currency'])
                     part_of_total = (
                         FVal(1)
                         if len(debited_rows) == 1
@@ -586,7 +593,11 @@ def _import_cryptocom_associated_entries(
                     base_amount_bought = deserialize_asset_amount(
                         credited_row['Amount'],
                     ) * part_of_total
-                    rate = Price(abs(quote_amount_sold / base_amount_bought))
+
+                    if base_amount_bought != ZERO:
+                        rate = Price(abs(quote_amount_sold / base_amount_bought))
+                    else:
+                        rate = Price(ZERO)
 
                     trade = Trade(
                         timestamp=timestamp,
@@ -606,7 +617,7 @@ def _import_cryptocom_associated_entries(
         # Compute investments profit
         if len(investments_withdrawals) != 0:
             for asset in investments_withdrawals:
-                asset_object = symbol_to_asset_or_token(asset)
+                asset_object = asset_from_cryptocom(asset)
                 if asset not in investments_deposits:
                     log.error(
                         f'Investment withdrawal without deposit at crypto.com. Ignoring '


Test patch:
diff --git a/rotkehlchen/tests/data/cryptocom_special_events.csv b/rotkehlchen/tests/data/cryptocom_special_events.csv
--- a/rotkehlchen/tests/data/cryptocom_special_events.csv
+++ b/rotkehlchen/tests/data/cryptocom_special_events.csv
@@ -12,4 +12,5 @@ Timestamp (UTC),Transaction Description,Currency,Amount,To Currency,To Amount,Na
 2021-01-01 22:00:00,Invest Deposit,BTC,-0.01,,,USD,-100,-100,invest_deposit
 2021-01-01 21:01:00,Invest Withdrawal,ETC,0.24,,,USD,100,100,invest_withdrawal
 2021-01-01 21:00:00,Invest Deposit,ETC,-1.0,,,USD,-100,-100,invest_deposit
-2021-10-28 03:16:37,Supercharger Reward,AXS,0.00356292,,,EUR,0.38,0.4430698990794,supercharger_reward_to_app_credited
\ No newline at end of file
+2021-10-28 03:16:37,Supercharger Reward,AXS,0.00356292,,,EUR,0.38,0.4430698990794,supercharger_reward_to_app_credited
+2021-10-28 03:16:38,USDC -> EUR,USDC,-12.94,,,EUR,-11.3,-12.64961995,card_top_up
\ No newline at end of file
diff --git a/rotkehlchen/tests/utils/dataimport.py b/rotkehlchen/tests/utils/dataimport.py
--- a/rotkehlchen/tests/utils/dataimport.py
+++ b/rotkehlchen/tests/utils/dataimport.py
@@ -531,6 +531,18 @@ def assert_cryptocom_special_events_import_results(rotki: Rotkehlchen):
         fee_currency=A_USD,
         link='',
         notes='MCO Earnings/Rewards Swap\nSource: crypto.com (CSV import)',
+    ), Trade(
+        timestamp=Timestamp(1635390998),
+        location=Location.CRYPTOCOM,
+        base_asset=symbol_to_asset_or_token('EUR'),
+        quote_asset=symbol_to_asset_or_token('USDC'),
+        trade_type=TradeType.BUY,
+        amount=AssetAmount(FVal('11.3')),
+        rate=Price(FVal('1.145132743362831858407079646')),
+        fee=Fee(ZERO),
+        fee_currency=A_USD,
+        link='',
+        notes='USDC -> EUR\nSource: crypto.com (CSV import)',
     )]
     assert trades == expected_trades