Skip to content

Commit

Permalink
Add hdfc registration context
Browse files Browse the repository at this point in the history
  • Loading branch information
asoong committed Dec 6, 2023
1 parent e70b971 commit b0c11ad
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 25 deletions.
58 changes: 33 additions & 25 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -99,23 +105,25 @@ const Providers: React.FC<ProvidersProps> = ({ children }) => {
<BalancesProvider>
<RampProvider>
<RegistrationProvider>
<DepositsProvider>
<PermissionsProvider>
<LiquidityProvider>
<OnRamperIntentsProvider>
<ProofGenSettingsProvider>
<PlatformSettings>
<GoogleOAuthProvider clientId={process.env.GOOGLE_CLIENT_ID || ""}>
<GoogleAuthProvider>
{ children }
</GoogleAuthProvider>
</GoogleOAuthProvider>
</PlatformSettings>
</ProofGenSettingsProvider>
</OnRamperIntentsProvider>
</LiquidityProvider>
</PermissionsProvider>
</DepositsProvider>
<HdfcRegistrationProvider>
<DepositsProvider>
<PermissionsProvider>
<LiquidityProvider>
<OnRamperIntentsProvider>
<ProofGenSettingsProvider>
<PlatformSettings>
<GoogleOAuthProvider clientId={process.env.GOOGLE_CLIENT_ID || ""}>
<GoogleAuthProvider>
{ children }
</GoogleAuthProvider>
</GoogleOAuthProvider>
</PlatformSettings>
</ProofGenSettingsProvider>
</OnRamperIntentsProvider>
</LiquidityProvider>
</PermissionsProvider>
</DepositsProvider>
</HdfcRegistrationProvider>
</RegistrationProvider>
</RampProvider>
</BalancesProvider>
Expand Down
20 changes: 20 additions & 0 deletions client/src/contexts/hdfc/Registration/RegistrationContext.ts
Original file line number Diff line number Diff line change
@@ -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<RegistrationValues>(defaultValues)

export default RegistrationContext
115 changes: 115 additions & 0 deletions client/src/contexts/hdfc/Registration/RegistrationProvider.tsx
Original file line number Diff line number Diff line change
@@ -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<string | null>(null);

const [shouldFetchHdfcRegistration, setShouldFetchHdfcRegistration] = useState<boolean>(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 (
<RegistrationContext.Provider
value={{
isRegistered,
registrationHash,
refetchRampAccount: refetchHdfcRampAccount,
shouldFetchRegistration: shouldFetchHdfcRegistration,
}}
>
{children}
</RegistrationContext.Provider>
);
};

export default RegistrationProvider
3 changes: 3 additions & 0 deletions client/src/contexts/hdfc/Registration/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as RegistrationContext } from './RegistrationContext'
export { default as RegistrationProvider } from './RegistrationProvider'

9 changes: 9 additions & 0 deletions client/src/hooks/hdfc/useHdfcRegistration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useContext } from 'react';

import { RegistrationContext } from '../../contexts/hdfc/Registration';

const useRegistration = () => {
return { ...useContext(RegistrationContext) }
};

export default useRegistration;

0 comments on commit b0c11ad

Please sign in to comment.