mirror of
https://github.com/luxfhe/example-smart-wallet.git
synced 2026-10-30 18:13:16 +00:00
132 lines
4.6 KiB
TypeScript
132 lines
4.6 KiB
TypeScript
import { useState } from "react";
|
|
import { useTargetNetwork } from "./useTargetNetwork";
|
|
import { MutateOptions } from "@tanstack/react-query";
|
|
import { Abi, ExtractAbiFunctionNames } from "abitype";
|
|
import { Config, UseWriteContractParameters, useWriteContract } from "wagmi";
|
|
import { WriteContractErrorType, WriteContractReturnType } from "wagmi/actions";
|
|
import { WriteContractVariables } from "wagmi/query";
|
|
import { useDeployedContractInfo, useTransactor } from "~~/hooks/scaffold-eth";
|
|
import { notification } from "~~/utils/scaffold-eth";
|
|
import {
|
|
ContractAbi,
|
|
ContractName,
|
|
ScaffoldWriteContractOptions,
|
|
ScaffoldWriteContractVariables,
|
|
} from "~~/utils/scaffold-eth/contract";
|
|
import { useChain } from "@account-kit/react";
|
|
|
|
/**
|
|
* Wrapper around wagmi's useWriteContract hook which automatically loads (by name) the contract ABI and address from
|
|
* the contracts present in deployedContracts.ts & externalContracts.ts corresponding to targetNetworks configured in scaffold.config.ts
|
|
* @param contractName - name of the contract to be written to
|
|
* @param writeContractParams - wagmi's useWriteContract parameters
|
|
*/
|
|
export const useScaffoldWriteContract = <TContractName extends ContractName>(
|
|
contractName: TContractName,
|
|
writeContractParams?: UseWriteContractParameters,
|
|
) => {
|
|
const { chain } = useChain();
|
|
const writeTx = useTransactor();
|
|
const [isMining, setIsMining] = useState(false);
|
|
const { targetNetwork } = useTargetNetwork();
|
|
|
|
const wagmiContractWrite = useWriteContract(writeContractParams);
|
|
|
|
const { data: deployedContractData } = useDeployedContractInfo(contractName);
|
|
|
|
const sendContractWriteAsyncTx = async <
|
|
TFunctionName extends ExtractAbiFunctionNames<ContractAbi<TContractName>, "nonpayable" | "payable">,
|
|
>(
|
|
variables: ScaffoldWriteContractVariables<TContractName, TFunctionName>,
|
|
options?: ScaffoldWriteContractOptions,
|
|
) => {
|
|
if (!deployedContractData) {
|
|
notification.error("Target Contract is not deployed, did you forget to run `yarn deploy`?");
|
|
return;
|
|
}
|
|
|
|
if (!chain?.id) {
|
|
notification.error("Please connect your wallet");
|
|
return;
|
|
}
|
|
if (chain?.id !== targetNetwork.id) {
|
|
notification.error("You are on the wrong network");
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setIsMining(true);
|
|
const { blockConfirmations, onBlockConfirmation, ...mutateOptions } = options || {};
|
|
const makeWriteWithParams = () =>
|
|
wagmiContractWrite.writeContractAsync(
|
|
{
|
|
abi: deployedContractData.abi as Abi,
|
|
address: deployedContractData.address,
|
|
...variables,
|
|
} as WriteContractVariables<Abi, string, any[], Config, number>,
|
|
mutateOptions as
|
|
| MutateOptions<
|
|
WriteContractReturnType,
|
|
WriteContractErrorType,
|
|
WriteContractVariables<Abi, string, any[], Config, number>,
|
|
unknown
|
|
>
|
|
| undefined,
|
|
);
|
|
const writeTxResult = await writeTx(makeWriteWithParams, { blockConfirmations, onBlockConfirmation });
|
|
|
|
return writeTxResult;
|
|
} catch (e: any) {
|
|
throw e;
|
|
} finally {
|
|
setIsMining(false);
|
|
}
|
|
};
|
|
|
|
const sendContractWriteTx = <
|
|
TContractName extends ContractName,
|
|
TFunctionName extends ExtractAbiFunctionNames<ContractAbi<TContractName>, "nonpayable" | "payable">,
|
|
>(
|
|
variables: ScaffoldWriteContractVariables<TContractName, TFunctionName>,
|
|
options?: Omit<ScaffoldWriteContractOptions, "onBlockConfirmation" | "blockConfirmations">,
|
|
) => {
|
|
if (!deployedContractData) {
|
|
notification.error("Target Contract is not deployed, did you forget to run `yarn deploy`?");
|
|
return;
|
|
}
|
|
if (!chain?.id) {
|
|
notification.error("Please connect your wallet");
|
|
return;
|
|
}
|
|
if (chain?.id !== targetNetwork.id) {
|
|
notification.error("You are on the wrong network");
|
|
return;
|
|
}
|
|
|
|
wagmiContractWrite.writeContract(
|
|
{
|
|
abi: deployedContractData.abi as Abi,
|
|
address: deployedContractData.address,
|
|
...variables,
|
|
} as WriteContractVariables<Abi, string, any[], Config, number>,
|
|
options as
|
|
| MutateOptions<
|
|
WriteContractReturnType,
|
|
WriteContractErrorType,
|
|
WriteContractVariables<Abi, string, any[], Config, number>,
|
|
unknown
|
|
>
|
|
| undefined,
|
|
);
|
|
};
|
|
|
|
return {
|
|
...wagmiContractWrite,
|
|
isMining,
|
|
// Overwrite wagmi's writeContactAsync
|
|
writeContractAsync: sendContractWriteAsyncTx,
|
|
// Overwrite wagmi's writeContract
|
|
writeContract: sendContractWriteTx,
|
|
};
|
|
};
|