From 8341593f7b84a8dfb74db3ab2e7be81636206aea Mon Sep 17 00:00:00 2001 From: Stephen Gordon Date: Thu, 21 Mar 2024 17:45:27 +0000 Subject: [PATCH] changed get token balance to use alchemy --- src/app/components/AuthPage/AuthPage.tsx | 19 +++------------ src/app/components/Balance/Balance.tsx | 23 +++++++++--------- src/app/components/activity/Activity.tsx | 1 - src/app/home/page.tsx | 7 +++--- src/app/hooks/useGetTokenBalance.tsx | 30 ++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 src/app/hooks/useGetTokenBalance.tsx diff --git a/src/app/components/AuthPage/AuthPage.tsx b/src/app/components/AuthPage/AuthPage.tsx index 9f9e56d..573066d 100644 --- a/src/app/components/AuthPage/AuthPage.tsx +++ b/src/app/components/AuthPage/AuthPage.tsx @@ -17,10 +17,9 @@ export default function AuthPage ({children} : {children: React.ReactNode}) { const { userAgent, isMobile, isStandalone, isIOS } = useUserAgent(); useEffect(() => { - /* console.log("standalone in auth page " , isStandalone) */ - - if (window) { + /* Uncomment for auth */ + /* if (window) { console.log("window is here") if (window.matchMedia('(display-mode: standalone)').matches) { console.log('standalone') @@ -36,20 +35,8 @@ export default function AuthPage ({children} : {children: React.ReactNode}) { router.push('/'); } - } - - /* if (!isStandalone) { + } */ - router.push('/'); - } */ - /* if (authenticated && zeroDevReady) { - // route home - console.log('authenticated'); - router.push('/home'); - } else { - router.push('/login'); - } - */ diff --git a/src/app/components/Balance/Balance.tsx b/src/app/components/Balance/Balance.tsx index 347196b..670700f 100644 --- a/src/app/components/Balance/Balance.tsx +++ b/src/app/components/Balance/Balance.tsx @@ -6,17 +6,13 @@ import { useBalance } from 'wagmi'; import { RootState } from '../../../GlobalRedux/store'; import { useDispatch, useSelector } from 'react-redux'; import { setBalance } from '@/GlobalRedux/Features/balance/balanceSlice'; -// hooks -import useGetBalance from '@/app/hooks/useGetBalance'; // React import { useEffect } from 'react'; // next import { usePathname } from 'next/navigation'; -import useGetAddress from '@/app/hooks/useGetAddress'; -// react -import { useState } from 'react'; + export default function Balance() { // Redux const dispatch = useDispatch(); @@ -25,15 +21,17 @@ export default function Balance() { // hooks const address = useSelector((state: RootState) => state.address.value); - const result = useBalance({ - // @ts-ignore - address: address, - token: '0x94a9D9AC8a22534E3FaCa9F4e7F2E2cf85d5E4C8', - }); + const checkBalance = async () => { try { - console.log('result', result); + console.log('balance', result?.data?.formatted); + + const result = useBalance({ + // @ts-ignore + address: address, + token: '0x94a9D9AC8a22534E3FaCa9F4e7F2E2cf85d5E4C8', + }); // @ts-ignore dispatch(setBalance(result?.data?.formatted)); } catch (error) { @@ -45,11 +43,12 @@ export default function Balance() { useEffect(() => { console.log(`Route changed to: ${pathname}`); checkBalance(); - }, [pathname, result]); + }, [pathname]); // Get the balance from Redux const balanceState = useSelector((state: RootState) => state.balance.value); console.log('balanceState', balanceState); + // Render the balance return
${balanceState}
; } diff --git a/src/app/components/activity/Activity.tsx b/src/app/components/activity/Activity.tsx index 42bc5dd..28d3020 100644 --- a/src/app/components/activity/Activity.tsx +++ b/src/app/components/activity/Activity.tsx @@ -49,7 +49,6 @@ export default function Activity() { useEffect(() => { /* dispatch(setTransactions(transactions)); */ - console.log('transactionState', transactionState); setTxs(transactionState?.slice(0, 3)); }, [transactionState]); // Add transactions as a dependency diff --git a/src/app/home/page.tsx b/src/app/home/page.tsx index a8c891e..4c51f73 100644 --- a/src/app/home/page.tsx +++ b/src/app/home/page.tsx @@ -33,6 +33,8 @@ import useGetAddress from '../hooks/useGetAddress'; import { motion, useScroll, useTransform } from 'framer-motion'; +import useGetTokenBalance from '../hooks/useGetTokenBalance'; + export default function Page() { // privy const { user, zeroDevReady, sendTransaction } = usePrivySmartAccount(); @@ -46,9 +48,7 @@ export default function Page() { let scale = useTransform(scrollYProgress, [0, 1], ['100%', '90%']); useEffect(() => { - console.log('hi'); console.log('user', user); - console.log('zeroDevReady', zeroDevReady); }, [zeroDevReady]); // next @@ -67,7 +67,8 @@ export default function Page() { // hooks const address = useGetAddress(); - + const usdcBalance = useGetTokenBalance(address as string) + console.log('usdcBalance', usdcBalance); return (
diff --git a/src/app/hooks/useGetTokenBalance.tsx b/src/app/hooks/useGetTokenBalance.tsx new file mode 100644 index 0000000..eaf8312 --- /dev/null +++ b/src/app/hooks/useGetTokenBalance.tsx @@ -0,0 +1,30 @@ +'use client'; +import { Alchemy, Network } from 'alchemy-sdk'; + +const useGetTokenBalance = async (ownerAddress: string) => { + try { + const config = { + apiKey: process.env.NEXT_PUBLIC_ALCHEMY_API_KEY, + network: Network.ETH_SEPOLIA, + }; + const alchemy = new Alchemy(config); + + const tokenContractAddresses = ['0x94a9D9AC8a22534E3FaCa9F4e7F2E2cf85d5E4C8']; + console.log('ownerAddress', ownerAddress, "tokenContractAddresses", tokenContractAddresses); + + + const data = await alchemy.core.getTokenBalances( + ownerAddress, + tokenContractAddresses + ); + + console.log('Token balance for Address'); + console.log(data); + + return data; + } catch (error) { + console.log('error', error); + } +}; + +export default useGetTokenBalance;