|
| 1 | +--- |
| 2 | +title: Using flashblocks for pre-confirmation |
| 3 | +description: A quick guide to use flashblocks for pre-confirmation transactions |
| 4 | +slug: wallets/transactions/use-flashblocks.mdx |
| 5 | +--- |
| 6 | + |
| 7 | +Flashblocks gives developers near-instant transaction visibility, reducing perceived block time from 2 seconds to 200 milliseconds. This enables apps to deliver faster, smoother user experiences for high-frequency use cases like DEXs, onchain gaming, and real-time interactions. |
| 8 | + |
| 9 | +With Flashblocks, developers can treat pending transactions as effectively confirmed within 200 ms, unlocking UX patterns that were previously impractical onchain. |
| 10 | + |
| 11 | +Flashblocks is enabled for developers on Base Sepolia and Mainnets. When other chains support flashblocks, support will be added accordingly. |
| 12 | + |
| 13 | +For General flashblocks support, please read [Flashblocks API Quickstart](https://www.alchemy.com/docs/reference/base-flashblocks-api-quickstart). |
| 14 | + |
| 15 | +**Prerequisites** |
| 16 | + |
| 17 | +- minimum Typescript version of 5 |
| 18 | + |
| 19 | +## Get UserOperation with pre-confirmation state |
| 20 | + |
| 21 | +<Tabs> |
| 22 | + <Tab title="React"> |
| 23 | + ## React |
| 24 | + |
| 25 | +Using React with Flashblocks is the same as [sending a UserOperation](/wallets/transactions/send/send-user-operations); the only difference is adding a waitForTxnTag: "pending" argument when using the useSendUserOperation hook. |
| 26 | + |
| 27 | +<Tip> |
| 28 | + In the below example, we use `ModularAccountV2` as the underlying Smart |
| 29 | + Contract type, which is our default and recommended account. To learn more |
| 30 | + about all the account options, check out our guide [on choosing a smart |
| 31 | + account](/wallets/smart-contracts/choosing-a-smart-account). |
| 32 | +</Tip> |
| 33 | + |
| 34 | + ```tsx twoslash |
| 35 | + import React from "react"; |
| 36 | + import { |
| 37 | + type UseSendUserOperationResult, |
| 38 | + useSendUserOperation, |
| 39 | + useSmartAccountClient, |
| 40 | + } from "@account-kit/react"; |
| 41 | + |
| 42 | + export default function MyOpSenderComponent() { |
| 43 | + const { client } = useSmartAccountClient({}); |
| 44 | + |
| 45 | + const { sendUserOperation, isSendingUserOperation } = useSendUserOperation({ |
| 46 | + client, |
| 47 | + // optional parameter that will wait for the transaction to be mined before returning |
| 48 | + waitForTxn: true, |
| 49 | + waitForTxnTag: "pending", |
| 50 | + onSuccess: ({ hash, request }) => { |
| 51 | + // [optional] Do something with the hash and request |
| 52 | + }, |
| 53 | + onError: (error) => { |
| 54 | + // [optional] Do something with the error |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + return ( |
| 59 | + <div> |
| 60 | + <button |
| 61 | + onClick={() => |
| 62 | + sendUserOperation({ |
| 63 | + uo: { |
| 64 | + target: "0xTARGET_ADDRESS", |
| 65 | + data: "0x", |
| 66 | + value: 0n, |
| 67 | + }, |
| 68 | + }) |
| 69 | + } |
| 70 | + disabled={isSendingUserOperation} |
| 71 | + > |
| 72 | + {isSendingUserOperation ? "Sending..." : "Send UO"} |
| 73 | + </button> |
| 74 | + </div> |
| 75 | + ); |
| 76 | + } |
| 77 | + ``` |
| 78 | + |
| 79 | + </Tab> |
| 80 | + <Tab title="Wallet SDK"> |
| 81 | + ## Wallet SDK |
| 82 | + |
| 83 | + Fetching a pre-confirmed UserOperation works the same way as fetching a mined UserOperation - you just need to pass a block tag to indicate that you want the pre-confirmed UO. |
| 84 | + |
| 85 | + ```ts twoslash |
| 86 | + // Send a user operation as always |
| 87 | + const { hash } = await client.sendUserOperation(...); |
| 88 | + |
| 89 | + // Wait for user operation with "pending" tag. |
| 90 | + // This is how you get userOperation pre-confirmation |
| 91 | + await client.waitForUserOperationTransaction({hash, tag: "pending"}); |
| 92 | + |
| 93 | + // Or, get user operation receipt with "pending" tag. |
| 94 | + let receipt = await client.getUserOperationReceipt(hash, "pending"); |
| 95 | + |
| 96 | + // There is a new field "status" in the receipt struct, it indicates if a userOperation receipt is mined or pre-confirmed |
| 97 | + // receipt: { |
| 98 | + // userOpHash: '...', |
| 99 | + // ... |
| 100 | + // status: 'preconfirmed' | 'mined' |
| 101 | + //} |
| 102 | + ``` |
| 103 | + |
| 104 | + </Tab> |
| 105 | + <Tab title="Wallet API"> |
| 106 | + ## Wallet API |
| 107 | + |
| 108 | + New statuses - `Preconfirmed`, `Preconfirmed Onchain Failure`, and `Preconfirmed Partial Onchain Failure` - have been introduced to indicate the UserOperation status when calling wallet_getCallsStatus. |
| 109 | + |
| 110 | + </Tab> |
| 111 | +</Tabs> |
0 commit comments