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 88edf08ca..8ee7d49cb 100644 --- a/packages/app/hooks/creator-token/use-creator-token-buy.ts +++ b/packages/app/hooks/creator-token/use-creator-token-buy.ts @@ -1,4 +1,3 @@ -import { providers } from "ethers"; import { useSWRConfig } from "swr"; import useSWRMutation from "swr/mutation"; @@ -26,7 +25,11 @@ import { } 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"; +import { + baseChain, + creatorTokenSwapRouterAddress, + isInsufficientFundsErrorFn, +} from "./utils"; export const useCreatorTokenBuy = (params: { username?: string; @@ -223,6 +226,10 @@ export const useCreatorTokenBuy = (params: { { onError: (error) => { { + if (isInsufficientFundsErrorFn(error)) { + toast.error(`Insufficient ${params.paymentMethod} balance`); + } + console.error("useCreatorTokenContractBuy", error); captureException(error); toast.error("Failed", { diff --git a/packages/app/hooks/creator-token/use-creator-token-sell.ts b/packages/app/hooks/creator-token/use-creator-token-sell.ts index b064fcae4..92deb7de5 100644 --- a/packages/app/hooks/creator-token/use-creator-token-sell.ts +++ b/packages/app/hooks/creator-token/use-creator-token-sell.ts @@ -19,6 +19,7 @@ import { useWallet } from "../use-wallet"; import { getContractBalanceOfTokenKey } from "./use-balance-of-token"; import { getTotalCollectedKey } from "./use-contract-total-collected"; import { getPriceToBuyNextKey } from "./use-creator-token-price-to-buy-next"; +import { useMaxGasPrices } from "./use-max-gas-prices"; import { useSwitchChain } from "./use-switch-chain"; import { baseChain } from "./utils"; @@ -34,6 +35,7 @@ export const useCreatorTokenSell = () => { const Alert = useAlert(); const switchChain = useSwitchChain(); const { mutate } = useSWRConfig(); + const { getMaxFeePerGasAndPriorityPrice } = useMaxGasPrices(); const user = useContext(UserContext); const { loginPromise } = useLogInPromise(); const state = useSWRMutation( @@ -121,40 +123,54 @@ export const useCreatorTokenSell = () => { requestPayload = request; } - const txHash = await wallet.walletClient?.writeContract?.( - requestPayload - ); + const maxPrices = await getMaxFeePerGasAndPriorityPrice(); + + if (maxPrices) { + const { maxFeePerGas, maxPriorityFeePerGas } = maxPrices; + + console.log("gas price sell", { + maxFeePerGas, + maxPriorityFeePerGas, + }); - const transaction = await publicClient.waitForTransactionReceipt({ - hash: txHash as any, - pollingInterval: 2000, - confirmations: 2, - }); - - if (transaction.status === "success") { - mutate(getTotalCollectedKey(arg.contractAddress)); - mutate( - getPriceToBuyNextKey({ - address: arg.contractAddress, - tokenAmount: 1, - }) - ); - mutate( - getContractBalanceOfTokenKey({ - ownerAddress: walletAddress, - contractAddress: arg.contractAddress, - }) - ); - await axios({ - url: "/v1/creator-token/poll-sell", - method: "POST", - data: { - creator_token_id: arg.creatorTokenId, - token_ids: tokenIds, - tx_hash: txHash, - }, + const txHash = await wallet.walletClient?.writeContract({ + ...requestPayload, + type: "eip1559", + maxFeePerGas, + maxPriorityFeePerGas, }); - return true; + + const transaction = await publicClient.waitForTransactionReceipt({ + hash: txHash as any, + pollingInterval: 2000, + confirmations: 2, + }); + + if (transaction.status === "success") { + mutate(getTotalCollectedKey(arg.contractAddress)); + mutate( + getPriceToBuyNextKey({ + address: arg.contractAddress, + tokenAmount: 1, + }) + ); + mutate( + getContractBalanceOfTokenKey({ + ownerAddress: walletAddress, + contractAddress: arg.contractAddress, + }) + ); + await axios({ + url: "/v1/creator-token/poll-sell", + method: "POST", + data: { + creator_token_id: arg.creatorTokenId, + token_ids: tokenIds, + tx_hash: txHash, + }, + }); + return true; + } } } else if (tokenIdsRes.token_ids_by_wallet) { Alert.alert( diff --git a/packages/app/hooks/creator-token/utils.ts b/packages/app/hooks/creator-token/utils.ts index b9ecbd4e6..b4422afce 100644 --- a/packages/app/hooks/creator-token/utils.ts +++ b/packages/app/hooks/creator-token/utils.ts @@ -1,3 +1,4 @@ +import { InsufficientFundsError, ContractFunctionExecutionError } from "viem"; import { baseGoerli, base } from "viem/chains"; import { isDEV } from "app/utilities"; @@ -18,3 +19,14 @@ export const creatorTokenSwapRouterAddress = isDEV : "0x2390491f26873090492792f64f3eea66f611a801"; export const baseChain = isDEV ? baseGoerli : base; + +export const isInsufficientFundsErrorFn = (error: any) => { + if (error instanceof ContractFunctionExecutionError) { + const isInsufficientFundsError = error.walk( + (e) => e instanceof InsufficientFundsError + ); + if (isInsufficientFundsError) { + return true; + } + } +};