typescript Copy code
"use client";
import React, { useEffect, useRef } from "react";
type Props = {};
type PriceData = {
[pair: string]: {
[exchange: string]: any;
extremum: any;
};
};
const exchanges = ["Binance", "ByBit", "Coinbase", "Kraken"];
const page = (props: Props) => {
let prices: PriceData = {
BTCUSDT: {
Binance: 0,
ByBit: 0,
Coinbase: 0,
Kraken: 0,
extremum: {
max: {
exchange: "",
price: "",
},
min: {
exchange: "",
price: "",
},
difference: ["", ""],
},
},
ETHUSDT: {
Binance: 0,
ByBit: 0,
Coinbase: 0,
Kraken: 0,
extremum: {
max: {
exchange: "",
price: "",
},
min: {
exchange: "",
price: "",
},
difference: ["", ""],
},
},
XRPUSDT: {
Binance: 0,
ByBit: 0,
Coinbase: 0,
Kraken: 0,
extremum: {
max: {
exchange: "",
price: "",
},
min: {
exchange: "",
price: "",
},
difference: ["", ""],
},
},
};
useEffect(() => {
const wsUrls = {
bybit: "wss://stream.bybit.com/contract/usdt/public/v3",
binance:
"wss://stream.binance.com:9443/stream?streams=btcusdt@trade/ethusdt@trade/xrpusdt@trade",
coinbase: "wss://ws-feed.exchange.coinbase.com",
kraken: "wss://ws.kraken.com/",
};
const wsMessages = {
bybit: (symbol: string) =>
JSON.stringify({
op: "subscribe",
args: [`publicTrade.${symbol}`],
req_id: "test",
}),
binance: "",
coinbase: (symbol: string) =>
JSON.stringify({
type: "subscribe",
product_ids: [symbol.replace("USDT", "-USDT")],
channels: ["ticker"],
}),
kraken: (symbol: string) =>
JSON.stringify({
event: "subscribe",
pair: [symbol.replace("USDT", "/USDT").replace("BTC", "XBT")],
subscription: {
name: "ticker",
},
}),
};
const wsEvents = {
bybit: (message: any, pair: string) => {
const symbol = structuredClone(message).data?.pop().s;
if (pair === symbol) {
const price = structuredClone(message).data?.pop().p;
prices[symbol].ByBit = price;
findMaxMin(symbol);
}
},
binance: (message: any, pair: string) => {
const symbol = message.data.s;
if (pair === symbol) {
const price = message.data.p;
prices[symbol].Binance = price;
findMaxMin(symbol);
}
},
coinbase: (message: any, pair: string) => {
const product_id = message.product_id?.replace("-", "");
if (pair === product_id) {
const price = message.price;
prices[product_id].Coinbase = price;
findMaxMin(product_id);
}
},
kraken: (message: any, pair: string) => {
const symbol = message[3].replace(/[\/]/g, "");
if (pair === symbol) {
const price = message[1]?.c[0];
prices[symbol].Kraken = price;
findMaxMin(symbol);
}
},
};
const findMaxMin = (pair: string) => {
let maxPrice = 0;
let minPrice = Infinity;
let maxExchange = "";
let minExchange = "";
for (let exchange in prices[pair]) {
if (exchange !== "extremum") {
const price = prices[pair][exchange];
if (price > maxPrice) {
maxPrice = price;
maxExchange = exchange;
}
if (price < minPrice) {
minPrice = price;
minExchange = exchange;
}
}
}
const extremum = {
max: {
exchange: maxExchange,
price: maxPrice,
},
min: {
exchange: minExchange,
price: minPrice,
},
difference: [maxPrice - minPrice, 100 - (minPrice * 100) / maxPrice],
};
prices[pair].extremum = extremum;
};
const wsInstances = {} as any;
for (let exchange of exchanges) {
wsInstances[exchange] = new WebSocket(wsUrls[exchange]);
wsInstances[exchange].onopen = () => {
for (let pair in prices) {
if (prices.hasOwnProperty(pair)) {
const message = wsMessages[exchange](pair);
if (message) {
wsInstances[exchange].send(message);
}
}
}
};
wsInstances[exchange].onmessage = (event: any) => {
const message = JSON.parse(event.data);
wsEvents[exchange](message, pair);
};
}
return () => {
for (let exchange in wsInstances) {
if (wsInstances.hasOwnProperty(exchange)) {
wsInstances[exchange].close();
}
}
};
}, []);
return <></>;
};
export default page;