From d6332b6b350cd27c63050a76d46b0ecb67adb6a5 Mon Sep 17 00:00:00 2001 From: Pranta Das <110393406+Prantadas7@users.noreply.github.com> Date: Sat, 7 Jan 2023 21:31:55 +0600 Subject: [PATCH 1/3] Added testnet --- src/index.tsx | 10 ++++++---- src/utils/fetchService.js | 8 +++++--- src/utils/types.ts | 2 ++ src/utils/urls.ts | 26 ++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 src/utils/urls.ts diff --git a/src/index.tsx b/src/index.tsx index 5c5d52c..31d0889 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -2,7 +2,7 @@ import React, { memo, useEffect, useCallback, useState } from 'react'; import { fetchCandleStickData } from './utils/fetchService'; import TradeView from './TradeView'; -import { WS_URL } from './utils/constants'; +import { getWebsocketUrl } from './utils/urls'; import { candleSocketAdaptor } from './utils/adaptor'; import { condleStickDefaultConfig, @@ -14,6 +14,8 @@ import { Props, CandleStickSocketData } from './utils/types'; const TradeViewChart: React.FC = ({ pair = 'BTCBUSD', interval = '1m', + useFuturesTestnet = false, + useSpotTestnet = false, candleStickConfig = condleStickDefaultConfig, histogramConfig = histogramDefaultConfig, chartLayout = defaultChartLayout, @@ -25,9 +27,9 @@ const TradeViewChart: React.FC = ({ ); const fetchCandleData = useCallback(async () => { - const candleData = await fetchCandleStickData(pair); + const candleData = await fetchCandleStickData(pair, interval, useFuturesTestnet, useSpotTestnet); setCandleData(candleData); - }, [pair]); + }, [pair, useFuturesTestnet, useSpotTestnet]); useEffect(() => { fetchCandleData(); @@ -35,7 +37,7 @@ const TradeViewChart: React.FC = ({ useEffect(() => { const ws = new WebSocket( - `${WS_URL}/${pair.toLocaleLowerCase()}@kline_${interval}` + `${getWebsocketUrl(useFuturesTestnet, useSpotTestnet)}/${pair.toLocaleLowerCase()}@kline_${interval}` ); // ws.onopen = () => console.log("open"); ws.onmessage = (e) => { diff --git a/src/utils/fetchService.js b/src/utils/fetchService.js index 2ae791a..d2e8aab 100644 --- a/src/utils/fetchService.js +++ b/src/utils/fetchService.js @@ -1,11 +1,13 @@ -import { BASE_URL } from "./constants"; +import { getBaseUrl } from "./urls"; import { parseCandleStickData } from "./candleStickService"; export const fetchCandleStickData = async ( symbol = "BTCBUSD", - interval = "1m" + interval = "1m", + useFuturesTestnet, + useSpotTestnet, ) => { - const url = `${BASE_URL}symbol=${symbol}&interval=${interval}`; + const url = `${getBaseUrl(useFuturesTestnet, useSpotTestnet)}symbol=${symbol}&interval=${interval}`; const result = await fetch(url); const data = await result.json(); return parseCandleStickData(data); diff --git a/src/utils/types.ts b/src/utils/types.ts index 18a6860..9dad816 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -56,6 +56,8 @@ export interface Props { candleStickConfig: CandleStickConfig; histogramConfig: HistogramConfig; chartLayout: DeffaultChartLayout; + useFuturesTestnet?: boolean; + useSpotTestnet?: boolean; containerStyle?: { [x: string]: any; }; diff --git a/src/utils/urls.ts b/src/utils/urls.ts new file mode 100644 index 0000000..6d7398c --- /dev/null +++ b/src/utils/urls.ts @@ -0,0 +1,26 @@ +export default URL = { + main: { + base: "https://api.binance.com/api/v3/klines?", + ws: "wss://stream.binance.com:9443/ws" + }, + future: { + base: "https://testnet.binancefuture.com/v1/klines?", + ws: "wss://stream.binancefuture.com/ws" + }, + spot: { + base: "https://testnet.binance.vision/api/v3/klines?", + ws: "wss://testnet.binance.vision/ws" + } +}; + +export function getBaseUrl(useFuturesTestnet: boolean, useSpotTestnet: boolean): string { + if (useFuturesTestnet && !useSpotTestnet) return URL.future.base; + else if (!useFuturesTestnet && useSpotTestnet) return URL.spot.base; + return URL.main.base; +} + +export function getWebsocketUrl(useFuturesTestnet: boolean, useSpotTestnet: boolean): string { + if (useFuturesTestnet && !useSpotTestnet) return URL.future.ws; + else if (!useFuturesTestnet && useSpotTestnet) return URL.spot.ws; + return URL.main.ws; +} \ No newline at end of file From 654aeb6001498664ab673313514f2d2313853f6c Mon Sep 17 00:00:00 2001 From: Prantadas7 Date: Sat, 7 Jan 2023 23:36:45 +0600 Subject: [PATCH 2/3] Added testnet support Co-authored-by: Rakibul Yeasin --- src/utils/urls.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/utils/urls.ts b/src/utils/urls.ts index 6d7398c..90e0c34 100644 --- a/src/utils/urls.ts +++ b/src/utils/urls.ts @@ -1,4 +1,4 @@ -export default URL = { +const url: {[key:string]:{[key:string]:string} } = { main: { base: "https://api.binance.com/api/v3/klines?", ws: "wss://stream.binance.com:9443/ws" @@ -14,13 +14,13 @@ export default URL = { }; export function getBaseUrl(useFuturesTestnet: boolean, useSpotTestnet: boolean): string { - if (useFuturesTestnet && !useSpotTestnet) return URL.future.base; - else if (!useFuturesTestnet && useSpotTestnet) return URL.spot.base; - return URL.main.base; + if (useFuturesTestnet && !useSpotTestnet) return url.future.base; + else if (!useFuturesTestnet && useSpotTestnet) return url.spot.base; + return url.main.base; } export function getWebsocketUrl(useFuturesTestnet: boolean, useSpotTestnet: boolean): string { - if (useFuturesTestnet && !useSpotTestnet) return URL.future.ws; - else if (!useFuturesTestnet && useSpotTestnet) return URL.spot.ws; - return URL.main.ws; + if (useFuturesTestnet && !useSpotTestnet) return url.future.ws; + else if (!useFuturesTestnet && useSpotTestnet) return url.spot.ws; + return url.main.ws; } \ No newline at end of file From d253f76bd61cffba720c728fa1f2dc6ddb41b3f6 Mon Sep 17 00:00:00 2001 From: Prantadas7 Date: Sun, 8 Jan 2023 20:23:43 +0600 Subject: [PATCH 3/3] Feat: Added spot & future testnet Co-authored-by: Rakibul Yeasin --- src/utils/urls.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/utils/urls.ts b/src/utils/urls.ts index 90e0c34..22f2e4d 100644 --- a/src/utils/urls.ts +++ b/src/utils/urls.ts @@ -4,7 +4,7 @@ const url: {[key:string]:{[key:string]:string} } = { ws: "wss://stream.binance.com:9443/ws" }, future: { - base: "https://testnet.binancefuture.com/v1/klines?", + base: "https://testnet.binancefuture.com/fapi/v1/klines?", ws: "wss://stream.binancefuture.com/ws" }, spot: { @@ -14,13 +14,21 @@ const url: {[key:string]:{[key:string]:string} } = { }; export function getBaseUrl(useFuturesTestnet: boolean, useSpotTestnet: boolean): string { - if (useFuturesTestnet && !useSpotTestnet) return url.future.base; - else if (!useFuturesTestnet && useSpotTestnet) return url.spot.base; + if (useFuturesTestnet && !useSpotTestnet) { + return url.future.base; + } + if (!useFuturesTestnet && useSpotTestnet) { + return url.spot.base; + } return url.main.base; } export function getWebsocketUrl(useFuturesTestnet: boolean, useSpotTestnet: boolean): string { - if (useFuturesTestnet && !useSpotTestnet) return url.future.ws; - else if (!useFuturesTestnet && useSpotTestnet) return url.spot.ws; + if (useFuturesTestnet && !useSpotTestnet) { + return url.future.ws; + } + if (!useFuturesTestnet && useSpotTestnet) { + return url.spot.ws; + } return url.main.ws; } \ No newline at end of file