Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
fix: insufficient funds error message (#2488)
Browse files Browse the repository at this point in the history
  • Loading branch information
intergalacticspacehighway committed Nov 1, 2023
1 parent 568b87b commit 709bf81
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 34 deletions.
11 changes: 9 additions & 2 deletions packages/app/hooks/creator-token/use-creator-token-buy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { providers } from "ethers";
import { useSWRConfig } from "swr";
import useSWRMutation from "swr/mutation";

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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", {
Expand Down
80 changes: 48 additions & 32 deletions packages/app/hooks/creator-token/use-creator-token-sell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
12 changes: 12 additions & 0 deletions packages/app/hooks/creator-token/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { InsufficientFundsError, ContractFunctionExecutionError } from "viem";
import { baseGoerli, base } from "viem/chains";

import { isDEV } from "app/utilities";
Expand All @@ -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;
}
}
};

0 comments on commit 709bf81

Please sign in to comment.