Skip to content

Commit

Permalink
add ANS support
Browse files Browse the repository at this point in the history
  • Loading branch information
maxknivets committed Oct 11, 2024
1 parent 53497ec commit faad389
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
27 changes: 25 additions & 2 deletions lib/hooks/useArweaveAddress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface Props {
const useArweaveAddress = ({ addressHash }: Props) => {
const { data, isLoading } = useLambdaState(addressHash);
const [ arweaveAddress, setArweaveAddress ] = useState<string | null>(null);
const [ ANS, setANS ] = useState<string | null>(null);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const searchArksByKey = (state: any, ethereumAddress: string) => {
Expand All @@ -27,18 +28,40 @@ const useArweaveAddress = ({ addressHash }: Props) => {
}
};

async function resolveANS(arweaveAddress: string) {
try {
if (!arweaveAddress) return null;
const ANS_RESOLVER_URL = 'https://ans-resolver.herokuapp.com/resolve/';
const response = await fetch(`${ ANS_RESOLVER_URL }${ arweaveAddress }`);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json() as { domain: string };
console.log(data )
return data;
} catch (e) {
console.log((e as Error).message);
return null;
}
}


useEffect(() => {
const fetchArweaveAddress = async() => {
if (addressHash && data) {
const result = searchArksByKey(data, addressHash);
setArweaveAddress(result ? result.arweaveLinkings : null);
const arweaveAddress = result ? result.arweaveLinkings : null;
setArweaveAddress(arweaveAddress);
resolveANS(arweaveAddress).then((data) => {
if (data?.domain) setANS(data.domain);
});
}
};

fetchArweaveAddress();
}, [ addressHash, data ]);

return { arweaveAddress, isLoading };
return { arweaveAddress, ANS, isLoading };
};

export default useArweaveAddress;
6 changes: 5 additions & 1 deletion lib/hooks/useLambdaState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export function useLambdaState(addressHash: string) {
return null;
}

const response = await fetch(`https://ark-lambda-api.vercel.app/api/ark-lambda/eth-info?hash=${ addressHash.toLowerCase() }`);
const response = await fetch(`https://ark-lambda-api.vercel.app/api/ark-lambda/eth-info?hash=${ addressHash.toLowerCase() }`, {
headers: {
'Cache-Control': 'no-cache',
},
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
Expand Down
1 change: 1 addition & 0 deletions nextjs/csp/policies/ad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function ad(): CspDev.DirectiveDescriptor {
'coinzilla.com',
'*.coinzilla.com',
'https://ark-lambda-api.vercel.app',
'https://ans-resolver.herokuapp.com/resolve/',
'https://ark-lambda-api.vercel.app/api',
'https://request-global.czilladx.com',

Expand Down
4 changes: 2 additions & 2 deletions ui/address/AddressDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const AddressDetails = ({ addressQuery, scrollRef }: Props) => {
const router = useRouter();
const addressHash = getQueryParamString(router.query.hash);

const { arweaveAddress, isLoading: loadingArweaveAddress } = useArweaveAddress({ addressHash });
const { arweaveAddress, ANS, isLoading: loadingArweaveAddress } = useArweaveAddress({ addressHash });

const countersQuery = useAddressCountersQuery({
hash: addressHash,
Expand Down Expand Up @@ -196,7 +196,7 @@ const AddressDetails = ({ addressQuery, scrollRef }: Props) => {
</DetailsInfoItem.Label>
<DetailsInfoItem.Value>
<EntityBase.Link href={ `https://viewblock.io/arweave/address/${ arweaveAddress }` }>
{ arweaveAddress }
{ ANS || arweaveAddress }
</EntityBase.Link>
</DetailsInfoItem.Value>
</>
Expand Down

0 comments on commit faad389

Please sign in to comment.