Skip to content

Instantly share code, notes, and snippets.

@technophile-04
Created October 15, 2023 05:14
Show Gist options
  • Save technophile-04/397983065d03d946383ef6cb63628bbf to your computer and use it in GitHub Desktop.
Save technophile-04/397983065d03d946383ef6cb63628bbf to your computer and use it in GitHub Desktop.
SE-2: useTransactor example with wagmi's useContractWrite hook
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>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment