From 4b2e907347a35f1825d5ee497a24b83a78ad22bb Mon Sep 17 00:00:00 2001
From: David Hintermann <David.Hintermann@ost.ch>
Date: Mon, 2 Dec 2024 10:35:43 +0000
Subject: [PATCH] wip: tested mint and burn according to supply

---
 Frontend/Pages/Index.cshtml.cs      |   6 +
 Frontend/Program.cs                 |   7 +
 Frontend/Rebalancer.cs              |   1 +
 grafana/dashboards/totalSupply.json | 518 +++++++++++++++++++++++++---
 4 files changed, 489 insertions(+), 43 deletions(-)

diff --git a/Frontend/Pages/Index.cshtml.cs b/Frontend/Pages/Index.cshtml.cs
index 9bf2ad5..6e2cbe2 100644
--- a/Frontend/Pages/Index.cshtml.cs
+++ b/Frontend/Pages/Index.cshtml.cs
@@ -51,5 +51,11 @@ public class IndexModel : PageModel
         if(Request.Query.ContainsKey("balanceReserves")){
             _rebalancer.MaintainLiquidReservesAsync().Wait();
         }
+        if(Request.Query.ContainsKey("testDebgger")){
+            var breaked = true;
+        }
+        if(Request.Query.ContainsKey("ensureRWA")){
+            _rebalancer.EnsureCoinSupplyMatchesRwaSupplyAsync().Wait();
+        }
     }
 }
diff --git a/Frontend/Program.cs b/Frontend/Program.cs
index de5357c..2699c93 100644
--- a/Frontend/Program.cs
+++ b/Frontend/Program.cs
@@ -89,6 +89,13 @@ builder.Services.AddOpenTelemetry()
                          "RoestiCoin");
     });
 
+builder.Services.AddSingleton<PotatoStorage>(sp =>
+{
+    var factory = sp.GetRequiredService<IDbContextFactory<PotatoStorage>>();
+    return factory.CreateDbContext();
+});
+builder.Services.AddSingleton<Rebalancer>();
+
 var app = builder.Build();
 
 var potatoStorageFactory = app.Services.GetRequiredService<IDbContextFactory<PotatoStorage>>();
diff --git a/Frontend/Rebalancer.cs b/Frontend/Rebalancer.cs
index 3cb3595..751afc0 100644
--- a/Frontend/Rebalancer.cs
+++ b/Frontend/Rebalancer.cs
@@ -42,6 +42,7 @@ public class Rebalancer
         _web3 = web3;
         _prometheusClient = prometheusClient;
         _pool = new(web3, chainSettings, account);
+        _account = account;
         _potatoMarket = potatoMarket;
         _logger = logger;
         RebalanceTradeExecutedCount = 0;
diff --git a/grafana/dashboards/totalSupply.json b/grafana/dashboards/totalSupply.json
index 9809b18..f289337 100644
--- a/grafana/dashboards/totalSupply.json
+++ b/grafana/dashboards/totalSupply.json
@@ -87,7 +87,7 @@
         "x": 0,
         "y": 0
       },
-      "id": 4,
+      "id": 6,
       "options": {
         "legend": {
           "calcs": [],
@@ -103,15 +103,11 @@
       "pluginVersion": "11.3.0+security-01",
       "targets": [
         {
-          "disableTextWrap": false,
           "editorMode": "code",
-          "expr": "Ethereum_Price_Current_CHF * Uniswap_ROCWETH_price_WETH_per_ROC",
-          "fullMetaSearch": false,
-          "includeNullMetadata": true,
-          "legendFormat": "price",
+          "expr": "Marketplace_Potato_Price_ETH{} * 1/Uniswap_ROCWETH_price_WETH_per_ROC",
+          "legendFormat": "current",
           "range": true,
-          "refId": "A",
-          "useBackend": false
+          "refId": "A"
         },
         {
           "datasource": {
@@ -119,28 +115,15 @@
             "uid": "PBFA97CFB590B2093"
           },
           "editorMode": "code",
-          "expr": "avg_over_time(Ethereum_Price_Current_CHF[5m]) * avg_over_time(Uniswap_ROCWETH_price_WETH_per_ROC[5m])",
+          "expr": "avg_over_time(Marketplace_Potato_Price_ETH{}[2m]) * 1/avg_over_time(Uniswap_ROCWETH_price_WETH_per_ROC[2m])",
           "hide": false,
           "instant": false,
-          "legendFormat": "Price average over 5 min",
+          "legendFormat": "avg over 2min",
           "range": true,
           "refId": "B"
-        },
-        {
-          "datasource": {
-            "type": "prometheus",
-            "uid": "PBFA97CFB590B2093"
-          },
-          "editorMode": "code",
-          "expr": "avg_over_time(Ethereum_Price_Current_CHF[2m]) * avg_over_time(Uniswap_ROCWETH_price_WETH_per_ROC[2m])",
-          "hide": false,
-          "instant": false,
-          "legendFormat": "Price average over 2 min",
-          "range": true,
-          "refId": "C"
         }
       ],
-      "title": "ROC Price in CHF",
+      "title": "ROC Price [kg Potato]",
       "type": "timeseries"
     },
     {
@@ -230,13 +213,26 @@
           "expr": "Uniswap_ROCWETH_price_WETH_per_ROC",
           "fullMetaSearch": false,
           "includeNullMetadata": true,
-          "legendFormat": "__auto",
+          "legendFormat": "current price",
           "range": true,
           "refId": "A",
           "useBackend": false
+        },
+        {
+          "datasource": {
+            "type": "prometheus",
+            "uid": "PBFA97CFB590B2093"
+          },
+          "editorMode": "code",
+          "expr": "Marketplace_Potato_Price_ETH",
+          "hide": false,
+          "instant": false,
+          "legendFormat": "Desired Price",
+          "range": true,
+          "refId": "B"
         }
       ],
