Repository: ccxt/ccxt
File: ts/src/pro/okx.ts (TypeScript)

Code before:
     */
    async watchTrades (symbol: string, since: Int = undefined, limit: Int = undefined, params = {}): Promise<Trade[]> {
        return await this.watchTradesForSymbols ([ symbol ], since, limit, params);
    }

    /**
     * @method
     * @name okx#watchTradesForSymbols
     * @see https://www.okx.com/docs-v5/en/#order-book-trading-market-data-ws-trades-channel
     * @see https://www.okx.com/docs-v5/en/#order-book-trading-market-data-ws-all-trades-channel
     * @description get the list of most recent trades for a particular symbol
     * @param {string} symbols
     * @param {int} [since] timestamp in ms of the earliest trade to fetch
     * @param {int} [limit] the maximum amount of trades to fetch
     * @param {object} [params] extra parameters specific to the exchange API endpoint
     * @param {string} [params.channel] the channel to subscribe to, trades by default. Can be trades, trades-all
     * @returns {object[]} a list of [trade structures]{@link https://docs.ccxt.com/#/?id=public-trades}
     */
    async watchTradesForSymbols (symbols: string[], since: Int = undefined, limit: Int = undefined, params = {}): Promise<Trade[]> {
        const symbolsLength = symbols.length;
        if (symbolsLength === 0) {
            throw new ArgumentsRequired (this.id + ' watchTradesForSymbols() requires a non-empty array of symbols');
        }
        await this.loadMarkets ();
        symbols = this.marketSymbols (symbols);
        const channel = this.safeString (params, 'channel', 'trades');
        const topics = [];
        const messageHashes = [];
        for (let i = 0; i < symbols.length; i++) {
            const symbol = symbols[i];
            messageHashes.push (channel + ':' + symbol);
            const marketId = this.marketId (symbol);
            const topic: Dict = {
                'channel': channel,
                'instId': marketId,
            };
            topics.push (topic);
        }
        const request: Dict = {
            'op': 'subscribe',
            'args': topics,
        };
        let access = 'public';
        if (channel === 'trades-all') {
            access = 'business';
            await this.authenticate ({ 'access': access });
        }
        const url = this.getUrl (channel, access);
        const trades = await this.watchMultiple (url, messageHashes, request, messageHashes);
        if (this.newUpdates) {
            const first = this.safeValue (trades, 0);

Review comment (suggestion):
we should always use  [ channel, params ] = this.handleOptionAndParams (params, 'watchTrades', 'channel', 'trades'); in these cases

Code after:
     */
    async watchTrades (symbol: string, since: Int = undefined, limit: Int = undefined, params = {}): Promise<Trade[]> {
        return await this.watchTradesForSymbols ([ symbol ], since, limit, params);
    }

    /**
     * @method
     * @name okx#watchTradesForSymbols
     * @see https://www.okx.com/docs-v5/en/#order-book-trading-market-data-ws-trades-channel
     * @see https://www.okx.com/docs-v5/en/#order-book-trading-market-data-ws-all-trades-channel
     * @description get the list of most recent trades for a particular symbol
     * @param {string} symbols
     * @param {int} [since] timestamp in ms of the earliest trade to fetch
     * @param {int} [limit] the maximum amount of trades to fetch
     * @param {object} [params] extra parameters specific to the exchange API endpoint
     * @param {string} [params.channel] the channel to subscribe to, trades by default. Can be 'trades' and 'trades-all'
     * @returns {object[]} a list of [trade structures]{@link https://docs.ccxt.com/#/?id=public-trades}
     */
    async watchTradesForSymbols (symbols: string[], since: Int = undefined, limit: Int = undefined, params = {}): Promise<Trade[]> {
        const symbolsLength = symbols.length;
        if (symbolsLength === 0) {
            throw new ArgumentsRequired (this.id + ' watchTradesForSymbols() requires a non-empty array of symbols');
        }
        await this.loadMarkets ();
        symbols = this.marketSymbols (symbols);
        let channel = undefined;
        [ channel, params ] = this.handleOptionAndParams (params, 'watchTrades', 'channel', 'trades');
        const topics = [];
        const messageHashes = [];
        for (let i = 0; i < symbols.length; i++) {
            const symbol = symbols[i];
            messageHashes.push (channel + ':' + symbol);
            const marketId = this.marketId (symbol);
            const topic: Dict = {
                'channel': channel,
                'instId': marketId,
            };
            topics.push (topic);
        }
        const request: Dict = {
            'op': 'subscribe',
            'args': topics,
        };
        let access = 'public';
        if (channel === 'trades-all') {
            access = 'business';
            await this.authenticate ({ 'access': access });
        }
        const url = this.getUrl (channel, access);
        const trades = await this.watchMultiple (url, messageHashes, request, messageHashes);
        if (this.newUpdates) {