1

I'm trying to make the useWaitForTransaction to run to change state of my application from submitting to success or failure, but it's not running. It's odd because it runs if I hard code my hash to '0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060', but not a transaction of one of my contract interactions like '0x654b47a2b0b7dbea231548ee2527aff882f554dba2e66bea00765624a06ff5b8'.

Code is below:

const { data, isError, isLoading } = useContractReads({
    contracts: [
      {
        ...lotteryContract,
        functionName: "getLotteryNumber",
      },
      {
        ...lotteryContract,
        functionName: "lottery_state",
      },
      {
        ...lotteryContract,
        functionName: "getPotAmount",
      },
      {
        ...lotteryContract,
        functionName: "winners",
        args: -1,
      },
    ],
    onSuccess(data) {
      console.log("Success", data);
      setLotteryNumber(data[0].toString());
      setStatus(lotteryStatusMap[data[1]]);
      setCurrentPotAmount(ethers.utils.formatEther(data[2].toString()));
      setAmountOfEntries(
        Math.ceil(ethers.utils.formatEther(data[2].toString()) * 100)
      );
      setLastWinnerAddress(data[3]);
      setLoadingContract(false);
    },
  });

  const { config } = usePrepareContractWrite({
    address: contractData.address,
    abi: contractData.abi,
    chainId: 5,
    functionName: "enter",
    overrides: {
      value: ethers.utils.parseEther(entryFee.toString()),
    },
  });

  const enterRaffle = useContractWrite({
    ...config,
    onSuccess(data) {
      setEnteringLottery(true);
      setTxHash(data.hash);
      setEnteringError(null);
      console.log(data);
    },
    onError(error) {
      setEnteringError(error.message);
    },
  });

  const waitForTransaction = useWaitForTransaction({
    hash: enterRaffle.data?.hash,
    onSuccess(data) {
      setEnteringLottery(false);
      setEnteredSuccess(true);
      setEnteringError(null);
      console.log("Success", data);
    },
    onError(error) {
      setEnteringLottery(false);
      setEnteredSuccess(false);
      setEnteringError(error.message);
    },
  });

Any help is appreciated!

2 Answers 2

0
    ...config,
    onSuccess(data) {
      setEnteringLottery(true);
      setTxHash(data.hash);
      setEnteringError(null);
      console.log(data);
    },
    onError(error) {
      setEnteringError(error.message);
    },
  });

  const waitForTransaction = useWaitForTransaction({
    hash: enterRaffle?.hash,
    onSuccess(data) {
      setEnteringLottery(false);
      setEnteredSuccess(true);
      setEnteringError(null);
      console.log("Success", data);
    },
    onError(error) {
      setEnteringLottery(false);
      setEnteredSuccess(false);
      setEnteringError(error.message);
    },
  });

you should call the send function like this: write?.() or write()

I think the waitForTransaction hook is confused weither it's a function or the result from sendTransaction.

0

how about adding a ? after enterRaffle

  const waitForTransaction = useWaitForTransaction({
    hash: enterRaffle?.data?.hash,
    onSuccess(data) {
      setEnteringLottery(false);
      setEnteredSuccess(true);
      setEnteringError(null);
      console.log("Success", data);
    },
    onError(error) {
      setEnteringLottery(false);
      setEnteredSuccess(false);
      setEnteringError(error.message);
    },
  });

Your Answer

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

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