Skip to content

Commit

Permalink
correct createScaffoldWriteContract to take contractName as a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteAtATime committed Apr 16, 2024
1 parent 567d37e commit e7fd1fc
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions packages/svelte/src/lib/runes/scaffoldWriteContract.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type { Abi, ExtractAbiFunctionNames } from "abitype";
import { createTransactor } from "./transactor.svelte";
import { createTargetNetwork } from "./targetNetwork.svelte";
import {
contracts,
type ContractAbi,
type ContractName,
type scaffoldWriteContractOptions,
Expand All @@ -18,31 +17,33 @@ import { notification } from "$lib/utils/scaffold-eth/notification";
import type { WriteContractVariables } from "@wagmi/core/query";
import type { MutateOptions } from "@tanstack/svelte-query";
import type { WriteContractErrorType, WriteContractReturnType } from "@wagmi/core";
import { createDeployedContractInfo } from "./deployedContractInfo.svelte";

/**
* 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 - contract name
* @param writeContractParams - wagmi's useWriteContract parameters
*/
export const createScaffoldWriteContract = (writeContractParams?: CreateWriteContractParameters) => {
export const createScaffoldWriteContract = <TContractName extends ContractName>(
contractName: TContractName,
writeContractParams?: CreateWriteContractParameters,
) => {
const { chain } = $derived.by(createAccount());
const writeTx = $derived.by(createTransactor());
const targetNetwork = $derived.by(createTargetNetwork());
let isMining = $state(false);

const wagmiContractWrite = $derived.by(createWriteContract(writeContractParams));

const { data: deployedContractData } = $derived.by(createDeployedContractInfo(contractName));

const sendContractWriteTx = async <
TContractName extends ContractName,
TFunctionName extends ExtractAbiFunctionNames<ContractAbi<TContractName>, "nonpayable" | "payable">,
>(
variables: scaffoldWriteContractVariables<TContractName, TFunctionName>,
options?: scaffoldWriteContractOptions,
) => {
const { contractName, ...wagmiVariables } = variables;
const deployedContractData = contracts?.[targetNetwork.id]?.[contractName];

if (!deployedContractData) {
notification.error("Target Contract is not deployed, did you forget to run `yarn deploy`?");
return;
Expand All @@ -64,16 +65,16 @@ export const createScaffoldWriteContract = (writeContractParams?: CreateWriteCon
{
abi: deployedContractData.abi as Abi,
address: deployedContractData.address,
...wagmiVariables,
...variables,
} as WriteContractVariables<Abi, string, any[], Config, number>,
mutateOptions as
| MutateOptions<
WriteContractReturnType,
WriteContractErrorType,
WriteContractVariables<Abi, string, any[], Config, number>,
unknown
>
| undefined,
| MutateOptions<
WriteContractReturnType,
WriteContractErrorType,
WriteContractVariables<Abi, string, any[], Config, number>,
unknown
>
| undefined,
);
const writeTxResult = await writeTx(makeWriteWithParams, { blockConfirmations, onBlockConfirmation });

Expand Down

0 comments on commit e7fd1fc

Please sign in to comment.