Repository: rotki/rotki

Issue:
Selling FIAT for Crypto gives warning for documented acquisitions
## Problem Definition

When users buy crypto with FIAT we don't follow through time to see where/when the FIAT was acquired as this is the one thing we don't need to do.

But apparently if a trade is a sell of FIAT for crypto (essentially exactly the same thing as buying crypto with fiat) then rotki gets confused and tries to trace the origin of the sold fiat. Example error:

```
No documented acquisition found for EUR(Euro) before 02/02/2021 18:18:09. Let rotki know how you acquired it via a ledger action
```

It's easy to reproduce by making a manual trade that's selling fiat for crypto.

## Task

Fix it. Probably by detecting such sells and treating them same way as fiat buys of crypto as far as acquisition basis is concerned.


Discussion:

Resolving patch:
diff --git a/rotkehlchen/accounting/events.py b/rotkehlchen/accounting/events.py
--- a/rotkehlchen/accounting/events.py
+++ b/rotkehlchen/accounting/events.py
@@ -480,6 +480,15 @@ def add_sell(
             )
             return
 
+        if selling_asset.is_fiat():
+            # Should be handled by a virtual buy
+            logger.debug(
+                f'Skipping sell trade of {selling_asset.identifier} for '
+                f'{receiving_asset.identifier if receiving_asset else "nothing"} at {timestamp} '
+                f'since selling of FIAT of something will just be treated as a buy.',
+            )
+            return
+
         logger.debug(
             f'Processing sell trade of {selling_asset.identifier} for '
             f'{receiving_asset.identifier if receiving_asset else "nothing"} at {timestamp}',


Test patch:
diff --git a/rotkehlchen/tests/unit/test_accounting.py b/rotkehlchen/tests/unit/test_accounting.py
--- a/rotkehlchen/tests/unit/test_accounting.py
+++ b/rotkehlchen/tests/unit/test_accounting.py
@@ -797,3 +797,56 @@ def test_defi_event_zero_amount(accountant):
         'time': 1467279735,
         'type': 'defi_event',
     }
+
+
+@pytest.mark.parametrize('mocked_price_queries', [prices])
+def test_sell_fiat_for_crypto(accountant):
+    """
+    Test for https://github.com/rotki/rotki/issues/2993
+    Make sure that selling fiat for crypto does not give warnings due to
+    inability to trace the source of the sold fiat.
+    """
+    history = [{
+        'timestamp': 1476979735,
+        'base_asset': 'EUR',
+        'quote_asset': 'BTC',
+        'trade_type': 'sell',
+        'rate': 0.002,
+        'fee': 0.0012,
+        'fee_currency': 'EUR',
+        'amount': 2000,
+        'location': 'kraken',
+    }, {
+        'timestamp': 1496979735,
+        'base_asset': 'CHF',
+        'quote_asset': 'ETH',
+        'trade_type': 'sell',
+        'rate': 0.004,
+        'fee': 0.02,
+        'fee_currency': 'EUR',
+        'amount': 500,
+        'location': 'kraken',
+    }, {
+        'timestamp': 1506979735,
+        'base_asset': 'ETH',
+        'quote_asset': 'EUR',
+        'trade_type': 'sell',
+        'rate': 25000,
+        'fee': 0.02,
+        'fee_currency': 'EUR',
+        'amount': 1,
+        'location': 'kraken',
+    }]
+    result = accounting_history_process(
+        accountant,
+        1436979735,
+        1519693374,
+        history,
+    )
+    assert FVal(result['overview']['total_profit_loss']) == FVal(25000 - 0.02 - 250 * 1.001 - 0.01)
+    assert accountant.events.cost_basis.get_calculated_asset_amount(A_ETH) == FVal(1)
+    assert accountant.events.cost_basis.get_calculated_asset_amount(A_BTC) == FVal(4)
+    warnings = accountant.msg_aggregator.consume_warnings()
+    assert len(warnings) == 0
+    errors = accountant.msg_aggregator.consume_errors()
+    assert len(errors) == 0
diff --git a/rotkehlchen/tests/utils/history.py b/rotkehlchen/tests/utils/history.py
--- a/rotkehlchen/tests/utils/history.py
+++ b/rotkehlchen/tests/utils/history.py
@@ -114,6 +114,7 @@
             1491062063: FVal(47.865),
             1493291104: FVal(53.175),
             1493737200: FVal(69.505),
+            1496979735: FVal(251.36),
             1501062063: FVal(175.44),
             1511626623: FVal(396.56),
             1512561941: FVal(380.34),
@@ -128,6 +129,11 @@
             1512561942: ZERO,
         },
     },
+    'CHF': {
+        'EUR': {
+            1496979735: FVal(1.001),
+        },
+    },
     'LTC': {
         'EUR': {
             1493650800: FVal(14.07),