Created
January 20, 2015 20:12
-
-
Save erezwanderman/af74557cfdc863715355 to your computer and use it in GitHub Desktop.
Get balance of multiple bitcoin addresses using insight API
This file contains hidden or 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
| List<string> lstAddresses = new List<string>() { | |
| "1Jz6HEKNJpYjXAYRLn49QUUUrizL5qGxjM", | |
| "1JoktQJhCzuCQkt3GnQ8Xddcq4mUgNyXEa", | |
| "18vo26nEeDt1Tw17CuWap3F5rKJjBXSMmL", | |
| }; | |
| string URL = "https://insight.bitpay.com/api/addrs/utxo"; | |
| var request = WebRequest.Create(new Uri(URL)) as HttpWebRequest; | |
| if (request == null) | |
| throw new Exception("Non HTTP WebRequest"); | |
| string strData = "addrs=" + string.Join(",", lstAddresses); | |
| byte[] data = Encoding.ASCII.GetBytes(strData); | |
| request.Method = "POST"; | |
| request.Timeout = 15000; | |
| request.ContentType = "application/x-www-form-urlencoded"; | |
| request.ContentLength = data.Length; | |
| var reqStream = request.GetRequestStream(); | |
| reqStream.Write(data, 0, data.Length); | |
| reqStream.Close(); | |
| var response = request.GetResponse(); | |
| var resStream = response.GetResponseStream(); | |
| var resStreamReader = new StreamReader(resStream); | |
| var resString = resStreamReader.ReadToEnd(); | |
| decimal balance = 0; | |
| Newtonsoft.Json.Linq.JContainer results = JsonConvert.DeserializeObject<dynamic>(resString); | |
| if (results is JArray) | |
| { | |
| foreach (JContainer utxo in results) | |
| { | |
| balance += (decimal)utxo["amount"]; | |
| } | |
| System.Windows.Forms.MessageBox.Show("balance: " + balance); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment