Skip to content

Commit

Permalink
Custom error class for minimum amount error (#321)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
artursapek authored Sep 12, 2024
1 parent 5810ebb commit cc51d7c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
10 changes: 4 additions & 6 deletions connect/src/routes/tokenBridge/automatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -167,12 +170,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
29 changes: 28 additions & 1 deletion connect/src/routes/types.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
}
}

0 comments on commit cc51d7c

Please sign in to comment.