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 175e962 commit 9b642ee
Showing 1 changed file with 71 additions and 42 deletions.
113 changes: 71 additions & 42 deletions packages/nextjs/hooks/scaffold-stark/useScaffoldMultiWriteContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,53 +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>;

// TODO: see if we still need this
// 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);

// 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,
);
// // 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[]),
// 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: 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 @@ -94,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

0 comments on commit 9b642ee

Please sign in to comment.