Skip to content

Commit

Permalink
added zeta coin
Browse files Browse the repository at this point in the history
  • Loading branch information
Raj100 committed Apr 3, 2024
1 parent ae8a38c commit 0479cc5
Show file tree
Hide file tree
Showing 14 changed files with 322 additions and 86 deletions.
27 changes: 26 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"react-confetti": "^6.1.0",
"react-dom": "^18",
"react-firebase-hooks": "^5.1.1",
"react-redux": "^9.1.0"
"react-redux": "^9.1.0",
"swiper": "^11.1.0",
"swiper-react": "^1.0.0"
},
"devDependencies": {
"autoprefixer": "^10.0.1",
Expand Down
75 changes: 38 additions & 37 deletions src/app/Login/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ const page = () => {
console.log(error);
} else {
alert("Verification link sent to your email");
console.log(data);
const { data, error } = await supabase
.from("UserData")
.insert([{ email: email, balance: 2000000, Name: name }])
Expand Down Expand Up @@ -166,42 +165,44 @@ const page = () => {
)}
</div>
{register && (
<div className="my-2 relative bg-white flex justify-center items-center">
{/* {setpassword1 &&} */}
<input
className=" p-2 grow"
type={passwordt}
name=""
placeholder="Confirm Password"
id=""
onChange={(e) => {
setpassword1(e.target.value);
}}
/>
{passwordt == "password" ? (
<i
className="fa-solid fa-eye-slash bg-white mr-2"
onClick={passwordtoggle}
></i>
) : (
<i
className="fa-solid fa-eye mr-2"
onClick={passwordtoggle}
></i>
)}

<div className="my-2 relative bg-white flex justify-center items-center">
<input
className=" p-2 grow"
type={passwordt}
name=""
placeholder="Your Name"
id=""
onChange={(e) => {
setname(e.target.value);
}}
/>
</div>
<>
<div className="my-2 relative bg-white flex justify-center items-center">
{/* {setpassword1 &&} */}
<input
className=" p-2 grow"
type={passwordt}
name=""
placeholder="Confirm Password"
id=""
onChange={(e) => {
setpassword1(e.target.value);
}}
/>
{passwordt == "password" ? (
<i
className="fa-solid fa-eye-slash bg-white mr-2"
onClick={passwordtoggle}
></i>
) : (
<i
className="fa-solid fa-eye mr-2"
onClick={passwordtoggle}
></i>
)}
</div>
<div className="my-2 relative bg-white flex justify-center items-center">
<input
className=" p-2 grow"
type="text"
name=""
placeholder="Your Name"
id=""
onChange={(e) => {
setname(e.target.value);
}}
/>
</div>
</>
)}
{!register && (
<button
Expand Down
3 changes: 2 additions & 1 deletion src/app/Tutorial/page.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react'
import Navbar from '@/components/Navbar/Navbar'
import TradingViewWidget from '../Widgets/Chart'
const page = () => {
return (
<div>
<Navbar></Navbar>

<TradingViewWidget/>
</div>
)
}
Expand Down
148 changes: 114 additions & 34 deletions src/app/VirtualTrading/Chart.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { LineStyle, TickMarkType, createChart } from "lightweight-charts";
import React, { useEffect, useRef, useState } from "react";
import React, { use, useEffect, useRef, useState } from "react";
import axios from "axios";
// import "../App.css"
import { AppContext } from "@/components/AppContext/AppContext";
Expand All @@ -13,7 +13,7 @@ const Chart = () => {
const [candlePrice, setCandlePrice] = useState(null);
const [linePrice, setLinePrice] = useState(null);
const { theme } = useContext(AppContext);
const [zeta, setzeta] = useState(100000);
const [zeta, setzeta] = useState();

const initialData = [];
const [quantity, setQuantity] = useState(1); // State to hold the quantity to buy
Expand All @@ -22,7 +22,10 @@ const Chart = () => {
const [userEmail, setUserEmail] = useState(null);
const [Trades, setTrades] = useState([]);
const fetchData = async () => {
let { data: Trades, error } = await supabase.from("Trades").select("*").eq("email", userEmail);
let { data: Trades, error } = await supabase
.from("Trades")
.select("*")
.eq("email", userEmail);
if (Trades) {
setTrades(Trades);
console.log(Trades);
Expand All @@ -32,15 +35,19 @@ const Chart = () => {
};
useEffect(() => {
const fetchData = async () => {
let { data: Trades, error } = await supabase.from("Trades").select("*").eq("email", userEmail);
if (Trades) {
setTrades(Trades);
console.log(Trades);
} else {
console.log(error);
}
};
let { data: Trades, error } = await supabase
.from("Trades")
.select("*")
.eq("email", userEmail);
if (Trades) {
setTrades(Trades);
console.log(Trades);
} else {
console.log(error);
}
};
fetchData();
console.log(zeta);
}, [currentPrice]);

useEffect(() => {
Expand All @@ -53,35 +60,76 @@ const Chart = () => {
} else {
setUserEmail(null);
}

};
fetchUser();
}, []);

const handlesell = async (id) => {
try {
const { data, error } = await supabase
.from('Trades')
.update({ status: 'sell' })
.eq('id', id)
.from("Trades")
.update({ status: "sell" })
.eq("id", id)
.single(); // Assuming id is unique, use single() to update only one row

if (error) {
throw error;
}

console.log('Successfully updated status to "sell"', data);

// Calculate the new balance after selling
let anna = zeta + (quantity * currentPrice); // Assuming 'quantity' is available
console.log(anna);

// Update the balance in UserData table
const { data2, error2 } = await supabase
.from("UserData")
.update({ balance: anna })
.eq("email", userEmail)
.single();

if (error2) {
throw error2;
}

} catch (error) {
console.error('Error selling stock', error);
alert('Error selling stock');
console.error("Error selling stock", error);
alert("Error selling stock");
}

fetchData();
}

const handleBuy = async () => {
};
const fetchbalace = async () => {
let { data: UserData, error } = await supabase
.from('UserData')
.select('balance')
.eq('email', userEmail);
setzeta(UserData.balance);
console.log(zeta);
alert(zeta);
}

useEffect(() => {
const fetchbalace = async () => {
let { data: UserData, error } = await supabase
.from('UserData')
.select('balance')
.eq('email', userEmail);
setzeta(UserData.balance);
console.log(zeta);
alert(zeta);
}
fetchbalace();
},[])
const handleBuy = async () => {
fetchbalace(); // Fetch balance before buying
console.log(`Buying ${quantity} units at price ${currentPrice}`);
// Add your buy logic here
let { data: UserData, error } = await supabase
.from("UserData")
.select("balance");
setzeta(UserData[0].balance); // Initialize zeta with balance
console.log(zeta);

if (zeta >= quantity * currentPrice) {
try {
const { data, error } = await supabase
Expand All @@ -98,16 +146,33 @@ const Chart = () => {
},
])
.select();

// Deduct purchase amount from balance
let c = quantity * currentPrice;
setzeta(prevZeta => prevZeta - c); // Use callback function to ensure correct state update

// Update balance in UserData table
const { data: updatedUserData, error: updateError } = await supabase
.from("UserData")
.update('balance', zeta)
.select(); // Assuming there's only one row for user data

if (updateError) {
throw updateError;
}

} catch (error) {
alert("Error buying stock");
console.log(error);
}
setzeta(zeta - quantity * currentPrice);

} else {
alert("Cannot buy less coin");
}
fetchData();
};
};



useEffect(() => {
axios(
`https://api.binance.com/api/v3/klines?symbol=${symbol}&interval=1m&limit=5`
Expand Down Expand Up @@ -363,15 +428,30 @@ const Chart = () => {
<div>profit</div>
</div>
<ul className="">
{Trades.map((trade) => (
<li className="border-b p-2 grid grid-cols-5">
{Trades.map((trade) => (
<li className="border-b p-2 grid grid-cols-5">
<div>{trade.Symbol}</div>
<div>{trade.Quantity}</div>
<div>{trade.buyprice}</div>
<div>{Math.round((trade.buyprice-currentPrice) * 100) / 100}</div>
<div> {trade.status==="buy" ? <button onClick={() => handlesell(trade.id)} className="bg-blue-500 px-4"> Sell</button> : "Already Sold"}</div>
</li>
))}
<div>{trade.Quantity}</div>
<div>{trade.buyprice}</div>
<div>
{Math.round((trade.buyprice - currentPrice) * 100) / 100}
</div>
<div>
{" "}
{trade.status === "buy" ? (
<button
onClick={() => handlesell(trade.id)}
className="bg-blue-500 px-4"
>
{" "}
Sell
</button>
) : (
"Already Sold"
)}
</div>
</li>
))}
</ul>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/app/VirtualTrading/page.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import Navbar from '@/components/Navbar/Navbar'
import Chart from './Chart'
import { AppContext } from '@/components/AppContext/AppContext'
const page = () => {
return (
<div>
Expand Down
Loading

0 comments on commit 0479cc5

Please sign in to comment.