Skip to content

Commit b7e4e92

Browse files
committed
Add support for Bitflyer's "me/getbalance" endpoint.
1 parent 1dfa0de commit b7e4e92

File tree

5 files changed

+109
-1
lines changed

5 files changed

+109
-1
lines changed

xchange-bitflyer/src/main/java/org/knowm/xchange/bitflyer/Bitflyer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javax.ws.rs.core.MediaType;
1212

1313
import org.knowm.xchange.bitflyer.dto.BitflyerException;
14+
import org.knowm.xchange.bitflyer.dto.account.BitflyerBalance;
1415
import org.knowm.xchange.bitflyer.dto.account.BitflyerMarginAccount;
1516
import org.knowm.xchange.bitflyer.dto.account.BitflyerMarginStatus;
1617
import org.knowm.xchange.bitflyer.dto.account.BitflyerMarginTransaction;
@@ -78,6 +79,12 @@ BitflyerMarginStatus getMarginStatus(@HeaderParam(ACCESS_KEY) String apiKey,
7879
@HeaderParam(ACCESS_TIMESTAMP) SynchronizedValueFactory<Long> nonce,
7980
@HeaderParam(ACCESS_SIGN) ParamsDigest paramsDigest) throws IOException, BitflyerException;
8081

82+
@GET
83+
@Path("/me/getbalance")
84+
List<BitflyerBalance> getBalances(@HeaderParam(ACCESS_KEY) String apiKey,
85+
@HeaderParam(ACCESS_TIMESTAMP) SynchronizedValueFactory<Long> nonce,
86+
@HeaderParam(ACCESS_SIGN) ParamsDigest paramsDigest) throws IOException, BitflyerException;
87+
8188
@GET
8289
@Path("me/getcollateralaccounts")
8390
List<BitflyerMarginAccount> getMarginAccounts(@HeaderParam(ACCESS_KEY) String apiKey,

xchange-bitflyer/src/main/java/org/knowm/xchange/bitflyer/BitflyerAdapters.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
import java.util.regex.Matcher;
1010
import java.util.regex.Pattern;
1111

12+
import org.knowm.xchange.bitflyer.dto.account.BitflyerBalance;
1213
import org.knowm.xchange.bitflyer.dto.account.BitflyerMarket;
1314
import org.knowm.xchange.bitflyer.dto.marketdata.BitflyerTicker;
1415
import org.knowm.xchange.currency.Currency;
1516
import org.knowm.xchange.currency.CurrencyPair;
17+
import org.knowm.xchange.dto.account.Balance;
18+
import org.knowm.xchange.dto.account.Wallet;
1619
import org.knowm.xchange.dto.marketdata.Ticker;
1720
import org.knowm.xchange.dto.meta.CurrencyMetaData;
1821
import org.knowm.xchange.dto.meta.CurrencyPairMetaData;
@@ -41,6 +44,25 @@ public static CurrencyPair adaptCurrencyPair(String productCode) {
4144
return currencies.size() >= 2 ? new CurrencyPair(currencies.get(0), currencies.get(1)) : null;
4245
}
4346

47+
/**
48+
* Adapts a list of BitflyerBalance objects to Wallet.
49+
*
50+
* @param balances Some BitflyerBalances from the API
51+
* @return A Wallet with balances in it
52+
*/
53+
public static Wallet adaptAccountInfo(List<BitflyerBalance> balances) {
54+
List<Balance> adaptedBalances = new ArrayList<>(balances.size());
55+
56+
for (BitflyerBalance balance : balances) {
57+
adaptedBalances.add(new Balance(
58+
Currency.getInstance(balance.getCurrencyCode()),
59+
balance.getAmount(),
60+
balance.getAvailable()));
61+
}
62+
63+
return new Wallet(adaptedBalances);
64+
}
65+
4466
/**
4567
* Adapts a BitflyerTicker to a Ticker Object
4668
*
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.knowm.xchange.bitflyer.dto.account;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
7+
8+
import java.math.BigDecimal;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
@JsonInclude(JsonInclude.Include.NON_NULL)
13+
@JsonPropertyOrder({
14+
"currency_code",
15+
"amount",
16+
"available"
17+
})
18+
public class BitflyerBalance {
19+
@JsonProperty("currency_code")
20+
private String currencyCode;
21+
@JsonProperty("amount")
22+
private BigDecimal amount;
23+
@JsonProperty("available")
24+
private BigDecimal available;
25+
@JsonIgnore
26+
private Map<String, Object> additionalProperties = new HashMap<>();
27+
28+
public String getCurrencyCode() {
29+
return currencyCode;
30+
}
31+
32+
public void setCurrencyCode(String currencyCode) {
33+
this.currencyCode = currencyCode;
34+
}
35+
36+
public BigDecimal getAmount() {
37+
return amount;
38+
}
39+
40+
public void setAmount(BigDecimal amount) {
41+
this.amount = amount;
42+
}
43+
44+
public BigDecimal getAvailable() {
45+
return available;
46+
}
47+
48+
public void setAvailable(BigDecimal available) {
49+
this.available = available;
50+
}
51+
52+
public Map<String, Object> getAdditionalProperties() {
53+
return additionalProperties;
54+
}
55+
56+
public void setAdditionalProperties(Map<String, Object> additionalProperties) {
57+
this.additionalProperties = additionalProperties;
58+
}
59+
60+
@Override
61+
public String toString() {
62+
return "BitflyerBalance{" +
63+
"currencyCode='" + currencyCode + '\'' +
64+
", amount=" + amount +
65+
", available=" + available +
66+
", additionalProperties=" + additionalProperties +
67+
'}';
68+
}
69+
}

xchange-bitflyer/src/main/java/org/knowm/xchange/bitflyer/service/BitflyerAccountService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66

77
import org.knowm.xchange.Exchange;
8+
import org.knowm.xchange.bitflyer.BitflyerAdapters;
89
import org.knowm.xchange.currency.Currency;
910
import org.knowm.xchange.dto.account.AccountInfo;
1011
import org.knowm.xchange.dto.account.FundingRecord;
@@ -25,7 +26,7 @@ public BitflyerAccountService(Exchange exchange) {
2526

2627
@Override
2728
public AccountInfo getAccountInfo() throws IOException {
28-
throw new NotYetImplementedForExchangeException();
29+
return new AccountInfo(BitflyerAdapters.adaptAccountInfo(getBitflyerBalances()));
2930
}
3031

3132
@Override

xchange-bitflyer/src/main/java/org/knowm/xchange/bitflyer/service/BitflyerAccountServiceRaw.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import org.knowm.xchange.Exchange;
77
import org.knowm.xchange.bitflyer.dto.BitflyerException;
8+
import org.knowm.xchange.bitflyer.dto.account.BitflyerBalance;
89
import org.knowm.xchange.bitflyer.dto.account.BitflyerMarginAccount;
910
import org.knowm.xchange.bitflyer.dto.account.BitflyerMarginStatus;
1011
import org.knowm.xchange.bitflyer.dto.account.BitflyerMarginTransaction;
@@ -30,6 +31,14 @@ public BitflyerMarginStatus getBitflyerMarginStatus() throws IOException {
3031
}
3132
}
3233

34+
public List<BitflyerBalance> getBitflyerBalances() throws IOException {
35+
try {
36+
return bitflyer.getBalances(apiKey, exchange.getNonceFactory(), signatureCreator);
37+
} catch (BitflyerException e) {
38+
throw handleError(e);
39+
}
40+
}
41+
3342
public List<BitflyerMarginAccount> getBitflyerMarginAccounts() throws IOException {
3443

3544
try {

0 commit comments

Comments
 (0)