Skip to content

Sample app in Rust that decodes Binance API SBE

License

Notifications You must be signed in to change notification settings

binance/binance-sbe-rust-sample-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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). It decodes the payload from STDIN and prints it as YAML to STDOUT.

Getting Started

  1. Clone the source code:
git clone [email protected]:binance/binance-sbe-rust-sample-app.git
  1. Navigate to the directory where the Cargo.toml file is located and build sbe-sample-app with the following command:
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

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

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, 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):
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.

  1. Clone & build simple-binary-encoding:
git clone https://github.com/real-logic/simple-binary-encoding.git --branch 1.30.0
cd simple-binary-encoding
./gradlew
cd ..
  1. Run the SbeTool code generator built in the previous step:
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
  1. Format the generated code:
cargo fmt -p spot_sbe
  1. Add the following to the top of spot_sbe/src/lib.rs:
#![allow(ambiguous_glob_reexports)]
  1. Fix clippy warnings:
cargo clippy --fix -p spot_sbe --allow-dirty -- -D clippy::all

Releases

No releases published

Packages

No packages published

Languages