Skip to content

Commit

Permalink
Remove dead balance APIs. the remaining 2 already support bech32 address
Browse files Browse the repository at this point in the history
  • Loading branch information
Coding-Enthusiast committed Apr 13, 2020
1 parent e6685f3 commit 204badc
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 122 deletions.
4 changes: 2 additions & 2 deletions WatchOnlyBitcoinWallet/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.2.0.0")]
[assembly: AssemblyFileVersion("3.2.0.0")]
[assembly: AssemblyVersion("3.2.1.0")]
[assembly: AssemblyFileVersion("3.2.1.0")]
2 changes: 0 additions & 2 deletions WatchOnlyBitcoinWallet/Services/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public enum PriceServiceNames
}
public enum BalanceServiceNames
{
BlockchainInfo,
BlockExplorer,
BlockCypher,
Blockonomics
}
Expand Down
76 changes: 38 additions & 38 deletions WatchOnlyBitcoinWallet/Services/BalanceServices/BlockExplorer.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
//using System;
//using System.Collections.Generic;
//using System.Net.Http;
//using System.Threading.Tasks;

namespace WatchOnlyBitcoinWallet.Services.BalanceServices
{
public class BlockExplorer : BalanceApi
{
public override async Task<Response> UpdateBalancesAsync(List<Models.BitcoinAddress> addrList)
{
Response resp = new Response();
//namespace WatchOnlyBitcoinWallet.Services.BalanceServices
//{
// public class BlockExplorer : BalanceApi
// {
// public override async Task<Response> UpdateBalancesAsync(List<Models.BitcoinAddress> addrList)
// {
// Response resp = new Response();

using (HttpClient client = new HttpClient())
{
foreach (var addr in addrList)
{
try
{
string result = await client.GetStringAsync("https://blockexplorer.com/api/addr/" + addr.Address + "/balance");
decimal bal = Int64.Parse(result) * Satoshi;
addr.Difference = bal - addr.Balance;
addr.Balance = bal;
}
catch (Exception ex)
{
string errMsg = (ex.InnerException == null) ? ex.Message : ex.Message + " " + ex.InnerException;
resp.Errors.Add(errMsg);
break;
}
}
}
// using (HttpClient client = new HttpClient())
// {
// foreach (var addr in addrList)
// {
// try
// {
// string result = await client.GetStringAsync("https://blockexplorer.com/api/addr/" + addr.Address + "/balance");
// decimal bal = Int64.Parse(result) * Satoshi;
// addr.Difference = bal - addr.Balance;
// addr.Balance = bal;
// }
// catch (Exception ex)
// {
// string errMsg = (ex.InnerException == null) ? ex.Message : ex.Message + " " + ex.InnerException;
// resp.Errors.Add(errMsg);
// break;
// }
// }
// }

return resp;
}
// return resp;
// }

public override Task<Response> UpdateTransactionListAsync(List<Models.BitcoinAddress> addrList)
{
throw new NotImplementedException();
}
// public override Task<Response> UpdateTransactionListAsync(List<Models.BitcoinAddress> addrList)
// {
// throw new NotImplementedException();
// }

}
}
// }
//}
66 changes: 33 additions & 33 deletions WatchOnlyBitcoinWallet/Services/BalanceServices/BlockchainInfo.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using WatchOnlyBitcoinWallet.Models;
//using Newtonsoft.Json.Linq;
//using System;
//using System.Collections.Generic;
//using System.Threading.Tasks;
//using WatchOnlyBitcoinWallet.Models;

