From 9ac44ca9a84083f0cabd7d28f241f01b7bb0d3a3 Mon Sep 17 00:00:00 2001 From: Artur Sapek Date: Tue, 6 Feb 2024 09:59:09 -0500 Subject: [PATCH] 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. --- connect/src/routes/tokenBridge/automatic.ts | 8 ++------ connect/src/routes/types.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/connect/src/routes/tokenBridge/automatic.ts b/connect/src/routes/tokenBridge/automatic.ts index d19ae699d..c14644c30 100644 --- a/connect/src/routes/tokenBridge/automatic.ts +++ b/connect/src/routes/tokenBridge/automatic.ts @@ -15,6 +15,7 @@ import { Wormhole } from "../../wormhole.js"; import type { StaticRouteMethods } from "../route.js"; import { AutomaticRoute } from "../route.js"; import type { + MinAmountError, Quote, QuoteResult, Receipt, @@ -167,12 +168,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..259e8e7df 100644 --- a/connect/src/routes/types.ts +++ b/connect/src/routes/types.ts @@ -84,3 +84,19 @@ 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; + } +}