You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am writing a React hook to combine writeContractAsync and waitForTransactionReceipt. This is because most of the times my frontend needs to wait for the tx to be confirmed and status must reflect that i.e. it must be pending until confirmed.
The implementation works however I am facing an issue related to the parameters' type inference.
With Parameters<typeof writeContractAsync> when I use writeAndWaitForReceipt, params[0] are broken. Eg. abi args are not inferred (foo doesn't exist and typescript should report this as an error) and value is not recognized as a valid param.
Whereas when I type writeAndWaitForReceipt as typeof writeContractAsync types are inferred correctly but of course now the return type must be writeContractAsync's rather than Promise<{ hash: Hash; receipt: TransactionReceipt }> which is what I want.
Any idea about why this is happening?
Full hook implementation:
import{useState,useCallback}from"react";import{waitForTransactionReceipt}from"@wagmi/core";import{useWriteContract,useConfig}from"wagmi";import{UserRejectedRequestError,typeHash,typeTransactionReceipt,}from"viem";exportfunctionuseWriteAndWaitForReceipt(){constconfig=useConfig();const{
writeContractAsync,
writeContract,status: originalStatus,
reset,
...writeContractRest}=useWriteContract();const[status,setStatus]=useState<typeoforiginalStatus>("idle");const[receipt,setReceipt]=useState<TransactionReceipt>();// Ideally we want writeAndWaitForReceipt// to accept the same variables (first argument of writeContractAsync)// and define custom onSuccess,onError,onSettled// as well as return {hash, receipt} instead of just hash// @todo figure out why Parameters<typeof writeContractAsync> messes up type inference.constwriteAndWaitForReceipt=useCallback(asyncfunctionwriteAndWaitForReceipt(
...params: Parameters<typeofwriteContractAsync>): Promise<{hash: Hash;receipt: TransactionReceipt}>{setStatus("pending");try{consthash=awaitwriteContractAsync(params[0]);constreceipt=awaitwaitForTransactionReceipt(config,{ hash });setReceipt(receipt);setStatus("success");return{ hash, receipt };}catch(error){setStatus(errorinstanceofUserRejectedRequestError ? "idle" : "error");throwerror;}},[writeContractAsync,config],);return{
...writeContractRest,reset: ()=>{setReceipt(undefined);setStatus("idle");reset();},
writeAndWaitForReceipt,
status,
receipt,};}exporttypeUseWriteAndWaitForReceipt=ReturnType<typeofuseWriteAndWaitForReceipt>;
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am writing a React hook to combine
writeContractAsync
andwaitForTransactionReceipt
. This is because most of the times my frontend needs to wait for the tx to be confirmed andstatus
must reflect that i.e. it must bepending
until confirmed.The implementation works however I am facing an issue related to the parameters' type inference.
With
Parameters<typeof writeContractAsync>
when I usewriteAndWaitForReceipt
,params[0]
are broken. Eg. abi args are not inferred (foo doesn't exist and typescript should report this as an error) andvalue
is not recognized as a valid param.Whereas when I type
writeAndWaitForReceipt
astypeof writeContractAsync
types are inferred correctly but of course now the return type must be writeContractAsync's rather thanPromise<{ hash: Hash; receipt: TransactionReceipt }>
which is what I want.Any idea about why this is happening?
Full hook implementation:
Beta Was this translation helpful? Give feedback.
All reactions