Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom error class for minimum amount error #321

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
kev1n-peters marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
Loading