Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outdated documentation #411

Closed
shestakov-railways opened this issue Feb 1, 2025 · 7 comments · Fixed by #412 · May be fixed by #398
Closed

Outdated documentation #411

shestakov-railways opened this issue Feb 1, 2025 · 7 comments · Fixed by #412 · May be fixed by #398

Comments

@shestakov-railways
Copy link

shestakov-railways commented Feb 1, 2025

Hello,

I feel documentation & examples are outdated.

I am checking this file
https://github.com/tiagosiebler/bybit-api/blob/master/examples/ws-public-v5.ts
But when I try to do the same, I get errors

e.g.
wsClient.subscribeV5('kline.5.XRPUSDT', 'linear');

Returns

UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "(kline.5.XRPUSDT) failed to subscribe".

ws exception: {
req_id: 'kline.5.XRPUSDT',
success: false,
ret_msg: 'Params Error',
op: 'subscribe',
conn_id: 'cthp2bgd1vqpp1lds2o0-l9ny',
wsKey: 'v5LinearPublic',
isWSAPIResponse: undefined
}

I can't understand what I am doing wrong

I am testing on livenet on demo trading account. Connection itself & requests like create order / remove order work fine

@JJ-Cro
Copy link
Contributor

JJ-Cro commented Feb 1, 2025

Hey, you should, as it say, wrap the function you want inside a try/catch to get the error logged.

That should show you what you are doing wrong.

Otherwise, there is a public beta version of the bybit api in testing, which will soon be released with completely renewed and modernised WebsocketClient and new examples together with it. Its still not public to use freely, but just so you are aware that updates are coming :)

@JJ-Cro
Copy link
Contributor

JJ-Cro commented Feb 1, 2025

And sorry, just saw you posted the error. @tiagosiebler can you check?

@shestakov-railways
Copy link
Author

shestakov-railways commented Feb 1, 2025

In fact, it's already wrapped but this is ignored

Image Image

When I try to add .catch it shows an error as well (for beta version)

I tested it both on beta version & current version

@shestakov-railways
Copy link
Author

shestakov-railways commented Feb 1, 2025

config.js

const { DefaultLogger, RestClientV5, WebsocketClient } = require('bybit-api');
require('dotenv').config();

const API_KEY = process.env.API_DEMO_KEY;
const API_SECRET = process.env.API_DEMO_SECRET_KEY;

const customLogger = {
    ...DefaultLogger,
    // silly: (...params) => console.log('trace', ...params),
};

const restClient = new RestClientV5({
    key: API_KEY,
    secret: API_SECRET,
    demoTrading: true,
    // logger: customLogger,
});

const wsClient = new WebsocketClient({
    key: API_KEY,
    secret: API_SECRET,
    market: 'v5',
    demoTrading: true,
    // logger: customLogger,
});

module.exports = {
    restClient,
    wsClient,
    customLogger,
};

index.js

const { restClient, wsClient } = require('./config');
const {
    getAllDerivatives
} = require('./service');
const { monitorPrice } = require('./priceMonitor');

let isBotStarted = false;

async function startBot() {
    if (isBotStarted) {
        console.log('Bot is already running.');
        return;
    }

    try {
        let derivates = await getAllDerivatives({
            restClient
        });
        derivates = derivates.filter(instrument => instrument.status === 'Trading');
        let symbols = derivates.map(instrument => instrument.symbol);
        symbols = symbols.filter(symbol => symbol.endsWith('USDT'));

        monitorPrice({
            wsClient,
            restClient,
            symbols
        });

        isBotStarted = true;
    } catch (error) {
        console.error('Error in startBot:', error);
    }
}

module.exports = { startBot };

priceMonitor.js

async function monitorPrice({ wsClient, restClient, symbols }) {
    wsClient.on('error', (data) => {
        console.error('ws exception: ', data);
    });

    try{
        wsClient.subscribeV5('kline.5.XRPUSDT', 'linear');
    }catch(e){
        console.log('error', e);
    }
}

module.exports = { monitorPrice };

service.js

async function getAllDerivatives({
    restClient
}) {
    try {
        const linearResponse = await restClient.getInstrumentsInfo({
            category: 'linear',
            limit: 1000
        });
        const linearInstruments = linearResponse.result.list || [];

        const allDerivatives = linearInstruments;
        return allDerivatives;
    } catch (error) {
        console.error('Error when fetching derivatives:', error);
    }
}

module.exports = {
    getAllDerivatives
};

@tiagosiebler
Copy link
Owner

Thanks for all the info to reproduce the issue. Interesting, seems to be related to demo trading. Looking into it.

tiagosiebler pushed a commit that referenced this issue Feb 1, 2025
…ly resolve demo trading url for private topics
tiagosiebler added a commit that referenced this issue Feb 1, 2025
fix(v3.10.31, #411): demo trading wss should only be used for private topics.
@tiagosiebler
Copy link
Owner

You've found a bug! Public topics (such as klines) should not be routed to the demo trading connection. This was unintentionally happening. Fix incoming via #412, should be on npm shortly as of v3.10.31, as soon as the PR tests finish and I've merged it.

Regarding the promise that seems impossible to catch, that kind of behaviour is definitely not desired.

I've pinned down what's causing it and I'm evaluating the better way of fixing it separately (most likely with the next release) without unintended consequences. Keep an eye on #398 for the next major release, where I'm like to include a more robust fix, once I'm happy with my testing (that fix is not in the beta release yet).

@tiagosiebler
Copy link
Owner

If you notice any other examples or scenarios that aren't behaving as expected, please continue opening issues. This was a really important one to fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants