gateio / gateapi-nodejs

TypeScript client SDK for Gate APIv4 used in NodeJS(can be compiled to javascript)
79 stars 25 forks source link

Add Signature for Futures Websocket API #10

Open sghoul opened 2 years ago

sghoul commented 2 years ago

I have tried to make Websocket calls with auth without success so far.

I suggest adding a SIGN for futures Websocket requests.

ghost commented 2 years ago

Unfortunately, there are only examples written in python and go on the gate.io page. And again unfortunately some of the go examples are missing.

Anyway, first you need to connect to v4 websocket api.

Then you have to look at the code of which feature you want to use on the web page.

https://www.gate.io/docs/developers/apiv4/ws/en/

Now let's continue with the sample authentication code on the web page.

const crypto = require('crypto');

const WebSocket = require('ws');
const GateApi = require('gate-api');

let wsclient = new WebSocket('wss://api.gateio.ws/ws/v4/');

wsclient["on"]('message', data => {
    const obj = JSON.parse(data);

   // enjoy with data
});

wsclient["on"]('open', () => {
    console.log("Connected to Service");
     subscribe2Orders();
});

function subscribe2Orders() {
    const time = parseInt((new Date().getTime()) / 1000);
    const message = `channel=${"spot.orders"}&event=${"subscribe"}&time=${time}`;

    const hash = crypto.createHmac('sha512', API_SECRET)
        .update(message)
        .digest('hex');

    const request = {
        'id': (time * 1e6),
        'time': time,
        'channel': 'spot.orders',
        'event': 'subscribe',
        'payload': ["BTC_USDT"],
        'auth': { 'method': 'api_key', 'KEY': API_KEY, 'SIGN': hash }
    }

    wsclient.send(JSON.stringify(request));
}

Attention! 1 - npm i ws 2 - npm i gate-api don't forget to install

3 - Please do the necessary warnings and precautions for ApiKey and ApiSecret by following and checking the gate.io documentation link I gave above!

sghoul commented 2 years ago

Brilliant!! It works fine. Thanks!

On Wed, May 11, 2022 at 9:09 PM comnerd23 @.***> wrote:

Unfortunately, there are only examples written in python and go on the gate.io page. And again unfortunately some of the go examples are missing.

Anyway, first you need to connect to v4 websocket api.

Then you have to look at the code of which feature you want to use on the web page.

https://www.gate.io/docs/developers/apiv4/ws/en/

Now let's continue with the sample authentication code on the web page.

const crypto = require('crypto');

const WebSocket = require('ws'); const GateApi = require('gate-api');

let wsclient = new WebSocket('wss://api.gateio.ws/ws/v4/');

wsclient["on"]('message', data => { const obj = JSON.parse(data);

// enjoy with data });

wsclient["on"]('open', () => { console.log("Connected to Service"); subscribe2Orders(); });

function subscribe2Orders() { const time = parseInt((new Date().getTime()) / 1000); const message = channel=${"spot.orders"}&event=${"subscribe"}&time=${time};

const hash = crypto.createHmac('sha512', API_SECRET)
    .update(message)
    .digest('hex');

const request = {
    'id': (time * 1e6),
    'time': time,
    'channel': 'spot.orders',
    'event': 'subscribe',
    'payload': ["BTC_USDT"],
    'auth': { 'method': 'api_key', 'KEY': API_KEY, 'SIGN': hash }
}

wsclient.send(JSON.stringify(request));

}

Attention! 1 - npm i ws 2 - npm i-gate-api don't forget to install

3 - Please do the necessary warnings and precautions for ApiKey and ApiSecret by following and checking the gate.io documentation link https://www.gate.io/docs/developers/apiv4/ws/en/ I gave above!

— Reply to this email directly, view it on GitHub https://github.com/gateio/gateapi-nodejs/issues/10#issuecomment-1124190761, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACIX6NJKC7BXTQDT4BX7LADVJQAXVANCNFSM5QAI5OIA . You are receiving this because you authored the thread.Message ID: @.***>