From a213d103ac54368d7f0f318e136634b3edd92125 Mon Sep 17 00:00:00 2001
From: David Hintermann <David.Hintermann@ost.ch>
Date: Mon, 2 Dec 2024 16:39:10 +0000
Subject: [PATCH] wip: add automatic rebalancing

---
 .../BackgroundServices/RebalanceService.cs    |   2 +-
 Frontend/PoolV3Client.cs                      |   3 +-
 grafana/dashboards/totalSupply.json           | 178 ++++++++++++++----
 3 files changed, 141 insertions(+), 42 deletions(-)

diff --git a/Frontend/BackgroundServices/RebalanceService.cs b/Frontend/BackgroundServices/RebalanceService.cs
index 408579c..d914a60 100644
--- a/Frontend/BackgroundServices/RebalanceService.cs
+++ b/Frontend/BackgroundServices/RebalanceService.cs
@@ -16,7 +16,7 @@ public class RebalanceService : BackgroundService
 
     public RebalanceService(
         Rebalancer rebalancer,
-        Logger<RebalanceService> logger
+        ILogger<RebalanceService> logger
     )
     {
         _logger = logger;
diff --git a/Frontend/PoolV3Client.cs b/Frontend/PoolV3Client.cs
index d3b8a84..64019bb 100644
--- a/Frontend/PoolV3Client.cs
+++ b/Frontend/PoolV3Client.cs
@@ -165,7 +165,7 @@ public class PoolV3Client
     /*
     this function should swap the tokens in the pool
     */
-    public async Task<decimal> SwapAsync(string fromTokenAddress, string toTokenAddress, decimal amount, decimal amountOutMinimum = 0, uint sqrtPriceLimitX96 = 0, bool doApproval = true)
+    public async Task SwapAsync(string fromTokenAddress, string toTokenAddress, decimal amount, decimal amountOutMinimum = 0, uint sqrtPriceLimitX96 = 0, bool doApproval = true)
     {
 
         if (doApproval)
@@ -200,7 +200,6 @@ public class PoolV3Client
             SqrtPriceLimitX96 = sqrtPriceLimitX96,
         };
         var exactInputSingleFunctionTxnReceipt = await contractHandler.SendRequestAndWaitForReceiptAsync(exactInputSingleFunction);
-        return 0.0m;
     }
 
     /// <summary>
diff --git a/grafana/dashboards/totalSupply.json b/grafana/dashboards/totalSupply.json
index efdbdd1..df2ac7f 100644
--- a/grafana/dashboards/totalSupply.json
+++ b/grafana/dashboards/totalSupply.json
@@ -241,7 +241,6 @@
         "type": "prometheus",
         "uid": "PBFA97CFB590B2093"
       },
-      "description": "averaged over 2 minutes",
       "fieldConfig": {
         "defaults": {
           "color": {
@@ -249,7 +248,7 @@
           },
           "custom": {
             "axisBorderShow": false,
-            "axisCenteredZero": false,
+            "axisCenteredZero": true,
             "axisColorMode": "text",
             "axisLabel": "",
             "axisPlacement": "auto",
@@ -280,18 +279,13 @@
               "mode": "off"
             }
           },
-          "decimals": 8,
           "mappings": [],
           "thresholds": {
-            "mode": "absolute",
+            "mode": "percentage",
             "steps": [
               {
                 "color": "green",
                 "value": null
-              },
-              {
-                "color": "red",
-                "value": 80
               }
             ]
           }
@@ -299,12 +293,12 @@
         "overrides": []
       },
       "gridPos": {
-        "h": 14,
+        "h": 8,
         "w": 12,
         "x": 0,
         "y": 8
       },
