diff --git a/docs/recipes/read-contract-data.md b/docs/recipes/read-contract-data.md new file mode 100644 index 0000000..93bdf75 --- /dev/null +++ b/docs/recipes/read-contract-data.md @@ -0,0 +1,145 @@ +--- +sidebar_position: 6 +title: Read Data from a Contract +description: Learn how to read data from a deployed smart contract using the useScaffoldReadContract hook and display it in your UI. +--- + +# Read Data from a Contract on the Network + +This recipe demonstrates how to read data from a smart contract deployed on a network using the [`useScaffoldReadContract`](https://github.com/Scaffold-Stark/scaffold-stark-2/blob/main/packages/nextjs/hooks/scaffold-stark/useScaffoldReadContract.ts) hook. You will learn how to read contract data and display it in your dApp's UI. + +
+Here is the full code, which we will be implementing in the guide below: + +```tsx title="components/GetContractData.tsx" +import { useScaffoldReadContract } from "~~/hooks/scaffold-stark"; +import { useAccount } from "@starknet-react/core"; + +export const GetContractData = () => { + const { address: connectedAddress } = useAccount(); + + const { data: contractData, isLoading } = useScaffoldReadContract({ + contractName: "YourContract", + functionName: "yourFunction", + args: [connectedAddress], // Passing connected account as an argument + }); + + return ( +
+

Contract Data

+ + {isLoading ? ( + + ) : ( +
+ Data: {contractData ? contractData.toString() : "No data available"} +
+ )} +
+ ); +}; + +``` +
+ +## Implementation guide + +### Step 1: Set Up the Component +Start by creating a new component in your `component` folder of your application. This component will handle the reading of contract data and display it. + +```tsx title="components/GetContractData.tsx" +export const ContractReader = () => { + return ( +
+

Contract Data

+
+ ); +}; + +``` + +### Step 2: Use the `useScaffoldReadContract` Hook to read data + +To read data from the contract, you will use the [`useScaffoldReactContract`](https://github.com/Scaffold-Stark/scaffold-stark-2/blob/main/packages/nextjs/hooks/scaffold-stark/useScaffoldReadContract.ts) hook. This hook interacts with the contract, fetches data from a specific function, and can be configured to pass arguments (such as the connected user's account address). + +```tsx title="components/GetContractData.tsx" +import { useScaffoldReadContract } from "~~/hooks/scaffold-stark"; +import { useAccount } from "@starknet-react/core"; + +export const ContractReader = () => { + const { address: connectedAddress } = useAccount(); + + const { data: contractData, isLoading } = useScaffoldReadContract({ + contractName: "YourContract", + functionName: "yourFunction", + args: [connectedAddress], // Passing connected account as an argument + }); + + return ( +
+

Contract Data

+ {isLoading ? Loading... :

{contractData}

} +
+ ); +}; +``` + +### Step 3: Display the Contract Data + +Once you've fetched the data from the contract, you can display it in the UI. You can check if the data is loading using the isLoading variable and render a spinner or message while the data is being retrieved. + +```tsx title="components/GetContractData.tsx" +export const ContractReader = () => { + const { address: connectedAddress } = useAccount(); + + const { data: contractData, isLoading } = useScaffoldReadContract({ + contractName: "YourContract", + functionName: "yourFunction", + args: [connectedAddress], + }); + + return ( +
+

Contract Data

+ {isLoading ? ( + + ) : ( +

{contractData ? contractData.toString() : "No data available"}

+ )} +
+ ); +}; + +``` + +### Step 4: Add Styling and Final Touches + +Enhance your component’s appearance with some basic styling to make the UI more user-friendly. You can also format the data and improve its readability. + +```tsx title="components/GetContractData.tsx" +export const ContractReader = () => { + const { address: connectedAddress } = useAccount(); + + const { data: contractData, isLoading } = useScaffoldReadContract({ + contractName: "YourContract", + functionName: "yourFunction", + args: [connectedAddress], + }); + + return ( +
+

Contract Data

+ + {isLoading ? ( + + ) : ( +
+ Data: {contractData ? contractData.toString() : "No data available"} +
+ )} +
+ ); +}; +``` + +By following this guide, you have successfully implemented a contract reader in your dApp that fetches and displays data from a deployed contract using the [`useScaffoldReactContract`](https://github.com/Scaffold-Stark/scaffold-stark-2/blob/main/packages/nextjs/hooks/scaffold-stark/useScaffoldReadContract.ts) hook.