diff --git a/docs/recipes/ReadUintFromContract.md b/docs/recipes/ReadUintFromContract.md new file mode 100644 index 0000000..4cb56fe --- /dev/null +++ b/docs/recipes/ReadUintFromContract.md @@ -0,0 +1,199 @@ +--- +sidebar_position: 3 +title: Read a uint from a contract +description: Learn how to read from contract functions which accepts arguments / no arguments and display them on UI. +--- + +# Read a `uint` from a contract + +This recipe demonstrates how to read data from contract functions and display it on the UI. We'll showcase an example that accepts some arguments (parameters), and another with no arguments at all. + +
+Here is the full code, which we will be implementing in the guide below: + +```tsx title="components/GreetingsCount.tsx" +import { useAccount } from "wagmi"; +import { useScaffoldContractRead } from "~~/hooks/scaffold-eth"; + +export const GreetingsCount = () => { + const { address: connectedAddress } = useAccount(); + + const { data: totalCounter, isLoading: isTotalCounterLoading } = useScaffoldContractRead({ + contractName: "YourContract", + functionName: "totalCounter", + watch: true, + }); + + const { data: connectedAddressCounter, isLoading: isConnectedAddressCounterLoading } = useScaffoldContractRead({ + contractName: "YourContract", + functionName: "userGreetingCounter", + args: [connectedAddress], // passing args to function + watch: true, + }); + + return ( +
+
+

Greetings Count

+
+

Total Greetings count:

+ {isTotalCounterLoading ? ( + + ) : ( +

{totalCounter ? totalCounter.toString() : 0}

+ )} +

Your Greetings count:

+ {isConnectedAddressCounterLoading ? ( + + ) : ( +

{connectedAddressCounter ? connectedAddressCounter.toString() : 0}

+ )} +
+
+
+ ); +}; +``` + +
+ +## Implementation guide + +### Step 1: Create a new Component + +Begin by creating a new component in the "components" folder of your application. + +```tsx title="components/GreetingsCount.tsx" +export const GreetingsCount = () => { + return ( +
+

Total Greetings count:

+

Your Greetings count:

+
+ ); +}; +``` + +### Step 2: Retrieve total greetings count + +Initialize the [useScaffoldContractRead](/hooks/useScaffoldContractRead) hook to read from the contract. This hook provides the `data` which contains the return value of the function. + +```tsx title="components/GreetingsCount.tsx" +//highlight-start +import { useScaffoldContractRead } from "~~/hooks/scaffold-eth"; +// highlight-end + +export const GreetingsCount = () => { + // highlight-start + const { data: totalCounter } = useScaffoldContractRead({ + contractName: "YourContract", + functionName: "totalCounter", + watch: true, + }); + // highlight-end + + return ( +
+

Total Greetings count:

+ //highlight-start +

{totalCounter ? totalCounter.toString() : 0}

+ //highlight-end +

Your Greetings count:

+
+ ); +}; +``` + +In the line `const {data: totalCounter} = useScaffoldContractRead({...})` we are using [destructuring asssignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) to assign `data` to a new name `totalCounter`. + +In the contract, `totalCounter` returns an `uint` value, which is represented as a [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) in javascript and can be converted to a readable string using `.toString()`. + +### Step 3: Retrieve connected address greetings count + +We can get the connected address using the [useAccount](https://wagmi.sh/react/hooks/useAccount) hook and pass it to `args` key in the `useScaffoldContractRead` hook configuration. This will be used as an argument to read the contract function. + +```tsx title="components/GreetingsCount.tsx" +import { useScaffoldContractRead } from "~~/hooks/scaffold-eth"; +//highlight-start +import { useAccount } from "wagmi"; +//highlight-end + +export const GreetingsCount = () => { + //highlight-start + const { address: connectedAddress } = useAccount(); + //highlight-end + + const { data: totalCounter } = useScaffoldContractRead({ + contractName: "YourContract", + functionName: "totalCounter", + watch: true, + }); + + //highlight-start + const { data: connectedAddressCounter } = useScaffoldContractRead({ + contractName: "YourContract", + functionName: "userGreetingCounter", + args: [connectedAddress], // passing args to function + watch: true, + }); + //highlight-end + + return ( +
+

Total Greetings count:

+

{totalCounter ? totalCounter.toString() : 0}

+

Your Greetings count:

+ //highlight-start +

{connectedAddressCounter ? connectedAddressCounter.toString() : 0}

+ //highlight-end +
+ ); +}; +``` + +### Step 4: Bonus adding loading state + +We can use `isLoading` returned from the [`useScaffoldContractRead`](/hooks/scaffold-eth#usescaffoldcontractread) hook. This variable is set to `true` while fetching data from the contract. + +```tsx title="components/GreetingsCount.tsx" +import { useScaffoldContractRead } from "~~/hooks/scaffold-eth"; +import { useAccount } from "wagmi"; + +export const GreetingsCount = () => { + const { address: connectedAddress } = useAccount(); + + // highlight-start + const { data: totalCounter, isLoading: isTotalCounterLoading } = useScaffoldContractRead({ + // highlight-end + contractName: "YourContract", + functionName: "totalCounter", + watch: true, + }); + + // highlight-start + const { data: connectedAddressCounter, isLoading: isConnectedAddressCounterLoading } = useScaffoldContractRead({ + // highlight-end + contractName: "YourContract", + functionName: "userGreetingCounter", + args: [connectedAddress], // passing args to function + watch: true, + }); + + return ( +
+

Total Greetings count:

+ // highlight-start + {isTotalCounterLoading ? "Loading..." :

{totalCounter ? totalCounter.toString() : 0}

} + // highlight-end +

Your Greetings count:

+ // highlight-start + {isConnectedAddressCounterLoading ? ( + "Loading..." + ) : ( +

{connectedAddressCounter ? connectedAddressCounter.toString() : 0}

+ )} + // highlight-end +
+ ); +}; +```