Skip to content

Commit

Permalink
chore: move quote req validator to util file
Browse files Browse the repository at this point in the history
  • Loading branch information
micaelae committed Oct 28, 2024
1 parent ab342ba commit 3157b10
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
32 changes: 0 additions & 32 deletions ui/pages/bridge/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,6 @@ export type BridgeAsset = {
icon?: string;
};

export const isValidQuoteRequest = (
partialRequest: Partial<QuoteRequest>,
requireAmount = true,
): partialRequest is QuoteRequest => {
const STRING_FIELDS = ['srcTokenAddress', 'destTokenAddress'];
if (requireAmount) {
STRING_FIELDS.push('srcTokenAmount');
}
const NUMBER_FIELDS = ['srcChainId', 'destChainId', 'slippage'];

return (
STRING_FIELDS.every(
(field) =>
field in partialRequest &&
typeof partialRequest[field as keyof typeof partialRequest] ===
'string' &&
partialRequest[field as keyof typeof partialRequest] !== undefined &&
partialRequest[field as keyof typeof partialRequest] !== '' &&
partialRequest[field as keyof typeof partialRequest] !== null,
) &&
NUMBER_FIELDS.every(
(field) =>
field in partialRequest &&
typeof partialRequest[field as keyof typeof partialRequest] ===
'number' &&
partialRequest[field as keyof typeof partialRequest] !== undefined &&
!isNaN(Number(partialRequest[field as keyof typeof partialRequest])) &&
partialRequest[field as keyof typeof partialRequest] !== null,
)
);
};

export type QuoteRequest = {
walletAddress: string;
destWalletAddress?: string;
Expand Down
33 changes: 33 additions & 0 deletions ui/pages/bridge/utils/quote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { QuoteRequest } from '../types';

export const isValidQuoteRequest = (
partialRequest: Partial<QuoteRequest>,
requireAmount = true,
): partialRequest is QuoteRequest => {
const STRING_FIELDS = ['srcTokenAddress', 'destTokenAddress'];
if (requireAmount) {
STRING_FIELDS.push('srcTokenAmount');
}
const NUMBER_FIELDS = ['srcChainId', 'destChainId', 'slippage'];

return (
STRING_FIELDS.every(
(field) =>
field in partialRequest &&
typeof partialRequest[field as keyof typeof partialRequest] ===
'string' &&
partialRequest[field as keyof typeof partialRequest] !== undefined &&
partialRequest[field as keyof typeof partialRequest] !== '' &&
partialRequest[field as keyof typeof partialRequest] !== null,
) &&
NUMBER_FIELDS.every(
(field) =>
field in partialRequest &&
typeof partialRequest[field as keyof typeof partialRequest] ===
'number' &&
partialRequest[field as keyof typeof partialRequest] !== undefined &&
!isNaN(Number(partialRequest[field as keyof typeof partialRequest])) &&
partialRequest[field as keyof typeof partialRequest] !== null,
)
);
};

0 comments on commit 3157b10

Please sign in to comment.