我正在使用 paymaster 服务以及
useWriteContracts
库中的 wagmi
钩子开发 dApp。提交交易后,我收到的不是标准交易哈希,而是一个长 ID。以下是我的设置的概要:
const capabilities = useMemo(() => {
if (!availableCapabilities || !account.chainId) return {};
const capabilitiesForChain = availableCapabilities[account.chainId];
if (
capabilitiesForChain["paymasterService"] &&
capabilitiesForChain["paymasterService"].supported
) {
return {
paymasterService: {
url: `${document.location.origin}/api/paymaster`,
},
};
}
return {};
}, [availableCapabilities, account.chainId]);
const { writeContracts } = useWriteContracts({
mutation: {
onSuccess: async (id) => {
console.log("Returned ID:", id);
// I want to extract the transaction hash here
},
onError: (err) => {
console.log("Transaction Error", err);
},
},
});
// Later in the code, I call writeContracts
writeContracts({
contracts: [
{
address: contractAddress,
abi: contractAbi,
functionName: "function name",
args: [function arguments],
value: tx value,
},
],
capabilities,
});
返回的
id
不是典型的交易哈希,而是一个长字符串,如下所示:
0x9c5df149aee141828a73238bfd76218b73d4f70cdc1dd2f30362081fbd24b5bf0000000000000000000000000000000000000000000000000000000000014a34
我需要实际的交易哈希来跟踪链上交易(例如,0x1b1730bfba3a955b76dfdf74b11dc3088edb16c215ca30aaf131c3765c355f56
)。有没有办法从响应中提取交易哈希,或者在将 useWriteContracts 与付款人一起使用时获取它的其他方法?
我提供示例代码。
data = {
chainId: chain.id,
__mode: "prepared",
abi: PresaleContractABI,
address: "0x9e499f31906674fE5091067Fa83ccbe2851038Aa",
functionName: "buyToken",
args: [
parseUnits(payTokenAmount, global.PROJECT_TOKEN.decimals),
],
value: 0,
};
const preparedData = await prepareWriteContract(data);
const writeData = await writeContract(preparedData);
const txHash = writeData.hash;
const txPendingData = waitForTransaction(writeData);
toast.promise(txPendingData, {
pending: `Waiting for transaction ${txHash} to be confirmed... 👌`,
});
const txData = await txPendingData;
console.log("Transaction Hash:", txHash);