Skip to content

Instantly share code, notes, and snippets.

@technophile-04
Created October 15, 2023 05:14
Show Gist options
  • Select an option

  • Save technophile-04/397983065d03d946383ef6cb63628bbf to your computer and use it in GitHub Desktop.

Select an option

Save technophile-04/397983065d03d946383ef6cb63628bbf to your computer and use it in GitHub Desktop.

Revisions

  1. technophile-04 created this gist Oct 15, 2023.
    40 changes: 40 additions & 0 deletions ContractInteraction.tsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    import { parseEther } from "viem";
    import { useContractWrite } from "wagmi";
    import { ArrowSmallRightIcon } from "@heroicons/react/24/outline";
    import DeployedContracts from "~~/generated/deployedContracts";
    import { useTransactor } from "~~/hooks/scaffold-eth";

    export const ContractInteraction = () => {
    const yourContractAbi = DeployedContracts[31337][0].contracts.YourContract.abi;
    const yourContractAddress = DeployedContracts[31337][0].contracts.YourContract.address;

    const writeTx = useTransactor();

    const { writeAsync, isLoading } = useContractWrite({
    address: yourContractAddress,
    abi: yourContractAbi,
    functionName: "setGreeting",
    value: parseEther("0.01"),
    args: ["Hello world!"],
    });

    const handleSetGreeting = async () => {
    try {
    await writeTx(writeAsync, { blockConfirmations: 1 });
    } catch (e) {
    console.log("Unexpected error in writeTx", e);
    }
    };

    return (
    <button className="btn btn-primary" onClick={handleSetGreeting} disabled={isLoading}>
    {isLoading ? (
    <span className="loading loading-spinner loading-sm"></span>
    ) : (
    <>
    Send <ArrowSmallRightIcon className="w-3 h-3 mt-0.5" />
    </>
    )}
    </button>
    );
    };