-
Notifications
You must be signed in to change notification settings - Fork 504
/
validation.tsx
63 lines (52 loc) · 1.68 KB
/
validation.tsx
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { convertBaseToStandard } from 'data/components/exchange/services'
import { Props } from './template.success'
import { SBCheckoutFormValuesType } from 'data/types'
import { SBPairType, SBPaymentMethodType } from 'core/types'
import BigNumber from 'bignumber.js'
export const getMaxMin = (
pair: SBPairType,
minOrMax?: 'min' | 'max',
allValues?: SBCheckoutFormValuesType,
method?: SBPaymentMethodType
) => {
switch (minOrMax || 'max') {
case 'max':
const defaultMax = convertBaseToStandard('FIAT', 0)
if (!allValues) return defaultMax
if (!method) return defaultMax
if (!pair) return defaultMax
const max = BigNumber.minimum(method.limits.max, pair.buyMax).toString()
return convertBaseToStandard('FIAT', max)
case 'min':
const defaultMin = convertBaseToStandard('FIAT', 0)
if (!allValues) return defaultMin
if (!method) return defaultMin
if (!pair) return defaultMin
const min = BigNumber.maximum(method.limits.min, pair.buyMin).toString()
return convertBaseToStandard('FIAT', min)
}
}
export const maximumAmount = (
value: string,
allValues: SBCheckoutFormValuesType,
restProps: Props
) => {
if (!value) return true
const { pair, method } = restProps
if (!method) return true
return Number(value) > Number(getMaxMin(pair, 'max', allValues, method))
? 'ABOVE_MAX'
: false
}
export const minimumAmount = (
value: string,
allValues: SBCheckoutFormValuesType,
restProps: Props
) => {
if (!value) return true
const { pair, method } = restProps
if (!method) return true
return Number(value) < Number(getMaxMin(pair, 'min', allValues, method))
? 'BELOW_MIN'
: false
}