Skip to content

Commit

Permalink
Merge pull request #4881 from bigscoop/binance-currencyinfo
Browse files Browse the repository at this point in the history
[binance] Add getting of currency infos
  • Loading branch information
timmolter authored May 22, 2024
2 parents 0b77a88 + a3d34f6 commit 8dad542
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 2 deletions.
1 change: 1 addition & 0 deletions xchange-binance/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http-client.private.env.json
11 changes: 11 additions & 0 deletions xchange-binance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Using IntelliJ Idea HTTP client

There are *.http files stored in `src/test/resources/rest` that can be used with IntelliJ Idea HTTP Client.

Some requests need authorization, so the api credentials have to be stored in `http-client.private.env.json` in module's root. Sample content can be found in `example.http-client.private.env.json`

> [!CAUTION]
> Never commit your api credentials to the repository!

[HTTP Client documentation](https://www.jetbrains.com/help/idea/http-client-in-product-code-editor.html)
6 changes: 6 additions & 0 deletions xchange-binance/example.http-client.private.env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"default": {
"api_key": "replace_me",
"api_secret": "replace_me"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@
import java.util.List;
import java.util.Map;
import org.knowm.xchange.binance.dto.BinanceException;
import org.knowm.xchange.binance.dto.account.*;
import org.knowm.xchange.binance.dto.account.AssetDetail;
import org.knowm.xchange.binance.dto.account.AssetDividendResponse;
import org.knowm.xchange.binance.dto.account.BinanceAccountInformation;
import org.knowm.xchange.binance.dto.account.BinanceCurrencyInfo;
import org.knowm.xchange.binance.dto.account.BinanceDeposit;
import org.knowm.xchange.binance.dto.account.BinanceWithdraw;
import org.knowm.xchange.binance.dto.account.DepositAddress;
import org.knowm.xchange.binance.dto.account.TransferHistory;
import org.knowm.xchange.binance.dto.account.TransferSubUserHistory;
import org.knowm.xchange.binance.dto.account.WithdrawResponse;
import org.knowm.xchange.binance.dto.trade.BinanceCancelledOrder;
import org.knowm.xchange.binance.dto.trade.BinanceDustLog;
import org.knowm.xchange.binance.dto.trade.BinanceListenKey;
Expand Down Expand Up @@ -329,6 +338,17 @@ BinanceDustLog getDustLog(
@QueryParam(SIGNATURE) ParamsDigest signature)
throws IOException, BinanceException;


@GET
@Path("/sapi/v1/capital/config/getall")
List<BinanceCurrencyInfo> getCurrencyInfos(
@QueryParam("recvWindow") Long recvWindow,
@QueryParam("timestamp") SynchronizedValueFactory<Long> timestamp,
@HeaderParam(X_MBX_APIKEY) String apiKey,
@QueryParam(SIGNATURE) ParamsDigest signature)
throws IOException, BinanceException;


/**
* Submit a withdraw request.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.knowm.xchange.binance.config.converter;

import com.fasterxml.jackson.databind.util.StdConverter;
import org.knowm.xchange.currency.Currency;

/** Converts string value {@code Currency} */
public class StringToCurrencyConverter extends StdConverter<String, Currency> {

@Override
public Currency convert(String value) {
return Currency.getInstance(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package org.knowm.xchange.binance.dto.account;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.math.BigDecimal;
import java.util.List;
import lombok.Builder;
import lombok.Data;
import lombok.extern.jackson.Jacksonized;
import org.knowm.xchange.binance.config.converter.StringToCurrencyConverter;
import org.knowm.xchange.currency.Currency;

@Data
@Builder
@Jacksonized
public class BinanceCurrencyInfo {

@JsonProperty("coin")
@JsonDeserialize(converter = StringToCurrencyConverter.class)
private Currency currency;

@JsonProperty("depositAllEnable")
private Boolean depositEnabled;

@JsonProperty("withdrawAllEnable")
private Boolean withdrawEnabled;

@JsonProperty("name")
private String name;

@JsonProperty("free")
private BigDecimal free;

@JsonProperty("locked")
private BigDecimal locked;

@JsonProperty("freeze")
private BigDecimal freeze;

@JsonProperty("withdrawing")
private BigDecimal withdrawing;

@JsonProperty("ipoing")
private BigDecimal ipoing;

@JsonProperty("ipoable")
private BigDecimal ipoable;

@JsonProperty("storage")
private BigDecimal storage;

@JsonProperty("isLegalMoney")
private Boolean isLegalMoney;

@JsonProperty("trading")
private Boolean trading;

@JsonProperty("networkList")
private List<Network> networks;


@Data
@Builder
@Jacksonized
public static class Network {

@JsonProperty("network")
private String id;

@JsonProperty("coin")
@JsonDeserialize(converter = StringToCurrencyConverter.class)
private Currency currency;

@JsonProperty("withdrawIntegerMultiple")
private BigDecimal withdrawIntegerMultiple;

@JsonProperty("isDefault")
private Boolean isDefault;

@JsonProperty("depositEnable")
private Boolean depositEnabled;

@JsonProperty("withdrawEnable")
private Boolean withdrawEnabled;

@JsonProperty("depositDesc")
private String depositDesc;

@JsonProperty("withdrawDesc")
private String withdrawDesc;

@JsonProperty("specialTips")
private String specialTips;

@JsonProperty("name")
private String name;

@JsonProperty("resetAddressStatus")
private Boolean resetAddressStatus;

@JsonProperty("addressRegex")
private String addressRegex;

@JsonProperty("memoRegex")
private String memoRegex;

@JsonProperty("withdrawFee")
private BigDecimal withdrawFee;

@JsonProperty("withdrawMin")
private BigDecimal withdrawMin;

@JsonProperty("withdrawMax")
private BigDecimal withdrawMax;

@JsonProperty("minConfirm")
private Integer minConfirm;

@JsonProperty("unLockConfirm")
private Integer unLockConfirm;

@JsonProperty("sameAddress")
private Boolean sameAddress;

@JsonProperty("estimatedArrivalTime")
private Integer estimatedArrivalTime;

@JsonProperty("busy")
private Boolean busy;

@JsonProperty("contractAddressUrl")
private String contractAddressUrl;

@JsonProperty("contractAddress")
private String contractAddress;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@
import org.knowm.xchange.binance.BinanceAdapters;
import org.knowm.xchange.binance.BinanceExchange;
import org.knowm.xchange.binance.dto.BinanceException;
import org.knowm.xchange.binance.dto.account.*;
import org.knowm.xchange.binance.dto.account.AssetDetail;
import org.knowm.xchange.binance.dto.account.AssetDividendResponse;
import org.knowm.xchange.binance.dto.account.BinanceAccountInformation;
import org.knowm.xchange.binance.dto.account.BinanceCurrencyInfo;
import org.knowm.xchange.binance.dto.account.BinanceDeposit;
import org.knowm.xchange.binance.dto.account.BinanceWithdraw;
import org.knowm.xchange.binance.dto.account.DepositAddress;
import org.knowm.xchange.binance.dto.account.TransferHistory;
import org.knowm.xchange.binance.dto.account.TransferSubUserHistory;
import org.knowm.xchange.binance.dto.account.WithdrawResponse;
import org.knowm.xchange.binance.dto.account.futures.BinanceFutureAccountInformation;
import org.knowm.xchange.client.ResilienceRegistries;
import org.knowm.xchange.currency.Currency;
Expand All @@ -30,6 +39,16 @@ public BinanceAccountInformation account() throws BinanceException, IOException
.call();
}


public List<BinanceCurrencyInfo> currencyInfos() throws BinanceException, IOException {
return decorateApiCall(
() -> binance.getCurrencyInfos(getRecvWindow(), getTimestampFactory(), apiKey, signatureCreator))
.withRetry(retry("currencyInfo"))
.withRateLimiter(rateLimiter(REQUEST_WEIGHT_RATE_LIMITER), 5)
.call();
}


public BinanceFutureAccountInformation futuresAccount() throws BinanceException, IOException {
return decorateApiCall(
() ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ public List<Ticker> getTickers(Params params) throws IOException {
}
}

@Override
public OrderBook getOrderBook(CurrencyPair currencyPair, Object... args) throws IOException {
return getOrderBook((Instrument) currencyPair, args);
}


@Override
public OrderBook getOrderBook(Instrument instrument, Object... args) throws IOException {
return getBinanceOrderBook(instrument, args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.knowm.xchange.binance.service.account;

import static org.assertj.core.api.Assertions.assertThat;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
Expand All @@ -11,6 +13,7 @@
import org.junit.Test;
import org.knowm.xchange.binance.BinanceExchangeIntegration;
import org.knowm.xchange.binance.dto.account.AssetDetail;
import org.knowm.xchange.binance.dto.account.BinanceCurrencyInfo;
import org.knowm.xchange.binance.dto.account.BinanceDeposit;
import org.knowm.xchange.binance.dto.account.TransferHistory;
import org.knowm.xchange.binance.service.BinanceAccountService;
Expand Down Expand Up @@ -48,6 +51,15 @@ public void testAssetDetail() throws Exception {
Assert.assertFalse(assetDetails.isEmpty());
}


@Test
public void testCurrencyInfos() throws Exception {
assumeProduction();
List<BinanceCurrencyInfo> currencyInfos = accountService.currencyInfos();
assertThat(currencyInfos).isNotEmpty();
}


@Test
public void testMetaData() {

Expand Down
16 changes: 16 additions & 0 deletions xchange-binance/src/test/resources/rest/sign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function gen_sign(request) {
const pattern = RegExp("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?");
const url = request.url.tryGetSubstituted();
const matches = url.match(pattern);

const timestamp = Math.floor(Date.now()).toFixed();
const query = matches[7] || "";

const payloadToSign = query.replace("&signature={{signature}}", "").replace("{{timestamp}}", timestamp);

const apiSecret = request.environment.get("api_secret");
const sign = crypto.hmac.sha256().withTextSecret(apiSecret).updateWithText(payloadToSign).digest().toHex();

request.variables.set("timestamp", timestamp);
request.variables.set("signature", sign);
}
8 changes: 8 additions & 0 deletions xchange-binance/src/test/resources/rest/wallet.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### All Coins' Information (USER_DATA)
< {%
import {gen_sign} from 'sign.js'
gen_sign(request);
%}

GET {{api_host}}/sapi/v1/capital/config/getall?timestamp={{timestamp}}&signature={{signature}}
X-MBX-APIKEY: {{api_key}}

0 comments on commit 8dad542

Please sign in to comment.