Skip to content

Commit 57903a2

Browse files
Add Blockchair and remove dead Blockonomics
1 parent 8a572a2 commit 57903a2

File tree

4 files changed

+81
-89
lines changed

4 files changed

+81
-89
lines changed

WatchOnlyBitcoinWallet/Services/ApiBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public enum BalanceServiceNames
2121
{
2222
MempoolSpace,
2323
BlockCypher,
24-
Blockonomics,
24+
Blockchair,
2525
}
2626

2727
public abstract class ApiBase
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 Autarkysoft.Bitcoin;
7+
using Newtonsoft.Json.Linq;
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Diagnostics;
11+
using System.Linq;
12+
using System.Threading.Tasks;
13+
using WatchOnlyBitcoinWallet.Models;
14+
15+
namespace WatchOnlyBitcoinWallet.Services.BalanceServices
16+
{
17+
public class Blockchair : ApiBase, IBalanceApi
18+
{
19+
public async Task<Response> UpdateBalancesAsync(List<BitcoinAddress> addrList)
20+
{
21+
Response resp = new();
22+
string all = string.Join(',', addrList.Select(x => x.Address));
23+
string url = $"https://api.blockchair.com/bitcoin/addresses/balances?addresses={all}";
24+
Response<JObject> apiResp = await SendApiRequestAsync(url);
25+
if (!apiResp.IsSuccess)
26+
{
27+
resp.Error = apiResp.Error;
28+
return resp;
29+
}
30+
Debug.Assert(apiResp.Result is not null);
31+
32+
JToken? data = apiResp.Result["data"];
33+
if (data is null)
34+
{
35+
resp.Error = BuildError("data", "blockchair");
36+
return resp;
37+
}
38+
39+
List<string> updatedAddrs = new(addrList.Count);
40+
foreach (JToken item in data)
41+
{
42+
string addr = item.Path.Substring("data.".Length);
43+
JToken? value = item.First;
44+
if (value is null)
45+
{
46+
resp.Error = "";
47+
return resp;
48+
}
49+
50+
decimal bal = (ulong)value * Constants.Satoshi;
51+
updatedAddrs.Add(addr);
52+
BitcoinAddress? address = addrList.Find(x => x.Address == addr);
53+
if (address is null)
54+
{
55+
resp.Error = $"Blockchair API returned {addr} that was not found in your wallet!";
56+
return resp;
57+
}
58+
address.Difference = bal - address.Balance;
59+
address.Balance = bal;
60+
}
61+
62+
foreach (var item in addrList)
63+
{
64+
if (!updatedAddrs.Contains(item.Address))
65+
{
66+
item.Balance = 0;
67+
}
68+
}
69+
70+
resp.IsSuccess = true;
71+
return resp;
72+
}
73+
74+
public Task<Response> UpdateTransactionListAsync(List<BitcoinAddress> addrList)
75+
{
76+
throw new NotImplementedException();
77+
}
78+
}
79+
}

WatchOnlyBitcoinWallet/Services/BalanceServices/Blockonomics.cs

Lines changed: 0 additions & 87 deletions
This file was deleted.

WatchOnlyBitcoinWallet/ViewModels/MainWindowViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private async void GetBalance()
194194
{
195195
BalanceServiceNames.MempoolSpace => new MempoolSpace(),
196196
BalanceServiceNames.BlockCypher => new BlockCypher(),
197-
BalanceServiceNames.Blockonomics => new Blockonomics(),
197+
BalanceServiceNames.Blockchair => new Blockchair(),
198198
_ => new BlockCypher(),
199199
};
200200

0 commit comments

Comments
 (0)