Skip to content
Snippets Groups Projects
Commit f38f8b6a authored by Andri Joos's avatar Andri Joos :blush:
Browse files

calculate potato eth price

parent 056962bb
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
......
......@@ -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);
......
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;
}
}
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
......@@ -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");
}
}
......@@ -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>();
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment