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 d07e05c40..7c1cb66eb 100644 --- a/packages/app/hooks/creator-token/use-creator-token-buy.ts +++ b/packages/app/hooks/creator-token/use-creator-token-buy.ts @@ -23,6 +23,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"; @@ -35,6 +36,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, @@ -65,18 +67,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, + }; +};