Skip to content

Commit cc51d7c

Browse files
authoredSep 12, 2024··
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
1 parent 5810ebb commit cc51d7c

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed
 

‎connect/src/routes/tokenBridge/automatic.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import { TransferState } from "../../types.js";
1414
import { Wormhole } from "../../wormhole.js";
1515
import type { StaticRouteMethods } from "../route.js";
1616
import { AutomaticRoute } from "../route.js";
17+
import {
18+
MinAmountError,
19+
} from '../types.js';
1720
import type {
1821
Quote,
1922
QuoteResult,
@@ -167,12 +170,7 @@ export class AutomaticTokenBridgeRoute<N extends Network>
167170
// Min amount is fee + 5%
168171
const minAmount = (fee * 105n) / 100n;
169172
if (amount.units(amt) < minAmount) {
170-
throw new Error(
171-
`Minimum amount is ${amount.display({
172-
amount: minAmount.toString(),
173-
decimals: amt.decimals,
174-
})}`,
175-
);
173+
throw new MinAmountError(amount.fromBaseUnits(minAmount, amt.decimals));
176174
}
177175

178176
const redeemableAmount = amount.units(amt) - fee;

‎connect/src/routes/types.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { TokenId } from "@wormhole-foundation/sdk-definitions";
22
import type { AttestationReceipt, TransferReceipt } from "../types.js";
3-
import type { amount } from "@wormhole-foundation/sdk-base";
3+
import { amount } from "@wormhole-foundation/sdk-base";
44
import type { QuoteWarning } from "../warnings.js";
55

66
// Extend Options to provide custom options
@@ -84,3 +84,30 @@ export type QuoteError = {
8484
success: false;
8585
error: Error;
8686
};
87+
88+
// Special error to return from quote() or validate() when the
89+
// given transfer amount is too small. Used to helpfully
90+
// show a minimum amount in the interface.
91+
export class MinAmountError extends Error {
92+
min: amount.Amount;
93+
94+
constructor(min: amount.Amount) {
95+
super(`Minimum transfer amount is ${amount.display(min)}`);
96+
this.min = min;
97+
}
98+
99+
minAmount(): amount.Amount {
100+
return this.min;
101+
}
102+
}
103+
104+
// Special error to return from quote() or validate() when the
105+
// protocol can't provide a quote.
106+
export class UnavailableError extends Error {
107+
internalError: Error;
108+
109+
constructor(internalErr: Error) {
110+
super(`Unable to fetch a quote`);
111+
this.internalError = internalErr;
112+
}
113+
}

0 commit comments

Comments
 (0)
Please sign in to comment.