From 120999a3357a4aa41b4d56a6d7685b9f5ed3d0c4 Mon Sep 17 00:00:00 2001 From: Nishan Date: Tue, 31 Oct 2023 09:03:43 +0530 Subject: [PATCH] fix: gas estimation --- .../use-creator-token-approve.ts | 15 +++++++++++-- .../creator-token/use-creator-token-buy.ts | 21 +++++++++++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/packages/app/hooks/creator-token/use-creator-token-approve.ts b/packages/app/hooks/creator-token/use-creator-token-approve.ts index b6742e9a3..c883b8e6d 100644 --- a/packages/app/hooks/creator-token/use-creator-token-approve.ts +++ b/packages/app/hooks/creator-token/use-creator-token-approve.ts @@ -44,8 +44,17 @@ export const useApproveToken = () => { const { gasPrice } = await publicClient.estimateFeesPerGas({ type: "legacy", }); + const { maxFeePerGas, maxPriorityFeePerGas } = + await publicClient.estimateFeesPerGas({ + type: "eip1559", + }); - if (gasPrice) { + console.log("gas price approve", { + gasPrice, + maxFeePerGas, + maxPriorityFeePerGas, + }); + if (gasPrice && maxFeePerGas) { const { request } = await publicClient.simulateContract({ address: usdcAddress, account: walletAddress, @@ -53,7 +62,9 @@ export const useApproveToken = () => { functionName: "approve", args: [creatorTokenContract, maxPrice], chain: chain, - maxFeePerGas: gasPrice * 2n, + maxFeePerGas: + maxFeePerGas > gasPrice * 2n ? maxFeePerGas : gasPrice * 2n, + maxPriorityFeePerGas, }); const hash = await walletClient?.writeContract(request); diff --git a/packages/app/hooks/creator-token/use-creator-token-buy.ts b/packages/app/hooks/creator-token/use-creator-token-buy.ts index 611b668f3..9a41fe977 100644 --- a/packages/app/hooks/creator-token/use-creator-token-buy.ts +++ b/packages/app/hooks/creator-token/use-creator-token-buy.ts @@ -76,8 +76,17 @@ export const useCreatorTokenBuy = (params: { const { gasPrice } = await publicClient.estimateFeesPerGas({ type: "legacy", }); - - if (gasPrice) { + const { maxFeePerGas, maxPriorityFeePerGas } = + await publicClient.estimateFeesPerGas({ + type: "eip1559", + }); + + console.log("gas price buy", { + gasPrice, + maxFeePerGas, + maxPriorityFeePerGas, + }); + if (gasPrice && maxFeePerGas) { if (tokenAmount === 1) { const { request } = await publicClient.simulateContract({ address: profileData?.data?.profile.creator_token.address, @@ -87,7 +96,9 @@ export const useCreatorTokenBuy = (params: { args: [priceToBuyNext.data?.totalPrice], chain: baseChain, type: "eip1559", - maxFeePerGas: gasPrice * 2n, + maxFeePerGas: + maxFeePerGas > gasPrice * 2n ? maxFeePerGas : gasPrice * 2n, + maxPriorityFeePerGas, }); requestPayload = request; console.log("token amount 1 simulation ", request); @@ -100,7 +111,9 @@ export const useCreatorTokenBuy = (params: { args: [tokenAmount, priceToBuyNext.data?.totalPrice], chain: baseChain, type: "eip1559", - maxFeePerGas: gasPrice * 2n, + maxFeePerGas: + maxFeePerGas > gasPrice * 2n ? maxFeePerGas : gasPrice * 2n, + maxPriorityFeePerGas, }); console.log("bulk buy request", request); requestPayload = request;