namespace WatchOnlyBitcoinWallet.Services.BalanceServices
{
public class BlockchainInfo : BalanceApi
{
public override async Task<Response> UpdateBalancesAsync(List<BitcoinAddress> addrList)
{
Response resp = new Response();
foreach (var addr in addrList)
{
string url = "https://blockchain.info/address/" + addr.Address + "?format=json&limit=0";
Response<JObject> apiResp = await SendApiRequestAsync(url);
if (apiResp.Errors.Any())
{
resp.Errors.AddRange(apiResp.Errors);
break;
}
decimal bal = (Int64)apiResp.Result["final_balance"] * Satoshi;
addr.Difference = bal - addr.Balance;
addr.Balance = bal;
}
return resp;
}
//namespace WatchOnlyBitcoinWallet.Services.BalanceServices
//{
// public class BlockchainInfo : BalanceApi
// {
// public override async Task<Response> UpdateBalancesAsync(List<BitcoinAddress> addrList)
// {
// Response resp = new Response();
// foreach (var addr in addrList)
// {
// string url = "https://blockchain.info/address/" + addr.Address + "?format=json&limit=0";
// Response<JObject> apiResp = await SendApiRequestAsync(url);
// if (apiResp.Errors.Any())
// {
// resp.Errors.AddRange(apiResp.Errors);
// break;
// }
// decimal bal = (Int64)apiResp.Result["final_balance"] * Satoshi;
// addr.Difference = bal - addr.Balance;
// addr.Balance = bal;
// }
// return resp;
// }

public override Task<Response> UpdateTransactionListAsync(List<BitcoinAddress> addrList)
{
throw new System.NotImplementedException();
}
// public override Task<Response> UpdateTransactionListAsync(List<BitcoinAddress> addrList)
// {
// throw new System.NotImplementedException();
// }

}
}
// }
//}
55 changes: 8 additions & 47 deletions WatchOnlyBitcoinWallet/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,67 +210,28 @@ private async void GetBalance()
BalanceApi api = null;
switch (SettingsInstance.SelectedBalanceApi)
{
case BalanceServiceNames.BlockchainInfo:
api = new BlockchainInfo();
break;
case BalanceServiceNames.BlockExplorer:
api = new BlockExplorer();
break;
case BalanceServiceNames.BlockCypher:
api = new BlockCypher();
break;
case BalanceServiceNames.Blockonomics:
api = new Blockonomics();
break;
default:
api = new BlockchainInfo();
api = new BlockCypher();
break;
}

// Not all exchanges support Bech32 addresses!
// The following "if" is to solve that.
bool hasSegWit = AddressList.Any(x => x.Address.StartsWith("bc1", System.StringComparison.InvariantCultureIgnoreCase));
if (hasSegWit && !SettingsInstance.SelectedBalanceApi.Equals(BalanceServiceNames.Blockonomics))
Response resp = await api.UpdateBalancesAsync(AddressList.ToList());
if (resp.Errors.Any())
{
BalanceApi segApi = new Blockonomics();
List<BitcoinAddress> legacyAddrs = new List<BitcoinAddress>(AddressList.Where(x =>
!x.Address.StartsWith("bc1", System.StringComparison.OrdinalIgnoreCase)));
List<BitcoinAddress> segWitAddrs = new List<BitcoinAddress>(AddressList.Where(x =>
x.Address.StartsWith("bc1", System.StringComparison.OrdinalIgnoreCase)));

Response respSW = await segApi.UpdateBalancesAsync(segWitAddrs);
if (respSW.Errors.Any())
{
Errors = "SegWit API error: " + respSW.Errors.GetErrors();
Status = "Error in SegWit API! Continue updating legacy balances...";
}
Response resp = await api.UpdateBalancesAsync(legacyAddrs);
if (resp.Errors.Any())
{
Errors = resp.Errors.GetErrors();
Status = "Encountered an error!";
}
else
{
DataManager.WriteFile(AddressList, DataManager.FileType.Wallet);
RaisePropertyChanged("BitcoinBalance");
Status = "Balance Update Success!";
}
Errors = resp.Errors.GetErrors();
Status = "Encountered an error!";
}
else
{
Response resp = await api.UpdateBalancesAsync(AddressList.ToList());
if (resp.Errors.Any())
{
Errors = resp.Errors.GetErrors();
Status = "Encountered an error!";
}
else
{
DataManager.WriteFile(AddressList, DataManager.FileType.Wallet);
RaisePropertyChanged("BitcoinBalance");
Status = "Balance Update Success!";
}
DataManager.WriteFile(AddressList, DataManager.FileType.Wallet);
RaisePropertyChanged("BitcoinBalance");
Status = "Balance Update Success!";
}

IsReceiving = false;
Expand Down

0 comments on commit 204badc

Please sign in to comment.