From b0c11ad7354cad4b984ce6b4267c7bc3de847050 Mon Sep 17 00:00:00 2001 From: Alex Soong Date: Tue, 5 Dec 2023 16:16:45 -0800 Subject: [PATCH] Add hdfc registration context --- client/src/App.tsx | 58 +++++---- .../hdfc/Registration/RegistrationContext.ts | 20 +++ .../Registration/RegistrationProvider.tsx | 115 ++++++++++++++++++ .../src/contexts/hdfc/Registration/index.ts | 3 + client/src/hooks/hdfc/useHdfcRegistration.ts | 9 ++ 5 files changed, 180 insertions(+), 25 deletions(-) create mode 100644 client/src/contexts/hdfc/Registration/RegistrationContext.ts create mode 100644 client/src/contexts/hdfc/Registration/RegistrationProvider.tsx create mode 100644 client/src/contexts/hdfc/Registration/index.ts create mode 100644 client/src/hooks/hdfc/useHdfcRegistration.ts diff --git a/client/src/App.tsx b/client/src/App.tsx index 38acef615..1e905a840 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -18,18 +18,24 @@ import { TopNav } from "@components/layouts/TopNav"; import { MobileLandingPage } from "@components/MobileLandingPage"; import { EnvironmentBanner } from '@components/layouts/EnvironmentBanner'; +// Common Contexts import AccountProvider from "./contexts/common/Account/AccountProvider"; -import SmartContractsProvider from './contexts/common/SmartContracts/SmartContractsProvider'; import BalancesProvider from "./contexts/common/Balances/BalancesProvider"; -import RampProvider from './contexts/venmo/Ramp/RampProvider'; -import RegistrationProvider from './contexts/venmo/Registration/RegistrationProvider'; -import DepositsProvider from './contexts/venmo/Deposits/DepositsProvider'; -import PermissionsProvider from './contexts/venmo/Permissions/PermissionsProvider'; -import PlatformSettings from './contexts/common/PlatformSettings/PlatformSettingsProvider'; -import OnRamperIntentsProvider from './contexts/venmo/OnRamperIntents/OnRamperIntentsProvider'; +import GoogleAuthProvider from './contexts/common/GoogleAuth/GoogleAuthProvider'; import LiquidityProvider from './contexts/common/Liquidity/LiquidityProvider'; +import PlatformSettings from './contexts/common/PlatformSettings/PlatformSettingsProvider'; import ProofGenSettingsProvider from "./contexts/common/ProofGenSettings/ProofGenSettingsProvider"; -import GoogleAuthProvider from './contexts/common/GoogleAuth/GoogleAuthProvider'; +import SmartContractsProvider from './contexts/common/SmartContracts/SmartContractsProvider'; + +// Venmo Contexts +import DepositsProvider from './contexts/venmo/Deposits/DepositsProvider'; +import OnRamperIntentsProvider from './contexts/venmo/OnRamperIntents/OnRamperIntentsProvider'; +import PermissionsProvider from './contexts/venmo/Permissions/PermissionsProvider'; +import RampProvider from './contexts/venmo/Ramp/RampProvider'; +import RegistrationProvider from './contexts/venmo/Registration/RegistrationProvider'; + +// HDFC Contexts +import HdfcRegistrationProvider from './contexts/hdfc/Registration/RegistrationProvider'; import "./App.css"; import "./styles.css"; @@ -99,23 +105,25 @@ const Providers: React.FC = ({ children }) => { - - - - - - - - - { children } - - - - - - - - + + + + + + + + + + { children } + + + + + + + + + diff --git a/client/src/contexts/hdfc/Registration/RegistrationContext.ts b/client/src/contexts/hdfc/Registration/RegistrationContext.ts new file mode 100644 index 000000000..509ad74be --- /dev/null +++ b/client/src/contexts/hdfc/Registration/RegistrationContext.ts @@ -0,0 +1,20 @@ +import { createContext } from 'react' + + +interface RegistrationValues { + isRegistered: boolean; + registrationHash: string | null; + refetchRampAccount: (() => void) | null; + shouldFetchRegistration: boolean; +} + +const defaultValues: RegistrationValues = { + isRegistered: false, + registrationHash: null, + refetchRampAccount: null, + shouldFetchRegistration: false +}; + +const RegistrationContext = createContext(defaultValues) + +export default RegistrationContext diff --git a/client/src/contexts/hdfc/Registration/RegistrationProvider.tsx b/client/src/contexts/hdfc/Registration/RegistrationProvider.tsx new file mode 100644 index 000000000..f9999d937 --- /dev/null +++ b/client/src/contexts/hdfc/Registration/RegistrationProvider.tsx @@ -0,0 +1,115 @@ +import React, { useEffect, useState, ReactNode } from 'react' +import { useContractRead } from 'wagmi' + +import { esl, ZERO_ADDRESS } from '@helpers/constants' +import useAccount from '@hooks/useAccount' +import useSmartContracts from '@hooks/useSmartContracts'; + +import RegistrationContext from './RegistrationContext' + + +interface ProvidersProps { + children: ReactNode; +} + +const RegistrationProvider = ({ children }: ProvidersProps) => { + /* + * Contexts + */ + + const { isLoggedIn, loggedInEthereumAddress } = useAccount(); + const { hdfcRampAddress, hdfcRampAbi } = useSmartContracts(); + + /* + * State + */ + + const [registrationHash, setRegistrationHash] = useState(null); + + const [shouldFetchHdfcRegistration, setShouldFetchHdfcRegistration] = useState(false); + + /* + * Helpers + */ + + // The !! operator will convert any truthy value to true and any falsy value to false. + const isRegistered = !!(registrationHash && registrationHash !== ZERO_ADDRESS); + + /* + * Contract Reads + */ + + // getAccountVenmoId(address _account) external view returns (bytes32) + const { + data: hdfcRampAccountRaw, + refetch: refetchHdfcRampAccount, + } = useContractRead({ + address: hdfcRampAddress, + abi: hdfcRampAbi, + functionName: 'getAccountInfo', + args: [ + loggedInEthereumAddress + ], + enabled: shouldFetchHdfcRegistration, + }) + + /* + * Hooks + */ + + useEffect(() => { + esl && console.log('shouldFetchHdfcRegistration_1'); + esl && console.log('checking isLoggedIn: ', isLoggedIn); + esl && console.log('checking loggedInEthereumAddress: ', loggedInEthereumAddress); + esl && console.log('checking hdfcRampAddress: ', hdfcRampAddress); + + if (isLoggedIn && loggedInEthereumAddress && hdfcRampAddress) { + esl && console.log('shouldFetchHdfcRegistration_2'); + + setShouldFetchHdfcRegistration(true); + } else { + esl && console.log('shouldFetchHdfcRegistration_3'); + + setShouldFetchHdfcRegistration(false); + + setRegistrationHash(null); + } + }, [isLoggedIn, loggedInEthereumAddress, hdfcRampAddress]); + + useEffect(() => { + esl && console.log('hdfcRampAccountRaw_1'); + esl && console.log('checking hdfcRampAccountRaw: ', hdfcRampAccountRaw); + + if (hdfcRampAccountRaw) { + esl && console.log('hdfcRampAccountRaw_2'); + + const rampAccountData = hdfcRampAccountRaw as any; + const rampAccountProcessed = rampAccountData.venmoIdHash; + + setRegistrationHash(rampAccountProcessed); + } else { + esl && console.log('hdfcRampAccountRaw_3'); + + setRegistrationHash(null); + } + }, [hdfcRampAccountRaw]); + + /* + * Provider + */ + + return ( + + {children} + + ); +}; + +export default RegistrationProvider diff --git a/client/src/contexts/hdfc/Registration/index.ts b/client/src/contexts/hdfc/Registration/index.ts new file mode 100644 index 000000000..c6572fc7d --- /dev/null +++ b/client/src/contexts/hdfc/Registration/index.ts @@ -0,0 +1,3 @@ +export { default as RegistrationContext } from './RegistrationContext' +export { default as RegistrationProvider } from './RegistrationProvider' + diff --git a/client/src/hooks/hdfc/useHdfcRegistration.ts b/client/src/hooks/hdfc/useHdfcRegistration.ts new file mode 100644 index 000000000..424ff70ad --- /dev/null +++ b/client/src/hooks/hdfc/useHdfcRegistration.ts @@ -0,0 +1,9 @@ +import { useContext } from 'react'; + +import { RegistrationContext } from '../../contexts/hdfc/Registration'; + +const useRegistration = () => { + return { ...useContext(RegistrationContext) } +}; + +export default useRegistration;