Function useSendGasTokenTransaction

  • Parameters

    Returns UseMutationResult<UserOpResponse, any, UseSendGasTokenTransactionProps, any>

    Description

    Sends a transaction, taking an erc20 token of choice as payment for the gas.

    Mutation function args: UseSendGasTokenTransactionProps

    Example


    import { useSendGasTokenTransaction, useUserOpWait, Options, useSmartAccount, mergeOptions } from "@biconomy/useAA"
    import { polygonAmoy } from "viem/chains"
    import { encodeFunctionData, parseAbi } from "wagmi"

    export const SendGasTokenTx = () => {

    const { smartAccountAddress } = useSmartAccount();

    const {
    mutate,
    data: userOpResponse,
    error,
    isPending,
    } = useSendGasTokenTransaction();

    const {
    isLoading: waitIsLoading,
    isSuccess: waitIsSuccess,
    error: waitError,
    data: waitData,
    } = useUserOpWait(userOpResponse);

    useEffect(() => {
    if (waitData?.success === "true") {
    console.log(waitData?.receipt?.transactionHash);
    }
    }, [waitData]);


    const transactions = useMemo(
    () => ({
    to: "0x1758f42Af7026fBbB559Dc60EcE0De3ef81f665e",
    data: encodeFunctionData({
    abi: parseAbi(["function safeMint(address _to)"]),
    functionName: "safeMint",
    args: [smartAccountAddress as Hex],
    }),
    }),
    [smartAccountAddress]
    );

    const { data: feeQuoteResponse, isLoading: isLoadingFee } = useTokenFees({ transactions });

    const mintNftTx = () => mutate({ transactions, tokenParam: feeQuotes[0] });

    // OR if you know in advance which token you'd like to use

    const alternativeMintTx = () => mutate({ transactions, tokenParam: "0xf555F8d9Cf90f9d95D34488e6C852796D9acBd31" });

    return (
    <ErrorGuard errors={[error, waitError]}>
    <Button
    title="Mint NFT"
    onClickFunc={mintNftTx}
    isLoading={isPending || waitIsLoading}
    />
    </ErrorGuard>
    );
    };