Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi , i try to modify this example api-usage-examples/V5_demo/api_demo/Encryption_HMAC.sh at master · bybit-exchange/api-usage-examples · GitHub[^]
wiht this code
_ByBit_PlaceOrder1() {
    # Read Bybit API keys and set them as global variables
    _Init_BYBIT_ApiSecretIDData "bybit"

    # API URL for placing an order
    local apiUrl="${ServerBybit}v5/order/create"
    local RECVWINDOW=5000
    local TIMESTAMP=$(($(date +%s%N)/1000000))

    # Set your timestamp inside the window of reception
    local TIMESTAMP_INSIDE_WINDOW=$((TIMESTAMP + RECVWINDOW))

    # Compare with the current timestamp
    if [ "$TIMESTAMP_INSIDE_WINDOW" -gt "$TIMESTAMP" ]; then
        local URLPART2=""
        local category=""
        local symbol=""
        local side=""
        local positionidx=""
        local ordertype=""
        local qty=""
        local price=""
        local timeinforce=""
        local orderlinkid=""

        # Read function parameters
        while [ "$#" -gt 0 ]; do
            case "$1" in
                "--category" )
                    shift
                    category="$1"
                    ;;
                "--symbol" )
                    shift
                    symbol="$1"
                    ;;
                "--side" )
                    shift
                    side="$1"
                    ;;
                "--position-idx" )
                    shift
                    positionidx="$1"
                    ;;
                "--order-type" )
                    shift
                    ordertype="$1"
                    ;;
                "--qty" )
                    shift
                    qty="$1"
                    ;;
                "--price" )
                    shift
                    price="$1"
                    ;;
                "--time-in-force" )
                    shift
                    timeinforce="$1"
                    ;;
                "--order-link-id" )
                    shift
                    orderlinkid="$1"
                    ;;
                * )
                    echo "Error: Unrecognized parameter: $1"
                    return 1
                    ;;
            esac
            shift
        done

        # Check if mandatory parameters are present
        if [ -z "$symbol" ] || [ -z "$category" ]; then
            echo "Error: Parameters --symbol, --category are required"
            return 1
        fi

        # Construct the original string for HMAC signature
        local ORIGINAL_STRING="${TIMESTAMP}${BYBITApiKey}${URLPART2}"
        URLPART2="category:${category},symbol:${symbol}," 
        [ "$side" ] && URLPART2+="side:${side},"
        [ "$positionidx" ] && URLPART2+="positionIdx:${positionidx},"
        [ "$ordertype" ] && URLPART2+="orderType:${ordertype},"
        [ "$qty" ] && URLPART2+="qty:${qty},"
        [ "$price" ] && URLPART2+="price:${price},"
        [ "$timeinforce" ] && URLPART2+="timeInForce:${timeinforce},"
        [ "$orderlinkid" ] && URLPART2+="orderLinkId:${orderlinkid}"

        # Calculate HMAC signature
        local SIGNATURE=$(echo -n "$ORIGINAL_STRING" | openssl dgst -sha256 -hmac "$BYBITSecretKey" | cut -c 18-)

        response=$(curl --location --request POST "${apiUrl}" \
        --header 'X-BAPI-SIGN-TYPE: 2' \
        --header 'X-BAPI-SIGN: '${SIGNATURE} \
        --header 'X-BAPI-API-KEY: '${BYBITApiKey} \
        --header 'X-BAPI-TIMESTAMP: '${TIMESTAMP_INSIDE_WINDOW}  \
        --header 'X-BAPI-RECV-WINDOW: 5000' \
        --header 'Content-Type: application/json' \
        --data-raw '{"'"$URLPART2"'"}')

        echo "$response"
        
    else
        echo "Error: Correct timestamp is outside the reception window."
        return 1
    fi
}

# Example call to the function
_ByBit_PlaceOrder1 --category "linear" --symbol "XLMDAI"  --side "SELL" --position-idx "0" --order-type "LIMIT" --qty "0.001" --time-in-force "GTC" --order-link-id "TEST1234"


What I have tried:

but retrun me
{"retCode":10002,"retMsg":"invalid request, please check your server timestamp or recv_window param. req_timestamp[1709483531664],server_timestamp[1709483526869],recv_window[5000]","result":{},"retExtInfo":{},"time":1709483526869}
any one can help me ?
Posted
Comments
Richard MacCutchan 3-Mar-24 12:16pm    
The message is clear, but the reason can only be guessed. You need to talk to the writer of the original code, or the owners of the server system.

1 solution

Hi,

So far I can only spot that you are using $URLPART2 in the definition of ORIGINAL_STRING before it is filled in with values (so URLPART2 is empty at this time). If is shall be empty, omit it, if it shall be populated, use it once it is populated. But I am not sure if that even relates to the issue you are facing.

Shell
local ORIGINAL_STRING="${TIMESTAMP}${BYBITApiKey}${URLPART2}"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900