-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix: Implement short living ens resolver for Name component after send flow
#21515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+276
−2
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d261fa5
Implement short living ens resolver for Name component after send flow
OGPoyraz 09afcf3
Update
OGPoyraz 2c0497d
Update test
OGPoyraz 8efe6a8
Merge branch 'main' into ogp/19072
OGPoyraz a78730e
Merge branch 'main' into ogp/19072
OGPoyraz c5a24a9
Merge branch 'main' into ogp/19072
OGPoyraz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -5,9 +5,13 @@ import { strings } from '../../../../../../locales/i18n'; | |
| import { isENS } from '../../../../../util/address'; | ||
| import { useSnapNameResolution } from '../../../../Snaps/hooks/useSnapNameResolution'; | ||
| import { getConfusableCharacterInfo } from '../../utils/send-address-validations'; | ||
| import { useSendFlowEnsResolutions } from './useSendFlowEnsResolutions'; | ||
| import { useSendType } from './useSendType'; | ||
|
|
||
| export const useNameValidation = () => { | ||
| const { fetchResolutions } = useSnapNameResolution(); | ||
| const { setResolvedAddress } = useSendFlowEnsResolutions(); | ||
| const { isEvmSendType } = useSendType(); | ||
|
|
||
| const validateName = useCallback( | ||
| async (chainId: string, to: string) => { | ||
|
|
@@ -24,6 +28,11 @@ export const useNameValidation = () => { | |
| } | ||
| const resolvedAddress = resolutions[0]?.resolvedAddress; | ||
|
|
||
| if (resolvedAddress && isEvmSendType) { | ||
| // Set short living cache of ENS resolution for the given chain and address for confirmation screen | ||
| setResolvedAddress(chainId, to, resolvedAddress); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
| } | ||
|
|
||
| return { | ||
| resolvedAddress, | ||
| ...getConfusableCharacterInfo(to), | ||
|
|
@@ -34,7 +43,7 @@ export const useNameValidation = () => { | |
| error: strings('send.could_not_resolve_name'), | ||
| }; | ||
| }, | ||
| [fetchResolutions], | ||
| [fetchResolutions, isEvmSendType, setResolvedAddress], | ||
| ); | ||
|
|
||
| return { | ||
|
|
||
93 changes: 93 additions & 0 deletions
93
app/components/Views/confirmations/hooks/send/useSendFlowEnsResolutions.test.ts
This file contains hidden or 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,93 @@ | ||
| import { renderHook } from '@testing-library/react-hooks'; | ||
| import { useSendFlowEnsResolutions } from './useSendFlowEnsResolutions'; | ||
|
|
||
| describe('useSendFlowEnsResolutions', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('stores and retrieves ENS name for an address', () => { | ||
| const { result } = renderHook(() => useSendFlowEnsResolutions()); | ||
| const chainId = '0x1'; | ||
| const ensName = 'vitalik.eth'; | ||
| const address = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'; | ||
|
|
||
| result.current.setResolvedAddress(chainId, ensName, address); | ||
| const retrieved = result.current.getResolvedENSName(chainId, address); | ||
|
|
||
| expect(retrieved).toBe(ensName); | ||
| }); | ||
|
|
||
| it('returns undefined for non-existent address', () => { | ||
| const { result } = renderHook(() => useSendFlowEnsResolutions()); | ||
| const chainId = '0x1'; | ||
| const address = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96046'; | ||
|
|
||
| const retrieved = result.current.getResolvedENSName(chainId, address); | ||
|
|
||
| expect(retrieved).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('differentiates between different chain IDs', () => { | ||
| const { result } = renderHook(() => useSendFlowEnsResolutions()); | ||
| const chainId1 = '0x1'; | ||
| const chainId2 = '0x89'; | ||
| const ensName = 'vitalik.eth'; | ||
| const address = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'; | ||
|
|
||
| result.current.setResolvedAddress(chainId1, ensName, address); | ||
| const retrieved1 = result.current.getResolvedENSName(chainId1, address); | ||
| const retrieved2 = result.current.getResolvedENSName(chainId2, address); | ||
|
|
||
| expect(retrieved1).toBe(ensName); | ||
| expect(retrieved2).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('returns undefined for expired cache entries', () => { | ||
| jest.useFakeTimers(); | ||
| const { result } = renderHook(() => useSendFlowEnsResolutions()); | ||
| const chainId = '0x1'; | ||
| const ensName = 'vitalik.eth'; | ||
| const address = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'; | ||
|
|
||
| result.current.setResolvedAddress(chainId, ensName, address); | ||
|
|
||
| jest.advanceTimersByTime(5 * 60 * 1000 + 1); | ||
|
|
||
| const retrieved = result.current.getResolvedENSName(chainId, address); | ||
|
|
||
| expect(retrieved).toBeUndefined(); | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| it('returns cached entry before expiration', () => { | ||
| jest.useFakeTimers(); | ||
| const { result } = renderHook(() => useSendFlowEnsResolutions()); | ||
| const chainId = '0x1'; | ||
| const ensName = 'vitalik.eth'; | ||
| const address = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'; | ||
|
|
||
| result.current.setResolvedAddress(chainId, ensName, address); | ||
|
|
||
| jest.advanceTimersByTime(5 * 60 * 1000 - 1000); | ||
|
|
||
| const retrieved = result.current.getResolvedENSName(chainId, address); | ||
|
|
||
| expect(retrieved).toBe(ensName); | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| it('overwrites existing cache entry for same chain and address', () => { | ||
| const { result } = renderHook(() => useSendFlowEnsResolutions()); | ||
| const chainId = '0x1'; | ||
| const ensName1 = 'vitalik.eth'; | ||
| const ensName2 = 'newname.eth'; | ||
| const address = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'; | ||
|
|
||
| result.current.setResolvedAddress(chainId, ensName1, address); | ||
| result.current.setResolvedAddress(chainId, ensName2, address); | ||
| const retrieved = result.current.getResolvedENSName(chainId, address); | ||
|
|
||
| expect(retrieved).toBe(ensName2); | ||
| }); | ||
| }); |
80 changes: 80 additions & 0 deletions
80
app/components/Views/confirmations/hooks/send/useSendFlowEnsResolutions.ts
This file contains hidden or 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,80 @@ | ||
| import { useCallback } from 'react'; | ||
|
|
||
| interface CacheEntry { | ||
| ensName: string; | ||
| timestamp: number; | ||
| } | ||
|
|
||
| const CACHE_TTL = 5 * 60 * 1000; | ||
|
|
||
| const ensResolutionCache = new Map<string, CacheEntry>(); | ||
|
|
||
| const createCacheKey = (chainId: string, address: string) => | ||
| `${chainId}:${address}`; | ||
|
|
||
| const isCacheEntryValid = (entry: CacheEntry): boolean => | ||
| Date.now() - entry.timestamp < CACHE_TTL; | ||
|
|
||
| // This hook is used to store and retrieve ENS resolutions for a given chain and address with short living cache | ||
| // especially to keep consistency of Name component in send flow | ||
| export const useSendFlowEnsResolutions = () => { | ||
| const setResolvedAddress = useCallback( | ||
| (chainId: string, ensName: string, address: string) => { | ||
| if ( | ||
| !isValidString(chainId) || | ||
| !isValidString(address) || | ||
| !isValidString(ensName) | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const lowerCaseChainId = chainId.toLowerCase(); | ||
| const lowerCaseAddress = address.toLowerCase(); | ||
| const lowerCaseEnsName = ensName.toLowerCase(); | ||
|
|
||
| ensResolutionCache.set( | ||
| createCacheKey(lowerCaseChainId, lowerCaseAddress), | ||
| { | ||
| ensName: lowerCaseEnsName, | ||
| timestamp: Date.now(), | ||
| }, | ||
| ); | ||
| }, | ||
| [], | ||
| ); | ||
|
|
||
| const getResolvedENSName = useCallback( | ||
| (chainId: string, address: string): string | undefined => { | ||
| if (!isValidString(chainId) || !isValidString(address)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const lowerCaseChainId = chainId.toLowerCase(); | ||
| const lowerCaseAddress = address.toLowerCase(); | ||
|
|
||
| const entry = ensResolutionCache.get( | ||
| createCacheKey(lowerCaseChainId, lowerCaseAddress), | ||
| ); | ||
| if (entry && isCacheEntryValid(entry)) { | ||
| return entry.ensName; | ||
| } | ||
|
|
||
| if (entry) { | ||
| ensResolutionCache.delete( | ||
| createCacheKey(lowerCaseChainId, lowerCaseAddress), | ||
| ); | ||
| } | ||
| return undefined; | ||
| }, | ||
| [], | ||
| ); | ||
|
|
||
| return { | ||
| setResolvedAddress, | ||
| getResolvedENSName, | ||
| }; | ||
| }; | ||
|
|
||
| function isValidString(value: unknown) { | ||
| return typeof value === 'string' && value.length > 0; | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.