Skip to content

Commit

Permalink
feat(useCollectNft): estimate gas manually and log estimation to posthog
Browse files Browse the repository at this point in the history
  • Loading branch information
BrickheadJohnny committed Jan 29, 2025
1 parent 98435fa commit a1a6645
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/components/[guild]/collect/hooks/useCollectNft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit a1a6645

Please sign in to comment.