Skip to content

Commit

Permalink
feat(buy): implemented logic to get in consideration proper limits ba…
Browse files Browse the repository at this point in the history
…sed on product, this affect coins like DOT
  • Loading branch information
milan-bc committed Apr 16, 2021
1 parent 9231750 commit d1bc822
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@ import { CoinType } from 'blockchain-wallet-v4/src/types'

export const convertBaseToStandard = (
coin: CoinType | 'FIAT',
value
value,
baseToStandard: boolean = true
): string => {
return Exchange.convertCoinToCoin({ coin, value, baseToStandard: true }).value
return Exchange.convertCoinToCoin({
coin,
value,
baseToStandard: baseToStandard
}).value
}

export const convertStandardToBase = (
coin: CoinType | 'FIAT',
value
value,
baseToStandard: boolean = false
): string => {
return Exchange.convertCoinToCoin({ coin, value, baseToStandard: false })
.value
return Exchange.convertCoinToCoin({
coin,
value,
baseToStandard: baseToStandard
}).value
}
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,15 @@ export const updatePaymentFailure = (error: string): SimpleBuyActionTypes => ({
}
})

export const fetchLimits = (currency: FiatType) => ({
export const fetchLimits = (
currency: FiatType,
cryptoCurrency?: CoinType,
side?: SBOrderActionType
) => ({
type: AT.FETCH_LIMITS,
currency
currency,
cryptoCurrency,
side
})

export const fetchLimitsFailure = (error: string): SimpleBuyActionTypes => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
CoinTypeEnum,
Everypay3DSResponseType,
FiatEligibleType,
OrderType,
ProductTypes,
SBAccountType,
SBCardStateType,
SBCardType,
Expand Down Expand Up @@ -229,7 +231,7 @@ export default ({
A.setStep({
step: 'ENTER_AMOUNT',
cryptoCurrency,
orderType: order.side || 'BUY',
orderType: order.side || OrderType.BUY,
fiatCurrency,
pair,
method
Expand Down Expand Up @@ -267,7 +269,7 @@ export default ({

// since two screens use this order creation saga and they have different
// forms, detect the order type and set correct form to submitting
if (orderType === 'SELL') {
if (orderType === OrderType.SELL) {
yield put(actions.form.startSubmit('previewSell'))
} else {
yield put(actions.form.startSubmit('simpleBuyCheckout'))
Expand All @@ -279,14 +281,14 @@ export default ({
fix === 'FIAT'
? convertStandardToBase('FIAT', values.amount)
: convertStandardToBase(coin, values.amount)
const inputCurrency = orderType === 'BUY' ? fiat : coin
const outputCurrency = orderType === 'BUY' ? coin : fiat
const inputCurrency = orderType === OrderType.BUY ? fiat : coin
const outputCurrency = orderType === OrderType.BUY ? coin : fiat
const input = { amount, symbol: inputCurrency }
const output = { amount, symbol: outputCurrency }

// used for sell only now, eventually buy as well
// TODO: use swap2 quote for buy AND sell
if (orderType === 'SELL') {
if (orderType === OrderType.SELL) {
const from = S.getSwapAccount(yield select())
const quote = S.getSellQuote(yield select()).getOrFail(NO_QUOTE)
if (!from) throw new Error(NO_ACCOUNT)
Expand Down Expand Up @@ -348,11 +350,11 @@ export default ({

if (!paymentType) throw new Error(NO_PAYMENT_TYPE)

if (orderType === 'BUY' && fix === 'CRYPTO') {
if (orderType === OrderType.BUY && fix === 'CRYPTO') {
// @ts-ignore
delete input.amount
}
if (orderType === 'BUY' && fix === 'FIAT') {
if (orderType === OrderType.BUY && fix === 'FIAT') {
// @ts-ignore
delete output.amount
}
Expand Down Expand Up @@ -415,7 +417,7 @@ export default ({
}

const error = errorHandler(e)
if (values?.orderType === 'SELL') {
if (values?.orderType === OrderType.SELL) {
yield put(actions.form.stopSubmit('previewSell', { _error: error }))
}
yield put(actions.form.stopSubmit('simpleBuyCheckout', { _error: error }))
Expand Down Expand Up @@ -1135,7 +1137,7 @@ export default ({
const pair = S.getSBPair(yield select())
if (!pair) throw new Error(NO_PAIR_SELECTED)
// Fetch rates
if (orderType === 'BUY') {
if (orderType === OrderType.BUY) {
yield put(A.fetchSBQuote(pair.pair, orderType, '0'))
// used for sell only now, eventually buy as well
// TODO: use swap2 quote for buy AND sell
Expand Down Expand Up @@ -1332,7 +1334,7 @@ export default ({
})
)
} else if (cryptoCurrency) {
orderType === 'BUY' &&
orderType === OrderType.BUY &&
(yield put(
// 🚨 SPECIAL TS-IGNORE
// Usually ENTER_AMOUNT should require a pair but
Expand All @@ -1348,7 +1350,7 @@ export default ({
orderType
})
))
orderType === 'SELL' &&
orderType === OrderType.SELL &&
(yield put(
A.setStep({
step: 'CRYPTO_SELECTION',
Expand Down Expand Up @@ -1377,14 +1379,24 @@ export default ({
}

const fetchLimits = function * ({
currency
}: ReturnType<typeof A.fetchSBFiatEligible>) {
cryptoCurrency,
currency,
side
}: ReturnType<typeof A.fetchLimits>) {
try {
yield put(A.fetchLimitsLoading())
const limits: ReturnType<typeof api.getSwapLimits> = yield call(
api.getSwapLimits,
currency
)
let limits
if (cryptoCurrency && side) {
limits = yield call(
api.getSBLimits,
currency,
ProductTypes.SIMPLEBUY,
cryptoCurrency,
side
)
} else {
limits = yield call(api.getSwapLimits, currency)
}
yield put(A.fetchLimitsSuccess(limits))
} catch (e) {
const error = errorHandler(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
TabMenuItem,
Text
} from 'blockchain-info-components'
import { SBPairType } from 'blockchain-wallet-v4/src/types'
import { OrderType, SBPairType } from 'blockchain-wallet-v4/src/types'
import { FlyoutWrapper } from 'components/Flyout'
import { CoinAccountListOption } from 'components/Form'
import { model } from 'data'
Expand Down Expand Up @@ -227,9 +227,9 @@ const CryptoSelector: React.FC<InjectedFormProps<{}, Props> &
</TabMenuItem>
<TabMenuItem
role='button'
selected={orderType === 'SELL'}
selected={orderType === OrderType.SELL}
onClick={() => {
setOrderType('SELL')
setOrderType(OrderType.SELL)
props.analyticsActions.logEvent('SB_SELL_BUTTON')
}}
data-e2e='sbSellButton'
Expand All @@ -244,7 +244,7 @@ const CryptoSelector: React.FC<InjectedFormProps<{}, Props> &
)}
</FlyoutWrapper>
<Currencies>
{orderType === 'SELL' ? (
{orderType === OrderType.SELL ? (
checkAccountsBalances ? (
SUPPORTED_COINS.map(coin => {
const accounts = props.accounts[coin] as Array<SwapAccountType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { find, isEmpty, pathOr, propEq, propOr } from 'ramda'
import { bindActionCreators } from 'redux'

import { Remote } from 'blockchain-wallet-v4/src'
import { OrderType } from 'blockchain-wallet-v4/src/types'
import { actions, selectors } from 'data'
import { getValidPaymentMethod } from 'data/components/simpleBuy/model'
import { RootState } from 'data/rootReducer'
Expand Down Expand Up @@ -39,8 +40,13 @@ class Checkout extends PureComponent<Props> {
this.props.simpleBuyActions.fetchSDDEligible()
this.props.simpleBuyActions.fetchSBCards()
this.props.brokerageActions.fetchBankTransferAccounts()
this.props.simpleBuyActions.fetchLimits(this.props.fiatCurrency || 'USD')
}
// we need to re-fetch limits for SB
this.props.simpleBuyActions.fetchLimits(
this.props.fiatCurrency,
this.props.cryptoCurrency,
this.props.orderType || OrderType.BUY
)
}

handleSubmit = () => {
Expand All @@ -64,7 +70,7 @@ class Checkout extends PureComponent<Props> {

// TODO: sell
// need to do kyc check
if (formValues?.orderType === 'SELL') {
if (formValues?.orderType === OrderType.SELL) {
return this.props.simpleBuyActions.setStep({
step: 'PREVIEW_SELL',
sellOrderType: this.props.swapAccount?.type
Expand All @@ -86,7 +92,7 @@ class Checkout extends PureComponent<Props> {
)
}
} else if (!method) {
const fiatCurrency = this.props.fiatCurrency || 'USD'
const fiatCurrency = this.props.fiatCurrency
const nextStep = hasPaymentAccount
? 'LINKED_PAYMENT_ACCOUNTS'
: 'PAYMENT_METHODS'
Expand Down Expand Up @@ -146,7 +152,7 @@ const mapStateToProps = (state: RootState, ownProps: OwnProps) => ({
data: getData(state, ownProps),
cryptoCurrency:
selectors.components.simpleBuy.getCryptoCurrency(state) || 'BTC',
fiatCurrency: selectors.components.simpleBuy.getFiatCurrency(state),
fiatCurrency: selectors.components.simpleBuy.getFiatCurrency(state) || 'USD',
formValues: selectors.form.getFormValues('simpleBuyCheckout')(state) as
| SBCheckoutFormValuesType
| undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
coinToString,
fiatToString
} from 'blockchain-wallet-v4/src/exchange/currency'
import { CoinType, SBPaymentMethodType } from 'blockchain-wallet-v4/src/types'
import {
CoinType,
OrderType,
SBPaymentMethodType
} from 'blockchain-wallet-v4/src/types'
import { BlueCartridge, ErrorCartridge } from 'components/Cartridge'
import { AmountTextBox } from 'components/Exchange'
import { FlyoutWrapper } from 'components/Flyout'
Expand Down Expand Up @@ -239,7 +243,8 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = props => {
method,
props.swapAccount,
props.isSddFlow,
sddLimit
sddLimit,
props.limits
)[fix]
const min: string = getMaxMin(
'min',
Expand All @@ -252,7 +257,8 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = props => {
method,
props.swapAccount,
props.isSddFlow,
sddLimit
sddLimit,
props.limits
)[fix]

const handleMinMaxClick = () => {
Expand Down Expand Up @@ -289,7 +295,8 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = props => {
method,
props.swapAccount,
props.isSddFlow,
sddLimit
sddLimit,
props.limits
)[fix]
const value = convertStandardToBase(conversionCoinType, maxMin)
props.simpleBuyActions.handleSBSuggestedAmountClick(
Expand All @@ -315,7 +322,7 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = props => {
const isSufficientEthForErc20 =
props.payment &&
props.swapAccount?.type === 'ACCOUNT' &&
props.orderType === 'SELL' &&
props.orderType === OrderType.SELL &&
isErc20 &&
// @ts-ignore
!props.payment.isSufficientEthForErc20
Expand Down Expand Up @@ -446,7 +453,7 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = props => {
</QuoteRow>
)}
</QuoteActionContainer>
{(!props.isSddFlow || props.orderType === 'SELL') &&
{(!props.isSddFlow || props.orderType === OrderType.SELL) &&
props.pair &&
Number(min) <= Number(max) && (
<Amounts onClick={handleMinMaxClick}>
Expand All @@ -461,7 +468,8 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = props => {
defaultMessage='{value} Minimum {orderType}'
values={{
value: getValue(min),
orderType: props.orderType === 'BUY' ? 'Buy' : 'Sell'
orderType:
props.orderType === OrderType.BUY ? 'Buy' : 'Sell'
}}
/>
</CustomErrorCartridge>
Expand All @@ -471,7 +479,7 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = props => {
id='modals.simplebuy.checkout.maxbuysell'
defaultMessage='{orderType} Max'
values={{
orderType: orderType === 'BUY' ? 'Buy' : 'Sell'
orderType: orderType === OrderType.BUY ? 'Buy' : 'Sell'
}}
/>
</BlueRedCartridge>
Expand All @@ -480,7 +488,7 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = props => {
</Amounts>
)}

{(!props.isSddFlow || props.orderType === 'SELL') &&
{(!props.isSddFlow || props.orderType === OrderType.SELL) &&
props.pair &&
Number(min) > Number(max) && (
<Amounts>
Expand All @@ -496,7 +504,7 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = props => {
</Amounts>
)}

{props.isSddFlow && props.orderType === 'BUY' && (
{props.isSddFlow && props.orderType === OrderType.BUY && (
<ActionsRow>
<ActionsItem>
<Text weight={500} size='14px' color='grey600'>
Expand Down Expand Up @@ -529,7 +537,7 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = props => {
<Payment
{...props}
method={method}
isSddFlow={props.isSddFlow && props.orderType === 'BUY'}
isSddFlow={props.isSddFlow && props.orderType === OrderType.BUY}
/>

{props.error && (
Expand Down Expand Up @@ -577,7 +585,7 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = props => {
</Amounts>
)}
</FlyoutWrapper>
{props.isSddFlow && props.orderType === 'BUY' && (
{props.isSddFlow && props.orderType === OrderType.BUY && (
<IncreaseLimits {...props} />
)}
{isSufficientEthForErc20 && (
Expand Down

0 comments on commit d1bc822

Please sign in to comment.