-      "id": 9,
+      "id": 12,
       "options": {
         "legend": {
           "calcs": [],
@@ -321,13 +315,26 @@
       "targets": [
         {
           "editorMode": "code",
-          "expr": "avg_over_time(Marketplace_Potato_Price_ETH[2m])",
-          "legendFormat": "__auto",
+          "expr": "Marketplace_Potato_Price_ETH{} * 1/Uniswap_ROCWETH_price_WETH_per_ROC-1",
+          "legendFormat": "current",
           "range": true,
           "refId": "A"
+        },
+        {
+          "datasource": {
+            "type": "prometheus",
+            "uid": "PBFA97CFB590B2093"
+          },
+          "editorMode": "code",
+          "expr": "avg_over_time(Marketplace_Potato_Price_ETH{}[2m]) * 1/avg_over_time(Uniswap_ROCWETH_price_WETH_per_ROC[2m])-1",
+          "hide": false,
+          "instant": false,
+          "legendFormat": "avg over 2min",
+          "range": true,
+          "refId": "B"
         }
       ],
-      "title": "Price of 1kg Potato [ETH]",
+      "title": "ROC Price [kg Potato]",
       "type": "timeseries"
     },
     {
@@ -441,7 +448,6 @@
         }
       ],
       "title": "Total Supply",
-      "transparent": true,
       "type": "timeseries"
     },
     {
@@ -718,6 +724,7 @@
         "type": "prometheus",
         "uid": "PBFA97CFB590B2093"
       },
