Repository: rotki/rotki

Issue:
Balancer trades end up having the wrong rate
## Problem Definition

As noticed by @kelsos balancer swaps, when turned into a trade have the inverse rate. Swaps are always treated as buys. And in a buy the rate is defined as:

```
All pairs should be in the form of ``BASECURRENCY_QUOTECURRENCY``. For a ``buy`` this means you are buying ``amount`` of the ``BASE`` currency at a price of ``rate`` ``QUOTE`` currency per ``BASE``. 
```

But it seems the backend balancer code calculates the rate as the inverse of what it should be.

## Task

Fix it.


Discussion:

Resolving patch:
diff --git a/rotkehlchen/chain/ethereum/modules/balancer/utils.py b/rotkehlchen/chain/ethereum/modules/balancer/utils.py
--- a/rotkehlchen/chain/ethereum/modules/balancer/utils.py
+++ b/rotkehlchen/chain/ethereum/modules/balancer/utils.py
@@ -365,7 +365,7 @@ def calculate_trade_from_swaps(
         base_asset=swaps[-1].token1,
         quote_asset=swaps[0].token0,
         amount=swaps[-1].amount1_out,
-        rate=Price(swaps[-1].amount1_out / swaps[0].amount0_in),
+        rate=Price(swaps[0].amount0_in / swaps[-1].amount1_out),
         swaps=swaps,
         trade_index=trade_index,
     )


Test patch:
diff --git a/rotkehlchen/tests/api/test_balancer.py b/rotkehlchen/tests/api/test_balancer.py
--- a/rotkehlchen/tests/api/test_balancer.py
+++ b/rotkehlchen/tests/api/test_balancer.py
@@ -121,7 +121,7 @@ def test_get_balances(
         base_asset=EthereumToken('WETH'),
         quote_asset=EthereumToken('AAVE'),
         amount=AssetAmount(FVal('1.616934038985744521')),
-        rate=Price(FVal('0.1435961933076020882129179755')),
+        rate=Price(FVal('6.963972908793392530935439799')),
         trade_index=1,
         swaps=[
             AMMSwap(
@@ -146,7 +146,7 @@ def test_get_balances(
         base_asset=EthereumToken('AAVE'),
         quote_asset=EthereumToken('WETH'),
         amount=AssetAmount(FVal('11.260286362820602094')),
-        rate=Price(FVal('7.061804774312194764662462992')),
+        rate=Price(FVal('0.1416068599966922676173010716')),
         trade_index=0,
         swaps=[
             AMMSwap(
@@ -171,7 +171,7 @@ def test_get_balances(
         base_asset=EthereumToken('WETH'),
         quote_asset=EthereumToken('SYN'),
         amount=AssetAmount(FVal('1.352902561458047718')),
-        rate=Price(FVal('0.001380394982972144001321983066')),
+        rate=Price(FVal('724.4303350385182691258363763')),
         trade_index=0,
         swaps=[
             AMMSwap(
@@ -201,7 +201,7 @@ def test_get_balances(
             decimals=18,
         ),
         amount=AssetAmount(FVal('0.205709519074945018')),
-        rate=Price(FVal('0.004296621671385733368065627638')),
+        rate=Price(FVal('232.7409943164679514496089589')),
         trade_index=0,
         swaps=[
             AMMSwap(
@@ -231,7 +231,7 @@ def test_get_balances(
         base_asset=EthereumToken('API3'),
         quote_asset=EthereumToken('WETH'),
         amount=AssetAmount(FVal('295.881648100500428692')),
-        rate=Price(FVal('298.7939728237741971462915003')),
+        rate=Price(FVal('0.003346787723157288562491614498')),
         trade_index=0,
         swaps=[
             AMMSwap(
@@ -256,7 +256,7 @@ def test_get_balances(
         base_asset=EthereumToken('WETH'),
         quote_asset=EthereumToken('MFT'),
         amount=AssetAmount(FVal('0.686544199299304057')),
-        rate=Price(FVal('0.000004102142824540561502137803041')),
+        rate=Price(FVal('243775.0324093115004367119900')),
         trade_index=0,
         swaps=[
             AMMSwap(
@@ -281,7 +281,7 @@ def test_get_balances(
         base_asset=EthereumToken('WETH'),
         quote_asset=EthereumToken('AAVE'),
         amount=AssetAmount(FVal('3.055412574642681758')),
-        rate=Price(FVal('0.1445898203393074319511006560')),
+        rate=Price(FVal('6.916116208273240607778771150')),
         trade_index=1,
         swaps=[
             AMMSwap(
@@ -306,7 +306,7 @@ def test_get_balances(
         base_asset=EthereumToken('AAVE'),
         quote_asset=EthereumToken('WETH'),
         amount=AssetAmount(FVal('21.131588567541018817')),
-        rate=Price(FVal('6.967603293995613380604376603')),
+        rate=Price(FVal('0.1435213742524287826717337545')),
         trade_index=0,
         swaps=[
             AMMSwap(
diff --git a/rotkehlchen/tests/unit/test_balancer.py b/rotkehlchen/tests/unit/test_balancer.py
--- a/rotkehlchen/tests/unit/test_balancer.py
+++ b/rotkehlchen/tests/unit/test_balancer.py
@@ -115,7 +115,7 @@ def test_get_trades_from_tx_swaps_1():
             base_asset=EthereumToken('AMPL'),
             quote_asset=EthereumToken('USDC'),
             amount=AssetAmount(FVal('14285.153512382')),
-            rate=Price(FVal('1.151914744569445582155783413')),
+            rate=Price(FVal('0.8681198020203941533693340847')),
             trade_index=0,
             swaps=TEST_SWAPS_TX_1,
         ),
@@ -132,7 +132,7 @@ def test_get_trades_from_tx_swaps_2():
             base_asset=EthereumToken('WETH'),
             quote_asset=EthereumToken('USDC'),
             amount=AssetAmount(FVal('8.05955457343053505')),
-            rate=Price(FVal('0.0006498998936028010652666316885')),
+            rate=Price(FVal('1538.698513176199118305140044')),
             trade_index=0,
             swaps=TEST_SWAPS_TX_2,
         ),
@@ -149,7 +149,7 @@ def test_get_trades_from_tx_swaps_3():
             base_asset=EthereumToken('REN'),
             quote_asset=EthereumToken('WETH'),
             amount=AssetAmount(FVal('3197.944466059874991601')),
-            rate=Price(FVal('1459.315931843101752940385571')),
+            rate=Price(FVal('0.0006852525749766945789823189473')),
             trade_index=0,
             swaps=[TEST_SWAPS_TX_3[0]],
         ),
@@ -158,7 +158,7 @@ def test_get_trades_from_tx_swaps_3():
             base_asset=EthereumToken('SNX'),
             quote_asset=EthereumToken('REN'),
             amount=AssetAmount(FVal('186.643670114899291543')),
-            rate=Price(FVal('0.05836363704741385665599011674')),
+            rate=Price(FVal('17.13395618555459553468367999')),
             trade_index=1,
             swaps=[TEST_SWAPS_TX_3[1]],
         ),
@@ -167,7 +167,7 @@ def test_get_trades_from_tx_swaps_3():
             base_asset=EthereumToken('WETH'),
             quote_asset=EthereumToken('SNX'),
             amount=AssetAmount(FVal('2.283067259355947642')),
-            rate=Price(FVal('0.01223222441966811342243170654')),
+            rate=Price(FVal('81.75127970936394514208635428')),
             trade_index=2,
             swaps=[TEST_SWAPS_TX_3[2]],
         ),