-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop' into hadi/xmtp-plugin
- Loading branch information
Showing
33 changed files
with
1,297 additions
and
459 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
packages/@justaname.id/react/src/lib/helpers/checkEnsValid/index.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,18 @@ | ||
import { SubnameResponse } from '@justaname.id/sdk'; | ||
|
||
export const checkEnsValid = (ens: SubnameResponse): void => { | ||
if ( | ||
ens.records.resolverAddress === '0x0000000000000000000000000000000000000000' | ||
) { | ||
throw new Error('Resolver address not found'); | ||
} | ||
|
||
let ethAddress = null; | ||
if (ens) { | ||
ethAddress = ens?.records.coins?.find((coin) => coin.id === 60)?.value; | ||
|
||
if (!ethAddress) { | ||
throw new Error('ETH address not found'); | ||
} | ||
} | ||
}; |
1 change: 1 addition & 0 deletions
1
packages/@justaname.id/react/src/lib/hooks/primaryName/index.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 +1,2 @@ | ||
export * from './usePrimaryName'; | ||
export * from './usePrimaryNameBatch'; |
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
169 changes: 169 additions & 0 deletions
169
packages/@justaname.id/react/src/lib/hooks/primaryName/usePrimaryNameBatch.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,169 @@ | ||
import { Address } from 'viem'; | ||
import { useQuery, useQueryClient } from '@tanstack/react-query'; | ||
import { ChainId } from '@justaname.id/sdk'; | ||
import { useJustaName } from '../../providers'; | ||
import { useEnsPublicClient } from '../client/useEnsPublicClient'; | ||
import { defaultOptions } from '../../query'; | ||
import { batch, getName } from '@ensdomains/ensjs/public'; | ||
import { buildPrimaryName } from './usePrimaryName'; | ||
|
||
export const buildPrimaryNameBatchByAddressesKey = ( | ||
addresses: string[], | ||
chainId: ChainId | undefined | ||
) => ['PRIMARY_NAME_BATCH', addresses, chainId]; | ||
|
||
export const buildPrimaryNameBatchKey = (chainId: ChainId | undefined) => [ | ||
'ALL_PRIMARY_NAME_BATCH', | ||
chainId, | ||
]; | ||
|
||
export type PrimaryNameRecord = Record<string | Address, string | null>; | ||
|
||
export interface UsePrimaryNameBatchParams { | ||
addresses?: Address[]; | ||
chainId?: ChainId; | ||
enabled?: boolean; | ||
} | ||
|
||
export interface UsePrimaryNameBatchResult { | ||
primaryNames: PrimaryNameRecord | undefined; | ||
allPrimaryNames: PrimaryNameRecord | undefined; | ||
isPrimaryNameBatchPending: boolean; | ||
isPrimaryNameBatchFetching: boolean; | ||
isPrimaryNameBatchLoading: boolean; | ||
refetchPrimaryNameBatch: () => void; | ||
} | ||
|
||
export interface getPrimaryNameBatchParams { | ||
addresses: Address[] | undefined; | ||
chainId?: ChainId; | ||
} | ||
|
||
export const usePrimaryNameBatch = ( | ||
params?: UsePrimaryNameBatchParams | ||
): UsePrimaryNameBatchResult => { | ||
const { chainId, justaname } = useJustaName(); | ||
const _enabled = params?.enabled !== undefined ? params.enabled : true; | ||
const _chainId = params?.chainId || chainId; | ||
const { ensClient } = useEnsPublicClient({ | ||
chainId: _chainId, | ||
}); | ||
const queryClient = useQueryClient(); | ||
|
||
const getPrimaryNameBatch = async ( | ||
_params: getPrimaryNameBatchParams | ||
): Promise<PrimaryNameRecord> => { | ||
if (!ensClient) { | ||
throw new Error('Public client not found'); | ||
} | ||
|
||
if (!params?.addresses || params?.addresses.length === 0) { | ||
throw new Error('Address is required'); | ||
} | ||
|
||
const primaryNameBatch: PrimaryNameRecord = params?.addresses.reduce( | ||
(acc, address) => { | ||
if (address) { | ||
acc[address] = null; | ||
} | ||
return acc; | ||
}, | ||
{} as PrimaryNameRecord | ||
); | ||
|
||
params?.addresses.forEach((address) => { | ||
const primaryName = queryClient.getQueryData( | ||
buildPrimaryName(address, _chainId) | ||
); | ||
if (typeof primaryName === 'string') { | ||
primaryNameBatch[address] = primaryName; | ||
} | ||
}); | ||
|
||
let checkMissing = Object.keys(primaryNameBatch).filter( | ||
(address) => primaryNameBatch[address] === null | ||
); | ||
|
||
const batchCalls = checkMissing?.map((address) => | ||
getName.batch({ address: address as Address }) | ||
); | ||
|
||
const names = await batch(ensClient, ...batchCalls); | ||
|
||
names.forEach((name, index) => { | ||
if (name && name.name) { | ||
primaryNameBatch[checkMissing[index]] = name?.name; | ||
} | ||
}); | ||
|
||
checkMissing = Object.keys(primaryNameBatch).filter( | ||
(address) => primaryNameBatch[address] === null | ||
); | ||
|
||
const justanamePrimaryNames = await Promise.all( | ||
checkMissing.map((address) => | ||
justaname.subnames.getPrimaryNameByAddress({ | ||
address: address, | ||
chainId: _chainId, | ||
}) | ||
) | ||
); | ||
|
||
justanamePrimaryNames.forEach((name, index) => { | ||
if (name.name) { | ||
primaryNameBatch[checkMissing[index]] = name.name; | ||
} else { | ||
primaryNameBatch[checkMissing[index]] = ''; | ||
} | ||
}); | ||
|
||
Object.keys(primaryNameBatch).forEach((address) => { | ||
queryClient.setQueryData( | ||
buildPrimaryName(address, _chainId), | ||
primaryNameBatch[address] | ||
); | ||
}); | ||
|
||
queryClient.setQueryData( | ||
buildPrimaryNameBatchKey(_chainId), | ||
(prev: PrimaryNameRecord) => ({ | ||
...prev, | ||
...primaryNameBatch, | ||
}) | ||
); | ||
|
||
return primaryNameBatch; | ||
}; | ||
|
||
const query = useQuery({ | ||
...defaultOptions, | ||
queryKey: buildPrimaryNameBatchByAddressesKey( | ||
params?.addresses || [], | ||
_chainId | ||
), | ||
queryFn: () => | ||
getPrimaryNameBatch({ | ||
addresses: params?.addresses, | ||
}), | ||
enabled: | ||
Boolean(params?.addresses) && Boolean(ensClient) && Boolean(_enabled), | ||
}); | ||
|
||
const allQuery = useQuery({ | ||
...defaultOptions, | ||
queryKey: buildPrimaryNameBatchKey(_chainId), | ||
queryFn: () => { | ||
return {} as PrimaryNameRecord; | ||
}, | ||
enabled: false, | ||
}); | ||
|
||
return { | ||
primaryNames: query.data, | ||
isPrimaryNameBatchPending: query.isPending, | ||
allPrimaryNames: allQuery.data, | ||
isPrimaryNameBatchFetching: query.isFetching, | ||
isPrimaryNameBatchLoading: query.isLoading, | ||
refetchPrimaryNameBatch: query.refetch, | ||
}; | ||
}; |
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,2 +1,3 @@ | ||
export * from './useRecords'; | ||
export * from './useAvatar'; | ||
export * from './useAvatar'; | ||
export * from './useStandardRecordsBatch'; |
2 changes: 1 addition & 1 deletion
2
packages/@justaname.id/react/src/lib/hooks/records/records-task-queue.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,3 +1,3 @@ | ||
import TaskQueue from '../../queues'; | ||
|
||
export const RecordsTaskQueue = new TaskQueue(3); | ||
export const RecordsTaskQueue = new TaskQueue(4); |
Oops, something went wrong.