Use the useWriteContractSponsored hook to initiate transactions on smart contracts with the transaction gas fees sponsored by a paymaster.

It uses the useWriteContract hook from wagmi under the hood.

Import

import { useWriteContractSponsored } from "@abstract-foundation/agw-react";

Usage

import { useWriteContractSponsored } from "@abstract-foundation/agw-react";
import { getGeneralPaymasterInput } from "viem/zksync";
import type { Abi } from "viem";

const contractAbi: Abi = [
  /* Your contract ABI here */
];

export default function App() {
  const { writeContractSponsored, data, error, isSuccess, isPending } =
    useWriteContractSponsored();

  const handleWriteContract = () => {
    writeContractSponsored({
      abi: contractAbi,
      address: "0xC4822AbB9F05646A9Ce44EFa6dDcda0Bf45595AA",
      functionName: "mint",
      args: ["0x273B3527BF5b607dE86F504fED49e1582dD2a1C6", BigInt(1)],
      paymaster: "0x5407B5040dec3D339A9247f3654E59EEccbb6391",
      paymasterInput: getGeneralPaymasterInput({
        innerInput: "0x",
      }),
    });
  };

  return (
    <div>
      <button onClick={handleWriteContract} disabled={isPending}>
        {isPending ? "Processing..." : "Execute Sponsored Transaction"}
      </button>
      {isSuccess && <div>Transaction Hash: {data}</div>}
      {error && <div>Error: {error.message}</div>}
    </div>
  );
}

Returns

Returns a UseWriteContractSponsoredReturnType<Config, unknown>.