-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add recipe for Read Contracts #31
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
||
<details open> | ||
<summary>Here is the full code, which we will be implementing in the guide below:</summary> | ||
|
||
```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 ( | ||
<div className="bg-base-300 p-6 rounded-lg max-w-md mx-auto mt-6"> | ||
<h2 className="text-lg font-bold mb-2">Contract Data</h2> | ||
|
||
{isLoading ? ( | ||
<span className="loading loading-spinner"></span> | ||
) : ( | ||
<div className="text-sm font-semibold"> | ||
Data: <span>{contractData ? contractData.toString() : "No data available"}</span> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
``` | ||
</details> | ||
|
||
## 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 ( | ||
<div> | ||
<h2>Contract Data</h2> | ||
</div> | ||
); | ||
}; | ||
|
||
``` | ||
|
||
### 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 ( | ||
<div> | ||
<h2>Contract Data</h2> | ||
{isLoading ? <span>Loading...</span> : <p>{contractData}</p>} | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
### 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 ( | ||
<div> | ||
<h2>Contract Data</h2> | ||
{isLoading ? ( | ||
<span className="loading loading-spinner"></span> | ||
) : ( | ||
<p>{contractData ? contractData.toString() : "No data available"}</p> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
``` | ||
|
||
### 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 ( | ||
<div className="bg-base-300 p-6 rounded-lg max-w-md mx-auto mt-6"> | ||
<h2 className="text-lg font-bold mb-2">Contract Data</h2> | ||
|
||
{isLoading ? ( | ||
<span className="loading loading-spinner"></span> | ||
) : ( | ||
<div className="text-sm font-semibold"> | ||
Data: <span>{contractData ? contractData.toString() : "No data available"}</span> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
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. |
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,143 @@ | ||
--- | ||
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. | ||
|
||
<details open> | ||
<summary>Here is the full code, which we will be implementing in the guide below:</summary> | ||
|
||
```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 ( | ||
<div className="bg-base-300 p-6 rounded-lg max-w-md mx-auto mt-6"> | ||
<h2 className="text-lg font-bold mb-2">Contract Data</h2> | ||
|
||
{isLoading ? ( | ||
<span className="loading loading-spinner"></span> | ||
) : ( | ||
<div className="text-sm font-semibold"> | ||
Data: <span>{contractData ? contractData.toString() : "No data available"}</span> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
``` | ||
</details> | ||
|
||
## 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 ( | ||
<div> | ||
<h2>Contract Data</h2> | ||
</div> | ||
); | ||
}; | ||
|
||
``` | ||
|
||
### 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 ( | ||
<div> | ||
<h2>Contract Data</h2> | ||
{isLoading ? <span>Loading...</span> : <p>{contractData}</p>} | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
### 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 ( | ||
<div> | ||
<h2>Contract Data</h2> | ||
{isLoading ? ( | ||
<span className="loading loading-spinner"></span> | ||
) : ( | ||
<p>{contractData ? contractData.toString() : "No data available"}</p> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
``` | ||
|
||
### 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 ( | ||
<div className="bg-base-300 p-6 rounded-lg max-w-md mx-auto mt-6"> | ||
<h2 className="text-lg font-bold mb-2">Contract Data</h2> | ||
|
||
{isLoading ? ( | ||
<span className="loading loading-spinner"></span> | ||
) : ( | ||
<div className="text-sm font-semibold"> | ||
Data: <span>{contractData ? contractData.toString() : "No data available"}</span> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
``` |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep the convention consistent with the other files!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@od-hunter I meant the file name. My bad for the unclear message.
ReadContractDataFromNetwork.md
-->read-contract-data.md
(would prefer this shorter name too)