Skip to content

Commit

Permalink
Merge pull request #1 from Quantum3-Labs/feat/docs-hook
Browse files Browse the repository at this point in the history
update docs hooks
  • Loading branch information
karlavasquez8 authored May 21, 2024
2 parents 636b348 + 974d199 commit fcc9263
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 56 deletions.
16 changes: 7 additions & 9 deletions docs/hooks/useScaffoldContract.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ const { data: yourContract } = useScaffoldContract({
await yourContract?.read.greeting();

// Used to write to a contract and can be called in any function
import { useWalletClient } from "wagmi";
import { useAccount } from "@starknet-react/core";

const { data: walletClient } = useWalletClient();
const { account } = useAccount();
const { data: yourContract } = useScaffoldContract({
contractName: "YourContract",
walletClient,
account,
});
const setGreeting = async () => {
// Call the method in any function
Expand All @@ -32,13 +32,11 @@ This example uses the `useScaffoldContract` hook to obtain a contract instance f

## Configuration

| Parameter | Type | Description |
| :-------------------------- | :----------------------------------------------------------------- | :---------------------------------------------------------------------------- |
| **contractName** | `string` | Name of the contract. |
| **walletClient** (optional) | [`WalletClient`](https://wagmi.sh/react/api/hooks/useWalletClient) | Wallet client must be passed in order to call `write` methods of the contract |
| Parameter | Type | Description |
| :--------------- | :------- | :-------------------- |
| **contractName** | `string` | Name of the contract. |

## Return Value

- `data` : Object representing viem's [contract instance](https://viem.sh/docs/contract/getContract.html#return-value). Which can be used to call `read` and `write` of the contract.

- `data`: An instance of the contract, which can be used to call `read` and `write` methods of the contract.
- `isLoading` : Boolean indicating if the contract is being loaded.
68 changes: 68 additions & 0 deletions docs/hooks/useScaffoldMultiWriteContract.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
sidebar_position: 2
---

# useScaffoldMultiWriteContract

Use this hook to write to state-changing functions of your smart contract.

```ts
const { writeAsync } = useScaffoldMultiWriteContract({
calls: [
{
contractName: "YourContract1",
functionName: "setGreeting",
args: ["Hello"],
},
{
contractName: "YourContract2",
functionName: "setCounter",
args: [42],
},
],
});
```

This example sends multiple transactions to the specified smart contracts to call the functions with the arguments passed in calls. The `writeAsync` function sends the transactions to the smart contracts.

## Usage Example

```tsx
<button
className="btn btn-primary"
onClick={async () => {
try {
await writeAsync();
} catch (e) {
console.error("Error sending transactions:", e);
}
}}
>
Send Transactions
</button>
```

This example demonstrates how to use the `writeAsync`function to send multiple transactions to the specified smart contracts, calling the functions with the arguments passed in calls.

## Configuration

| Parameter | Type | Description |
| :--------------------- | :------------------- | :---------------------------------------------------------------------------------------------------------------------------- |
| **calls** | `Array` | Array of configuration objects for the contract calls. Each object should contain `contractName`, `functionName`, and `args`. |
| **options** (optional) | `InvocationsDetails` | Additional options for the transactions. |

## Call Object Configuration

| Parameter | Type | Description |
| :------------------ | :---------- | :--------------------------------------------------------------------------------------------------------------- |
| **contractName** | `string` | Name of the contract to write to. |
| **functionName** | `string` | Name of the function to call. |
| **args** (optional) | `unknown[]` | Array of arguments to pass to the function (if any). Types are inferred from the contract's function parameters. |

You can also pass other arguments accepted by [writeContractAsync from starknet-react](https://starknet-react.com/hooks/mutation/usecontractwrite).

## Return Values

- `writeContractAsync` function sends the transaction to the smart contract.
- `isMining` property indicates whether the transaction is currently being mined.
- The extended object includes properties inherited from the useContractWrite hook from starknet-react. You can check the [useContractWrite return values](https://starknet-react.com/hooks/mutation/usecontractwrite) for the types.
4 changes: 2 additions & 2 deletions docs/hooks/useScaffoldReadContract.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ This example retrieves the data returned by the `userGreetingCounter` function o
| **watch** (optional) | `boolean` | Watches and refreshes data on new blocks. (default : `true`) |
| Other arguments | `various` | You can pass other arguments accepted by the useContractRead hook from @starknet-react/core. |

You can also pass other arguments accepted by [useReadContract wagmi hook](https://wagmi.sh/react/api/hooks/useReadContract#parameters).
You can also pass other arguments accepted by [useContractRead from Starknet-react](https://starknet-react.com/hooks/query/usecontractread).

## Return Values

- The retrieved data is stored in the `data` property of the returned object.
- The extended object includes properties inherited from the useContractRead hook of starknet-react. You can check the [useReadContract return values](https://starknet-react.com/hooks/query/usecontractread) documentation for the types.
- The extended object includes properties inherited from the useContractRead hook of starknet-react. You can check the [useContractRead return values](https://starknet-react.com/hooks/query/usecontractread) documentation for the types.
2 changes: 1 addition & 1 deletion docs/hooks/useScaffoldWatchContractEvent.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 3
sidebar_position: 7
---

# useScaffoldWatchContractEvent (Coming soon)
Expand Down
75 changes: 31 additions & 44 deletions docs/hooks/useScaffoldWriteContract.md
Original file line number Diff line number Diff line change
@@ -1,68 +1,55 @@
---
sidebar_position: 4
sidebar_position: 3
---

# useScaffoldWriteContract

Use this hook to write to state-changing functions of your smart contract.
Use this hook to write to your smart contract by calling state-mutating functions.

```ts
const { writeAsync } = useScaffoldWriteContract({
calls: [
{
contractName: "YourContract1",
functionName: "setGreeting",
args: ["Hello"],
},
{
contractName: "YourContract2",
functionName: "setCounter",
args: [42],
},
],
contractName: "YourContract",
functionName: "updateGreeting",
args: ["0xd8da6bf26964af9d7eed9e03e53415d37aa96045", "Hello, world!"],
});
```

This example sends multiple transactions to the specified smart contracts to call the functions with the arguments passed in calls. The writeAsync function sends the transactions to the smart contracts.
This example calls the updateGreeting function of the YourContract smart contract.

## Usage Example

```tsx
<button
className="btn btn-primary"
onClick={async () => {
try {
await writeAsync();
} catch (e) {
console.error("Error sending transactions:", e);
}
}}
>
Send Transactions
</button>
const { writeAsync } = useScaffoldWriteContract({
contractName: "YourContract",
functionName: "updateGreeting",
args: ["0xd8da6bf26964af9d7eed9e03e53415d37aa96045", "Hello, world!"],
options: { gas: 100000 },
});

const handleUpdateGreeting = async () => {
try {
const result = await writeAsync();
console.log("Transaction successful:", result);
} catch (error) {
console.error("Transaction failed:", error);
}
};
```

This example demonstrates how to use the writeAsync function to send multiple transactions to the specified smart contracts, calling the functions with the arguments passed in calls.
In this example, the updateGreeting function of the YourContract is called with the specified arguments. The transaction is sent using the `writeAsync` method, which handles the logic for sending the transaction and returns a promise

## Configuration

| Parameter | Type | Description |
| :--------------------- | :------------------- | :---------------------------------------------------------------------------------------------------------------------------- |
| **calls** | `Array` | Array of configuration objects for the contract calls. Each object should contain `contractName`, `functionName`, and `args`. |
| **options** (optional) | `InvocationsDetails` | Additional options for the transactions. |

## Call Object Configuration

| Parameter | Type | Description |
| :------------------ | :---------- | :--------------------------------------------------------------------------------------------------------------- |
| **contractName** | `string` | Name of the contract to write to. |
| **functionName** | `string` | Name of the function to call. |
| **args** (optional) | `unknown[]` | Array of arguments to pass to the function (if any). Types are inferred from the contract's function parameters. |
| Parameter | Type | Description |
| :-------------------- | :---------- | :--------------------------------------------------------------------------------------------------------------- |
| **contractName** | `string` | Name of the contract to write to. |
| **functionName** | `string` | Name of the function to call. |
| **args** (optional) | `unknown[]` | Array of arguments to pass to the function (if any). Types are inferred from the contract's function parameters. |
| **option** (optional) | `object` | Additional options for the transaction, such as gas and value. |

You can also pass other arguments accepted by [writeContractAsync from starknet-react](https://starknet-react.com/hooks/mutation/usecontractwrite).
You can also pass other arguments accepted by [useContractWrite from Starknet-react](https://starknet-react.com/hooks/mutation/usecontractwrite).

## Return Values

- `writeContractAsync` function sends the transaction to the smart contract.
- `isMining` property indicates whether the transaction is currently being mined.
- The extended object includes properties inherited from the useContractWrite hook from starknet-react. You can check the [usecontractwrite return values](https://starknet-react.com/hooks/mutation/usecontractwrite) for the types.
- The writeAsync method is used to send the write transaction. It returns a promise that resolves when the transaction is confirmed.
- The extended object includes properties inherited from the useContractWrite hook of starknet-react. You can check the [useContractWrite return values](https://starknet-react.com/hooks/mutation/usecontractwrite) documentation for the types.

0 comments on commit fcc9263

Please sign in to comment.