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

Feat: Added spot & future testnet #11

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -14,6 +14,8 @@ import { Props, CandleStickSocketData } from './utils/types';
const TradeViewChart: React.FC<Props> = ({
pair = 'BTCBUSD',
interval = '1m',
useFuturesTestnet = false,
useSpotTestnet = false,
candleStickConfig = condleStickDefaultConfig,
histogramConfig = histogramDefaultConfig,
chartLayout = defaultChartLayout,
Expand All @@ -25,17 +27,17 @@ const TradeViewChart: React.FC<Props> = ({
);

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();
}, [fetchCandleData]);

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) => {
Expand Down
8 changes: 5 additions & 3 deletions src/utils/fetchService.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export interface Props {
candleStickConfig: CandleStickConfig;
histogramConfig: HistogramConfig;
chartLayout: DeffaultChartLayout;
useFuturesTestnet?: boolean;
useSpotTestnet?: boolean;
containerStyle?: {
[x: string]: any;
};
Expand Down
34 changes: 34 additions & 0 deletions src/utils/urls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const url: {[key:string]:{[key:string]:string} } = {
main: {
base: "https://api.binance.com/api/v3/klines?",
ws: "wss://stream.binance.com:9443/ws"
},
future: {
base: "https://testnet.binancefuture.com/fapi/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;
}
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;
}
if (!useFuturesTestnet && useSpotTestnet) {
return url.spot.ws;
}
return url.main.ws;
}