我试图让useWaitForTransaction运行,以将应用程序的状态从提交更改为成功或失败,但它没有运行。这很奇怪,因为如果我硬将散列编码成'0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060',,而不是像'0x654b47a2b0b7dbea231548ee2527aff882f554dba2e66bea00765624a06ff5b8'.这样的契约交互的事务,它就会运行。
代码如下:
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);
},
});任何帮助都是非常感谢的!
发布于 2022-12-16 14:07:37
...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);
},
});您应该像这样调用send函数:write?.()或write()
我认为waitForTransaction钩子混淆了--这是一个函数或sendTransaction的结果。
https://ethereum.stackexchange.com/questions/141235
复制相似问题