https://github.com/binance/binance-sbe-java-sample-app
Sample app in Java that decodes Binance "exchangeInfo" endpoint's SBE response to YAML.
https://github.com/binance/binance-sbe-java-sample-app
binance-api crypto java sample sbe
Last synced: 8 months ago
JSON representation
Sample app in Java that decodes Binance "exchangeInfo" endpoint's SBE response to YAML.
- Host: GitHub
- URL: https://github.com/binance/binance-sbe-java-sample-app
- Owner: binance
- License: mit
- Created: 2023-11-27T09:46:42.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-10-03T06:47:25.000Z (over 1 year ago)
- Last Synced: 2024-11-08T08:38:21.990Z (over 1 year ago)
- Topics: binance-api, crypto, java, sample, sbe
- Language: Java
- Homepage:
- Size: 87.9 KB
- Stars: 5
- Watchers: 4
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# SBE Java Sample Application
This sample application is designed to decode `exchangeInfo` endpoint's response to YAML, when using the [Binance Spot API Simple Binary Encoding (SBE)](https://github.com/binance/binance-spot-api-docs/blob/master/faqs/sbe_faq.md).
Moreover, there are additional decoder classes that can be useful for decoding SBE responses from other endpoints in the Binance Spot API.
## Getting Started
1. Clone the source code to your local end.
```shell
git clone git@github.com:binance/binance-sbe-java-sample-app
```
2. Navigate to the directory where `pom.xml` file is located and build the project with `Maven` to generate `./target/sbe-sample-app-1.0-SNAPSHOT.jar`.
```shell
mvn clean install
```
## Usage
The following commands use `./target/sbe-sample-app-1.0-SNAPSHOT.jar` to output on STDOUT the `exchangeInfo` SBE response in YAML.
### Via reading SBE-encoded HTTP response from STDIN
```shell
curl -X GET -H 'Accept: application/sbe' -H 'X-MBX-SBE: 1:0' 'https://api.binance.com/api/v3/exchangeInfo' | java -jar ./target/sbe-sample-app-1.0-SNAPSHOT.jar -
```
### Via reading SBE-encoded WebSocket response from STDIN
```shell
echo '{"id":"93fb61ef-89f8-4d6e-b022-4f035a3fadad","method":"exchangeInfo","params":{}}' | ./tools/websocket_send.py 'wss://ws-api.binance.com:443/ws-api/v3?responseFormat=sbe&sbeSchemaId=1&sbeSchemaVersion=0' | java -jar ./target/sbe-sample-app-1.0-SNAPSHOT.jar -
```
Note: To run `websocket_send.py`, your Python 3 environment should include the `websocket-client` package with the integrated `create_connection` function.
### Via downloading SBE-encoded HTTP response within the app
```shell
java -jar ./target/sbe-sample-app-1.0-SNAPSHOT.jar 'https://api.binance.com/api/v3/exchangeInfo'
```
The `spot_sbe` directory contains the decoder classes, which you can consult to explore other endpoints.
### Testnet
To use the Spot Testnet API, you only need to replace:
- `api.binance.com` with `testnet.binance.vision` for the REST API
- `ws-api.binance.com` with `testnet.binance.vision` for the WebSocket API
## Update
### Java decoders
The `src/main/java/spot_sbe` directory contains code generated by [simple-binary-encoding](https://github.com/real-logic/simple-binary-encoding), which you will likely want to reuse verbatim. However, if you would like to update it, please navigate to the root of this project and follow these steps:
1) Download the [spot_prod_latest.xml](https://github.com/binance/binance-spot-api-docs/blob/master/sbe/schemas/spot_prod_latest.xml) schema file to `src/main/resources`:
```shell
mkdir -p src/main/resources
cd src/main/resources
curl -o spot_latest.xml https://raw.githubusercontent.com/binance/binance-spot-api-docs/master/sbe/schemas/$(curl -s https://raw.githubusercontent.com/binance/binance-spot-api-docs/master/sbe/schemas/spot_prod_latest.xml)
cd -
```
**Note:** If you are using the Spot Testnet API, replace `spot_prod_latest.xml` with `spot_testnet_latest.xml` in the above `curl ` command.
2) Clone & build [simple-binary-encoding](https://github.com/real-logic/simple-binary-encoding):
```shell
git clone https://github.com/real-logic/simple-binary-encoding.git --branch '1.30.0'
cd simple-binary-encoding
./gradlew
cd ..
```
3) Run the SBE tool code generator built in the previous step:
```shell
java \
-Dsbe.output.dir=src/main/java/ \
-Dsbe.target.language=Java \
-jar simple-binary-encoding/sbe-all/build/libs/sbe-all-1.30.0.jar \
src/main/resources/spot_latest.xml
```
4) Remove unneeded encoder files:
```shell
find src/main/java/spot_sbe -type f -name '*Encoder.java' -exec rm '{}' \;
```