0

I am getting this error in runtime when using usePrepareContractWrite my code is as follows from the wagmi documentation:

const { config } = usePrepareContractWrite({
        address: "0x07a06AD39f......................"
        abi: [{ "inputs": [
                            {
                                "internalType": "address",
                                "name": "operator",
                                "type": "address"
                            },
                            {
                                "internalType": "bool",
                                "name": "approved",
                                "type": "bool"
                            }
                        ],
                    "name": "setApprovalForAll",
                    "outputs": [],
                    "stateMutability": "nonpayable",
                    "type": "function"
                
            }],
        functionName: 'setApprovalForAll',
    });
    
    const { data, write } = useContractWrite(config)
    
    const { isLoading, isSuccess }= useWaitForTransaction({
        confirmations: 1,
        hash: data?.hash,
        onSuccess() {
            console.log("transaction success");
        }
    });
    
    const getPermission = async () => {
    ->    write({
                   args: ["0xaD1beE8B47464…………………………..", true],
                    from: address,
                });
    }    

1 Answer 1

0

Make sure that the usePrepareContractWrite hook has finished executing before calling the useContractWrite hook. You can do this by checking the isLoading property of the usePrepareContractWrite hook.

Your end-user is connected to a wallet? It should be. You can do this by checking the isConnected property of the wagmi context.

Here is an example of how to check the isLoading property of the usePrepareContractWrite hook before calling the useContractWrite hook:

const { config, isLoading } = usePrepareContractWrite({
  address: "0x07a06AD39f......................",
  abi: [{
    "inputs": [
      {
        "internalType": "address",
        "name": "operator",
        "type": "address"
      },
      {
        "internalType": "bool",
        "name": "approved",
        "type": "bool"
      }
    ],
    "name": "setApprovalForAll",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  }],
  functionName: 'setApprovalForAll',
});

if (!isLoading) {
  const { data, write } = useContractWrite(config);

  const { isLoading, isSuccess } = useWaitForTransaction({
    confirmations: 1,
    hash: data?.hash,
    onSuccess() {
      console.log("transaction success");
    }
  });

  const getPermission = async () => {
    write({
      args: ["0xaD1beE8B47464…………………………..", true],
      from: address,
    });
  }
}
New contributor
ankitaeth is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.