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>
}