Docs

useBuyWithCryptoStatus

A hook to get a status of Buy with Crypto transaction.

This hook is a React Query wrapper of the getBuyWithCryptoStatus function. You can also use that function directly.

Example

import { useSigner, useBuyWithCryptoQuote, useBuyWithCryptoStatus } from "@thirdweb-dev/react";
function Component() {
const buyWithCryptoQuoteQuery = useBuyWithCryptoQuote(swapParams);
const signer = useSigner();
const [buyTxHash, setBuyTxHash] = useState<string | undefined>();
const buyWithCryptoStatusQuery = useBuyWithCryptoStatus* (buyTxHash ? {
clientId: "YOUR_CLIENT_ID",
transactionHash: buyTxHash,
}: undefined);
async function handleBuyWithCrypto() {
if (!buyWithCryptoQuoteQuery.data || !signer) {
return;
}
// if approval is required
if (buyWithCryptoQuoteQuery.data.approval) {
const approveTx = await signer.sendTransaction(buyWithCryptoQuoteQuery.data.approval);
await approveTx.wait();
}
// send the transaction to buy crypto
// this promise is resolved when user confirms the transaction * in the wallet and the transaction is sent to the blockchain
const buyTx = await signer.sendTransaction(buyWithCryptoQuoteQuery.data.transactionRequest);
await buyTx.wait();
// set buyTx.transactionHash to poll the status of the swap * transaction
setBuyTxHash(buyTx.hash);
}
if (buyWithCryptoStatusQuery.data) {
console.log('buyWithCryptoStatusQuery.data', * buyWithCryptoStatusQuery.data)
}
return <button onClick={handleBuyWithCrypto}>Swap</button>
}

Parameters

Returns

A react query object which contains the data of type BuyWithCryptoStatus