Send Transaction
This example shows how to send a multicall transaction using the
useContractWrite
hook. We also show how to use the useWaitForTransaction
hook to fetch the transaction receipt to display a success or error message.
Send Transaction
Change the number of transactions to send:
The useContractWrite
hook
This hook accepts a list of Starknet calls object, represented as a list of
Call
objects.
The hook returns, among other things, a write
method that submits the
transaction, and a writeAsync
method that submits the transaction and returns
a promise with the transaction object. You should use write
if you're not
interested in using the return value, and writeAsync
if you are.
The value returned by writeAsync
is also available in data
.
Building the Call
list
Use the Contract.populateTransaction
functions to easily build a list of Call
objects.
First we instantiate a Contract
object using the useContract
hook. In this
example, we connect it to the network's native currency contract.
The next step is to build a list of calls based on user's input. In this
example, we create a number of calls equal to an user-provided counter.
Notice how we use contract.populateTransaction
to build a Call
object using
a syntax similar to a function call.
Fetching the transaction receipt
We fetch the transaction receipt using the useWaitForTransaction
hook. Notice
that before the user submits their transaction, the value of
tx?.transaction_hash
is undefined
and the hook won't fetch any data.
Only after the user submits their transaction this value will become available
and the hook will start fetching data.
We set the retry
flag to true
to refetch data if the request fails. This can
happen if the RPC provider is slower at picking up pending data.
Adding the UI
We can now connect the data from the hooks to our UI. In this example, we customize the button icon and text based on the current interaction and transaction status. We also ensure that the user can restart the flow by clicking the button after the transaction has been accepted.