-      "title": "WETH ROC",
+      "title": "WETH per ROC in Pool",
       "type": "timeseries"
     },
     {
@@ -244,6 +240,7 @@
         "type": "prometheus",
         "uid": "PBFA97CFB590B2093"
       },
+      "description": "averaged over 2 minutes",
       "fieldConfig": {
         "defaults": {
           "color": {
@@ -282,6 +279,7 @@
               "mode": "off"
             }
           },
+          "decimals": 8,
           "mappings": [],
           "thresholds": {
             "mode": "absolute",
@@ -300,18 +298,18 @@
         "overrides": []
       },
       "gridPos": {
-        "h": 8,
+        "h": 14,
         "w": 12,
         "x": 0,
         "y": 8
       },
-      "id": 2,
+      "id": 9,
       "options": {
         "legend": {
           "calcs": [],
           "displayMode": "list",
           "placement": "bottom",
-          "showLegend": false
+          "showLegend": true
         },
         "tooltip": {
           "mode": "single",
@@ -321,19 +319,14 @@
       "pluginVersion": "11.3.0+security-01",
       "targets": [
         {
-          "disableTextWrap": false,
-          "editorMode": "builder",
-          "expr": "Ethereum_Price_Current_CHF",
-          "fullMetaSearch": false,
-          "includeNullMetadata": true,
+          "editorMode": "code",
+          "expr": "avg_over_time(Marketplace_Potato_Price_ETH[2m])",
           "legendFormat": "__auto",
           "range": true,
-          "refId": "A",
-          "useBackend": false
+          "refId": "A"
         }
       ],
-      "title": "CHF/ETH course",
-      "transparent": true,
+      "title": "Price of 1kg Potato [ETH]",
       "type": "timeseries"
     },
     {
@@ -397,8 +390,8 @@
         "overrides": []
       },
       "gridPos": {
-        "h": 8,
-        "w": 12,
+        "h": 7,
+        "w": 6,
         "x": 12,
         "y": 8
       },
@@ -427,15 +420,454 @@
           "expr": "RoestiCoin_Supply_Total_ROC",
           "fullMetaSearch": false,
           "includeNullMetadata": true,
-          "legendFormat": "__auto",
+          "legendFormat": "Total Supply of ROC",
           "range": true,
           "refId": "A",
           "useBackend": false
+        },
+        {
+          "datasource": {
+            "type": "prometheus",
+            "uid": "PBFA97CFB590B2093"
+          },
+          "editorMode": "code",
+          "expr": "Storage_Potato_Supply_Weight_kg",
+          "hide": false,
+          "instant": false,
+          "legendFormat": "Potatos in Stock [kg]",
+          "range": true,
+          "refId": "B"
         }
       ],
-      "title": "Total Supply (ROC)",
+      "title": "Total Supply",
       "transparent": true,
       "type": "timeseries"
