Skip to content

Commit

Permalink
custom error class for minimum amount error
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
artursapek committed Sep 12, 2024
1 parent 5810ebb commit 9ac44ca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
8 changes: 2 additions & 6 deletions connect/src/routes/tokenBridge/automatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -167,12 +168,7 @@ export class AutomaticTokenBridgeRoute<N extends Network>
// 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;
Expand Down
16 changes: 16 additions & 0 deletions connect/src/routes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 9ac44ca

Please sign in to comment.