Viewing File: /home/ubuntu/efiexchange-node-base/src/controllers/exchange/functions/providerSelector.ts

import { subscribe as binanceSubscribe, unsubscribe as binanceUnsubscribe } from '../providers/binance';
import { subscribe as okxSubscribe, unsubscribe as okxUnsubscribe } from '../providers/okx';
import { subscribe as hercleSubscribe, unsubscribe as hercleUnsubscribe } from '../providers/hercle';
import WebSocket from 'ws';
import { title } from 'process';

interface Subscription {
  pair: string;
  provider: {
    subscribe: Function;
    unsubscribe: Function;
  };
}

const userSubscriptions: Map<string, Subscription[]> = new Map();

const isFiat = (currency: string) => ['USD', 'EUR'].includes(currency.toUpperCase());
const isCrypto = (currency: string) => !isFiat(currency);

export const subscribeToExchangeRate = (
  from: string,
  to: string,
  user_id: string,
  socket: WebSocket,
  type: string,
  currency_pair_id: string
) => {
  const pair = `${from.toUpperCase()}-${to.toUpperCase()}`;

  let provider: any;
  if (['EUR-USDT', 'EUR-USDC', 'USDT-EUR', 'USDC-EUR'].includes(pair)) {
    provider = { subscribe: hercleSubscribe, unsubscribe: hercleUnsubscribe, title: 'hercle' };
  } else if ((from.toUpperCase() === 'USD' || to.toUpperCase() === 'USD') && (isCrypto(to) || isCrypto(from))) {
    provider = { subscribe: okxSubscribe, unsubscribe: okxUnsubscribe, title: 'okx' };
  } else if ((from.toUpperCase() === 'EUR' || to.toUpperCase() === 'EUR') && (isCrypto(to) || isCrypto(from))) {
    provider = { subscribe: binanceSubscribe, unsubscribe: binanceUnsubscribe, title: 'binance' };
  } else if (isCrypto(from) && isCrypto(to)) {
    provider = { subscribe: binanceSubscribe, unsubscribe: binanceUnsubscribe, title: 'binance' };
  } else {
    console.log("No Matching provider found : ", pair);
    // socket.emit(type === 'convert' ? 'getConversion' : 'getExchangeRate', {
    //   exchange_rate: 0,
    //   currency_pair_id
    // });
    socket.emit('getConversion', {
      exchange_rate: 0,
      currency_pair_id
    });
    socket.emit('getExchangeRate', {
      exchange_rate: 0,
      currency_pair_id
    });
    return;
  }

  if (!userSubscriptions.has(user_id)) userSubscriptions.set(user_id, []);
  userSubscriptions.get(user_id)!.push({ pair, provider });

  provider.subscribe(pair, user_id, (clientId: string, data: { price: string, pair: string }) => {
    // if (clientId !== user_id) return;
    // console.log("Socket Response Emitted : " , { provider: provider.title, exchange_rate: data.price, pair: data.pair });
    socket.emit('getExchangeRate', { exchange_rate: data.price, currency_pair_id });
  });
};

export const cleanupUser = (user_id: string) => {
  const subs = userSubscriptions.get(user_id) || [];
  subs.forEach(({ pair, provider }) => {
    provider.unsubscribe(pair, user_id);
  });
  userSubscriptions.delete(user_id);
};

export const unsubscribeAll = (user_id: string) => {
  const subs = userSubscriptions.get(user_id) || [];
  subs.forEach(({ pair, provider }) => {
    provider.unsubscribe(pair, user_id);
  });
};
Back to Directory File Manager