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.toml
file is located and buildsbe-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.
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: 1:0' \
'https://api.binance.com/api/v3/exchangeInfo' \
| ./target/debug/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.
To use the Spot Testnet API, you only need to replace:
api.binance.com
withtestnet.binance.vision
for the REST APIws-api.binance.com
withtestnet.binance.vision
for 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://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.
- 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 ..
- 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
- 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