+    },
+    {
+      "datasource": {
+        "type": "prometheus",
+        "uid": "PBFA97CFB590B2093"
+      },
+      "description": "How many kilograms of Potato we are holding per ROC. This should be always at 1kg/ROC",
+      "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": 7,
+        "w": 6,
+        "x": 18,
+        "y": 8
+      },
+      "id": 7,
+      "options": {
+        "legend": {
+          "calcs": [],
+          "displayMode": "list",
+          "placement": "bottom",
+          "showLegend": true
+        },
+        "tooltip": {
+          "mode": "single",
+          "sort": "none"
+        }
+      },
+      "pluginVersion": "11.3.0+security-01",
+      "targets": [
+        {
+          "editorMode": "code",
+          "expr": "Storage_Potato_Supply_Weight_kg / RoestiCoin_Supply_Total_ROC",
+          "legendFormat": "__auto",
+          "range": true,
+          "refId": "A"
+        }
+      ],
+      "title": "Available Potatos [kg] per ROC",
+      "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
+              }
+            ]
+          },
+          "unit": "percentunit"
+        },
+        "overrides": []
+      },
+      "gridPos": {
+        "h": 7,
+        "w": 6,
+        "x": 18,
+        "y": 15
+      },
+      "id": 10,
+      "options": {
+        "legend": {
+          "calcs": [],
+          "displayMode": "list",
+          "placement": "bottom",
+          "showLegend": true
+        },
+        "tooltip": {
+          "mode": "single",
+          "sort": "none"
+        }
+      },
+      "pluginVersion": "11.3.0+security-01",
+      "targets": [
+        {
+          "editorMode": "code",
+          "expr": "Storage_Potato_Supply_Weight_kg/RoestiCoin_Supply_Total_ROC",
+          "legendFormat": "backing",
+          "range": true,
+          "refId": "A"
+        }
+      ],
+      "title": "Potato [kg]/ROC",
+      "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": 0,
+        "y": 22
+      },
+      "id": 4,
+      "options": {
+        "legend": {
+          "calcs": [],
+          "displayMode": "list",
+          "placement": "bottom",
+          "showLegend": true
+        },
+        "tooltip": {
+          "mode": "single",
+          "sort": "none"
+        }
+      },
+      "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",
+          "range": true,
+          "refId": "A",
+          "useBackend": false
+        },
+        {
+          "datasource": {
+            "type": "prometheus",
+            "uid": "PBFA97CFB590B2093"
+          },
+          "editorMode": "code",
+          "expr": "avg_over_time(Marketplace_Ethereum_Price_CHF[5m]) * avg_over_time(Uniswap_ROCWETH_price_WETH_per_ROC[5m])",
+          "hide": false,
+          "instant": false,
+          "legendFormat": "Price average over 5 min",
+          "range": true,
+          "refId": "B"
+        },
+        {
+          "datasource": {
+            "type": "prometheus",
+            "uid": "PBFA97CFB590B2093"
+          },
+          "editorMode": "code",
+          "expr": "avg_over_time(Marketplace_Ethereum_Price_CHF[2m]) * avg_over_time(Uniswap_ROCWETH_price_WETH_per_ROC[2m])",
+          "hide": false,
+          "instant": false,
+          "legendFormat": "Price average over 2 min",
+          "range": true,
+          "refId": "C"
+        }
+      ],
+      "title": "ROC Price in CHF",
+      "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": 8,
+      "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_Ethereum_Price_CHF[5m])",
+          "legendFormat": "ETH in CHF averaged 5min",
+          "range": true,
+          "refId": "A"
+        },
+        {
+          "datasource": {
+            "type": "prometheus",
+            "uid": "PBFA97CFB590B2093"
+          },
+          "editorMode": "code",
+          "expr": "avg_over_time(Marketplace_Ethereum_Price_CHF[2m])",
+          "hide": false,
+          "instant": false,
+          "legendFormat": "ETH in CHF averaged 2min",
+          "range": true,
+          "refId": "B"
+        },
+        {
+          "datasource": {
+            "type": "prometheus",
+            "uid": "PBFA97CFB590B2093"
+          },
+          "editorMode": "code",
+          "expr": "Marketplace_Ethereum_Price_CHF",
+          "hide": false,
+          "instant": false,
+          "legendFormat": "ETH in CHF",
+          "range": true,
+          "refId": "C"
+        }
+      ],
+      "title": "ETH price in CHF",
+      "type": "timeseries"
     }
   ],
   "preload": false,
@@ -452,6 +884,6 @@
   "timezone": "browser",
   "title": "Total Supply of ROC",
   "uid": "ce3nybz6khudcd",
-  "version": 2,
+  "version": 1,
   "weekStart": ""
 }
\ No newline at end of file
-- 
GitLab