-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: lazy reverse wallet ens/names (#10671)
- Loading branch information
Showing
6 changed files
with
108 additions
and
132 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
66 changes: 9 additions & 57 deletions
66
packages/mask/src/extension/popups/hooks/useVerifiedWallets.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 |
---|---|---|
@@ -1,60 +1,12 @@ | ||
import { compact } from 'lodash-es' | ||
import { useQuery } from '@tanstack/react-query' | ||
import { NetworkPluginID, type BindingProof, NextIDPlatform } from '@masknet/shared-base' | ||
import { useWallets, useWeb3State } from '@masknet/web3-hooks-base' | ||
import { isGreaterThan, isSameAddress, resolveNextIDPlatformWalletName } from '@masknet/web3-shared-base' | ||
import { type BindingProof, NextIDPlatform, EMPTY_LIST } from '@masknet/shared-base' | ||
import { isGreaterThan } from '@masknet/web3-shared-base' | ||
import { useMemo } from 'react' | ||
|
||
export function useVerifiedWallets(proofs?: BindingProof[]) { | ||
const wallets = useWallets() | ||
const { NameService } = useWeb3State(NetworkPluginID.PLUGIN_EVM) | ||
|
||
return useQuery({ | ||
enabled: !!NameService && !!proofs?.length, | ||
queryKey: ['popups', 'verified', 'wallets', proofs ? JSON.stringify(proofs) : undefined], | ||
queryFn: async () => { | ||
if (!NameService || !proofs) return | ||
|
||
const results = await Promise.all( | ||
proofs.map(async (x) => { | ||
if (x.platform === NextIDPlatform.Ethereum) { | ||
const domain = await NameService?.reverse?.(x.identity) | ||
|
||
if (domain) | ||
return { | ||
...x, | ||
name: domain, | ||
} | ||
|
||
const wallet = wallets.find((wallet) => isSameAddress(wallet.address, x.identity)) | ||
|
||
if (wallet) | ||
return { | ||
...x, | ||
name: wallet.name, | ||
} | ||
|
||
return { | ||
...x, | ||
name: '', | ||
} | ||
} | ||
return null | ||
}), | ||
) | ||
|
||
return compact(results) | ||
.sort((a, z) => (isGreaterThan(a.last_checked_at, z.last_checked_at) ? -1 : 1)) | ||
.map((x, index, list) => { | ||
if (!x.name) { | ||
const name = resolveNextIDPlatformWalletName(x.platform) | ||
return { | ||
...x, | ||
name: `${name} ${list.length - index}`, | ||
} | ||
} | ||
|
||
return x | ||
}) | ||
}, | ||
}) | ||
return useMemo(() => { | ||
if (!proofs?.length) return EMPTY_LIST | ||
return proofs | ||
.filter((x) => x.platform === NextIDPlatform.Ethereum) | ||
.sort((a, z) => (isGreaterThan(a.last_checked_at, z.last_checked_at) ? -1 : 1)) | ||
}, [proofs]) | ||
} |
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
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
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
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