forked from btcpayserver/btcpayserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for CryptoMarket exchange rates (btcpayserver#3012)
* Add support for CryptoMarket exchange rates * Add unit test for CryptoMarket
- Loading branch information
1 parent
f7a0b91
commit 8a0660c
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
BTCPayServer.Rating/Providers/CryptoMarketExchangeRateProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using BTCPayServer.Rating; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace BTCPayServer.Services.Rates | ||
{ | ||
public class CryptoMarketExchangeRateProvider : IRateProvider | ||
{ | ||
private readonly HttpClient _httpClient; | ||
public CryptoMarketExchangeRateProvider(HttpClient httpClient) | ||
{ | ||
_httpClient = httpClient ?? new HttpClient(); | ||
} | ||
|
||
|
||
readonly List<string> SupportedPairs = new List<string>() | ||
{ | ||
"BTCARS", | ||
"BTCCLP", | ||
"BTCBRL" | ||
}; | ||
|
||
public async Task<PairRate[]> GetRatesAsync(CancellationToken cancellationToken) | ||
{ | ||
var response = await _httpClient.GetAsync("https://api.exchange.cryptomkt.com/api/3/public/ticker/", cancellationToken); | ||
var jobj = await response.Content.ReadAsAsync<JObject>(cancellationToken); | ||
|
||
return ((jobj as JObject) ?? new JObject()) | ||
.Properties() | ||
.Where(p => SupportedPairs.Contains(p.Name)) | ||
.Select(p => new PairRate(CurrencyPair.Parse(p.Name), CreateBidAsk(p))) | ||
.ToArray(); | ||
} | ||
private static BidAsk CreateBidAsk(JProperty p) | ||
{ | ||
var bid = decimal.Parse(p.Value["bid"].Value<string>(), System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture); | ||
var ask = decimal.Parse(p.Value["ask"].Value<string>(), System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture); | ||
if (bid > ask) | ||
return null; | ||
return new BidAsk(bid, ask); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters