From a1a66451b3c6694a49abb203cb941ea5c6ba9720 Mon Sep 17 00:00:00 2001 From: BrickheadJohnny Date: Wed, 29 Jan 2025 11:06:26 +0100 Subject: [PATCH] feat(useCollectNft): estimate gas manually and log estimation to posthog --- .../[guild]/collect/hooks/useCollectNft.ts | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/components/[guild]/collect/hooks/useCollectNft.ts b/src/components/[guild]/collect/hooks/useCollectNft.ts index 55db971244..e7489236df 100644 --- a/src/components/[guild]/collect/hooks/useCollectNft.ts +++ b/src/components/[guild]/collect/hooks/useCollectNft.ts @@ -133,30 +133,45 @@ const useCollectNft = () => { : BigInt(0) let hash: `0x${string}` | undefined = undefined + let estimatedGas: bigint | undefined = undefined if (isLegacyClaimArgs(claimData.args)) { const [address, userId, , signature] = claimData.args - const { request: legacyClaimRequest } = await publicClient.simulateContract({ + const args = { abi: legacyGuildRewardNftAbi, address: nftAddress, functionName: "claim", args: [address, BigInt(userId), signature], value: claimFee, account: walletClient.account, - } as const) + } as const + estimatedGas = await publicClient.estimateContractGas(args) + captureEvent("useCollectNFT:estimatedGas", { + estimatedGas, + }) + + const { request: legacyClaimRequest } = + await publicClient.simulateContract(args) hash = await walletClient.writeContract(legacyClaimRequest) } if (isClaimArgs(claimData.args)) { const [amount, address, userId, signedAt, signature] = claimData.args - const { request: newClaimRequest } = await publicClient.simulateContract({ + const args = { abi: guildRewardNftAbi, address: nftAddress, functionName: "claim", args: [BigInt(amount), address, BigInt(userId), BigInt(signedAt), signature], value: claimFee, account: walletClient.account, - } as const) + } as const + + estimatedGas = await publicClient.estimateContractGas(args) + const { request: newClaimRequest } = await publicClient.simulateContract(args) + captureEvent("useCollectNFT:estimatedGas", { + estimatedGas, + }) + hash = await walletClient.writeContract(newClaimRequest) }