Skip to content

Commit

Permalink
feat(sb): limit validation for FUNDS
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip London committed Jul 23, 2020
1 parent ee24ba4 commit 62e5c3e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
Expand Up @@ -15,19 +15,22 @@ export const getData = (state: RootState) => {
const suggestedAmountsR = selectors.components.simpleBuy.getSBSuggestedAmounts(
state
)
const sbBalancesR = selectors.components.simpleBuy.getSBBalances(state)
const userDataR = selectors.modules.profile.getUserData(state)

return lift(
(
invitations: ExtractSuccess<typeof invitationsR>,
sbBalances: ExtractSuccess<typeof sbBalancesR>,
suggestedAmounts: ExtractSuccess<typeof suggestedAmountsR>,
userData: ExtractSuccess<typeof userDataR>
) => ({
formErrors,
formValues,
invitations,
sbBalances,
suggestedAmounts,
userData
})
)(invitationsR, suggestedAmountsR, userDataR)
)(invitationsR, sbBalancesR, suggestedAmountsR, userDataR)
}
Expand Up @@ -127,7 +127,13 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = props => {
const prop = amtError === 'ABOVE_MAX' ? 'max' : 'min'
const value = convertStandardToBase(
'FIAT',
getMaxMin(props.pair, prop, props.formValues, props.method)
getMaxMin(
props.pair,
prop,
props.formValues,
props.sbBalances,
props.method
)
)
props.simpleBuyActions.handleSBSuggestedAmountClick(value)
}
Expand Down Expand Up @@ -182,6 +188,7 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = props => {
props.pair,
'max',
props.formValues,
props.sbBalances,
props.method
),
digits: 0
Expand All @@ -201,6 +208,7 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = props => {
props.pair,
'min',
props.formValues,
props.sbBalances,
props.method
),
digits: 0
Expand Down
@@ -1,13 +1,14 @@
import { convertBaseToStandard } from 'data/components/exchange/services'
import { Props } from './template.success'
import { SBBalancesType, SBPairType, SBPaymentMethodType } from 'core/types'
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,
minOrMax: 'min' | 'max',
allValues: SBCheckoutFormValuesType,
sbBalances: SBBalancesType,
method?: SBPaymentMethodType
) => {
switch (minOrMax || 'max') {
Expand All @@ -17,7 +18,10 @@ export const getMaxMin = (
if (!method) return defaultMax
if (!pair) return defaultMax

const max = BigNumber.minimum(method.limits.max, pair.buyMax).toString()
let max = BigNumber.minimum(method.limits.max, pair.buyMax).toString()

if (method.type === 'FUNDS' && sbBalances)
max = sbBalances[method.currency].available

return convertBaseToStandard('FIAT', max)
case 'min':
Expand All @@ -39,11 +43,11 @@ export const maximumAmount = (
) => {
if (!value) return true

const { pair, method } = restProps
console.log(restProps)
const { pair, method, sbBalances } = restProps
if (!method) return true

return Number(value) > Number(getMaxMin(pair, 'max', allValues, method))
return Number(value) >
Number(getMaxMin(pair, 'max', allValues, sbBalances, method))
? 'ABOVE_MAX'
: false
}
Expand All @@ -55,10 +59,11 @@ export const minimumAmount = (
) => {
if (!value) return true

const { pair, method } = restProps
const { pair, method, sbBalances } = restProps
if (!method) return true

return Number(value) < Number(getMaxMin(pair, 'min', allValues, method))
return Number(value) <
Number(getMaxMin(pair, 'min', allValues, sbBalances, method))
? 'BELOW_MIN'
: false
}

0 comments on commit 62e5c3e

Please sign in to comment.