-
Notifications
You must be signed in to change notification settings - Fork 503
/
selectors.ts
293 lines (228 loc) · 10.7 KB
/
selectors.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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import BigNumber from 'bignumber.js'
import { getQuote } from 'blockchain-wallet-v4-frontend/src/modals/BuySell/EnterAmount/Checkout/validation'
import { head, isEmpty, isNil, lift } from 'ramda'
import { createSelector } from 'reselect'
import {
BSPaymentMethodType,
BSPaymentTypes,
BSQuoteType,
ExtractSuccess,
FiatType,
FiatTypeEnum
} from '@core/types'
import { model, selectors } from 'data'
import { RootState } from 'data/rootReducer'
import { convertBaseToStandard, convertStandardToBase } from '../exchange/services'
import { getInputFromPair, getOutputFromPair } from '../swap/model'
import { getRate } from '../swap/utils'
import { LIMIT } from './model'
import { BSCardStateEnum, BSCheckoutFormValuesType } from './types'
const { FORM_BS_CHECKOUT } = model.components.buySell
const hasEligibleFiatCurrency = (currency) =>
currency === FiatTypeEnum.USD || currency === FiatTypeEnum.GBP || currency === FiatTypeEnum.EUR
export const getAddBank = (state: RootState) => state.components.buySell.addBank
export const getAddCardError = (state: RootState) => state.components.buySell.addCardError
export const getApplePayInfo = (state: RootState) => state.components.buySell.applePayInfo
export const getGooglePayInfo = (state: RootState) => state.components.buySell.googlePayInfo
export const getOrderType = (state: RootState) => state.components.buySell.orderType
export const getEverypay3DSDetails = (state: RootState) => state.components.buySell.everypay3DS
export const getBSAccount = (state: RootState) => state.components.buySell.account
export const getCryptoCurrency = (state: RootState) => state.components.buySell.cryptoCurrency
export const getDisplayBack = (state: RootState) => state.components.buySell.displayBack
export const getFiatCurrency = (state: RootState) => state.components.buySell.fiatCurrency
export const getBSOrders = (state: RootState) => state.components.buySell.orders
export const getOrigin = (state: RootState) => state.components.buySell.origin
export const getBSPaymentMethods = (state: RootState) => state.components.buySell.methods
export const getBSBalances = (state: RootState) => state.components.buySell.balances
export const getBSCards = (state: RootState) => state.components.buySell.cards
export const getCrossBorderLimits = (state: RootState) => state.components.buySell.crossBorderLimits
export const getDefaultPaymentMethod = (state: RootState) => {
const fiatCurrency = getFiatCurrency(state)
const orders = getBSOrders(state).getOrElse([])
const sbMethodsR = getBSPaymentMethods(state)
const actionType = getOrderType(state)
const sbBalancesR = getBSBalances(state)
const bankAccounts = selectors.components.brokerage.getBankTransferAccounts(state).getOrElse([])
const transform = (
sbMethods: ExtractSuccess<typeof sbMethodsR>,
sbBalances: ExtractSuccess<typeof sbBalancesR>
): BSPaymentMethodType | undefined => {
const lastOrder = orders.find((order) => {
if (actionType === 'BUY') {
return order.inputCurrency in FiatTypeEnum
}
return order.outputCurrency in FiatTypeEnum
})
if (actionType === 'SELL') {
let fiatCurrencyToUse = fiatCurrency
if (!hasEligibleFiatCurrency(fiatCurrencyToUse)) {
const currenciesToUse = [FiatTypeEnum.USD, FiatTypeEnum.GBP, FiatTypeEnum.EUR]
const balancesToUse = Object.keys(sbBalances)
.filter((key) => currenciesToUse.indexOf(FiatTypeEnum[key]) >= 0)
.reduce((acc, key) => {
acc[key] = sbBalances[key]
return acc
}, {})
if (!isEmpty(balancesToUse)) {
fiatCurrencyToUse = Object.keys(balancesToUse).reduce((a, b) =>
balancesToUse[a].available > balancesToUse[b].available ? a : b
) as FiatType
}
}
return sbMethods.methods.find(
(method) => method.type === BSPaymentTypes.FUNDS && method.currency === fiatCurrencyToUse
)
}
if (!lastOrder) return undefined
const methodsOfType = sbMethods.methods.filter(
(method) => method.type === lastOrder.paymentType
)
const method = head(methodsOfType)
switch (lastOrder.paymentType) {
case BSPaymentTypes.USER_CARD:
case BSPaymentTypes.PAYMENT_CARD:
if (!method) return
const cards = getBSCards(state).getOrElse([])
let card = cards.find(
(value) =>
value.id === lastOrder.paymentMethodId && value.state === BSCardStateEnum.ACTIVE
)
if (!card) {
const randomActiveCard = cards.find((value) => value.state === BSCardStateEnum.ACTIVE)
card = randomActiveCard
if (!card) return undefined
}
return {
...method,
...card,
card: card?.card || undefined,
type: BSPaymentTypes.USER_CARD
}
case BSPaymentTypes.FUNDS:
return methodsOfType.find((method) => {
return (
method.currency === lastOrder.inputCurrency &&
method.currency === fiatCurrency &&
(sbBalances[method?.currency]?.available || 0) > 0
)
})
case BSPaymentTypes.LINK_BANK:
case BSPaymentTypes.BANK_TRANSFER:
if (!method) return
const bankAccount = bankAccounts.find((acct) => acct.id === lastOrder.paymentMethodId)
if (bankAccount && bankAccount.state === 'ACTIVE') {
return {
...method,
...bankAccount,
state: 'ACTIVE',
type: lastOrder.paymentType as BSPaymentTypes
}
}
return undefined
case BSPaymentTypes.BANK_ACCOUNT:
case undefined:
return undefined
default:
break
}
}
return lift(transform)(sbMethodsR, sbBalancesR)
}
export const hasFiatBalances = (state: RootState) => {
const fiatBalances = Object.keys(state.components.buySell.balances.data).filter(
(currency) =>
currency in FiatTypeEnum &&
(state.components.buySell.balances.data[currency]?.available || 0) > 0
)
return fiatBalances.length > 0
}
export const getBSCard = (state: RootState) => state.components.buySell.card
export const getBSCardId = (state: RootState) => state.components.buySell.cardId
export const getBSFiatEligible = (state: RootState) => state.components.buySell.fiatEligible
export const getBSQuote = (state: RootState) => state.components.buySell.quote
export const hasQuote = createSelector(getBSQuote, (quoteR) => {
const quote = quoteR.getOrElse({} as BSQuoteType)
return !isNil(quote.rate)
})
export const getBSPairs = (state: RootState) => state.components.buySell.pairs
export const getBSPair = (state: RootState) => state.components.buySell.pair
export const getBSPaymentMethod = (state: RootState) => state.components.buySell.method
export const getBSMobilePaymentMethod = (state: RootState) =>
state.components.buySell.mobilePaymentMethod
export const getBSProviderDetails = (state: RootState) => state.components.buySell.providerDetails
export const getBSOrder = (state: RootState) => state.components.buySell.order
export const getBSLatestPendingOrder = (state: RootState) =>
state.components.buySell.orders.getOrElse([]).find((order) => {
return order.state === 'PENDING_CONFIRMATION' || order.state === 'PENDING_DEPOSIT'
})
export const getCancelableOrder = createSelector(getBSOrders, (ordersR) => {
const orders = ordersR.getOrElse([])
return orders.find((order) => order.state === 'PENDING_CONFIRMATION')
})
export const getBuyQuote = (state: RootState) => state.components.buySell.buyQuote
export const getSellQuote = (state: RootState) => state.components.buySell.sellQuote
export const getSellOrder = (state: RootState) => state.components.buySell.sellOrder
export const getStep = (state: RootState) => state.components.buySell.step
export const getSwapAccount = (state: RootState) => state.components.buySell.swapAccount
export const getOriginalFiatCurrency = (state: RootState) =>
state.components.buySell.originalFiatCurrency
// Sell specific (for now!)
// used for sell only now, eventually buy as well
// TODO: use swap2 quote for buy AND sell
export const getPayment = (state: RootState) => state.components.buySell.payment
export const getIncomingAmount = (state: RootState) => {
const quoteR = getSellQuote(state)
const values = (selectors.form.getFormValues(FORM_BS_CHECKOUT)(
state
) as BSCheckoutFormValuesType) || { amount: '0', fix: 'CRYPTO' }
return lift(({ quote, rate }: ExtractSuccess<typeof quoteR>) => {
const fromCoin = getInputFromPair(quote.pair)
const toCoin = getOutputFromPair(quote.pair)
const amount =
values.fix === 'CRYPTO'
? values.amount
: getQuote(quote.pair, rate, values.fix, values.amount)
const amtMinor = convertStandardToBase(fromCoin, amount)
const exRate = new BigNumber(getRate(quote.quote.priceTiers, toCoin, new BigNumber(amtMinor)))
const feeMajor = convertBaseToStandard(toCoin, quote.networkFee)
const amt = exRate.times(amount).minus(feeMajor)
const isNegative = amt.isLessThanOrEqualTo(0)
return {
amt: isNegative ? 0 : amt,
isNegative
}
})(quoteR)
}
export const getSddEligible = (state: RootState) => state.components.buySell.sddEligible
export const isUserSddEligible = (state: RootState) => {
const sddEligibleR = getSddEligible(state)
return lift((sddEligible: ExtractSuccess<typeof sddEligibleR>) => !sddEligible.eligible)(
sddEligibleR
)
}
export const getUserSddEligibleTier = (state: RootState) => {
const sddEligibleR = getSddEligible(state)
return lift((sddEligible: ExtractSuccess<typeof sddEligibleR>) => sddEligible.tier)(sddEligibleR)
}
export const getUserLimit = (state: RootState, type: BSPaymentTypes) => {
const sbMethodsR = getBSPaymentMethods(state)
return lift((sbMethods: ExtractSuccess<typeof sbMethodsR>) => {
const paymentMethod = sbMethods.methods.find((method) => method.type === type)
return paymentMethod?.limits || LIMIT
})(sbMethodsR)
}
export const getSddVerified = (state: RootState) => state.components.buySell.sddVerified
export const isUserSddVerified = (state: RootState) => {
const sddVerifiedR = getSddVerified(state)
return lift(
(sddVerified: ExtractSuccess<typeof sddVerifiedR>) =>
sddVerified.taskComplete && sddVerified.verified
)(sddVerifiedR)
}
export const getLimits = (state: RootState) => state.components.buySell.limits
export const getSddTransactionFinished = (state: RootState) =>
state.components.buySell.sddTransactionFinished
export const getCheckoutAccountCodes = (state: RootState) =>
state.components.buySell.checkoutDotComAccountCodes
export const getCheckoutApiKey = (state: RootState) => state.components.buySell.checkoutDotComApiKey
export const getAccumulatedTrades = (state: RootState) => state.components.buySell.accumulatedTrades