Skip to content

Commit

Permalink
Show insufficient funds page when fee estimation fail due to exceedin…
Browse files Browse the repository at this point in the history
…g amount (#437)
  • Loading branch information
JunichiSugiura authored Jul 2, 2024
1 parent ddc063e commit 9467094
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/keychain/src/components/Execute/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useCallback, useEffect, useMemo, useState } from "react";
import { Button } from "@chakra-ui/react";
import { formatEther } from "viem";
import { Policy, ResponseCodes } from "@cartridge/controller";
import {
import { Policy, ResponseCodes } from "@cartridge/controller"; import {
Container,
Content,
FOOTER_MIN_HEIGHT,
Expand All @@ -16,6 +15,7 @@ import { ErrorAlert } from "components/ErrorAlert";
import { Policies } from "Policies";
import { Fees } from "./Fees";
import { ExecuteCtx } from "utils/connection";
import { TransferAmountExceedsBalance } from "errors";

export const CONTRACT_ETH =
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7";
Expand Down Expand Up @@ -88,6 +88,12 @@ export function Execute() {
setFees({ base: fees.overall_fee, max: fees.suggestedMaxFee });
})
.catch((e) => {
if (e.message.includes("ERC20: transfer amount exceeds balance")) {
setIsInsufficient(true)
setError(new TransferAmountExceedsBalance())
return
}

setError(e);
});
}, [account, controller, setError, setFees, calls, chainId, ctx]);
Expand Down Expand Up @@ -139,9 +145,13 @@ export function Execute() {
</Content>

<Footer>
{error ? (
{error ? error.name === "TransferAmountExceedsBalance" ? (
<ErrorAlert
title={error.message}
/>
) : (
<ErrorAlert
title="Fee estimation failed"
title="Something went wrong"
description={error.message}
/>
) : (
Expand Down
9 changes: 9 additions & 0 deletions packages/keychain/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class TransferAmountExceedsBalance extends Error {
constructor() {
super("Transfer amount exceeds balance");

this.name = "TransferAmountExceedsBalance";

Object.setPrototypeOf(this, TransferAmountExceedsBalance.prototype);
}
}

0 comments on commit 9467094

Please sign in to comment.