From f38f8b6a594dd8327e273118f98c76ad5d4ab4da Mon Sep 17 00:00:00 2001 From: Andri Joos <andri@joos.io> Date: Fri, 29 Nov 2024 14:08:01 +0100 Subject: [PATCH] calculate potato eth price --- .../BackgroundServices/PrometheusService.cs | 8 ++--- .../Interfaces/IPotatoMarketplace.cs | 4 ++- .../Marketplaces/PotatoMarketplaceMock.cs | 30 +++++++++++++--- Frontend/Metrics/EthMetrics.cs | 34 ------------------- Frontend/Metrics/PotatoMarketplaceMetrics.cs | 4 ++- Frontend/Program.cs | 3 +- 6 files changed, 35 insertions(+), 48 deletions(-) delete mode 100644 Frontend/Metrics/EthMetrics.cs diff --git a/Frontend/BackgroundServices/PrometheusService.cs b/Frontend/BackgroundServices/PrometheusService.cs index 0a61701..677f6dd 100644 --- a/Frontend/BackgroundServices/PrometheusService.cs +++ b/Frontend/BackgroundServices/PrometheusService.cs @@ -9,10 +9,9 @@ namespace Frontend.BackgroundServices; public class PrometheusService : BackgroundService { - private readonly ILogger<PrometheusService> _logger ; + private readonly ILogger<PrometheusService> _logger; private readonly TimeSpan _interval = TimeSpan.FromSeconds(10); private readonly Erc20TokenMetrics _erc20TokenMetrics; - private readonly EthMetrics _ethMetrics; private readonly MeterListener _meterListener; private readonly PoolMetrics _poolMetrics; @@ -21,7 +20,6 @@ public class PrometheusService : BackgroundService public PrometheusService( ILogger<PrometheusService> logger, Erc20TokenMetrics tokenMetrics, - EthMetrics ethMetrics, PoolMetrics poolMetrics, ChainSettings chainSettings, Web3 web3, @@ -32,7 +30,6 @@ public class PrometheusService : BackgroundService { _logger = logger; _erc20TokenMetrics = tokenMetrics; - _ethMetrics = ethMetrics; _poolMetrics = poolMetrics; _interval = TimeSpan.FromSeconds(10); _meterListener = new MeterListener(); @@ -42,10 +39,9 @@ public class PrometheusService : BackgroundService listener.EnableMeasurementEvents(instrument); }; _erc20TokenMetrics.CreateMetrics(); - _ethMetrics.CreateMetrics(); potatoStorageMetrics.CreateMetrics(); potatoMarketplaceMetrics.CreateMetrics(); - PoolV3Client poolClient = new( web3,chainSettings,account); + PoolV3Client poolClient = new(web3, chainSettings, account); _poolMetrics.CreateMetrics(poolClient); } diff --git a/Frontend/Marketplaces/Interfaces/IPotatoMarketplace.cs b/Frontend/Marketplaces/Interfaces/IPotatoMarketplace.cs index 757223d..702b893 100644 --- a/Frontend/Marketplaces/Interfaces/IPotatoMarketplace.cs +++ b/Frontend/Marketplaces/Interfaces/IPotatoMarketplace.cs @@ -4,7 +4,9 @@ namespace Frontend.Marketplaces.Interfaces { public interface IPotatoMarketPlace { - public double PotatoPrice { get; } + public Task<double> EthPriceInChf { get; } + public Task<double> PotatoKgPriceInChf { get; } + public Task<double> PotatoKgPriceInEth { get; } public double PotatoKgToChf(double kg); public double ChfToPotatoKg(double chf); public PotatoTransaction Buy(double amountInChf); diff --git a/Frontend/Marketplaces/PotatoMarketplaceMock.cs b/Frontend/Marketplaces/PotatoMarketplaceMock.cs index fb3accf..dfbd2a2 100644 --- a/Frontend/Marketplaces/PotatoMarketplaceMock.cs +++ b/Frontend/Marketplaces/PotatoMarketplaceMock.cs @@ -1,22 +1,25 @@ using Frontend.Assets; using Frontend.Marketplaces.Interfaces; +using Newtonsoft.Json.Linq; namespace Frontend.Marketplaces; public class PotatoMarketplaceMock() : IPotatoMarketPlace { - private const double potatoKgPrice = 2.02; // source: https://www.blw.admin.ch/blw/de/home/markt/marktbeobachtung/kartoffeln.html + private const double potatoKgChfPrice = 2.02; // source: https://www.blw.admin.ch/blw/de/home/markt/marktbeobachtung/kartoffeln.html - public double PotatoPrice => potatoKgPrice; + public Task<double> EthPriceInChf => EthChfPrice(); + public Task<double> PotatoKgPriceInChf => Task.FromResult(potatoKgChfPrice); + public Task<double> PotatoKgPriceInEth => PotatoKgEthPrice(); public double ChfToPotatoKg(double chf) { - return chf / potatoKgPrice; + return chf / potatoKgChfPrice; } public double PotatoKgToChf(double kg) { - return kg * potatoKgPrice; + return kg * potatoKgChfPrice; } public PotatoTransaction Buy(double amountInChf) @@ -29,4 +32,23 @@ public class PotatoMarketplaceMock() : IPotatoMarketPlace { return PotatoKgToChf(transaction.Weight); } + + private static async Task<double> EthChfPrice() + { + string url = "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=CHF"; + + using var client = new HttpClient(); + HttpResponseMessage response = await client.GetAsync(url); + response.EnsureSuccessStatusCode(); + string responseBody = await response.Content.ReadAsStringAsync(); + JObject json = JObject.Parse(responseBody); + double priceInChf = json["CHF"]!.Value<double>(); + return priceInChf; + } + + private static async Task<double> PotatoKgEthPrice() + { + var chfEthPrice = 1 / await EthChfPrice(); + return chfEthPrice * potatoKgChfPrice; + } } diff --git a/Frontend/Metrics/EthMetrics.cs b/Frontend/Metrics/EthMetrics.cs deleted file mode 100644 index 6395b06..0000000 --- a/Frontend/Metrics/EthMetrics.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Diagnostics.Metrics; -using System.Net.Http; -using Newtonsoft.Json.Linq; -namespace Frontend.Metrics; - -public class EthMetrics{ - private readonly IMeterFactory _meterFactory; - private ObservableGauge<double>? _totalSupplyGauge; - - public EthMetrics(IMeterFactory meterFactory) - { - _meterFactory = meterFactory; - } - - public void CreateMetrics() - { - var meter = _meterFactory.Create("RoestiCoin"); - _totalSupplyGauge = meter.CreateObservableGauge("Ethereum.Price.Current", () => GetCurrentPriceInChf(), unit: "CHF"); - } - - public double GetCurrentPriceInChf(){ - string url = "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=CHF"; - using (HttpClient client = new HttpClient()) - { - HttpResponseMessage response = client.GetAsync(url).Result; - response.EnsureSuccessStatusCode(); - string responseBody = response.Content.ReadAsStringAsync().Result; - JObject json = JObject.Parse(responseBody); - double priceInChf = json["CHF"]!.Value<double>(); - return priceInChf; - } - - } -} \ No newline at end of file diff --git a/Frontend/Metrics/PotatoMarketplaceMetrics.cs b/Frontend/Metrics/PotatoMarketplaceMetrics.cs index 57f839e..afac2e8 100644 --- a/Frontend/Metrics/PotatoMarketplaceMetrics.cs +++ b/Frontend/Metrics/PotatoMarketplaceMetrics.cs @@ -14,6 +14,8 @@ public class PotatoMarketplaceMetrics( public void CreateMetrics() { var meter = MeterFactory.Create("RoestiCoin"); - meter.CreateObservableGauge("Marketplace.Potato.Price", () => marketplace.PotatoPrice, unit: "CHF"); + meter.CreateObservableGauge("Marketplace.Potato.Price.CHF", () => marketplace.PotatoKgPriceInChf.Result, unit: "CHF"); + meter.CreateObservableGauge("Marketplace.Potato.Price.ETH", () => marketplace.PotatoKgPriceInEth.Result, unit: "ETH"); + meter.CreateObservableGauge("Marketplace.Ethereum.Price.CHF", () => marketplace.EthPriceInChf.Result, unit: "CHF"); } } diff --git a/Frontend/Program.cs b/Frontend/Program.cs index de4846d..4a788d5 100644 --- a/Frontend/Program.cs +++ b/Frontend/Program.cs @@ -35,7 +35,6 @@ chainConfiguration.Bind(chainSettings); // Add services to the container. builder.Services.AddHostedService<PrometheusService>(); builder.Services.AddSingleton<Erc20TokenMetrics>(); -builder.Services.AddSingleton<EthMetrics>(); builder.Services.AddSingleton<PotatoStorageMetrics>(); builder.Services.AddSingleton<PotatoMarketplaceMetrics>(); builder.Services.AddSingleton<IPotatoMarketPlace>(sp => new PotatoMarketplaceMock()); @@ -48,7 +47,7 @@ Account account = new Account( builder.Services.AddSingleton(provider => chainSettings); -builder.Services.AddSingleton(provider=>account); +builder.Services.AddSingleton(provider => account); builder.Services.AddTransient<Web3>(sp => { var chainSettings = sp.GetRequiredService<ChainSettings>(); -- GitLab