| 
 | 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 | +}  | 
0 commit comments