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

feats: api integrations, real time data with ws #5

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@types/react-dom": "^18.0.9",
"@types/uuid": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^5.48.2",
"@types/uuid": "10.0.0",
"@typescript-eslint/parser": "^5.48.2",
"@vitejs/plugin-react": "^3.0.0",
"autoprefixer": "^10.4.13",
Expand Down
78 changes: 37 additions & 41 deletions src/pages/overview-bots/components/BotView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import TradeTable from './TradeTable';
import GoBack from './GoBack';
import CryptoStats from './CryptoStats';

const truncateDecimals = (value: number, decimalPlaces: number = 2): number => {
const multiplier = Math.pow(10, decimalPlaces);
return Math.trunc(value * multiplier) / multiplier;
};

interface IBotViewProps {
bot: IBotData;
onBack: () => void;
Expand Down Expand Up @@ -72,47 +77,38 @@ const SingleBotView: React.FC<IBotViewProps> = ({
setSearchParams,
cardBotData,
}) => {
const getChartTypeQuery = searchParams.get('chart') || 'trades';
const [tradePair, setTradePair] = useState('');

const getTradePair = (pair: string) => {
setTradePair(pair);
};

return (
<>
<div className="flex flex-col xl:flex-row justify-between gap-x-4">
<div id="left" className="w-full">
<div className="flex gap-x-4 overflow-x-auto mt-6 pb-4">
<div className="pt-3">
<button
onClick={onBack}
className="flex items-center text-blue-500 hover:text-blue-700"
>
<BackIcon className="mr-2" />
Go Back
</button>
<div className="mt-16">
<CustomPieChart
isEmpty
size={210}
innerRadius={90}
outerRadius={103}
cryptoStats={cryptoStats}
label={
<>
<h1 className="font-bold text-[2rem] text-center text-dark-300">
{bot.pnl.isPositive ? '+' : '-'}${bot.pnl.value}
</h1>
<p className="font-normal text-sm text-center text-dark-100">
{bot.pnl.isPositive ? '+' : '-'}
{bot.pnl.percentage}% P&L
</p>
</>
}
/>
</div>
</div>
const getChartTypeQuery = searchParams.get('chart') || 'trades';

return (
<>
<div className="flex flex-col xl:flex-row justify-between gap-x-4">
<div id="left" className="w-full">
<div className="flex gap-x-4 overflow-x-auto mt-6 pb-4">
<div className="pt-3">
<button onClick={onBack} className="flex items-center text-blue-500 hover:text-blue-700">
<BackIcon className="mr-2" />
Go Back
</button>
<div className="mt-16">
<CustomPieChart
isEmpty
size={210}
innerRadius={90}
outerRadius={103}
cryptoStats={cryptoStats}
label={
<>
<h1 className="font-bold text-[2rem] text-center text-dark-300">
{bot.pnl.isPositive ? '+' : '-'}${truncateDecimals(bot.pnl.value)}
</h1>
<p className="font-normal text-sm text-center text-dark-100">
{bot.pnl.isPositive ? '+' : '-'}{truncateDecimals(bot.pnl.percentage)}% P&L
</p>
</>
}
/>
</div>
</div>

<div className="flex-1">
<CryptoStats data={cryptoStats} showValue isEmpty />
Expand Down
15 changes: 11 additions & 4 deletions src/pages/overview-bots/components/Bots.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '../hooks/useProfile';
import CustomTabs from './CustomTabs';
import SingleBotView from './BotView';
import { useAppSelector } from '@store/hooks';

interface IBotsProps {
solData: ISolData[];
Expand All @@ -41,8 +42,12 @@ interface IBotsProps {
cardBotData: ICardBotData[];
}

const truncateDecimals = (value: number, decimalPlaces: number = 2): number => {
const multiplier = Math.pow(10, decimalPlaces);
return Math.trunc(value * multiplier) / multiplier;
};

const Bots: React.FC<IBotsProps> = ({
botsData,
timeTabs,
stratTabs,
tradeDateTabs,
Expand Down Expand Up @@ -75,8 +80,10 @@ const Bots: React.FC<IBotsProps> = ({
];
const [selectedBot, setSelectedBot] = useState<IBotData | null>(null);

const handleBotClick = (bot: IBotData | undefined) => {
if (!bot) return;
const botsData = useAppSelector((state) => state.auth.botsData);

const handleBotClick = (bot: IBotData | undefined) => {
if(!bot) return;

setSelectedBot(bot);
};
Expand Down Expand Up @@ -194,7 +201,7 @@ const Bots: React.FC<IBotsProps> = ({
</div>
</td>
<td>
${bot.portfolio.value} ({bot.portfolio.percentage}%)
${bot.portfolio}
</td>
<td>{bot.accuracy}%</td>
<td>{bot.sharpeRatio}</td>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/overview-bots/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import useModal from '@shared/hooks/useModal';
import TruncatedAddress from '@shared/components/TruncatedAddress';
import AppModal from '@components/ui/AppModal';
import LoginForm from '@shared/components/LoginForm';
import { useWallet } from '@solana/wallet-adapter-react';
import {
IChatTab,
IDateTab,
Expand All @@ -23,6 +22,7 @@ import {
ITimeTab,
} from '../hooks/useProfile';
import Switcher from './Switcher';
import { useWallet } from '@solana/wallet-adapter-react';

export interface IHeaderProps {
query: ITab | ITimeTab | IDateTab | IStratTab | IChatTab | IPerfTab;
Expand Down
20 changes: 4 additions & 16 deletions src/pages/overview-bots/hooks/useProfile.ts
MHHukiewitz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ export interface IBotData {
isPositive: boolean;
chartData: number[];
};
portfolio: {
value: number;
percentage: number;
};
portfolio: number;
accuracy: number;
sharpeRatio: number;
apr: number;
Expand Down Expand Up @@ -758,10 +755,7 @@ export default () => {
isPositive: true,
chartData: [50, 60, 40, 49, 38, 34, 80, 76, 95, 100],
},
portfolio: {
value: 9186,
percentage: 20,
},
portfolio: 9186,
accuracy: 65,
sharpeRatio: 2.81,
apr: 210,
Expand All @@ -778,10 +772,7 @@ export default () => {
isPositive: true,
chartData: [50, 60, 40, 49, 38, 34, 80, 76, 95, 100],
},
portfolio: {
value: 7036,
percentage: 11,
},
portfolio: 7036,
accuracy: 59,
sharpeRatio: 2.01,
apr: 187,
Expand All @@ -798,10 +789,7 @@ export default () => {
isPositive: false,
chartData: [90, 85, 80, 70, 60, 65, 75, 76, 95, 80],
},
portfolio: {
value: 3127,
percentage: 1,
},
portfolio: 1,
accuracy: 49,
sharpeRatio: 1.75,
apr: 165,
Expand Down
20 changes: 11 additions & 9 deletions src/store/auth/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ const authSlice = createSlice({
setBotData: (state, action: PayloadAction<IBotData[]>) => {
state.botsData = action.payload;
},
addBot: (state, action: PayloadAction<IBotData>) => {
state.botsData.unshift(action.payload);
},
updateBotStats: (state, action: PayloadAction<Partial<IBotData>>) => {
const index = state.botsData.findIndex(
(bot) => bot.id === action.payload.id
);
const index = state.botsData.findIndex(bot => bot.id === action.payload.id);
if (index !== -1) {
state.botsData[index] = { ...state.botsData[index], ...action.payload };
} else if (action.payload.id) {
Expand All @@ -60,13 +61,14 @@ const authSlice = createSlice({
},
});

export const {
setAddress,
setLoginStatus,
setUsdcBalance,
export const {
setAddress,
setLoginStatus,
setUsdcBalance,
setBotData,
updateBotStats,
resetAuth,
addBot,
updateBotStats,
resetAuth
} = authSlice.actions;

export default authSlice.reducer;
Loading