From 6876ebe6dc0777e262fe1a4a735ffb065ed834f9 Mon Sep 17 00:00:00 2001 From: Nishan Date: Wed, 1 Nov 2023 07:50:42 +0530 Subject: [PATCH] 2x + priority --- .../use-creator-token-approve.ts | 16 +++++--------- .../creator-token/use-creator-token-buy.ts | 16 +++++--------- .../hooks/creator-token/use-max-gas-prices.ts | 21 +++++++++++++++++++ 3 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 packages/app/hooks/creator-token/use-max-gas-prices.ts 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 8d437378b..f6f998027 100644 --- a/packages/app/hooks/creator-token/use-creator-token-approve.ts +++ b/packages/app/hooks/creator-token/use-creator-token-approve.ts @@ -5,10 +5,12 @@ import { publicClient } from "app/lib/wallet-public-client"; import { isDEV } from "app/utilities"; import { useWallet } from "../use-wallet"; +import { useMaxGasPrices } from "./use-max-gas-prices"; import { usdcAddress } from "./utils"; export const useApproveToken = () => { const wallet = useWallet(); + const { getMaxFeePerGasAndPriorityPrice } = useMaxGasPrices(); const state = useSWRMutation( "approveToken", async function approveToken( @@ -44,18 +46,10 @@ export const useApproveToken = () => { return true; } - const { maxFeePerGas: estimatedMaxFeePerGas, maxPriorityFeePerGas } = - await publicClient.estimateFeesPerGas({ - type: "eip1559", - }); - - const latestBlockBaseFeePerGas = (await publicClient.getBlock()) - .baseFeePerGas; + const maxPrices = await getMaxFeePerGasAndPriorityPrice(); - if (maxPriorityFeePerGas) { - const maxFeePerGas = latestBlockBaseFeePerGas - ? latestBlockBaseFeePerGas * 2n + maxPriorityFeePerGas - : estimatedMaxFeePerGas; + if (maxPrices) { + const { maxFeePerGas, maxPriorityFeePerGas } = maxPrices; console.log("gas price approve", { maxFeePerGas, 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 a32d28bec..fba9a9c80 100644 --- a/packages/app/hooks/creator-token/use-creator-token-buy.ts +++ b/packages/app/hooks/creator-token/use-creator-token-buy.ts @@ -24,6 +24,7 @@ import { getPriceToBuyNextKey, useCreatorTokenPriceToBuyNext, } from "./use-creator-token-price-to-buy-next"; +import { useMaxGasPrices } from "./use-max-gas-prices"; import { useSwitchChain } from "./use-switch-chain"; import { baseChain, creatorTokenSwapRouterAddress } from "./utils"; @@ -36,6 +37,7 @@ export const useCreatorTokenBuy = (params: { const { data: profileData } = useUserProfile({ address: username }); const wallet = useWallet(); const approveToken = useApproveToken(); + const { getMaxFeePerGasAndPriorityPrice } = useMaxGasPrices(); const priceToBuyNext = useCreatorTokenPriceToBuyNext({ address: profileData?.data?.profile.creator_token?.address, tokenAmount, @@ -66,18 +68,10 @@ export const useCreatorTokenBuy = (params: { if (walletAddress && profileData?.data?.profile.creator_token) { const switchChainSuccess = await switchChain.trigger(); - const { maxFeePerGas: estimatedMaxFeePerGas, maxPriorityFeePerGas } = - await publicClient.estimateFeesPerGas({ - type: "eip1559", - }); - - const latestBlockBaseFeePerGas = (await publicClient.getBlock()) - .baseFeePerGas; + const maxPrices = await getMaxFeePerGasAndPriorityPrice(); - if (maxPriorityFeePerGas) { - const maxFeePerGas = latestBlockBaseFeePerGas - ? latestBlockBaseFeePerGas * 2n + maxPriorityFeePerGas - : estimatedMaxFeePerGas; + if (maxPrices) { + const { maxFeePerGas, maxPriorityFeePerGas } = maxPrices; console.log("gas price buy", { maxFeePerGas, diff --git a/packages/app/hooks/creator-token/use-max-gas-prices.ts b/packages/app/hooks/creator-token/use-max-gas-prices.ts new file mode 100644 index 000000000..05ab886f4 --- /dev/null +++ b/packages/app/hooks/creator-token/use-max-gas-prices.ts @@ -0,0 +1,21 @@ +import { publicClient } from "app/lib/wallet-public-client"; + +export const useMaxGasPrices = () => { + const getMaxFeePerGasAndPriorityPrice = async () => { + // In Wei. Gwei = 0.001. Recommended by base team + const maxPriorityFeePerGas = 1000000n; + const latestBlockBaseFeePerGas = (await publicClient.getBlock()) + .baseFeePerGas; + if (latestBlockBaseFeePerGas) { + const maxFeePerGas = latestBlockBaseFeePerGas * 2n + maxPriorityFeePerGas; + return { + maxFeePerGas, + maxPriorityFeePerGas, + }; + } + }; + + return { + getMaxFeePerGasAndPriorityPrice, + }; +};