From cc51d7c5baa96e8fa79174cbe49037475d9aed24 Mon Sep 17 00:00:00 2001 From: Artur Sapek Date: Thu, 12 Sep 2024 16:17:10 -0400 Subject: [PATCH] Custom error class for minimum amount error (#321) * custom error class for minimum amount error this should be used to communicate that validate() or quote() have failed, but only because the user's transfer amount was too small. UI should use this to show a helpful error so the user can increase their transfer size. * new special error type: UnavailableError --- connect/src/routes/tokenBridge/automatic.ts | 10 +++---- connect/src/routes/types.ts | 29 ++++++++++++++++++++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/connect/src/routes/tokenBridge/automatic.ts b/connect/src/routes/tokenBridge/automatic.ts index d19ae699d..0aeee76f2 100644 --- a/connect/src/routes/tokenBridge/automatic.ts +++ b/connect/src/routes/tokenBridge/automatic.ts @@ -14,6 +14,9 @@ import { TransferState } from "../../types.js"; import { Wormhole } from "../../wormhole.js"; import type { StaticRouteMethods } from "../route.js"; import { AutomaticRoute } from "../route.js"; +import { + MinAmountError, +} from '../types.js'; import type { Quote, QuoteResult, @@ -167,12 +170,7 @@ export class AutomaticTokenBridgeRoute // Min amount is fee + 5% const minAmount = (fee * 105n) / 100n; if (amount.units(amt) < minAmount) { - throw new Error( - `Minimum amount is ${amount.display({ - amount: minAmount.toString(), - decimals: amt.decimals, - })}`, - ); + throw new MinAmountError(amount.fromBaseUnits(minAmount, amt.decimals)); } const redeemableAmount = amount.units(amt) - fee; diff --git a/connect/src/routes/types.ts b/connect/src/routes/types.ts index e68e36888..2975d8aa7 100644 --- a/connect/src/routes/types.ts +++ b/connect/src/routes/types.ts @@ -1,6 +1,6 @@ import type { TokenId } from "@wormhole-foundation/sdk-definitions"; import type { AttestationReceipt, TransferReceipt } from "../types.js"; -import type { amount } from "@wormhole-foundation/sdk-base"; +import { amount } from "@wormhole-foundation/sdk-base"; import type { QuoteWarning } from "../warnings.js"; // Extend Options to provide custom options @@ -84,3 +84,30 @@ export type QuoteError = { success: false; error: Error; }; + +// Special error to return from quote() or validate() when the +// given transfer amount is too small. Used to helpfully +// show a minimum amount in the interface. +export class MinAmountError extends Error { + min: amount.Amount; + + constructor(min: amount.Amount) { + super(`Minimum transfer amount is ${amount.display(min)}`); + this.min = min; + } + + minAmount(): amount.Amount { + return this.min; + } +} + +// Special error to return from quote() or validate() when the +// protocol can't provide a quote. +export class UnavailableError extends Error { + internalError: Error; + + constructor(internalErr: Error) { + super(`Unable to fetch a quote`); + this.internalError = internalErr; + } +}