An open API service indexing awesome lists of open source software.

https://github.com/binance/binance-sbe-rust-sample-app

Sample app in Rust that decodes Binance API SBE
https://github.com/binance/binance-sbe-rust-sample-app

binance-api crypto rust sample-app sbe

Last synced: 10 months ago
JSON representation

Sample app in Rust that decodes Binance API SBE

Awesome Lists containing this project

README

          

# SBE Rust Sample Application

This sample application is designed to decode `exchangeInfo` endpoint's response to YAML, when using [Binance Spot API Simple Binary Encoding (SBE)](https://github.com/binance/binance-spot-api-docs/blob/master/faqs/sbe_faq.md). It decodes the payload from STDIN and prints it as YAML to STDOUT.

## Getting Started

1. Clone the source code:
```shell
git clone git@github.com:binance/binance-sbe-rust-sample-app.git
```

2. Navigate to the directory where the `Cargo.toml` file is located and build `sbe-sample-app` with the following command:
```shell
cargo build
```

**Note:** This sample application was built and tested with the Rust toolchain specified in the `rust-toolchain.toml` file.

## Usage

The following commands fetch the exchangeInfo SBE response, which the sbe-sample-app then decodes and outputs as YAML to STDOUT.

### Pipe REST exchangeInfo SBE response into sbe-sample-app

```shell
curl -X GET -H 'Accept: application/sbe' -H 'X-MBX-SBE: 1:0' \
'https://api.binance.com/api/v3/exchangeInfo' \
| ./target/debug/sbe-sample-app
```

### Pipe WebSocket exchangeInfo SBE response into sbe-sample-app

```shell
echo '{"id":"93fb61ef-89f8-4d6e-b022-4f035a3fadad","method":"exchangeInfo","params":{"symbol":"BTCUSDT"}}' \
| ./tools/websocket_send.py 'wss://ws-api.binance.com:443/ws-api/v3?responseFormat=sbe&sbeSchemaId=1&sbeSchemaVersion=0' \
| ./target/debug/sbe-sample-app
```

Note: To run `websocket_send.py`, your Python 3 environment should include the `websocket-client` package with the integrated `create_connection` function.

- There are additional decoder classes in the `spot_sbe` folder that can be useful for decoding SBE responses from other endpoints in the Binance Spot API.

### 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

## Updates

### Rust decoders

The `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 schema file ([spot_prod_latest.xml](https://github.com/binance/binance-spot-api-docs/blob/master/sbe/schemas/spot_prod_latest.xml)):
```shell
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)
```

**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 SbeTool code generator built in the previous step:
```shell
java \
-Dsbe.output.dir=. \
-Dsbe.target.language=Rust \
-jar simple-binary-encoding/sbe-all/build/libs/sbe-all-1.30.0.jar \
spot_latest.xml
```

4) Format the generated code:
```shell
cargo fmt -p spot_sbe
```

5) Add the following to the top of `spot_sbe/src/lib.rs`:
```rust
#![allow(ambiguous_glob_reexports)]
```

6) Fix clippy warnings:
```rust
cargo clippy --fix -p spot_sbe --allow-dirty -- -D clippy::all
```