Skip to content

Commit

Permalink
changed get token balance to use alchemy
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen-Gordon committed Mar 21, 2024
1 parent 606ba5c commit 8341593
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 32 deletions.
19 changes: 3 additions & 16 deletions src/app/components/AuthPage/AuthPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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');
}
*/



Expand Down
23 changes: 11 additions & 12 deletions src/app/components/Balance/Balance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -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 <div className='text-white'>${balanceState}</div>;
}
1 change: 0 additions & 1 deletion src/app/components/activity/Activity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions src/app/home/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -67,7 +67,8 @@ export default function Page() {

// hooks
const address = useGetAddress();

const usdcBalance = useGetTokenBalance(address as string)
console.log('usdcBalance', usdcBalance);
return (
<div id='render' className='min-h-[150vh]'>
<div className='absolute right-4 top-4'>
Expand Down
30 changes: 30 additions & 0 deletions src/app/hooks/useGetTokenBalance.tsx
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 8341593

Please sign in to comment.