-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
180 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
client/src/contexts/hdfc/Registration/RegistrationContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
115
client/src/contexts/hdfc/Registration/RegistrationProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |