diff --git a/packages/nextjs/hooks/scaffold-stark/useScaffoldMultiWriteContract.ts b/packages/nextjs/hooks/scaffold-stark/useScaffoldMultiWriteContract.ts index 16728c86..1aa73f3c 100644 --- a/packages/nextjs/hooks/scaffold-stark/useScaffoldMultiWriteContract.ts +++ b/packages/nextjs/hooks/scaffold-stark/useScaffoldMultiWriteContract.ts @@ -11,7 +11,7 @@ import { UseScaffoldWriteConfig, } from "~~/utils/scaffold-stark/contract"; import { useSendTransaction, useNetwork, Abi } from "@starknet-react/core"; -import { InvocationsDetails } from "starknet"; +import { Contract as StarknetJsContract, InvocationsDetails } from "starknet"; import { notification } from "~~/utils/scaffold-stark"; import { useMemo } from "react"; import { useTransactor } from "./useTransactor"; @@ -34,45 +34,53 @@ export const useScaffoldMultiWriteContract = < const { chain } = useNetwork(); const sendTxnWrapper = useTransactor(); - const parsedCalls = useMemo(() => { - if (calls) { - return calls.map((call) => { - const functionName = call.functionName; - const contractName = call.contractName; - const unParsedArgs = call.args as any[]; - const contract = contracts?.[targetNetwork.network]?.[ - contractName as ContractName - ] as Contract; + // TODO: commented out in case we need it again + // const parsedCalls = useMemo(() => { + // if (calls) { + // return calls.map((call) => { + // const functionName = call.functionName; + // const contractName = call.contractName; + // const unParsedArgs = call.args as any[]; + // const contract = contracts?.[targetNetwork.network]?.[ + // contractName as ContractName + // ] as Contract; - const abiFunction = getFunctionsByStateMutability( - contract?.abi || [], - "external", - ).find((fn) => fn.name === functionName); + // // TODO: see if we still need this + // // const abiFunction = getFunctionsByStateMutability( + // // contract?.abi || [], + // // "external", + // // ).find((fn) => fn.name === functionName); - return { - contractAddress: contract?.address, - entrypoint: functionName, - calldata: - abiFunction && unParsedArgs && contract - ? parseFunctionParams({ - abiFunction, - isRead: false, - inputs: unParsedArgs as any[], - isReadArgsParsing: false, - abi: contract.abi, - }).flat() - : [], - }; - }); - } else { - return []; - } - }, [calls]); + // // we convert to starknetjs contract instance here since deployed data may be undefined if contract is not deployed + // const contractInstance = new StarknetJsContract( + // contract.abi, + // contract.address, + // ); + + // return { + // ...contractInstance.populate(functionName, unParsedArgs as any[]), + + // // TODO: see if we still need this + // // calldata: + // // abiFunction && unParsedArgs && contract + // // ? parseFunctionParams({ + // // abiFunction, + // // isRead: false, + // // inputs: unParsedArgs as any[], + // // isReadArgsParsing: false, + // // abi: contract.abi, + // // }).flat() + // // : [], + // }; + // }); + // } else { + // return []; + // } + // }, [calls, targetNetwork.network]); // TODO add custom options - const sendTransactionInstance = useSendTransaction({ - calls: parsedCalls, - }); + + const sendTransactionInstance = useSendTransaction({}); const sendContractWriteTx = async () => { if (!chain?.id) { @@ -86,8 +94,37 @@ export const useScaffoldMultiWriteContract = < if (sendTransactionInstance.sendAsync) { try { + // we just parse calldata here so that it will only parse on demand. + // use IIFE pattern + const parsedCalls = (() => { + if (calls) { + return calls.map((call) => { + const functionName = call.functionName; + const contractName = call.contractName; + const unParsedArgs = call.args as any[]; + const contract = contracts?.[targetNetwork.network]?.[ + contractName as ContractName + ] as Contract; + // we convert to starknetjs contract instance here since deployed data may be undefined if contract is not deployed + const contractInstance = new StarknetJsContract( + contract.abi, + contract.address, + ); + + return contractInstance.populate( + functionName, + unParsedArgs as any[], + ); + }); + } else { + return []; + } + })(); + // setIsMining(true); - return await sendTxnWrapper(() => sendTransactionInstance.sendAsync()); + return await sendTxnWrapper(() => + sendTransactionInstance.sendAsync(parsedCalls), + ); } catch (e: any) { throw e; } finally { @@ -119,3 +156,5 @@ export function createContractCall< ) { return { contractName, functionName, args }; } +import { error } from "console"; +import { id } from "ethers";