-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: refresh pending contract calls
- Loading branch information
Showing
6 changed files
with
182 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@starknet-react/core": patch | ||
--- | ||
|
||
Refresh pending contract call |
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
"use client"; | ||
import React, { useState } from "react"; | ||
|
||
import { BlockTag } from "starknet"; | ||
import { useContractRead, useNetwork } from "@starknet-react/core"; | ||
|
||
import { StarknetProvider } from "@/components/starknet/provider"; | ||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; | ||
import { Label } from "@/components/ui/label"; | ||
import { | ||
Select, | ||
SelectContent, | ||
SelectGroup, | ||
SelectItem, | ||
SelectTrigger, | ||
SelectValue, | ||
} from "@/components/ui/select"; | ||
import { Button } from "@/components/ui/button"; | ||
|
||
function ContractRead() { | ||
const [blockIdentifier, setBlockIdentifier] = useState("latest"); | ||
const { chain } = useNetwork(); | ||
|
||
const { data, refetch, fetchStatus, status } = useContractRead({ | ||
abi: [ | ||
{ | ||
inputs: [], | ||
name: "symbol", | ||
outputs: [ | ||
{ | ||
name: "symbol", | ||
type: "felt", | ||
}, | ||
], | ||
stateMutability: "view", | ||
type: "function", | ||
}, | ||
], | ||
functionName: "symbol", | ||
address: chain.nativeCurrency.address, | ||
args: [], | ||
watch: true, | ||
blockIdentifier: | ||
blockIdentifier === "latest" ? BlockTag.latest : BlockTag.pending, | ||
}); | ||
|
||
return ( | ||
<Card className="max-w-[400px] mx-auto"> | ||
<CardHeader> | ||
<CardTitle>Contract read</CardTitle> | ||
</CardHeader> | ||
<CardContent className="space-y-4"> | ||
<div className="space-y-1"> | ||
<Label>Block identifier</Label> | ||
<Select value={blockIdentifier} onValueChange={setBlockIdentifier}> | ||
<SelectTrigger className="w[200px]"> | ||
<SelectValue /> | ||
</SelectTrigger> | ||
<SelectContent> | ||
<SelectGroup> | ||
<SelectItem value="latest">Latest</SelectItem> | ||
<SelectItem value="pending">Pending</SelectItem> | ||
</SelectGroup> | ||
</SelectContent> | ||
</Select> | ||
</div> | ||
<div className="space-y-1"> | ||
<Label>Fetch status</Label> | ||
<p>{fetchStatus}</p> | ||
</div> | ||
<div className="space-y-1"> | ||
<Label>Status</Label> | ||
<p>{status}</p> | ||
</div> | ||
<div className="space-y-1"> | ||
<Label>Call result</Label> | ||
<p>{JSON.stringify(data)}</p> | ||
</div> | ||
<div className="space-y-1"> | ||
<Button onClick={() => refetch()}>Refetch data</Button> | ||
</div> | ||
</CardContent> | ||
</Card> | ||
); | ||
} | ||
|
||
export function ContractReadDemo() { | ||
return ( | ||
<StarknetProvider> | ||
<ContractRead /> | ||
</StarknetProvider> | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
title: Contract Read | ||
priority: 90 | ||
--- | ||
|
||
This example shows how to make a contract call. | ||
|
||
<DemoContainer> | ||
<ContractReadDemo /> | ||
</DemoContainer> | ||
|
||
## The `useContractRead` hook | ||
|
||
The `useContractRead` hook is used to make a read-only call to a smart contract. | ||
|
||
The hook takes the following parameters: | ||
|
||
- `abi`: the smart contract ABI. Generated by Scarb and available from block explorers. | ||
- `address`: the smart contract address. | ||
- `functionName`: the function being invoked. | ||
- `args`: function arguments. | ||
- `blockIdentifier`: make a call against a specific block number, or the latest or pending block. | ||
|
||
```tsx | ||
const { chain } = useNetwork(); | ||
|
||
const { data, refetch, fetchStatus, status } = useContractRead({ | ||
abi: [ | ||
{ | ||
inputs: [], | ||
name: "symbol", | ||
outputs: [ | ||
{ | ||
name: "symbol", | ||
type: "felt", | ||
}, | ||
], | ||
stateMutability: "view", | ||
type: "function", | ||
}, | ||
], | ||
functionName: "symbol", | ||
address: chain.nativeCurrency.address, | ||
args: [], | ||
watch: true, | ||
blockIdentifier: | ||
blockIdentifier === "latest" ? BlockTag.latest : BlockTag.pending, | ||
}); | ||
``` | ||
|
||
## Refreshing data | ||
|
||
You can refresh data either manually or automatically. | ||
|
||
- Manually: call the `refetch` function. | ||
- Automatically (default): if `watch` is true and the `blockIdentifier` is `BlockTag.latest`, the hook will | ||
refresh data on every new block. If it's `BlockTag.pending`, data is refreshed | ||
every few seconds. | ||
- Automatically (custom): set `watch` to true and use the `refetchInterval` parameter to change | ||
how data is refreshed. Set it to `false` to always disable refreshing data, or | ||
specify a custom interval. |
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