Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit b75dcdf

Browse files
author
hunter001
committed
chore: rename file to align with file naming convention
1 parent 394d237 commit b75dcdf

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

docs/recipes/read-contract-data.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
---
2+
sidebar_position: 6
3+
title: Read Data from a Contract
4+
description: Learn how to read data from a deployed smart contract using the useScaffoldReadContract hook and display it in your UI.
5+
---
6+
7+
# Read Data from a Contract on the Network
8+
9+
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.
10+
11+
<details open>
12+
<summary>Here is the full code, which we will be implementing in the guide below:</summary>
13+
14+
```tsx title="components/GetContractData.tsx"
15+
import { useScaffoldReadContract } from "~~/hooks/scaffold-stark";
16+
import { useAccount } from "@starknet-react/core";
17+
18+
export const GetContractData = () => {
19+
const { address: connectedAddress } = useAccount();
20+
21+
const { data: contractData, isLoading } = useScaffoldReadContract({
22+
contractName: "YourContract",
23+
functionName: "yourFunction",
24+
args: [connectedAddress], // Passing connected account as an argument
25+
});
26+
27+
return (
28+
<div className="bg-base-300 p-6 rounded-lg max-w-md mx-auto mt-6">
29+
<h2 className="text-lg font-bold mb-2">Contract Data</h2>
30+
31+
{isLoading ? (
32+
<span className="loading loading-spinner"></span>
33+
) : (
34+
<div className="text-sm font-semibold">
35+
Data: <span>{contractData ? contractData.toString() : "No data available"}</span>
36+
</div>
37+
)}
38+
</div>
39+
);
40+
};
41+
42+
```
43+
</details>
44+
45+
## Implementation guide
46+
47+
### Step 1: Set Up the Component
48+
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.
49+
50+
```tsx title="components/GetContractData.tsx"
51+
export const ContractReader = () => {
52+
return (
53+
<div>
54+
<h2>Contract Data</h2>
55+
</div>
56+
);
57+
};
58+
59+
```
60+
61+
### Step 2: Use the `useScaffoldReadContract` Hook to read data
62+
63+
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).
64+
65+
```tsx title="components/GetContractData.tsx"
66+
import { useScaffoldReadContract } from "~~/hooks/scaffold-stark";
67+
import { useAccount } from "@starknet-react/core";
68+
69+
export const ContractReader = () => {
70+
const { address: connectedAddress } = useAccount();
71+
72+
const { data: contractData, isLoading } = useScaffoldReadContract({
73+
contractName: "YourContract",
74+
functionName: "yourFunction",
75+
args: [connectedAddress], // Passing connected account as an argument
76+
});
77+
78+
return (
79+
<div>
80+
<h2>Contract Data</h2>
81+
{isLoading ? <span>Loading...</span> : <p>{contractData}</p>}
82+
</div>
83+
);
84+
};
85+
```
86+
87+
### Step 3: Display the Contract Data
88+
89+
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.
90+
91+
```tsx title="components/GetContractData.tsx"
92+
export const ContractReader = () => {
93+
const { address: connectedAddress } = useAccount();
94+
95+
const { data: contractData, isLoading } = useScaffoldReadContract({
96+
contractName: "YourContract",
97+
functionName: "yourFunction",
98+
args: [connectedAddress],
99+
});
100+
101+
return (
102+
<div>
103+
<h2>Contract Data</h2>
104+
{isLoading ? (
105+
<span className="loading loading-spinner"></span>
106+
) : (
107+
<p>{contractData ? contractData.toString() : "No data available"}</p>
108+
)}
109+
</div>
110+
);
111+
};
112+
113+
```
114+
115+
### Step 4: Add Styling and Final Touches
116+
117+
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.
118+
119+
```tsx title="components/GetContractData.tsx"
120+
export const ContractReader = () => {
121+
const { address: connectedAddress } = useAccount();
122+
123+
const { data: contractData, isLoading } = useScaffoldReadContract({
124+
contractName: "YourContract",
125+
functionName: "yourFunction",
126+
args: [connectedAddress],
127+
});
128+
129+
return (
130+
<div className="bg-base-300 p-6 rounded-lg max-w-md mx-auto mt-6">
131+
<h2 className="text-lg font-bold mb-2">Contract Data</h2>
132+
133+
{isLoading ? (
134+
<span className="loading loading-spinner"></span>
135+
) : (
136+
<div className="text-sm font-semibold">
137+
Data: <span>{contractData ? contractData.toString() : "No data available"}</span>
138+
</div>
139+
)}
140+
</div>
141+
);
142+
};
143+
```
144+
145+
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.

0 commit comments

Comments
 (0)