From 2bc6499f1b040ed8b3043d2b829a4f47e0fdafa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Schj=C3=B8nhaug?= Date: Mon, 2 Dec 2024 11:47:35 +0100 Subject: [PATCH] Added support for Bitmynt and Bare Bitcoin --- .../Providers/BareBitcoinRateProvider.cs | 39 +++++++++++++++++++ .../Providers/BitmyntRateProvider.cs | 39 +++++++++++++++++++ BTCPayServer/Hosting/BTCPayServerServices.cs | 2 + 3 files changed, 80 insertions(+) create mode 100644 BTCPayServer.Rating/Providers/BareBitcoinRateProvider.cs create mode 100644 BTCPayServer.Rating/Providers/BitmyntRateProvider.cs diff --git a/BTCPayServer.Rating/Providers/BareBitcoinRateProvider.cs b/BTCPayServer.Rating/Providers/BareBitcoinRateProvider.cs new file mode 100644 index 000000000..6ca0d5239 --- /dev/null +++ b/BTCPayServer.Rating/Providers/BareBitcoinRateProvider.cs @@ -0,0 +1,39 @@ +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using BTCPayServer.Rating; +using Newtonsoft.Json.Linq; + +namespace BTCPayServer.Services.Rates +{ + public class BareBitcoinRateProvider : IRateProvider + { + private readonly HttpClient _httpClient; + + public RateSourceInfo RateSourceInfo => new("barebitcoin", "Bare Bitcoin", "https://api.bb.no/price"); + + public BareBitcoinRateProvider(HttpClient httpClient) + { + _httpClient = httpClient ?? new HttpClient(); + } + + public async Task GetRatesAsync(CancellationToken cancellationToken) + { + using var response = await _httpClient.GetAsync(RateSourceInfo.Url, cancellationToken); + response.EnsureSuccessStatusCode(); + + var jobj = await response.Content.ReadAsAsync(cancellationToken); + + // Extract market and otc prices + var market = jobj["market"].Value(); + var buy = jobj["buy"].Value(); + var sell = jobj["sell"].Value(); + + // Create currency pair for BTC/NOK + var pair = new CurrencyPair("BTC", "NOK"); + + // Return single pair rate with sell/buy as bid/ask + return new[] { new PairRate(pair, new BidAsk(sell, buy)) }; + } + } +} \ No newline at end of file diff --git a/BTCPayServer.Rating/Providers/BitmyntRateProvider.cs b/BTCPayServer.Rating/Providers/BitmyntRateProvider.cs new file mode 100644 index 000000000..fec24f906 --- /dev/null +++ b/BTCPayServer.Rating/Providers/BitmyntRateProvider.cs @@ -0,0 +1,39 @@ +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using BTCPayServer.Rating; +using Newtonsoft.Json.Linq; + +namespace BTCPayServer.Services.Rates +{ + public class BitmyntRateProvider : IRateProvider + { + private readonly HttpClient _httpClient; + + public RateSourceInfo RateSourceInfo => new("bitmynt", "Bitmynt", "https://ny.bitmynt.no/data/rates.json"); + + public BitmyntRateProvider(HttpClient httpClient) + { + _httpClient = httpClient ?? new HttpClient(); + } + + public async Task GetRatesAsync(CancellationToken cancellationToken) + { + using var response = await _httpClient.GetAsync(RateSourceInfo.Url, cancellationToken); + response.EnsureSuccessStatusCode(); + + var jobj = await response.Content.ReadAsAsync(cancellationToken); + + // Extract bid and ask prices from current_rate object + var currentRate = jobj["current_rate"]; + var bid = currentRate["bid"].Value(); + var ask = currentRate["ask"].Value(); + + // Create currency pair for BTC/NOK + var pair = new CurrencyPair("BTC", "NOK"); + + // Return single pair rate with bid/ask + return new[] { new PairRate(pair, new BidAsk(bid, ask)) }; + } + } +} \ No newline at end of file diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index 36949f1b3..698fb2d6a 100644 --- a/BTCPayServer/Hosting/BTCPayServerServices.cs +++ b/BTCPayServer/Hosting/BTCPayServerServices.cs @@ -587,6 +587,8 @@ o.GetRequiredService>().ToDictionary(o => o.P services.AddRateProvider(); services.AddRateProvider(); services.AddRateProvider(); + services.AddRateProvider(); + services.AddRateProvider(); services.AddSingleton(); services.AddSingleton(o => o.GetRequiredService());