Skip to main content

usePrepareUserOperation

Hook for preparing a UserOp to be sent via useSendUserOperation for smart accounts.

It automates the generation of calldata and fetches essential parameters like paymaster data and estimated gas fees for smart account transactions.

import { usePrepareUserOperation } from 'moonchute';

Usage

import { usePrepareUserOperation, useSendUserOperation } from 'moonchute';

function PrepareUserOperationComponent() {
const { config, error } = usePrepareUserOperation({
address: '0xContractAddress',
abi: contractAbi,
functionName: 'mint',
});
const { write } = useSendUserOperation(config);

return (
<>
<button disabled={!write} onClick={() => write?.()}>
Send UserOp
</button>
{error && (
<div>
An error occurred preparing the user operation: {error.message}
</div>
)}
</>
);
}

Return Value

{
userOpHash: Hex,
userOp: PrepareUserOperationResult,
chainId: number,
accountType: number,
}

PrepareUserOperationResult

{
sender: Hex,
nonce: Hex,
initCode: Hex,
callData: Hex,
callGasLimit: number,
verificationGasLimit: number,
preVerificationGas: number,
maxFeePerGas: Hex,
maxPriorityFeePerGas: Hex,
paymasterAndData: Hex,
signature: Hex,
}

Parameters

address (required)

The contract address to interact with.

abi (required)

The ABI of the contract.

functionName (required)

The function to call on the contract.

args (optional)

Arguments for the function call.

value (optional)

Value to send with the transaction (for payable functions).

Note

This hook automatically integrates with wagmi's useAccount and useNetwork hooks to retrieve the user's account and network details, streamlining the process of preparing transactions in dApp development.