forked from Scaffold-Stark/scaffold-stark-2
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from Quantum3-Labs/core-hooks
Core hooks
- Loading branch information
Showing
22 changed files
with
4,643 additions
and
5,836 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./useOutsideClick"; | ||
export * from "./useDeployedContractInfo"; |
62 changes: 62 additions & 0 deletions
62
packages/nextjs/hooks/scaffold-stark/useDeployedContractInfo.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,62 @@ | ||
import { useEffect, useState, useMemo } from "react"; | ||
import { useTargetNetwork } from "./useTargetNetwork"; | ||
import { useIsMounted } from "usehooks-ts"; | ||
import { | ||
ContractCodeStatus, | ||
ContractName, | ||
Contract, | ||
contracts, | ||
} from "~~/utils/scaffold-stark/contract"; | ||
import { useProvider } from "@starknet-react/core"; | ||
import { BlockIdentifier, RpcProvider } from "starknet"; | ||
|
||
export const useDeployedContractInfo = <TContractName extends ContractName>( | ||
contractName: TContractName | ||
) => { | ||
const isMounted = useIsMounted(); | ||
const { targetNetwork } = useTargetNetwork(); | ||
const deployedContract = contracts?.[targetNetwork.network]?.[ | ||
contractName as ContractName | ||
] as Contract<TContractName>; | ||
const [status, setStatus] = useState<ContractCodeStatus>( | ||
ContractCodeStatus.LOADING | ||
); | ||
const publicNodeUrl = targetNetwork.rpcUrls.public.http[0]; | ||
|
||
// Use useMemo to memoize the publicClient object | ||
const publicClient = useMemo(() => { | ||
return new RpcProvider({ | ||
nodeUrl: publicNodeUrl, | ||
}); | ||
}, [publicNodeUrl]); | ||
|
||
useEffect(() => { | ||
const checkContractDeployment = async () => { | ||
if (!deployedContract) { | ||
setStatus(ContractCodeStatus.NOT_FOUND); | ||
return; | ||
} | ||
const contractClasHash = await publicClient.getClassAt( | ||
deployedContract.address, | ||
"Pending" as BlockIdentifier | ||
); | ||
|
||
if (!isMounted()) { | ||
return; | ||
} | ||
// If contract code is `0x` => no contract deployed on that address | ||
if (contractClasHash == undefined) { | ||
setStatus(ContractCodeStatus.NOT_FOUND); | ||
return; | ||
} | ||
setStatus(ContractCodeStatus.DEPLOYED); | ||
}; | ||
|
||
checkContractDeployment(); | ||
}, [isMounted, contractName, deployedContract, publicClient]); | ||
|
||
return { | ||
data: status === ContractCodeStatus.DEPLOYED ? deployedContract : undefined, | ||
isLoading: status === ContractCodeStatus.LOADING, | ||
}; | ||
}; |
38 changes: 38 additions & 0 deletions
38
packages/nextjs/hooks/scaffold-stark/useScaffoldContract.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,38 @@ | ||
import { useMemo } from "react"; | ||
import { useDeployedContractInfo } from "~~/hooks/scaffold-stark"; | ||
import { ContractName } from "~~/utils/scaffold-stark/contract"; | ||
import { useTargetNetwork } from "./useTargetNetwork"; | ||
import { Contract, RpcProvider } from "starknet"; | ||
import { useAccount } from "@starknet-react/core"; | ||
|
||
export const useScaffoldContract = <TContractName extends ContractName>({ | ||
contractName, | ||
}: { | ||
contractName: TContractName; | ||
}) => { | ||
const { data: deployedContractData, isLoading: deployedContractLoading } = | ||
useDeployedContractInfo(contractName); | ||
|
||
const { targetNetwork } = useTargetNetwork(); | ||
const { account } = useAccount(); | ||
const publicNodeUrl = targetNetwork.rpcUrls.public.http[0]; | ||
|
||
const publicClient = useMemo(() => { | ||
return new RpcProvider({ | ||
nodeUrl: publicNodeUrl, | ||
}); | ||
}, [publicNodeUrl]); | ||
let contract = undefined; | ||
if (deployedContractData) { | ||
contract = new Contract( | ||
[...deployedContractData.abi], | ||
deployedContractData.address, | ||
account || publicClient | ||
); | ||
} | ||
|
||
return { | ||
data: contract, | ||
isLoading: deployedContractLoading, | ||
}; | ||
}; |
46 changes: 46 additions & 0 deletions
46
packages/nextjs/hooks/scaffold-stark/useScaffoldContractRead.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,46 @@ | ||
import { useTargetNetwork } from "./useTargetNetwork"; | ||
import { useContractRead } from "@starknet-react/core"; | ||
import { ExtractAbiFunctionNames } from "abi-wan-kanabi/dist/kanabi"; | ||
import { BlockIdentifier, BlockTag } from "starknet"; | ||
import { useDeployedContractInfo } from "~~/hooks/scaffold-stark"; | ||
import { | ||
AbiFunctionOutputs, | ||
ContractAbi, | ||
ContractName, | ||
ExtractAbiFunctionNamesScaffold, | ||
UseScaffoldReadConfig, | ||
} from "~~/utils/scaffold-stark/contract"; | ||
|
||
export const useScaffoldContractRead = < | ||
TContractName extends ContractName, | ||
TFunctionName extends ExtractAbiFunctionNamesScaffold< | ||
ContractAbi<TContractName>, | ||
"view" | ||
> | ||
>({ | ||
contractName, | ||
functionName, | ||
args, | ||
...readConfig | ||
}: UseScaffoldReadConfig<TContractName, TFunctionName>) => { | ||
const { data: deployedContract } = useDeployedContractInfo(contractName); | ||
const { targetNetwork } = useTargetNetwork(); | ||
|
||
return useContractRead({ | ||
chainId: targetNetwork.id, | ||
functionName, | ||
address: deployedContract?.address, | ||
abi: deployedContract?.abi, | ||
watch: true, | ||
args, | ||
enabled: !Array.isArray(args) || !args.some((arg) => arg === undefined), | ||
blockIdentifier: "pending" as BlockIdentifier, | ||
...(readConfig as any), | ||
}) as Omit<ReturnType<typeof useContractRead>, "data" | "refetch"> & { | ||
data: AbiFunctionOutputs<ContractAbi, TFunctionName> | undefined; | ||
// refetch: (options?: { | ||
// throwOnError: boolean; | ||
// cancelRefetch: boolean; | ||
// }) => Promise<AbiFunctionOutputs<ContractAbi, TFunctionName>>; | ||
}; | ||
}; |
Oops, something went wrong.