Skip to content

Commit e02a58e

Browse files
Add CoinGecko price API
1 parent 246de73 commit e02a58e

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

WatchOnlyBitcoinWallet/Services/ApiBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public enum PriceServiceNames
1515
MempoolSpace,
1616
Bitfinex,
1717
Coindesk,
18+
CoinGecko,
1819
}
1920
public enum BalanceServiceNames
2021
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// WatchOnlyBitcoinWallet
2+
// Copyright (c) 2016 Coding Enthusiast
3+
// Distributed under the MIT software license, see the accompanying
4+
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
5+
6+
using Newtonsoft.Json.Linq;
7+
using System.Diagnostics;
8+
using System.Threading.Tasks;
9+
10+
namespace WatchOnlyBitcoinWallet.Services.PriceServices
11+
{
12+
public class CoinGecko : ApiBase, IPriceApi
13+
{
14+
public async Task<Response<decimal>> UpdatePriceAsync()
15+
{
16+
Response<decimal> resp = new();
17+
Response<JObject> apiResp = await SendApiRequestAsync("https://api.coingecko.com/api/v3/exchange_rates");
18+
if (!apiResp.IsSuccess)
19+
{
20+
resp.Error = apiResp.Error;
21+
return resp;
22+
}
23+
Debug.Assert(apiResp.Result is not null);
24+
25+
JToken? x = apiResp.Result["rates"]?["usd"]?["value"];
26+
if (x is null)
27+
{
28+
resp.Error = "JSON in API response doesn't include the params " +
29+
"(API may have changed, please report this as a bug).";
30+
return resp;
31+
}
32+
33+
resp.Result = (decimal)x;
34+
return resp;
35+
}
36+
}
37+
}

WatchOnlyBitcoinWallet/ViewModels/SettingsViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ private async void UpdatePrice()
6565
PriceServiceNames.MempoolSpace => new MempoolSpace(),
6666
PriceServiceNames.Bitfinex => new Bitfinex(),
6767
PriceServiceNames.Coindesk => new Coindesk(),
68+
PriceServiceNames.CoinGecko => new CoinGecko(),
6869
_ => throw new NotImplementedException(),
6970
};
7071

0 commit comments

Comments
 (0)