Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into hadi/xmtp-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
HadiKhai committed Nov 28, 2024
2 parents 3408e33 + c75715f commit c123018
Show file tree
Hide file tree
Showing 33 changed files with 1,297 additions and 459 deletions.
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');
}
}
};
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './usePrimaryName';
export * from './usePrimaryNameBatch';
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,14 @@ export const usePrimaryName = (

let name = '';

try {
const primaryNameGetByAddressResponse =
await justaname.subnames.getPrimaryNameByAddress({
address: params?.address,
chainId: _chainId,
});

const primaryNameGetByAddressResponse =
await justaname.subnames.getPrimaryNameByAddress({
address: params?.address,
chainId: _chainId,
});
if (primaryNameGetByAddressResponse.name) {
name = primaryNameGetByAddressResponse.name;
} catch (error) {
/* empty */
}
if (!name) {
} else {
const taskFn = () => {
if (!params?.address) {
throw new Error('Address is required');
Expand Down
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,
};
};
3 changes: 2 additions & 1 deletion packages/@justaname.id/react/src/lib/hooks/records/index.ts
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';
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);
Loading

0 comments on commit c123018

Please sign in to comment.