Skip to content
Draft
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
57 changes: 35 additions & 22 deletions wormhole-connect/src/hooks/useAmountValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,30 @@ export const useAmountValidation = (props: Props): HookReturn => {
// Min amount available
const minAmount = useMemo(
() =>
Object.values(props.quotes).reduce((minAmount, quoteResult) => {
if (quoteResult?.success) {
return minAmount;
}

if (!isMinAmountError(quoteResult?.error)) {
return minAmount;
}

if (!minAmount) {
return quoteResult.error.min;
}

const minAmountNum = BigInt(quoteResult.error.min.amount);
const existingMin = BigInt(minAmount.amount);
if (minAmountNum < existingMin) {
return quoteResult.error.min;
} else {
return minAmount;
}
}, undefined as sdkAmount.Amount | undefined),
Object.values(props.quotes).reduce(
(minAmount, quoteResult) => {
if (quoteResult?.success) {
return minAmount;
}

if (!isMinAmountError(quoteResult?.error)) {
return minAmount;
}

if (!minAmount) {
return quoteResult.error.min;
}

const minAmountNum = BigInt(quoteResult.error.min.amount);
const existingMin = BigInt(minAmount.amount);
if (minAmountNum < existingMin) {
return quoteResult.error.min;
} else {
return minAmount;
}
},
undefined as sdkAmount.Amount | undefined,
),
[props.quotes],
);

Expand All @@ -59,8 +62,12 @@ export const useAmountValidation = (props: Props): HookReturn => {
});
}, [props.routes, props.quotes]);

const noRoutes = useMemo(() => {
return props.routes.length === 0;
}, [props.routes]);

// Don't show errors when no amount is set or it's loading
if (!amount || props.disabled) {
if (!amount || props.disabled || props.isLoading) {
return {};
}

Expand All @@ -73,6 +80,12 @@ export const useAmountValidation = (props: Props): HookReturn => {
}
}

if (noRoutes) {
return {
error: 'No routes found for this transaction.',
};
}

if (allRoutesFailed) {
if (minAmount) {
const formattedAmount = sdkAmount.display(minAmount);
Expand Down
Loading