199 lines
5.3 KiB
Bash
Executable File
199 lines
5.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
help_text="\
|
|
Manage orders and user data at Binance.
|
|
|
|
Usage: binance_rest_client.sh [OPTIONS]
|
|
|
|
Options:
|
|
--usds-future Use USDS Futures venue
|
|
--coin-future Use Coin Futures venue
|
|
--spot Use Spots venue
|
|
--us Use US venue
|
|
--prod Use production url
|
|
--dev Use development url
|
|
--dry Print the command to console without actually running it
|
|
--op CREATE_ORDER | CANCEL_REPLACE | OPEN_ORDERS | CANCEL_OPEN_ORDERS | ACCOUNT
|
|
--<key>=<value> Query parameter (e.g., --symbol=btcusdt, --limit=5)
|
|
|
|
"
|
|
|
|
function print_help_and_exit() {
|
|
echo "$help_text"
|
|
exit "$1"
|
|
}
|
|
|
|
declare this_dir="$(readlink -f $(dirname ${BASH_SOURCE[0]}))"
|
|
source "$this_dir/binance.env"
|
|
|
|
env_name="binance"
|
|
venue="BINANCE"
|
|
declare -u mode
|
|
declare -u op
|
|
declare -A query_params
|
|
declare -i dry
|
|
dry=0
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--usds-future)
|
|
venue="BINANCE_USDS_FUTURES"
|
|
shift 1
|
|
;;
|
|
--coin-future)
|
|
venue="BINANCE_COIN_FUTURES"
|
|
shift 1
|
|
;;
|
|
--spot)
|
|
venue="BINANCE"
|
|
shift 1
|
|
;;
|
|
--us)
|
|
venue="BINANCE_US"
|
|
shift 1
|
|
;;
|
|
--prod)
|
|
mode="PROD"
|
|
shift 1
|
|
;;
|
|
--dev)
|
|
mode="DEV"
|
|
shift 1
|
|
;;
|
|
--op)
|
|
if [[ -n "$op" ]]; then
|
|
echo "Unexpected argument '$1'"
|
|
print_help_and_exit 1
|
|
elif [[ -z "$2" ]]; then
|
|
echo "Op (--op) must be non-empty"
|
|
print_help_and_exit 1
|
|
fi
|
|
op="$2"
|
|
shift 2
|
|
;;
|
|
--*=*)
|
|
key="${1%%=*}"
|
|
key="${key#--}"
|
|
value="${1#*=}"
|
|
if [[ -z "$key" ]]; then
|
|
echo "Invalid --key=value arg (key is empty). $1"
|
|
print_help_and_exit 1
|
|
fi
|
|
query_params["$key"]="$value"
|
|
shift 1
|
|
;;
|
|
--dry)
|
|
dry=1
|
|
shift 1
|
|
;;
|
|
-h)
|
|
;&
|
|
--help)
|
|
print_help_and_exit 0
|
|
;;
|
|
*)
|
|
if [[ -z "$1" ]]; then
|
|
echo "Invalid positional argument '$1'"
|
|
print_help_and_exit 1
|
|
elif [[ -z "$symbol" ]]; then
|
|
symbol="$1"
|
|
else
|
|
echo "Unexpected argument '$1'"
|
|
print_help_and_exit 1
|
|
fi
|
|
shift 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [ -z "$mode" ]; then
|
|
mode="DEV"
|
|
fi
|
|
if [ -z "$op" ]; then
|
|
op="ACCOUNT"
|
|
fi
|
|
|
|
if [ ! -f "$this_dir/$env_name.env" ]; then
|
|
echo "Env file '$this_dir/$env_name.env' does not exist"
|
|
print_help_and_exit 1
|
|
fi
|
|
|
|
source "$this_dir/$env_name.env"
|
|
|
|
# The param names below represent environment variables. For example, if venue=BINANCE
|
|
# and mode=DEV, then the base url env var is BINANCE_DEV_URL.
|
|
base_url_name="${venue}_${mode}_URL"
|
|
path_name="${venue}_${op}_PATH"
|
|
params_name="${venue}_${op}_PARAMS"
|
|
api_key_name="${venue}_${mode}_API_KEY"
|
|
api_key=${!api_key_name}
|
|
secret_key_name="${venue}_${mode}_SECRET_KEY"
|
|
secret_key=${!secret_key_name}
|
|
method_name="${venue}_${op}_METHOD"
|
|
method="${!method_name:-GET}"
|
|
is_signed_name="${venue}_${op}_IS_SIGNED"
|
|
is_signed="${!is_signed_name:-0}"
|
|
is_keyed_name="${venue}_${op}_IS_KEYED"
|
|
is_keyed="${!is_keyed_name:-0}"
|
|
|
|
# Notes on query params
|
|
# =============================================================================
|
|
# The user specifies query parameters as --key=value args on the command line.
|
|
# For each venue/op, there is a query parameter template environment variable
|
|
# (see binance.env for details).
|
|
# - For example, if the venue is BINANCE, and the op is DEPTH, then the
|
|
# query parameter template variable is BINANCE_DEPTH_PARAMS. Perhaps this:
|
|
#
|
|
# BINANCE_DEPTH_PARAMS='symbol=${SYMBOL}&limit=${LIMIT:-5}&foo=${BAR:-OMIT}'
|
|
#
|
|
# - In this case, the command line arg '--symbol=<symbol>' is REQUIRED because
|
|
# it has no default value, while the arg '--limit=<limit>' is OPTIONAL, since
|
|
# it has a default value of 5. The last arg '--foo=<bar>' is also optional,
|
|
# but unlike the --limit=<limit> arg, it has a special default value, OMIT.
|
|
# This is not a true default value, but a sentinel to indicate the value is
|
|
# undefined, and should be omitted from the request.
|
|
# =============================================================================
|
|
|
|
# Translate --key=value args into variables, then expand the query params
|
|
# template and append it to the url.
|
|
for key in "${!query_params[@]}"; do
|
|
val=${query_params[$key]}
|
|
declare -u keyu
|
|
keyu="$key"
|
|
eval "$keyu=$val"
|
|
done
|
|
params_template=${!params_name}
|
|
eval "params=\"$params_template\""
|
|
# Remove undefined optional parameters (ones with a value of OMIT).
|
|
params=$(echo "$params" | sed -E 's/(&)?[^&=]+=OMIT//g' | sed 's/^&//;s/&$//')
|
|
|
|
# Sign the request (if necessary)
|
|
declare -A headers
|
|
if [ "$is_signed" -eq 1 ]; then
|
|
if [ -n "$params" ]; then
|
|
params="$params&"
|
|
fi
|
|
# Signed requests require a timestamp, and api key header
|
|
params="${params}timestamp=$(date +%s%3N)"
|
|
signature="$(echo -n "$params" | openssl dgst -sha256 -hmac "$secret_key" | awk '{print $NF}')"
|
|
params="$params&signature=$signature"
|
|
headers["X-MBX-APIKEY"]="$api_key"
|
|
elif [ "$is_keyed" -eq 1 ]; then
|
|
headers["X-MBX-APIKEY"]="$api_key"
|
|
fi
|
|
url="${!base_url_name}${!path_name}"
|
|
if [ -n "$params" ]; then
|
|
url="$url?$params"
|
|
fi
|
|
|
|
headers_str=""
|
|
for key in "${!headers[@]}"; do
|
|
headers_str+="-H \"${key}: ${headers[$key]}\" "
|
|
done
|
|
|
|
if [ $dry -ne 0 ]; then
|
|
echo curl $headers_str -X $method \"$url\"
|
|
else
|
|
eval curl $headers_str -X $method \"$url\"
|
|
fi
|