+      "description": "averaged over 2 minutes",
       "fieldConfig": {
         "defaults": {
           "color": {
@@ -756,6 +763,7 @@
               "mode": "off"
             }
           },
+          "decimals": 8,
           "mappings": [],
           "thresholds": {
             "mode": "absolute",
@@ -774,12 +782,104 @@
         "overrides": []
       },
       "gridPos": {
-        "h": 8,
+        "h": 14,
         "w": 12,
         "x": 0,
+        "y": 16
+      },
+      "id": 9,
+      "options": {
+        "legend": {
+          "calcs": [],
+          "displayMode": "list",
+          "placement": "bottom",
+          "showLegend": true
+        },
+        "tooltip": {
+          "mode": "single",
+          "sort": "none"
+        }
+      },
+      "pluginVersion": "11.3.0+security-01",
+      "targets": [
+        {
+          "editorMode": "code",
+          "expr": "avg_over_time(Marketplace_Potato_Price_ETH[2m])",
+          "legendFormat": "__auto",
+          "range": true,
+          "refId": "A"
+        }
+      ],
+      "title": "Price of 1kg Potato [ETH]",
+      "type": "timeseries"
+    },
+    {
+      "datasource": {
+        "type": "prometheus",
+        "uid": "PBFA97CFB590B2093"
+      },
+      "fieldConfig": {
+        "defaults": {
+          "color": {
+            "mode": "palette-classic"
+          },
+          "custom": {
+            "axisBorderShow": false,
+            "axisCenteredZero": false,
+            "axisColorMode": "text",
+            "axisLabel": "",
+            "axisPlacement": "auto",
+            "barAlignment": 0,
+            "barWidthFactor": 0.6,
+            "drawStyle": "line",
+            "fillOpacity": 0,
+            "gradientMode": "none",
+            "hideFrom": {
+              "legend": false,
+              "tooltip": false,
+              "viz": false
+            },
+            "insertNulls": false,
+            "lineInterpolation": "linear",
+            "lineWidth": 1,
+            "pointSize": 5,
+            "scaleDistribution": {
+              "type": "linear"
+            },
+            "showPoints": "auto",
+            "spanNulls": false,
+            "stacking": {
+              "group": "A",
+              "mode": "none"
+            },
+            "thresholdsStyle": {
+              "mode": "off"
+            }
+          },
+          "mappings": [],
+          "thresholds": {
+            "mode": "absolute",
+            "steps": [
+              {
+                "color": "green",
+                "value": null
+              },
+              {
+                "color": "red",
+                "value": 80
+              }
+            ]
+          }
+        },
+        "overrides": []
+      },
+      "gridPos": {
+        "h": 8,
+        "w": 12,
+        "x": 12,
         "y": 22
       },
-      "id": 4,
+      "id": 8,
       "options": {
         "legend": {
           "calcs": [],
@@ -795,15 +895,11 @@
       "pluginVersion": "11.3.0+security-01",
       "targets": [
         {
-          "disableTextWrap": false,
           "editorMode": "code",
-          "expr": "Marketplace_Ethereum_Price_CHF * Uniswap_ROCWETH_price_WETH_per_ROC",
-          "fullMetaSearch": false,
-          "includeNullMetadata": true,
-          "legendFormat": "price",
+          "expr": "avg_over_time(Marketplace_Ethereum_Price_CHF[5m])",
+          "legendFormat": "ETH in CHF averaged 5min",
           "range": true,
-          "refId": "A",
-          "useBackend": false
+          "refId": "A"
         },
         {
           "datasource": {
@@ -811,10 +907,10 @@
             "uid": "PBFA97CFB590B2093"
           },
           "editorMode": "code",
-          "expr": "avg_over_time(Marketplace_Ethereum_Price_CHF[5m]) * avg_over_time(Uniswap_ROCWETH_price_WETH_per_ROC[5m])",
+          "expr": "avg_over_time(Marketplace_Ethereum_Price_CHF[2m])",
           "hide": false,
           "instant": false,
-          "legendFormat": "Price average over 5 min",
+          "legendFormat": "ETH in CHF averaged 2min",
           "range": true,
           "refId": "B"
         },
@@ -824,15 +920,15 @@
             "uid": "PBFA97CFB590B2093"
           },
           "editorMode": "code",
-          "expr": "avg_over_time(Marketplace_Ethereum_Price_CHF[2m]) * avg_over_time(Uniswap_ROCWETH_price_WETH_per_ROC[2m])",
+          "expr": "Marketplace_Ethereum_Price_CHF",
           "hide": false,
           "instant": false,
-          "legendFormat": "Price average over 2 min",
+          "legendFormat": "ETH in CHF",
           "range": true,
           "refId": "C"
         }
       ],
-      "title": "ROC Price in CHF",
+      "title": "ETH price in CHF",
       "type": "timeseries"
     },
     {
@@ -898,10 +994,10 @@
       "gridPos": {
         "h": 8,
         "w": 12,
-        "x": 12,
-        "y": 22
+        "x": 0,
+        "y": 30
       },
-      "id": 8,
+      "id": 4,
       "options": {
         "legend": {
           "calcs": [],
@@ -917,11 +1013,15 @@
       "pluginVersion": "11.3.0+security-01",
       "targets": [
         {
+          "disableTextWrap": false,
           "editorMode": "code",
-          "expr": "avg_over_time(Marketplace_Ethereum_Price_CHF[5m])",
-          "legendFormat": "ETH in CHF averaged 5min",
+          "expr": "Marketplace_Ethereum_Price_CHF * Uniswap_ROCWETH_price_WETH_per_ROC",
+          "fullMetaSearch": false,
+          "includeNullMetadata": true,
+          "legendFormat": "price",
           "range": true,
-          "refId": "A"
+          "refId": "A",
+          "useBackend": false
         },
         {
           "datasource": {
@@ -929,10 +1029,10 @@
             "uid": "PBFA97CFB590B2093"
           },
           "editorMode": "code",
-          "expr": "avg_over_time(Marketplace_Ethereum_Price_CHF[2m])",
+          "expr": "avg_over_time(Marketplace_Ethereum_Price_CHF[5m]) * avg_over_time(Uniswap_ROCWETH_price_WETH_per_ROC[5m])",
           "hide": false,
           "instant": false,
-          "legendFormat": "ETH in CHF averaged 2min",
+          "legendFormat": "Price average over 5 min",
           "range": true,
           "refId": "B"
         },
@@ -942,15 +1042,15 @@
             "uid": "PBFA97CFB590B2093"
           },
           "editorMode": "code",
-          "expr": "Marketplace_Ethereum_Price_CHF",
+          "expr": "avg_over_time(Marketplace_Ethereum_Price_CHF[2m]) * avg_over_time(Uniswap_ROCWETH_price_WETH_per_ROC[2m])",
           "hide": false,
           "instant": false,
-          "legendFormat": "ETH in CHF",
+          "legendFormat": "Price average over 2 min",
           "range": true,
           "refId": "C"
         }
       ],
-      "title": "ETH price in CHF",
+      "title": "ROC Price in CHF",
       "type": "timeseries"
     }
   ],
-- 
GitLab