From b1a0d619731d8ed74dcc29808b2f03a1fce0a8d3 Mon Sep 17 00:00:00 2001 From: crebsy <121096251+crebsy@users.noreply.github.com> Date: Thu, 2 Nov 2023 19:17:38 +0100 Subject: [PATCH] feat: calc swap volumes based on the current res (#655) * feat: calc swap volumes based on the current res * fix: adapt panel titles * feat: adapt dash --- .../provisioning/dashboards/yearn/yETH.json | 243 ++++++++++-------- scripts/exporters/yeth.py | 3 +- yearn/apy/common.py | 4 +- yearn/yeth.py | 42 ++- 4 files changed, 179 insertions(+), 113 deletions(-) diff --git a/grafana/provisioning/dashboards/yearn/yETH.json b/grafana/provisioning/dashboards/yearn/yETH.json index 0982ec836..7ee9e37b6 100644 --- a/grafana/provisioning/dashboards/yearn/yETH.json +++ b/grafana/provisioning/dashboards/yearn/yETH.json @@ -18,7 +18,7 @@ "editable": true, "fiscalYearStartMonth": 0, "graphTooltip": 0, - "id": null, + "id": 10, "links": [], "liveNow": false, "panels": [ @@ -79,7 +79,7 @@ }, "textMode": "auto" }, - "pluginVersion": "10.0.1", + "pluginVersion": "10.2.0", "targets": [ { "datasource": { @@ -142,7 +142,7 @@ }, "textMode": "value" }, - "pluginVersion": "10.0.1", + "pluginVersion": "10.2.0", "targets": [ { "datasource": { @@ -206,7 +206,7 @@ }, "textMode": "auto" }, - "pluginVersion": "10.0.1", + "pluginVersion": "10.2.0", "targets": [ { "datasource": { @@ -270,7 +270,7 @@ }, "textMode": "value" }, - "pluginVersion": "10.0.1", + "pluginVersion": "10.2.0", "targets": [ { "datasource": { @@ -334,7 +334,7 @@ }, "textMode": "value" }, - "pluginVersion": "10.0.1", + "pluginVersion": "10.2.0", "targets": [ { "datasource": { @@ -364,6 +364,7 @@ "mode": "palette-classic" }, "custom": { + "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "series", "axisLabel": "", @@ -377,6 +378,7 @@ "tooltip": false, "viz": false }, + "insertNulls": false, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, @@ -518,6 +520,7 @@ "mode": "palette-classic" }, "custom": { + "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "series", "axisLabel": "", @@ -531,6 +534,7 @@ "tooltip": false, "viz": false }, + "insertNulls": false, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, @@ -721,7 +725,7 @@ }, "textMode": "value_and_name" }, - "pluginVersion": "10.0.1", + "pluginVersion": "10.2.0", "repeat": "lst", "repeatDirection": "h", "targets": [ @@ -788,7 +792,7 @@ }, "textMode": "auto" }, - "pluginVersion": "10.0.1", + "pluginVersion": "10.2.0", "targets": [ { "datasource": { @@ -817,6 +821,7 @@ "mode": "palette-classic" }, "custom": { + "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", @@ -830,6 +835,7 @@ "tooltip": false, "viz": false }, + "insertNulls": false, "lineInterpolation": "linear", "lineStyle": { "fill": "solid" @@ -986,6 +992,7 @@ "mode": "palette-classic" }, "custom": { + "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", @@ -999,6 +1006,7 @@ "tooltip": false, "viz": false }, + "insertNulls": false, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, @@ -1084,6 +1092,7 @@ "mode": "palette-classic" }, "custom": { + "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", @@ -1097,6 +1106,7 @@ "tooltip": false, "viz": false }, + "insertNulls": false, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, @@ -1212,6 +1222,7 @@ "mode": "palette-classic" }, "custom": { + "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", @@ -1225,6 +1236,7 @@ "tooltip": false, "viz": false }, + "insertNulls": false, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, @@ -1246,7 +1258,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "red", @@ -1312,7 +1325,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null } ] }, @@ -1341,7 +1355,7 @@ }, "textMode": "auto" }, - "pluginVersion": "10.0.1", + "pluginVersion": "10.2.0", "targets": [ { "datasource": { @@ -1375,7 +1389,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null } ] }, @@ -1404,7 +1419,7 @@ }, "textMode": "auto" }, - "pluginVersion": "10.0.1", + "pluginVersion": "10.2.0", "targets": [ { "datasource": { @@ -1433,6 +1448,7 @@ "mode": "palette-classic" }, "custom": { + "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", @@ -1446,6 +1462,7 @@ "tooltip": false, "viz": false }, + "insertNulls": false, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, @@ -1467,7 +1484,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "red", @@ -1555,6 +1573,7 @@ "mode": "palette-classic" }, "custom": { + "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", @@ -1568,6 +1587,7 @@ "tooltip": false, "viz": false }, + "insertNulls": false, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 7, @@ -1589,7 +1609,8 @@ "mode": "absolute", "steps": [ { - "color": "purple" + "color": "purple", + "value": null } ] }, @@ -1599,7 +1620,7 @@ { "matcher": { "id": "byName", - "options": "sum daily swap volume outgoing" + "options": "Sum swap volume outgoing" }, "properties": [ { @@ -1614,7 +1635,7 @@ { "matcher": { "id": "byName", - "options": "Sum Daily swap volume incoming" + "options": "Sum swap volume incoming" }, "properties": [ { @@ -1633,7 +1654,7 @@ "options": { "mode": "exclude", "names": [ - "Sum Daily swap volume incoming" + "Sum swap volume incoming" ], "prefix": "All except:", "readOnly": true @@ -1682,7 +1703,7 @@ "editorMode": "code", "expr": "sum(yeth{param=\"volume_in_usd\",product=~\"$lst\"})", "instant": false, - "legendFormat": "Sum Daily swap volume incoming", + "legendFormat": "Sum swap volume incoming", "range": true, "refId": "A" }, @@ -1695,7 +1716,7 @@ "expr": "sum(yeth{param=\"volume_out_usd\",product=~\"$lst\"})", "hide": false, "instant": false, - "legendFormat": "sum daily swap volume outgoing", + "legendFormat": "Sum swap volume outgoing", "range": true, "refId": "B" } @@ -1706,7 +1727,7 @@ { "datasource": { "type": "prometheus", - "uid": "DS_PROMETHEUS" + "uid": "PBFE396EC0B189D67" }, "fieldConfig": { "defaults": { @@ -1714,21 +1735,33 @@ "mode": "palette-classic" }, "custom": { + "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", - "fillOpacity": 80, + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 0, "gradientMode": "none", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, - "lineWidth": 1, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 5, + "pointSize": 5, "scaleDistribution": { "type": "linear" }, + "showPoints": "auto", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, "thresholdsStyle": { "mode": "off" } @@ -1738,7 +1771,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null } ] }, @@ -1754,26 +1788,16 @@ }, "id": 8, "options": { - "barRadius": 0, - "barWidth": 1, - "fullHighlight": false, - "groupWidth": 0.7, "legend": { "calcs": [], "displayMode": "list", "placement": "right", "showLegend": true }, - "orientation": "auto", - "showValue": "auto", - "stacking": "normal", "tooltip": { "mode": "single", "sort": "none" - }, - "xField": "Time", - "xTickLabelRotation": 0, - "xTickLabelSpacing": 100 + } }, "targets": [ { @@ -1782,20 +1806,20 @@ "uid": "DS_PROMETHEUS" }, "editorMode": "code", - "expr": "yeth{param=\"volume_in_eth\",product=~\"$lst\"}", + "expr": "increase(yeth{param=\"volume_in_eth\",product=~\"$lst\"}[$__interval])", "instant": false, "legendFormat": "{{product}}", "range": true, "refId": "A" } ], - "title": "Daily swap volume incoming ETH", - "type": "barchart" + "title": "Swap volume incoming ETH", + "type": "timeseries" }, { "datasource": { "type": "prometheus", - "uid": "DS_PROMETHEUS" + "uid": "PBFE396EC0B189D67" }, "fieldConfig": { "defaults": { @@ -1803,21 +1827,33 @@ "mode": "palette-classic" }, "custom": { + "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", - "fillOpacity": 80, + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 0, "gradientMode": "none", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, - "lineWidth": 1, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 5, + "pointSize": 5, "scaleDistribution": { "type": "linear" }, + "showPoints": "auto", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, "thresholdsStyle": { "mode": "off" } @@ -1827,7 +1863,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null } ] }, @@ -1843,26 +1880,16 @@ }, "id": 9, "options": { - "barRadius": 0, - "barWidth": 1, - "fullHighlight": false, - "groupWidth": 0.7, "legend": { "calcs": [], "displayMode": "list", "placement": "right", "showLegend": true }, - "orientation": "auto", - "showValue": "auto", - "stacking": "normal", "tooltip": { "mode": "single", "sort": "none" - }, - "xField": "Time", - "xTickLabelRotation": 0, - "xTickLabelSpacing": 100 + } }, "targets": [ { @@ -1871,20 +1898,20 @@ "uid": "DS_PROMETHEUS" }, "editorMode": "code", - "expr": "yeth{param=\"volume_out_eth\",product=~\"$lst\"}", + "expr": "increase(yeth{param=\"volume_out_eth\",product=~\"$lst\"}[$__interval])", "instant": false, "legendFormat": "{{product}}", "range": true, "refId": "A" } ], - "title": "Daily swap volume outgoing ETH", - "type": "barchart" + "title": "Swap volume outgoing ETH", + "type": "timeseries" }, { "datasource": { "type": "prometheus", - "uid": "DS_PROMETHEUS" + "uid": "PBFE396EC0B189D67" }, "fieldConfig": { "defaults": { @@ -1892,21 +1919,33 @@ "mode": "palette-classic" }, "custom": { + "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", - "fillOpacity": 80, + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 0, "gradientMode": "none", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, - "lineWidth": 1, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 5, + "pointSize": 5, "scaleDistribution": { "type": "linear" }, + "showPoints": "auto", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, "thresholdsStyle": { "mode": "off" } @@ -1916,7 +1955,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null } ] }, @@ -1932,26 +1972,16 @@ }, "id": 10, "options": { - "barRadius": 0, - "barWidth": 1, - "fullHighlight": false, - "groupWidth": 0.7, "legend": { "calcs": [], "displayMode": "list", "placement": "right", "showLegend": true }, - "orientation": "auto", - "showValue": "auto", - "stacking": "normal", "tooltip": { "mode": "single", "sort": "none" - }, - "xField": "Time", - "xTickLabelRotation": 0, - "xTickLabelSpacing": 100 + } }, "targets": [ { @@ -1960,20 +1990,20 @@ "uid": "DS_PROMETHEUS" }, "editorMode": "code", - "expr": "yeth{param=\"volume_in_usd\",product=~\"$lst\"}", + "expr": "increase(yeth{param=\"volume_in_usd\",product=~\"$lst\"}[$__interval])", "instant": false, "legendFormat": "{{product}}", "range": true, "refId": "A" } ], - "title": "Daily swap volume incoming USD", - "type": "barchart" + "title": "Swap volume incoming USD", + "type": "timeseries" }, { "datasource": { "type": "prometheus", - "uid": "DS_PROMETHEUS" + "uid": "PBFE396EC0B189D67" }, "fieldConfig": { "defaults": { @@ -1981,21 +2011,33 @@ "mode": "palette-classic" }, "custom": { + "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", - "fillOpacity": 80, + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 0, "gradientMode": "none", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, - "lineWidth": 1, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 5, + "pointSize": 5, "scaleDistribution": { "type": "linear" }, + "showPoints": "auto", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, "thresholdsStyle": { "mode": "off" } @@ -2005,7 +2047,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null } ] }, @@ -2021,26 +2064,16 @@ }, "id": 11, "options": { - "barRadius": 0, - "barWidth": 1, - "fullHighlight": false, - "groupWidth": 0.7, "legend": { "calcs": [], "displayMode": "list", "placement": "right", "showLegend": true }, - "orientation": "auto", - "showValue": "auto", - "stacking": "normal", "tooltip": { "mode": "single", "sort": "none" - }, - "xField": "Time", - "xTickLabelRotation": 0, - "xTickLabelSpacing": 100 + } }, "targets": [ { @@ -2049,15 +2082,15 @@ "uid": "DS_PROMETHEUS" }, "editorMode": "code", - "expr": "yeth{param=\"volume_out_usd\",product=~\"$lst\"}", + "expr": "increase(yeth{param=\"volume_out_usd\",product=~\"$lst\"}[$__interval])", "instant": false, "legendFormat": "{{product}}", "range": true, "refId": "A" } ], - "title": "Daily swap volume outgoing USD", - "type": "barchart" + "title": "Swap volume outgoing USD", + "type": "timeseries" }, { "datasource": { @@ -2074,7 +2107,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null } ] }, @@ -2103,7 +2137,7 @@ }, "textMode": "value_and_name" }, - "pluginVersion": "10.0.1", + "pluginVersion": "10.2.0", "targets": [ { "datasource": { @@ -2137,7 +2171,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "red", @@ -2171,7 +2206,7 @@ }, "textMode": "value_and_name" }, - "pluginVersion": "10.0.1", + "pluginVersion": "10.2.0", "repeat": "lst", "repeatDirection": "h", "targets": [ @@ -2202,6 +2237,7 @@ "mode": "palette-classic" }, "custom": { + "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", @@ -2215,6 +2251,7 @@ "tooltip": false, "viz": false }, + "insertNulls": false, "lineInterpolation": "linear", "lineStyle": { "fill": "solid" @@ -2239,7 +2276,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "red", @@ -2289,15 +2327,18 @@ "type": "timeseries" } ], - "refresh": "", + "refresh": false, "schemaVersion": 38, - "style": "dark", "tags": [], "templating": { "list": [ { "allValue": ".*", - "current": {}, + "current": { + "selected": false, + "text": "All", + "value": "$__all" + }, "datasource": { "type": "prometheus", "uid": "DS_PROMETHEUS" @@ -2329,6 +2370,6 @@ "timezone": "", "title": "yETH", "uid": "eef3a622-567d-4587-8d2e-358039eb97e1", - "version": 14, + "version": 23, "weekStart": "" } \ No newline at end of file diff --git a/scripts/exporters/yeth.py b/scripts/exporters/yeth.py index c8f1fbdfd..614401f63 100644 --- a/scripts/exporters/yeth.py +++ b/scripts/exporters/yeth.py @@ -31,8 +31,7 @@ def main(): data_fn = YETH().metrics_for_export, export_fn = _post, start_block = closest_block_after_timestamp(start[chain.id]), - concurrency=constants.CONCURRENCY, - resolution='1d', + concurrency=constants.CONCURRENCY ).run() class YETH: diff --git a/yearn/apy/common.py b/yearn/apy/common.py index dd1c9d1e6..705052124 100644 --- a/yearn/apy/common.py +++ b/yearn/apy/common.py @@ -57,7 +57,6 @@ class ApySamples: now: int week_ago: int month_ago: int - day_ago: int class ApyError(ValueError): @@ -85,7 +84,6 @@ def get_samples(now_time: Optional[datetime] = None) -> ApySamples: now = web3.eth.block_number else: now = closest_block_after_timestamp(int(now_time.timestamp()), True) - day_ago = closest_block_after_timestamp(int((now_time - timedelta(days=1)).timestamp()), True) week_ago = closest_block_after_timestamp(int((now_time - timedelta(days=7)).timestamp()), True) month_ago = closest_block_after_timestamp(int((now_time - timedelta(days=31)).timestamp()), True) - return ApySamples(now, week_ago, month_ago, day_ago) + return ApySamples(now, week_ago, month_ago) diff --git a/yearn/yeth.py b/yearn/yeth.py index b8704afd2..dcfcaf8b2 100644 --- a/yearn/yeth.py +++ b/yearn/yeth.py @@ -2,7 +2,7 @@ import os import re import logging -from datetime import datetime, timezone +from datetime import datetime, timezone, timedelta import eth_retry @@ -10,7 +10,7 @@ from pprint import pformat from y import Contract, magic, Network -from y.time import get_block_timestamp +from y.time import get_block_timestamp, closest_block_after_timestamp from y.contracts import contract_creation_block_async from y.exceptions import PriceError, yPriceMagicError @@ -210,8 +210,9 @@ class Registry(metaclass = Singleton): def __init__(self) -> None: self.st_yeth = StYETH() self.swap_volumes = {} + self.resolution = os.environ.get('RESOLUTION', '1h') # Default: 1 hour - async def _get_daily_swap_volumes(self, from_block, to_block): + async def _get_swap_volumes(self, from_block, to_block): logs = get_logs_asap([str(YETH_POOL)], None, from_block=from_block, to_block=to_block) events = decode_logs(logs) @@ -251,13 +252,16 @@ async def _get_daily_swap_volumes(self, from_block, to_block): } async def describe(self, block=None): + to_block = chain.height + now_time = datetime.today() if block: + to_block = block block_timestamp = get_block_timestamp(block) - samples = get_samples(datetime.fromtimestamp(block_timestamp)) - else: - samples = get_samples() + now_time = datetime.fromtimestamp(block_timestamp) - self.swap_volumes = await self._get_daily_swap_volumes(samples.day_ago, samples.now) + from_block = self._get_from_block(now_time) + + self.swap_volumes = await self._get_swap_volumes(from_block, to_block) products = await self.active_products_at(block) data = await asyncio.gather(*[product.describe(block=block) for product in products]) return {product.name: desc for product, desc in zip(products, data)} @@ -273,3 +277,27 @@ async def active_products_at(self, block=None): blocks = await asyncio.gather(*[contract_creation_block_async(str(product.address)) for product in products]) products = [product for product, deploy_block in zip(products, blocks) if deploy_block <= block] return products + + def _get_from_block(self, now_time): + from_time = None + match self.resolution: + case "1d": + from_time = (now_time - timedelta(days=1)).timestamp() + case "1h": + from_time = (now_time - timedelta(hours=1)).timestamp() + case "30m": + from_time = (now_time - timedelta(minutes=30)).timestamp() + case "15m": + from_time = (now_time - timedelta(minutes=15)).timestamp() + case "5m": + from_time = (now_time - timedelta(minutes=5)).timestamp() + case "1m": + from_time = (now_time - timedelta(minutes=1)).timestamp() + case "30s": + from_time = (now_time - timedelta(seconds=30)).timestamp() + case "15s": + from_time = (now_time - timedelta(seconds=15)).timestamp() + case _: + raise Exception(f"Invalid resolution {self.resolution} specified!") + + return closest_block_after_timestamp(int(from_time), True)