txs) {
135 | this.txs = txs;
136 | }
137 | }
138 |
--------------------------------------------------------------------------------
/src/main/java/info/blockchain/api/etc/Base58.java:
--------------------------------------------------------------------------------
1 | package info.blockchain.api.etc;
2 |
3 | /**
4 | * Copyright 2011 Google Inc.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import java.math.BigInteger;
20 | import java.security.MessageDigest;
21 | import java.security.NoSuchAlgorithmException;
22 | import java.text.ParseException;
23 | import java.util.Arrays;
24 |
25 | /**
26 | * Base58 is a way to encode Bitcoin addresses (or arbitrary data) as alphanumeric strings.
27 | *
28 | * Note that this is not the same base58 as used by Flickr, which you may find referenced around the Internet.
29 | *
30 | * Satoshi explains: why base-58 instead of standard base-64 encoding?
31 | *
32 | * - Don't want 0OIl characters that look the same in some fonts and
33 | * could be used to create visually identical looking account numbers.
34 | * - A string with non-alphanumeric characters is not as easily accepted as an account number.
35 | * - E-mail usually won't line-break if there's no punctuation to break at.
36 | * - Doubleclicking selects the whole number as one word if it's all alphanumeric.
37 | *
38 | *
39 | * However, note that the encoding/decoding runs in O(n²) time, so it is not useful for large data.
40 | *
41 | * The basic idea of the encoding is to treat the data bytes as a large number represented using
42 | * base-256 digits, convert the number to be represented using base-58 digits, preserve the exact
43 | * number of leading zeros (which are otherwise lost during the mathematical operations on the
44 | * numbers), and finally represent the resulting base-58 digits as alphanumeric ASCII characters.
45 | */
46 | public class Base58 {
47 | public static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
48 | private static final char ENCODED_ZERO = ALPHABET[0];
49 | private static final int[] INDEXES = new int[128];
50 |
51 | static {
52 | Arrays.fill(INDEXES, -1);
53 | for (int i = 0; i < ALPHABET.length; i++) {
54 | INDEXES[ALPHABET[i]] = i;
55 | }
56 | }
57 |
58 | /**
59 | * Encodes the given bytes as a base58 string (no checksum is appended).
60 | *
61 | * @param input the bytes to encode
62 | * @return the base58-encoded string
63 | */
64 | public static String encode (byte[] input) {
65 | if (input.length == 0) {
66 | return "";
67 | }
68 | // Count leading zeros.
69 | int zeros = 0;
70 | while (zeros < input.length && input[zeros] == 0) {
71 | ++zeros;
72 | }
73 | // Convert base-256 digits to base-58 digits (plus conversion to ASCII characters)
74 | input = Arrays.copyOf(input, input.length); // since we modify it in-place
75 | char[] encoded = new char[input.length * 2]; // upper bound
76 | int outputStart = encoded.length;
77 | for (int inputStart = zeros; inputStart < input.length; ) {
78 | encoded[--outputStart] = ALPHABET[divmod(input, inputStart, 256, 58)];
79 | if (input[inputStart] == 0) {
80 | ++inputStart; // optimization - skip leading zeros
81 | }
82 | }
83 | // Preserve exactly as many leading encoded zeros in output as there were leading zeros in input.
84 | while (outputStart < encoded.length && encoded[outputStart] == ENCODED_ZERO) {
85 | ++outputStart;
86 | }
87 | while (--zeros >= 0) {
88 | encoded[--outputStart] = ENCODED_ZERO;
89 | }
90 | // Return encoded string (including encoded leading zeros).
91 | return new String(encoded, outputStart, encoded.length - outputStart);
92 | }
93 |
94 | /**
95 | * Decodes the given base58 string into the original data bytes.
96 | *
97 | * @param input the base58-encoded string to decode
98 | * @return the decoded data bytes
99 | * @throws ParseException if the given string is not a valid base58 string
100 | */
101 | public static byte[] decode (String input) throws ParseException {
102 | if (input.length() == 0) {
103 | return new byte[0];
104 | }
105 | // Convert the base58-encoded ASCII chars to a base58 byte sequence (base58 digits).
106 | byte[] input58 = new byte[input.length()];
107 | for (int i = 0; i < input.length(); ++i) {
108 | char c = input.charAt(i);
109 | int digit = c < 128 ? INDEXES[c] : -1;
110 | if (digit < 0) {
111 | throw new ParseException("Illegal character " + c + " at position " + i, i);
112 | }
113 | input58[i] = (byte) digit;
114 | }
115 | // Count leading zeros.
116 | int zeros = 0;
117 | while (zeros < input58.length && input58[zeros] == 0) {
118 | ++zeros;
119 | }
120 | // Convert base-58 digits to base-256 digits.
121 | byte[] decoded = new byte[input.length()];
122 | int outputStart = decoded.length;
123 | for (int inputStart = zeros; inputStart < input58.length; ) {
124 | decoded[--outputStart] = divmod(input58, inputStart, 58, 256);
125 | if (input58[inputStart] == 0) {
126 | ++inputStart; // optimization - skip leading zeros
127 | }
128 | }
129 | // Ignore extra leading zeroes that were added during the calculation.
130 | while (outputStart < decoded.length && decoded[outputStart] == 0) {
131 | ++outputStart;
132 | }
133 | // Return decoded data (including original number of leading zeros).
134 | return Arrays.copyOfRange(decoded, outputStart - zeros, decoded.length);
135 | }
136 |
137 | public static BigInteger decodeToBigInteger (String input) throws ParseException {
138 | return new BigInteger(1, decode(input));
139 | }
140 |
141 | /**
142 | * Decodes the given base58 string into the original data bytes, using the checksum in the
143 | * last 4 bytes of the decoded data to verify that the rest are correct. The checksum is
144 | * removed from the returned data.
145 | *
146 | * @param input the base58-encoded string to decode (which should include the checksum)
147 | * @throws ParseException if the input is not base 58 or the checksum does not validate.
148 | */
149 | public static byte[] decodeChecked (String input) throws ParseException {
150 | byte[] decoded = decode(input);
151 | if (decoded.length < 4) {
152 | throw new ParseException("Input too short", 0);
153 | }
154 | byte[] data = Arrays.copyOfRange(decoded, 0, decoded.length - 4);
155 | byte[] checksum = Arrays.copyOfRange(decoded, decoded.length - 4, decoded.length);
156 | byte[] actualChecksum = Arrays.copyOfRange(hashTwice(data), 0, 4);
157 | if (!Arrays.equals(checksum, actualChecksum)) {
158 | throw new ParseException("Checksum does not validate", 0);
159 | }
160 | return data;
161 | }
162 |
163 | /**
164 | * Divides a number, represented as an array of bytes each containing a single digit
165 | * in the specified base, by the given divisor. The given number is modified in-place
166 | * to contain the quotient, and the return value is the remainder.
167 | *
168 | * @param number the number to divide
169 | * @param firstDigit the index within the array of the first non-zero digit
170 | * (this is used for optimization by skipping the leading zeros)
171 | * @param base the base in which the number's digits are represented (up to 256)
172 | * @param divisor the number to divide by (up to 256)
173 | * @return the remainder of the division operation
174 | */
175 | private static byte divmod (byte[] number, int firstDigit, int base, int divisor) {
176 | // this is just long division which accounts for the base of the input digits
177 | int remainder = 0;
178 | for (int i = firstDigit; i < number.length; i++) {
179 | int digit = (int) number[i] & 0xFF;
180 | int temp = remainder * base + digit;
181 | number[i] = (byte) (temp / divisor);
182 | remainder = temp % divisor;
183 | }
184 | return (byte) remainder;
185 | }
186 |
187 | public static byte[] hashTwice (byte[] input) {
188 | MessageDigest digest = null;
189 | try {
190 | digest = MessageDigest.getInstance("SHA-256");
191 | digest.update(input, 0, input.length);
192 | return digest.digest(digest.digest());
193 | } catch (NoSuchAlgorithmException e) {
194 | throw new RuntimeException(e);
195 | }
196 |
197 | }
198 | }
--------------------------------------------------------------------------------
/src/main/java/info/blockchain/api/exchangerates/Currency.java:
--------------------------------------------------------------------------------
1 | package info.blockchain.api.exchangerates;
2 |
3 | import java.math.BigDecimal;
4 |
5 | /**
6 | * Used in response to the `getTicker` method in the `ExchangeRates` class.
7 | */
8 | public class Currency {
9 | private BigDecimal buy;
10 | private BigDecimal sell;
11 | private BigDecimal last;
12 | private BigDecimal price15m;
13 | private String symbol;
14 |
15 | public Currency (double buy, double sell, double last, double price15m, String symbol) {
16 | this.buy = BigDecimal.valueOf(buy);
17 | this.sell = BigDecimal.valueOf(sell);
18 | this.last = BigDecimal.valueOf(last);
19 | this.price15m = BigDecimal.valueOf(price15m);
20 | this.symbol = symbol;
21 | }
22 |
23 | /**
24 | * @return Current buy price
25 | */
26 | public BigDecimal getBuy () {
27 | return buy;
28 | }
29 |
30 | /**
31 | * @return Current sell price
32 | */
33 | public BigDecimal getSell () {
34 | return sell;
35 | }
36 |
37 | /**
38 | * @return Most recent market price
39 | */
40 | public BigDecimal getLast () {
41 | return last;
42 | }
43 |
44 | /**
45 | * @return 15 minutes delayed market price
46 | */
47 | public BigDecimal getPrice15m () {
48 | return price15m;
49 | }
50 |
51 | /**
52 | * @return Currency symbol
53 | */
54 | public String getSymbol () {
55 | return symbol;
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/info/blockchain/api/exchangerates/ExchangeRates.java:
--------------------------------------------------------------------------------
1 | package info.blockchain.api.exchangerates;
2 |
3 | import com.google.gson.JsonElement;
4 | import com.google.gson.JsonObject;
5 | import com.google.gson.JsonParser;
6 | import info.blockchain.api.APIException;
7 | import info.blockchain.api.HttpClient;
8 |
9 | import java.io.IOException;
10 | import java.math.BigDecimal;
11 | import java.util.HashMap;
12 | import java.util.Map;
13 | import java.util.Map.Entry;
14 |
15 | /**
16 | * This class reflects the functionality documented
17 | * at https://blockchain.info/api/exchange_rates_api. It allows users to fetch the latest
18 | * ticker data and convert amounts between BTC and fiat currencies.
19 | */
20 | public class ExchangeRates {
21 |
22 | private final String apiCode;
23 |
24 | public ExchangeRates () {
25 | this(null);
26 | }
27 |
28 | public ExchangeRates (String apiCode) {
29 | this.apiCode = apiCode;
30 | }
31 |
32 | /**
33 | * Gets the price ticker from https://blockchain.info/ticker
34 | *
35 | * @return A map of currencies where the key is a 3-letter currency symbol and the
36 | * value is the `Currency` class
37 | * @throws APIException If the server returns an error
38 | */
39 | public Map getTicker () throws APIException, IOException {
40 | Map params = new HashMap();
41 | if (apiCode != null) {
42 | params.put("api_code", apiCode);
43 | }
44 |
45 | String response = HttpClient.getInstance().get("ticker", params);
46 | JsonObject ticker = new JsonParser().parse(response).getAsJsonObject();
47 |
48 | Map resultMap = new HashMap();
49 | for (Entry ccyKVP : ticker.entrySet()) {
50 | JsonObject ccy = ccyKVP.getValue().getAsJsonObject();
51 | Currency currency = new Currency(ccy.get("buy").getAsDouble(), ccy.get("sell").getAsDouble(), ccy.get("last").getAsDouble(), ccy.get("15m").getAsDouble(), ccy.get("symbol").getAsString());
52 |
53 | resultMap.put(ccyKVP.getKey(), currency);
54 | }
55 |
56 | return resultMap;
57 | }
58 |
59 | /**
60 | * Converts x value in the provided currency to BTC.
61 | *
62 | * @param currency Currency code
63 | * @param value Value to convert
64 | * @return Converted value in BTC
65 | * @throws APIException If the server returns an error
66 | */
67 | public BigDecimal toBTC (String currency, BigDecimal value) throws APIException, IOException {
68 | Map params = new HashMap();
69 | params.put("currency", currency);
70 | params.put("value", String.valueOf(value));
71 | if (apiCode != null) {
72 | params.put("api_code", apiCode);
73 | }
74 |
75 | String response = HttpClient.getInstance().get("tobtc", params);
76 | return new BigDecimal(response);
77 | }
78 |
79 | /**
80 | * Converts x value in BTC to the provided currency.
81 | *
82 | * @param currency Currency code
83 | * @param value Value to convert
84 | * @return Converted value in the provided currency
85 | * @throws APIException If the server returns an error
86 | */
87 | public BigDecimal toFiat (String currency, BigDecimal value) throws APIException, IOException {
88 | Map params = new HashMap();
89 | params.put("currency", currency);
90 | params.put("value", String.valueOf(value.multiply(BigDecimal.valueOf(100000000L)))); // The endpoint is expecting satoshi
91 | if (apiCode != null) {
92 | params.put("api_code", apiCode);
93 | }
94 |
95 | String response = HttpClient.getInstance().get("frombtc", params);
96 | return new BigDecimal(response);
97 | }
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/src/main/java/info/blockchain/api/pushtx/PushTx.java:
--------------------------------------------------------------------------------
1 | package info.blockchain.api.pushtx;
2 |
3 | import info.blockchain.api.APIException;
4 | import info.blockchain.api.HttpClient;
5 |
6 | import java.io.IOException;
7 | import java.util.HashMap;
8 | import java.util.Map;
9 |
10 | /**
11 | * This class reflects the functionality provided at
12 | * https://blockchain.info/pushtx. It allows users to broadcast hex encoded
13 | * transactions to the bitcoin network.
14 | */
15 | public class PushTx {
16 | /**
17 | * Pushes a hex encoded transaction to the network.
18 | *
19 | * @param tx Hex encoded transaction
20 | * @throws APIException If the server returns an error (malformed tx etc.)
21 | */
22 | public static void pushTx (String tx) throws APIException, IOException {
23 | pushTx(tx, null);
24 | }
25 |
26 | /**
27 | * Pushes a hex encoded transaction to the network.
28 | *
29 | * @param tx Hex encoded transaction
30 | * @param apiCode Blockchain.info API code (optional, nullable)
31 | * @throws APIException If the server returns an error (malformed tx etc.)
32 | */
33 | public static void pushTx (String tx, String apiCode) throws APIException, IOException {
34 | Map params = new HashMap();
35 | params.put("tx", tx);
36 |
37 | if (apiCode != null) {
38 | params.put("api_code", apiCode);
39 | }
40 |
41 | HttpClient.getInstance().post("pushtx", params);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/info/blockchain/api/receive/CallbackLog.java:
--------------------------------------------------------------------------------
1 | package info.blockchain.api.receive;
2 |
3 | import com.google.gson.JsonObject;
4 |
5 | public class CallbackLog {
6 | private String callback;
7 | private String callbackTime;
8 | private String response;
9 | private int responseCode;
10 |
11 | public CallbackLog (JsonObject callbackJson) {
12 | this(
13 | callbackJson.has("callback") ? callbackJson.get("callback").getAsString() : "",
14 | callbackJson.has("called_at") ? callbackJson.get("called_at").getAsString() : "",
15 | callbackJson.has("raw_response") ? callbackJson.get("raw_response").getAsString() : "",
16 | callbackJson.has("response_code") ? callbackJson.get("response_code").getAsInt() : 0
17 | );
18 | }
19 |
20 | public CallbackLog (String callback, String callbackTime, String response, int responseCode) {
21 | this.callback = callback;
22 | this.callbackTime = callbackTime;
23 | this.response = response;
24 | this.responseCode = responseCode;
25 | }
26 |
27 | public String getCallback () {
28 | return callback;
29 | }
30 |
31 | public void setCallback (String callback) {
32 | this.callback = callback;
33 | }
34 |
35 | public String getCallbackTime () {
36 | return callbackTime;
37 | }
38 |
39 | public void setCallbackTime (String callbackTime) {
40 | this.callbackTime = callbackTime;
41 | }
42 |
43 | public String getResponse () {
44 | return response;
45 | }
46 |
47 | public void setResponse (String response) {
48 | this.response = response;
49 | }
50 |
51 | public int getResponseCode () {
52 | return responseCode;
53 | }
54 |
55 | public void setResponseCode (int responseCode) {
56 | this.responseCode = responseCode;
57 | }
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/info/blockchain/api/receive/Receive.java:
--------------------------------------------------------------------------------
1 | package info.blockchain.api.receive;
2 |
3 | import com.google.gson.JsonObject;
4 | import com.google.gson.JsonParser;
5 | import info.blockchain.api.APIException;
6 | import info.blockchain.api.HttpClient;
7 |
8 | import java.io.IOException;
9 | import java.util.HashMap;
10 | import java.util.Map;
11 |
12 | /**
13 | * This class reflects the functionality necessary for using the receive-payments-api v2.
14 | * Passing on a xPUB, callbackUrl and the apiCode will return an address for receiving a payment.
15 | *
16 | * Upon receiving a payment on this address, the merchant will be notified using the callback URL.
17 | */
18 | public class Receive {
19 | private final String apiCode;
20 |
21 | /**
22 | * @param apiCode Blockchain.info API code for the receive-payments v2 API (different from normal API key)
23 | */
24 | public Receive (String apiCode) {
25 | this.apiCode = apiCode;
26 | }
27 |
28 | /**
29 | * Calls the receive-payments-api v2 and returns an address for the payment.
30 | *
31 | * @param xPUB Destination address where the payment should be sent
32 | * @param callbackUrl Callback URI that will be called upon payment
33 | * @return An instance of the ReceiveV2Response class
34 | * @throws APIException If the server returns an error
35 | */
36 | public ReceiveResponse receive (String xPUB, String callbackUrl) throws APIException, IOException {
37 | Map params = new HashMap();
38 | params.put("xpub", xPUB);
39 | params.put("callback", callbackUrl);
40 | params.put("key", apiCode);
41 |
42 | String response = HttpClient.getInstance().get("https://api.blockchain.info/", "v2/receive", params);
43 | JsonParser parser = new JsonParser();
44 | JsonObject obj = parser.parse(response).getAsJsonObject();
45 |
46 | return new ReceiveResponse(obj.get("index").getAsInt(), obj.get("address").getAsString(), obj.get("callback").getAsString());
47 | }
48 |
49 | /**
50 | * Calls the receive-payments-api v2 and returns the xpub gap of an xpub.
51 | *
52 | * @param xPUB Destination address where the payment should be sent
53 | * @return An instance of the ReceiveV2Response class
54 | * @throws APIException If the server returns an error
55 | */
56 | public int checkGap (String xPUB) throws APIException, IOException {
57 | Map params = new HashMap();
58 | params.put("xpub", xPUB);
59 | params.put("key", apiCode);
60 |
61 | String response = HttpClient.getInstance().get("https://api.blockchain.info/", "v2/receive/checkgap", params);
62 | JsonObject obj = new JsonParser().parse(response).getAsJsonObject();
63 |
64 | return obj.get("gap").getAsInt();
65 | }
66 |
67 | /**
68 | * Calls the receive-payments-api v2 and returns the callback log based on url.
69 | *
70 | * @param callbackUrl Callback URI that will be called upon payment
71 | * @return An instance of the ReceiveV2Response class
72 | * @throws APIException If the server returns an error
73 | */
74 | public CallbackLog getCallbackLog (String callbackUrl) throws APIException, IOException {
75 | Map params = new HashMap();
76 | params.put("callback", callbackUrl);
77 | params.put("key", apiCode);
78 |
79 | String response = HttpClient.getInstance().get("https://api.blockchain.info/", "v2/receive/callback", params);
80 | JsonObject obj = new JsonParser().parse(response).getAsJsonObject();
81 |
82 | return new CallbackLog(obj);
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/src/main/java/info/blockchain/api/receive/ReceiveResponse.java:
--------------------------------------------------------------------------------
1 | package info.blockchain.api.receive;
2 |
3 | /**
4 | * This class is used as a response object to the `ReceiveV2.receive` method.
5 | */
6 | public class ReceiveResponse {
7 | private int index;
8 | private String receivingAddress;
9 | private String callbackUrl;
10 |
11 | public ReceiveResponse (int index, String receivingAddress, String callbackUrl) {
12 | this.index = index;
13 | this.receivingAddress = receivingAddress;
14 | this.callbackUrl = callbackUrl;
15 | }
16 |
17 | /**
18 | * @return Index of the address in the account
19 | */
20 | public int getIndex () {
21 | return index;
22 | }
23 |
24 | /**
25 | * @return Address to be displayed for the customer at checkout.
26 | */
27 | public String getReceivingAddress () {
28 | return receivingAddress;
29 | }
30 |
31 | /**
32 | * @return Callback URI that will be called upon payment
33 | */
34 | public String getCallbackUrl () {
35 | return callbackUrl;
36 | }
37 | }
--------------------------------------------------------------------------------
/src/main/java/info/blockchain/api/statistics/Chart.java:
--------------------------------------------------------------------------------
1 | package info.blockchain.api.statistics;
2 |
3 | import com.google.gson.JsonElement;
4 | import com.google.gson.JsonObject;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | public class Chart {
10 |
11 | private String status;
12 | private String name;
13 | private String unit;
14 | private String period;
15 | private String description;
16 | private List values;
17 |
18 | public Chart (JsonObject chartJson) {
19 | this(
20 | chartJson.has("status") ? chartJson.get("status").getAsString() : "",
21 | chartJson.has("name") ? chartJson.get("name").getAsString() : "",
22 | chartJson.has("unit") ? chartJson.get("unit").getAsString() : "",
23 | chartJson.has("period") ? chartJson.get("period").getAsString() : "",
24 | chartJson.has("description") ? chartJson.get("description").getAsString() : "",
25 | null
26 | );
27 | values = getPoints(chartJson);
28 | }
29 |
30 | public Chart (String status, String name, String unit, String period, String description, List values) {
31 | this.status = status;
32 | this.name = name;
33 | this.unit = unit;
34 | this.period = period;
35 | this.description = description;
36 | this.values = values;
37 | }
38 |
39 | private List getPoints(JsonObject chartJson) {
40 | List points = new ArrayList();
41 | for (JsonElement pointElement : chartJson.getAsJsonArray()) {
42 | JsonObject pointJson = pointElement.getAsJsonObject();
43 | points.add(new Point(pointJson));
44 | }
45 |
46 | return points;
47 | }
48 |
49 | public String getStatus() {
50 | return status;
51 | }
52 |
53 | public String getName() {
54 | return name;
55 | }
56 |
57 | public String getUnit() {
58 | return unit;
59 | }
60 |
61 | public String getPeriod() {
62 | return period;
63 | }
64 |
65 | public String getDescription() {
66 | return description;
67 | }
68 |
69 | public List getValues() {
70 | return values;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/main/java/info/blockchain/api/statistics/Point.java:
--------------------------------------------------------------------------------
1 | package info.blockchain.api.statistics;
2 |
3 | import com.google.gson.JsonObject;
4 |
5 | public class Point {
6 |
7 | private float x;
8 | private float y;
9 |
10 | public Point (JsonObject pointJson) {
11 | this(
12 | pointJson.has("x") ? pointJson.get("x").getAsFloat() : 0.0F,
13 | pointJson.has("y") ? pointJson.get("y").getAsFloat() : 0.0F
14 | );
15 | }
16 |
17 | public Point (float x, float y) {
18 | this.x = x;
19 | this.y = y;
20 | }
21 |
22 | public float getX() {
23 | return x;
24 | }
25 |
26 | public float getY() {
27 | return y;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/info/blockchain/api/statistics/Statistics.java:
--------------------------------------------------------------------------------
1 | package info.blockchain.api.statistics;
2 |
3 | import com.google.gson.Gson;
4 | import com.google.gson.JsonObject;
5 | import com.google.gson.JsonParser;
6 | import com.google.gson.reflect.TypeToken;
7 | import info.blockchain.api.APIException;
8 | import info.blockchain.api.HttpClient;
9 |
10 | import java.io.IOException;
11 | import java.lang.reflect.Type;
12 | import java.util.HashMap;
13 | import java.util.Map;
14 |
15 | /**
16 | * This class reflects the functionality documented
17 | * at https://blockchain.info/api/charts_api
18 | */
19 | public class Statistics {
20 | private final String apiCode;
21 |
22 | public Statistics () {
23 | this(null);
24 | }
25 |
26 | public Statistics (String apiCode) {
27 | this.apiCode = apiCode;
28 | }
29 |
30 | /**
31 | * Gets the network statistics.
32 | *
33 | * @return An instance of the StatisticsResponse class
34 | * @throws APIException If the server returns an error
35 | */
36 | public StatisticsResponse getStats () throws APIException, IOException {
37 | Map params = new HashMap();
38 | params.put("format", "json");
39 | if (apiCode != null) {
40 | params.put("api_code", apiCode);
41 | }
42 |
43 | String response = HttpClient.getInstance().get("stats", params);
44 | return new StatisticsResponse(response);
45 | }
46 |
47 | /**
48 | * This method can be used to get and manipulate data behind all Blockchain.info's charts.
49 | *
50 | * @param type of chart (Example: transactions-per-second, total-bitcoins)
51 | * @param timeSpan (Example: 5weeks)
52 | * @param rollingAverage (Example: 8hours)
53 | * @return {@code Chart} represents the series of data of the chart
54 | * @throws APIException If the server returns an error
55 | */
56 | public Chart getChart(String type, String timeSpan, String rollingAverage) throws APIException, IOException {
57 | Map params = new HashMap();
58 | params.put("format", "json");
59 | if (apiCode != null) {
60 | params.put("api_code", apiCode);
61 | }
62 | if (timeSpan != null) {
63 | params.put("timespan", timeSpan);
64 | }
65 | if (rollingAverage != null) {
66 | params.put("rollingAverage", rollingAverage);
67 | }
68 |
69 | String response = HttpClient.getInstance().get("charts/" + type, params);
70 | JsonObject chartJson = new JsonParser().parse(response).getAsJsonObject();
71 |
72 | return new Chart(chartJson);
73 | }
74 |
75 | /**
76 | * This method can be used to get the data behind Blockchain.info's pools information.
77 | *
78 | * @param timeSpan (Example: 5weeks)
79 | * @return a map of pool name and the number of blocks it mined
80 | * @throws APIException If the server returns an error
81 | */
82 | public Map getPools(String timeSpan) throws APIException, IOException {
83 | Map params = new HashMap();
84 | params.put("format", "json");
85 | if (apiCode != null) {
86 | params.put("api_code", apiCode);
87 | }
88 | if (timeSpan != null) {
89 | params.put("timespan", timeSpan);
90 | }
91 |
92 | String response = HttpClient.getInstance().get("pools", params);
93 | Type type = new TypeToken