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.Tests/ThirdPartyTests.cs b/BTCPayServer.Tests/ThirdPartyTests.cs index d16034780..948073a98 100644 --- a/BTCPayServer.Tests/ThirdPartyTests.cs +++ b/BTCPayServer.Tests/ThirdPartyTests.cs @@ -208,6 +208,18 @@ namespace BTCPayServer.Tests e => e.CurrencyPair == new CurrencyPair("BTC", "LBP") && e.BidAsk.Bid > 1.0m); // 1 BTC will always be more than 1 LBP (I hope) } + else if (name == "bitmynt") + { + Assert.Contains(exchangeRates.ByExchange[name], + e => e.CurrencyPair == new CurrencyPair("BTC", "NOK") && + e.BidAsk.Bid > 1.0m); // 1 BTC will always be more than 1 NOK + } + else if (name == "barebitcoin") + { + Assert.Contains(exchangeRates.ByExchange[name], + e => e.CurrencyPair == new CurrencyPair("BTC", "NOK") && + e.BidAsk.Bid > 1.0m); // 1 BTC will always be more than 1 NOK + } else { if (name == "kraken") diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index 36949f1b3..f0bcb870c 100644 --- a/BTCPayServer/Hosting/BTCPayServerServices.cs +++ b/BTCPayServer/Hosting/BTCPayServerServices.cs @@ -532,7 +532,8 @@ o.GetRequiredService>().ToDictionary(o => o.P { "TRY", "btcturk" }, { "UGX", "yadio"}, { "RSD", "bitpay"}, - { "NGN", "bitnob"} + { "NGN", "bitnob"}, + { "NOK", "barebitcoin"} }) { var r = new DefaultRules.Recommendation(rule.Key, rule.Value); @@ -587,6 +588,8 @@ o.GetRequiredService>().ToDictionary(o => o.P services.AddRateProvider(); services.AddRateProvider(); services.AddRateProvider(); + services.AddRateProvider(); + services.AddRateProvider(); services.AddSingleton(); services.AddSingleton(o => o.GetRequiredService());