-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
Copy pathquote.ts
36 lines (34 loc) · 1.31 KB
/
quote.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import type { QuoteRequest } from '../../types/bridge';
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,
) &&
(requireAmount
? Boolean((partialRequest.srcTokenAmount ?? '').match(/^[1-9]\d*$/u))
: true)
);
};