Skip to content

Commit

Permalink
fix: add multiwrite fix
Browse files Browse the repository at this point in the history
  • Loading branch information
metalboyrick committed Oct 18, 2024
1 parent e6982ee commit ec0c7c7
Showing 1 changed file with 76 additions and 37 deletions.
113 changes: 76 additions & 37 deletions packages/nextjs/hooks/scaffold-stark/useScaffoldMultiWriteContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<TContractName>;
// 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<TContractName>;

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) {
Expand All @@ -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<TContractName>;
// 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 {
Expand Down Expand Up @@ -119,3 +156,5 @@ export function createContractCall<
) {
return { contractName, functionName, args };
}
import { error } from "console";
import { id } from "ethers";

0 comments on commit ec0c7c7

Please sign in to comment.