diff --git a/packages/nextjs/components/stake/StakeContractInteraction.tsx b/packages/nextjs/components/stake/StakeContractInteraction.tsx index 56c1d8f1..d3c39664 100644 --- a/packages/nextjs/components/stake/StakeContractInteraction.tsx +++ b/packages/nextjs/components/stake/StakeContractInteraction.tsx @@ -66,7 +66,6 @@ export const StakeContractInteraction = ({ address }: { address?: string }) => { const { sendAsync: withdrawETH } = useScaffoldWriteContract({ contractName: "Staker", functionName: "withdraw", - args: [] }); const { sendAsync: stakeEth } = useScaffoldMultiWriteContract({ diff --git a/packages/nextjs/hooks/scaffold-stark/useScaffoldReadContract.ts b/packages/nextjs/hooks/scaffold-stark/useScaffoldReadContract.ts index b9b9431c..fae3d663 100644 --- a/packages/nextjs/hooks/scaffold-stark/useScaffoldReadContract.ts +++ b/packages/nextjs/hooks/scaffold-stark/useScaffoldReadContract.ts @@ -37,4 +37,4 @@ export const useScaffoldReadContract = < }) as Omit, "data"> & { data: AbiFunctionOutputs | undefined; }; -}; \ No newline at end of file +}; diff --git a/packages/nextjs/hooks/scaffold-stark/useScaffoldWriteContract.ts b/packages/nextjs/hooks/scaffold-stark/useScaffoldWriteContract.ts index 61f41df7..7632c9e8 100644 --- a/packages/nextjs/hooks/scaffold-stark/useScaffoldWriteContract.ts +++ b/packages/nextjs/hooks/scaffold-stark/useScaffoldWriteContract.ts @@ -1,4 +1,4 @@ -import { useEffect, useMemo } from "react"; +import { useCallback, useEffect, useMemo } from "react"; import { useTargetNetwork } from "./useTargetNetwork"; import { useDeployedContractInfo, @@ -12,8 +12,14 @@ import { parseFunctionParams, UseScaffoldWriteConfig, } from "~~/utils/scaffold-stark/contract"; -import { useSendTransaction, useNetwork, Abi } from "@starknet-react/core"; +import { + useSendTransaction, + useNetwork, + Abi, + useContract, +} from "@starknet-react/core"; import { notification } from "~~/utils/scaffold-stark"; +import { Contract as StarknetJsContract } from "starknet"; type UpdatedArgs = Parameters< ReturnType["sendAsync"] @@ -45,93 +51,100 @@ export const useScaffoldWriteContract = < [deployedContractData?.abi, functionName], ); - const parsedParams = useMemo(() => { - if (args && abiFunction && deployedContractData) { - const parsed = parseFunctionParams({ - abiFunction, - abi: deployedContractData.abi, - inputs: args as any[], - isRead: false, - isReadArgsParsing: false, - }).flat(Infinity); - return parsed; - } - return []; - }, [args, abiFunction, deployedContractData]); + // TODO: see if we need this bit later + // const parsedParams = useMemo(() => { + // if (args && abiFunction && deployedContractData) { + // const parsed = parseFunctionParams({ + // abiFunction, + // abi: deployedContractData.abi, + // inputs: args as any[], + // isRead: false, + // isReadArgsParsing: true, + // }).flat(Infinity); + // return parsed; + // } + // return []; + // }, [args, abiFunction, deployedContractData]); - const sendTransactionInstance = useSendTransaction({ - calls: deployedContractData - ? [ - { - contractAddress: deployedContractData?.address, - entrypoint: functionName, - calldata: parsedParams, - }, - ] - : [], - }); + // leave blank for now since default args will be called by the trigger function anyway + const sendTransactionInstance = useSendTransaction({}); - const sendContractWriteTx = async (params?: { - args?: UseScaffoldWriteConfig["args"]; - }) => { - // if no args supplied, use the one supplied from hook - let newArgs = params?.args; - if (Object.keys(newArgs || {}).length <= 0) { - newArgs = args; - } + const sendContractWriteTx = useCallback( + async (params?: { + args?: UseScaffoldWriteConfig["args"]; + }) => { + // if no args supplied, use the one supplied from hook + let newArgs = params?.args; + if (Object.keys(newArgs || {}).length <= 0) { + newArgs = args; + } - if (!deployedContractData) { - console.error( - "Target Contract is not deployed, did you forget to run `yarn deploy`?", + if (!deployedContractData) { + console.error( + "Target Contract is not deployed, did you forget to run `yarn deploy`?", + ); + return; + } + if (!chain?.id) { + console.error("Please connect your wallet"); + return; + } + if (chain?.id !== targetNetwork.id) { + console.error("You are on the wrong network"); + return; + } + + // TODO: see if we need this back, keeping this here + // let newParsedParams = + // newArgs && abiFunction && deployedContractData + // ? parseFunctionParams({ + // abiFunction, + // abi: deployedContractData.abi, + // inputs: newArgs as any[], + // isRead: false, + // isReadArgsParsing: false, + // }) + // : parsedParams; + + // we convert to starknetjs contract instance here since deployed data may be undefined if contract is not deployed + const contractInstance = new StarknetJsContract( + deployedContractData.abi, + deployedContractData.address, ); - return; - } - if (!chain?.id) { - console.error("Please connect your wallet"); - return; - } - if (chain?.id !== targetNetwork.id) { - console.error("You are on the wrong network"); - return; - } - let newParsedParams = - newArgs && abiFunction && deployedContractData - ? parseFunctionParams({ - abiFunction, - abi: deployedContractData.abi, - inputs: newArgs as any[], - isRead: false, - isReadArgsParsing: false, - }).flat(Infinity) - : parsedParams; - const newCalls = [ - { - contractAddress: deployedContractData.address, - entrypoint: functionName, - calldata: newParsedParams, - }, - ]; + const newCalls = deployedContractData + ? [contractInstance.populate(functionName, newArgs as any[])] + : []; - if (sendTransactionInstance.sendAsync) { - try { - // setIsMining(true); - return await sendTxnWrapper(() => - sendTransactionInstance.sendAsync(newCalls as any[]), - ); - } catch (e: any) { - throw e; - } finally { - // setIsMining(false); + if (sendTransactionInstance.sendAsync) { + try { + // setIsMining(true); + return await sendTxnWrapper(() => + sendTransactionInstance.sendAsync(newCalls as any[]), + ); + } catch (e: any) { + throw e; + } finally { + // setIsMining(false); + } + } else { + notification.error("Contract writer error. Try again."); + return; } - } else { - notification.error("Contract writer error. Try again."); - return; - } - }; + }, + [ + args, + chain?.id, + deployedContractData, + functionName, + sendTransactionInstance, + sendTxnWrapper, + targetNetwork.id, + ], + ); return { ...sendTransactionInstance, sendAsync: sendContractWriteTx, }; -}; \ No newline at end of file +}; diff --git a/packages/nextjs/utils/scaffold-stark/contract.ts b/packages/nextjs/utils/scaffold-stark/contract.ts index 6a8fb479..759153d0 100644 --- a/packages/nextjs/utils/scaffold-stark/contract.ts +++ b/packages/nextjs/utils/scaffold-stark/contract.ts @@ -458,10 +458,7 @@ const decodeParamsWithType = (paramType: string, param: any): unknown => { : `Err(${parseParamWithType(error, result.unwrap(), isRead)})`; }, param); } else if (isCairoContractAddress(paramType)) { - return tryParsingParamReturnObject( - getChecksumAddress, - `0x${param.toString(16)}`, - ); + return tryParsingParamReturnObject(validateAndParseAddress, param); } else if (isCairoU256(paramType)) { return tryParsingParamReturnObject(uint256.uint256ToBN, param); } else if (isCairoByteArray(paramType)) { @@ -783,13 +780,11 @@ export function parseFunctionParams({ args: inputs, }); - console.debug({ formattedInputs }); - formattedInputs.forEach((inputItem) => { const { type: inputType, value: inputValue } = inputItem; parsedInputs.push( - parseParamWithType(inputType, inputValue, isRead, !!isReadArgsParsing), + deepParseValues(inputValue, isRead, inputType, !!isReadArgsParsing), ); }); @@ -841,7 +836,10 @@ function formatInputForParsing({ _formatInput(structValue, argIndex, variants as AbiParameter[]), ]; }); - return { type: structName, value: { variant: Object.fromEntries } }; + return { + type: structName, + value: { variant: Object.fromEntries(formattedEntries) }, + }; } const { members } = structDef as AbiStruct; @@ -948,4 +946,4 @@ function encodeCustomEnumWithParsedVariants( return [numActiveVariant.toString(), ...parsedParameter]; } return [numActiveVariant.toString(), parsedParameter]; -} +} \ No newline at end of file