Skip to content

Commit

Permalink
ref: update starknetid hooks for address_to_domain update (#431)
Browse files Browse the repository at this point in the history
We will soon update the starknet ID naming contract function
`address_to_domain`, adding an additional `hint` parameter that will
allow us to support CCIP reverse resolving of domains. This change will
break `useStarkName` & `useStarkProfile` hooks. In prevision of this, to
avoid any errors for people using these hooks, I updated them so that
they will first try to make the query with the new `hint` parameter, and
if it fails try without.

I also took this opportunity to fix #419
  • Loading branch information
fracek committed May 2, 2024
2 parents 2252eea + 89cbfe0 commit 8d9f4ed
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 110 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-buses-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@starknet-react/core": minor
---

Fix starknetID hooks to work with latest version of StarknetID contracts
4 changes: 2 additions & 2 deletions packages/core/src/hooks/useStarkAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function queryFn({

const namingContract = contract ?? StarknetIdNamingContract[network];
const p = new Provider(provider);
const encodedDomain = decodeDomain(name);
const encodedDomain = encodeDomain(name);
const result = await p.callContract({
contractAddress: namingContract as string,
entrypoint: "domain_to_address",
Expand All @@ -106,7 +106,7 @@ const StarknetIdNamingContract: Record<string, string> = {
mainnet: "0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678",
};

const decodeDomain = (domain: string): string[] => {
const encodeDomain = (domain: string): string[] => {
if (!domain) return ["0"];

const encoded = [];
Expand Down
86 changes: 75 additions & 11 deletions packages/core/src/hooks/useStarkName.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { useMemo } from "react";
import { Provider, ProviderInterface } from "starknet";
import {
Call,
CallData,
Provider,
ProviderInterface,
starknetId,
} from "starknet";

import { UseQueryProps, UseQueryResult, useQuery } from "~/query";
import { useProvider } from "./useProvider";
Expand Down Expand Up @@ -67,19 +73,15 @@ export function useStarkName({
}: StarkNameArgs): StarkNameResult {
const { provider } = useProvider();
const { chain } = useNetwork();
contract =
chain.network === "sepolia"
? "0x0707f09bc576bd7cfee59694846291047e965f4184fe13dac62c56759b3b6fa7"
: contract;

const enabled = useMemo(
() => Boolean(enabled_ && address),
[enabled_, address],
[enabled_, address]
);

return useQuery({
queryKey: queryKey({ address, contract }),
queryFn: queryFn({ address, contract, provider }),
queryKey: queryKey({ address, contract, network: chain.network }),
queryFn: queryFn({ address, contract, provider, network: chain.network }),
enabled,
...props,
});
Expand All @@ -88,22 +90,84 @@ export function useStarkName({
function queryKey({
address,
contract,
network,
}: {
address?: string;
contract?: string;
network?: string;
}) {
return [{ entity: "starkName", address, contract }] as const;
return [{ entity: "starkName", address, contract, network }] as const;
}

function queryFn({
address,
contract,
provider,
}: StarkNameArgs & { provider: ProviderInterface }) {
network,
}: StarkNameArgs & { provider: ProviderInterface } & {
network: string;
}) {
return async function () {
if (!address) throw new Error("address is required");

const namingContract = contract ?? StarknetIdNamingContract[network];
const p = new Provider(provider);
return await p.getStarkName(address, contract);
try {
// remove fallback when starknetid naming contract is updated on mainnet
const calldata: Call = {
contractAddress: namingContract as string,
entrypoint: "address_to_domain",
calldata: CallData.compile({
address: address,
hint: [],
}),
};
const fallbackCalldata: Call = {
contractAddress: namingContract as string,
entrypoint: "address_to_domain",
calldata: CallData.compile({
address: address,
}),
};
const hexDomain = await executeWithFallback(
p,
calldata,
fallbackCalldata
);
const decimalDomain = hexDomain.result
.map((element) => BigInt(element))
.slice(1);
const stringDomain = starknetId.useDecoded(decimalDomain);
if (!stringDomain) {
throw new Error("Could not get stark name");
}
return stringDomain;
} catch (e) {
throw new Error("Could not get stark name");
}
};
}

const executeWithFallback = async (
provider: ProviderInterface,
initialCall: Call,
fallbackCall: Call
) => {
try {
// Attempt the initial call with the hint parameter
return await provider.callContract(initialCall);
} catch (initialError) {
// If the initial call fails, try with the fallback calldata without the hint parameter
try {
return await provider.callContract(fallbackCall);
} catch (fallbackError) {
throw fallbackError; // Re-throw to handle outside
}
}
};

const StarknetIdNamingContract: Record<string, string> = {
goerli: "0x3bab268e932d2cecd1946f100ae67ce3dff9fd234119ea2f6da57d16d29fce",
sepolia: "0x0707f09bc576bd7cfee59694846291047e965f4184fe13dac62c56759b3b6fa7",
mainnet: "0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678",
};
Loading

0 comments on commit 8d9f4ed

Please sign in to comment.