-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
696 additions
and
1 deletion.
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
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,45 @@ | ||
--- | ||
sidebar_position: 3 | ||
toc_max_heading_level: 5 | ||
--- | ||
|
||
# Faucets | ||
|
||
Faucets for deployment of smart contracts can be requested from the website. | ||
|
||
- The button is at the bottom left of the [Demo app](https://scaffold-stark-demo.vercel.app/) | ||
|
||
|
||
![Faucets Example](/img/faucets.jpg) | ||
|
||
- On clicking on it, You will get a pop up | ||
|
||
![Sepolia Example](/img/Sepolia.jpg) | ||
|
||
|
||
## Starknet Sepolia faucets | ||
|
||
You can get your faucets in the following websites : | ||
|
||
- [Starknet Foundation](https://starknet-faucet.vercel.app/) | ||
|
||
- [Alchemy](https://www.alchemy.com/faucets/starknet-sepolia) | ||
|
||
- [Blast](https://blastapi.io/faucets/starknet-sepolia-eth) | ||
|
||
|
||
## Devnet Faucets | ||
|
||
- To mint tokens, either to an existing address or a new one, use the following command: | ||
|
||
``` | ||
curl -d '{"amount":1000000000000000000, "address":"0x064b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691"}' -H "Content-Type: application/json" -X POST http://localhost:5050/mint | ||
``` | ||
|
||
- You can also transfer faucets as shown below | ||
|
||
- This is made possible following the installation procedures in the Readme file in [Scaffold-stark-2](https://github.com/Scaffold-Stark/scaffold-stark-2) | ||
|
||
|
||
![getFaucets Example](/img/getFaucets.jpg) |
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,189 @@ | ||
--- | ||
sidebar_position: 5 | ||
title: Batching multiple writes to a single transaction | ||
description: Learn how to perform multiple write operations to StarkNet smart contracts simultaneously. | ||
--- | ||
|
||
# Starknet Multi-Write Feature Recipe | ||
|
||
This recipe shows how to perform multiple contract write operations in a single transaction using the [`useScaffoldMultiWriteContract`](https://github.com/Scaffold-Stark/scaffold-stark-2/blob/main/packages/nextjs/hooks/scaffold-stark/useScaffoldMultiWriteContract.ts) hook from Scaffold-Stark. | ||
|
||
## Overview | ||
In this guide, we will implement a feature that allows writing to multiple contracts or executing multiple transactions in a single action. This is particularly useful when your decentralized application (dApp) requires multiple state changes at once. | ||
|
||
This documentation will walk through the code and steps necessary to create a button that triggers the multi-write contract interaction. | ||
|
||
<details open> | ||
<summary>Here is full implementation of the mult-write feature:</summary> | ||
|
||
```tsx title="components/MultiContractInteraction.tsx" | ||
import { useState } from "react"; | ||
import { useScaffoldMultiWriteContract } from "~~/hooks/scaffold-stark"; | ||
import { notification } from "~~/utils/scaffold-stark"; | ||
|
||
export const MultiSetData = () => { | ||
const [name, setName] = useState(""); | ||
const [age, setAge] = useState(0); | ||
|
||
const { sendAsync, isPending } = useScaffoldMultiWriteContract({ | ||
calls: [ | ||
{ | ||
contractName: "ProfileContract", | ||
functionName: "setName", | ||
args: [name], | ||
}, | ||
{ | ||
contractName: "ProfileContract", | ||
functionName: "setAge", | ||
args: [age], | ||
}, | ||
], | ||
}); | ||
|
||
const handleSetData = async () => { | ||
try { | ||
await sendAsync(); | ||
notification("Multi-write successful!", "success"); | ||
} catch (e) { | ||
console.error("Error in multi-write", e); | ||
notification("Multi-write failed.", "error"); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<input | ||
type="text" | ||
placeholder="Enter your name" | ||
className="input border border-primary" | ||
onChange={(e) => setName(e.target.value)} | ||
/> | ||
<input | ||
type="number" | ||
placeholder="Enter your age" | ||
className="input border border-primary" | ||
onChange={(e) => setAge(Number(e.target.value))} | ||
/> | ||
<button | ||
className="btn btn-primary" | ||
onClick={handleSetData} | ||
disabled={isPending} | ||
> | ||
{isPending ? ( | ||
<span className="loading loading-spinner loading-sm"></span> | ||
) : ( | ||
"Submit" | ||
)} | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
``` | ||
</details> | ||
|
||
## Implementation Guide | ||
|
||
### Step 1: Create a New Component | ||
|
||
Create a new component in your `component` folder, named `MultiContractInteraction.tsx`. This component will handle multiple write operations on different contracts | ||
|
||
|
||
```tsx title="components/MultiContractInteraction.tsx" | ||
export const MultiContractInteraction = () => { | ||
return <div>Your MultiContractInteraction</div> | ||
} | ||
``` | ||
|
||
### Step 2: Import Required Hooks and Utilities | ||
|
||
- import the [`useScaffoldMultiWriteContract`](https://github.com/Scaffold-Stark/scaffold-stark-2/blob/main/packages/nextjs/hooks/scaffold-stark/useScaffoldMultiWriteContract.ts) from scaffold-stark package to handle multiple contract function calls. | ||
- Use [` | ||
`](https://github.com/Scaffold-Stark/scaffold-stark-2/blob/main/packages/nextjs/utils/scaffold-stark/notification.tsx) utility to display success or error messages. | ||
|
||
```tsx title="components/MultiContractInteraction.tsx" | ||
import { useScaffoldMultiWriteContract } from "~~/hooks/scaffold-stark"; | ||
import { notification } from "~~/utils/scaffold-stark"; | ||
``` | ||
|
||
### Step 3: Set Up State Variables | ||
|
||
- Use the `useState` hook to track user inputs, `name` and `age`. | ||
|
||
```tsx title="components/MultiContractInteraction.tsx" | ||
const [name, setName] = useState(""); | ||
const [age, setAge] = useState(0); | ||
``` | ||
|
||
### Step 4: Configure the [`useScaffoldMultiWriteContract`](https://github.com/Scaffold-Stark/scaffold-stark-2/blob/main/packages/nextjs/hooks/scaffold-stark/useScaffoldMultiWriteContract.ts) Hook | ||
|
||
- Configure the hook with the necessary contract calls. Here, we call the `setName` and `setAge` functions of `ProfileContract` in sequence. | ||
|
||
```tsx title="components/MultiContractInteraction.tsx" | ||
const { sendAsync, isPending } = useScaffoldMultiWriteContract({ | ||
calls: [ | ||
{ | ||
contractName: "ProfileContract", | ||
functionName: "setName", | ||
args: [name], | ||
}, | ||
{ | ||
contractName: "ProfileContract", | ||
functionName: "setAge", | ||
args: [age], | ||
}, | ||
], | ||
}); | ||
|
||
``` | ||
|
||
- The `isPending` variable will manage the loading state of the button, and `sendAsync` will handle the contract transaction. | ||
|
||
### Step 5: Handle Submission | ||
- Create a `handleSetData` function that triggers the multi-write action. If successful, display a success notification; otherwise, log the error and display a failure message. | ||
|
||
```tsx title="components/MultiContractInteraction.tsx" | ||
const handleSetData = async () => { | ||
try { | ||
await sendAsync(); | ||
notification("Multi-write successful!", "success"); | ||
} catch (e) { | ||
console.error("Error in multi-write", e); | ||
notification("Multi-write failed.", "error"); | ||
} | ||
}; | ||
``` | ||
|
||
### Step 6: Create the UI | ||
- Add inputs for `name` and `age`, and a button to submit the data. | ||
- Disable the button while the transaction is pending. | ||
|
||
```tsx | ||
return ( | ||
<div> | ||
<input | ||
type="text" | ||
placeholder="Enter your name" | ||
className="input border border-primary" | ||
onChange={(e) => setName(e.target.value)} | ||
/> | ||
<input | ||
type="number" | ||
placeholder="Enter your age" | ||
className="input border border-primary" | ||
onChange={(e) => setAge(Number(e.target.value))} | ||
/> | ||
<button | ||
className="btn btn-primary" | ||
onClick={handleSetData} | ||
disabled={isPending} | ||
> | ||
{isPending ? ( | ||
<span className="loading loading-spinner loading-sm"></span> | ||
) : ( | ||
"Submit" | ||
)} | ||
</button> | ||
</div> | ||
); | ||
``` |
Oops, something went wrong.