From 7b8f1d63b58a13b10fc69b6e3ab3e3e08b1765cc Mon Sep 17 00:00:00 2001 From: Kobe Leenders Date: Wed, 22 Nov 2023 18:05:43 +0100 Subject: [PATCH] fix(mfi-v2-ui): points improvement --- .../desktop/Points/PointsSignIn.tsx | 5 +- .../desktop/Points/PointsSignUp.tsx | 4 +- .../src/hooks/useFirebaseAccount.tsx | 14 ++++-- .../src/pages/api/user/login.ts | 6 +-- .../src/pages/api/user/signup.ts | 46 +++++++++---------- apps/marginfi-v2-ui/src/pages/points.tsx | 11 +---- .../marginfi-v2-ui-state/src/lib/firebase.ts | 26 ++++------- 7 files changed, 43 insertions(+), 69 deletions(-) diff --git a/apps/marginfi-v2-ui/src/components/desktop/Points/PointsSignIn.tsx b/apps/marginfi-v2-ui/src/components/desktop/Points/PointsSignIn.tsx index cc679e2b09..8986338847 100644 --- a/apps/marginfi-v2-ui/src/components/desktop/Points/PointsSignIn.tsx +++ b/apps/marginfi-v2-ui/src/components/desktop/Points/PointsSignIn.tsx @@ -10,7 +10,6 @@ import { firebaseApi } from "@mrgnlabs/marginfi-v2-ui-state"; import { WalletButton } from "~/components/common/Wallet"; import { useWalletContext } from "~/hooks/useWalletContext"; import { MrgnTooltip } from "~/components/common/MrgnTooltip"; - interface PointsSignInProps {} export const PointsSignIn: FC = ({}) => { @@ -23,10 +22,8 @@ export const PointsSignIn: FC = ({}) => { return; } toast.info("Logging in..."); - const blockhashInfo = await connection.getLatestBlockhash(); try { - await firebaseApi.login(wallet); - // localStorage.setItem("authData", JSON.stringify(signedAuthData)); + await firebaseApi.login(wallet.publicKey.toBase58()); toast.success("Logged in successfully"); } catch (loginError: any) { toast.error(loginError.message); diff --git a/apps/marginfi-v2-ui/src/components/desktop/Points/PointsSignUp.tsx b/apps/marginfi-v2-ui/src/components/desktop/Points/PointsSignUp.tsx index 35d6af91e0..a92ce4ff2f 100644 --- a/apps/marginfi-v2-ui/src/components/desktop/Points/PointsSignUp.tsx +++ b/apps/marginfi-v2-ui/src/components/desktop/Points/PointsSignUp.tsx @@ -39,10 +39,8 @@ export const PointsSignUp: FC = ({ referralCode }) => { return; } toast.info("Logging in..."); - const blockhashInfo = await connection.getLatestBlockhash(); try { - await firebaseApi.signup(wallet, finalReferralCode); - // localStorage.setItem("authData", JSON.stringify(signedAuthData)); + await firebaseApi.signup(wallet.publicKey.toBase58(), finalReferralCode); toast.success("Signed up successfully"); } catch (signupError: any) { toast.error(signupError.message); diff --git a/apps/marginfi-v2-ui/src/hooks/useFirebaseAccount.tsx b/apps/marginfi-v2-ui/src/hooks/useFirebaseAccount.tsx index 2167119601..00ec5e80c6 100644 --- a/apps/marginfi-v2-ui/src/hooks/useFirebaseAccount.tsx +++ b/apps/marginfi-v2-ui/src/hooks/useFirebaseAccount.tsx @@ -1,12 +1,18 @@ import { firebaseApi } from "@mrgnlabs/marginfi-v2-ui-state"; import { onAuthStateChanged } from "firebase/auth"; import { useEffect } from "react"; +import { useRouter } from "next/router"; + import { toast } from "react-toastify"; import { useUserProfileStore } from "~/store"; import { useWalletContext } from "./useWalletContext"; +import React from "react"; const useFirebaseAccount = () => { const { connected, walletAddress } = useWalletContext(); + const { query: routerQuery } = useRouter(); + + const referralCode = React.useMemo(() => routerQuery.referralCode as string | undefined, [routerQuery.referralCode]); const [checkForFirebaseUser, setFirebaseUser, signoutFirebaseUser, fetchPoints, resetPoints, hasUser] = useUserProfileStore((state) => [ @@ -18,10 +24,6 @@ const useFirebaseAccount = () => { state.hasUser, ]); - useEffect(() => { - console.log({ hasUser }); - }, [hasUser]); - useEffect(() => { // NOTE: if more point-specific logic is added, move this to a separate hook const unsubscribe = onAuthStateChanged(firebaseApi.auth, (newUser) => { @@ -39,8 +41,10 @@ const useFirebaseAccount = () => { // Wallet connection side effect (auto-login attempt) useEffect(() => { if (!walletAddress) return; + + firebaseApi.loginOrSignup(walletAddress.toBase58(), referralCode).catch(console.error); checkForFirebaseUser(walletAddress.toBase58()); - }, [walletAddress, checkForFirebaseUser]); + }, [walletAddress, checkForFirebaseUser, referralCode]); // Wallet disconnection/change side effect (auto-logout) useEffect(() => { diff --git a/apps/marginfi-v2-ui/src/pages/api/user/login.ts b/apps/marginfi-v2-ui/src/pages/api/user/login.ts index 92de7640e3..994077956a 100644 --- a/apps/marginfi-v2-ui/src/pages/api/user/login.ts +++ b/apps/marginfi-v2-ui/src/pages/api/user/login.ts @@ -21,8 +21,6 @@ initFirebaseIfNeeded(); export interface LoginRequest { walletAddress: string; - // method: SigningMethod; - // signedAuthDataRaw: string; } export default async function handler(req: NextApiRequest, res: any) { @@ -50,7 +48,6 @@ export default async function handler(req: NextApiRequest, res: an return res.status(status).json({ error: error.message }); }*/ - let user; try { const userResult = await getFirebaseUserByWallet(walletAddress); if (userResult === undefined) { @@ -58,9 +55,8 @@ export default async function handler(req: NextApiRequest, res: an Sentry.captureException({ message: "User not found" }); return res.status(STATUS_NOT_FOUND).json({ error: "User not found" }); } else { - await logLoginAttempt(walletAddress, user.uid, "", true); + await logLoginAttempt(walletAddress, userResult.uid, "", true); } - user = userResult; } catch (error: any) { Sentry.captureException(error); return res.status(STATUS_INTERNAL_ERROR).json({ error: error.message }); // An unexpected error occurred diff --git a/apps/marginfi-v2-ui/src/pages/api/user/signup.ts b/apps/marginfi-v2-ui/src/pages/api/user/signup.ts index 1eda0263b8..02bb0b83c3 100644 --- a/apps/marginfi-v2-ui/src/pages/api/user/signup.ts +++ b/apps/marginfi-v2-ui/src/pages/api/user/signup.ts @@ -9,7 +9,6 @@ import base58 from "bs58"; import nacl from "tweetnacl"; import { SigningMethod, - SignupPayload, STATUS_BAD_REQUEST, STATUS_UNAUTHORIZED, STATUS_INTERNAL_ERROR, @@ -21,9 +20,7 @@ initFirebaseIfNeeded(); export interface SignupRequest { walletAddress: string; - payload: SignupPayload; - // method: SigningMethod; - // signedAuthDataRaw: string; + payload: firebaseApi.SignupPayload; } export default async function handler(req: NextApiRequest, res: any) { @@ -33,26 +30,27 @@ export default async function handler(req: NextApiRequest, res: a walletAddress, }); - // try { - // const signupData = validateAndUnpackSignupData(signedAuthDataRaw, method); - // signer = signupData.signer.toBase58(); - // payload = signupData.payload; - // } catch (error: any) { - // Sentry.captureException(error); - // let status; - // switch (error.message) { - // case "Invalid signup tx": - // case "Invalid signup payload": - // status = STATUS_BAD_REQUEST; - // break; - // case "Invalid signature": - // status = STATUS_UNAUTHORIZED; - // break; - // default: - // status = STATUS_INTERNAL_ERROR; - // } - // return res.status(status).json({ error: error.message }); - // } + /* signing logic + try { + const signupData = validateAndUnpackSignupData(signedAuthDataRaw, method); + signer = signupData.signer.toBase58(); + payload = signupData.payload; + } catch (error: any) { + Sentry.captureException(error); + let status; + switch (error.message) { + case "Invalid signup tx": + case "Invalid signup payload": + status = STATUS_BAD_REQUEST; + break; + case "Invalid signature": + status = STATUS_UNAUTHORIZED; + break; + default: + status = STATUS_INTERNAL_ERROR; + } + return res.status(status).json({ error: error.message }); + }*/ try { const user = await getFirebaseUserByWallet(walletAddress); diff --git a/apps/marginfi-v2-ui/src/pages/points.tsx b/apps/marginfi-v2-ui/src/pages/points.tsx index f528dd80e5..17c8a01d69 100644 --- a/apps/marginfi-v2-ui/src/pages/points.tsx +++ b/apps/marginfi-v2-ui/src/pages/points.tsx @@ -1,7 +1,6 @@ import React from "react"; import Link from "next/link"; -import { useRouter } from "next/router"; import { Button } from "@mui/material"; import FileCopyIcon from "@mui/icons-material/FileCopy"; @@ -14,15 +13,12 @@ import { PageHeader } from "~/components/common/PageHeader"; import { PointsLeaderBoard, PointsOverview, - PointsSignIn, - PointsSignUp, PointsCheckingUser, PointsConnectWallet, } from "~/components/desktop/Points"; const Points = () => { const { connected } = useWalletContext(); - const { query: routerQuery } = useRouter(); const [currentFirebaseUser, hasUser, userPointsData] = useUserProfileStore((state) => [ state.currentFirebaseUser, @@ -30,7 +26,6 @@ const Points = () => { state.userPointsData, ]); - const referralCode = React.useMemo(() => routerQuery.referralCode as string | undefined, [routerQuery.referralCode]); const [isReferralCopied, setIsReferralCopied] = React.useState(false); return ( @@ -41,12 +36,8 @@ const Points = () => { ) : currentFirebaseUser ? ( - ) : hasUser === null ? ( - - ) : hasUser ? ( - ) : ( - + )}