Skip to content

Commit

Permalink
feat(interest): simplify/fix logic for deposit account switching betw…
Browse files Browse the repository at this point in the history
…een nc and custodial accounts
  • Loading branch information
schnogz committed Jul 22, 2021
1 parent 4cd0b15 commit 4561667
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ export const setInterestStep = (name: InterestStep, data?: InterestStepMetadata)
payload: { data, name },
type: AT.SET_INTEREST_STEP
})
export const setCoinDisplay = (isCoinDisplayed: boolean) => ({
payload: { isCoinDisplayed },
export const setCoinDisplay = (isAmountDisplayedInCrypto: boolean) => ({
payload: { isAmountDisplayedInCrypto },
type: AT.SET_COIN_DISPLAY
})
export const showInterestModal = (step: InterestStep, coin: CoinType) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const INITIAL_STATE: InterestState = {
interestEligible: Remote.NotAsked,
interestLimits: Remote.NotAsked,
interestRate: Remote.NotAsked,
isCoinDisplayed: false,
isAmountDisplayedInCrypto: false,
payment: Remote.NotAsked,
step: {
data: {},
Expand Down Expand Up @@ -237,7 +237,7 @@ const interestReducer = (state = INITIAL_STATE, action: InterestActionTypes): In
case AT.SET_COIN_DISPLAY: {
return {
...state,
isCoinDisplayed: payload.isCoinDisplayed
isAmountDisplayedInCrypto: payload.isAmountDisplayedInCrypto
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,29 +199,30 @@ export default ({ api, coreSagas, networks }: { api: APIType; coreSagas: any; ne
const formValues: InterestDepositFormType = yield select(
selectors.form.getFormValues(DEPOSIT_FORM)
)
const coin = S.getCoinType(yield select())
const ratesR = S.getRates(yield select())
const userCurrency = (yield select(selectors.core.settings.getCurrency)).getOrFail(
'Failed to get user currency'
)
const rates = ratesR.getOrElse({} as RatesType)
const coin = S.getCoinType(yield select())
const rates = S.getRates(yield select()).getOrElse({} as RatesType)
const rate = rates[userCurrency].last
const isDisplayed = S.getCoinDisplay(yield select())
const isCustodialAccountSelected =
prop('type', formValues.interestDepositAccount) === 'CUSTODIAL'
const accountBalance = prop('balance', formValues.interestDepositAccount)

switch (action.meta.field) {
case 'depositAmount':
const value = isDisplayed
if (isCustodialAccountSelected) {
return yield put(A.setPaymentSuccess())
}
const isAmountDisplayedInCrypto = S.getIsAmountDisplayedInCrypto(yield select())
const value = isAmountDisplayedInCrypto
? new BigNumber(action.payload).toNumber()
: new BigNumber(action.payload).dividedBy(rate).toNumber()
const paymentR = S.getPayment(yield select())
if (paymentR) {
let payment = yield getOrUpdateProvisionalPaymentForCoin(coin, paymentR)
const paymentAmount = generateProvisionalPaymentAmount(coin, value)
payment = yield payment.amount(paymentAmount || 0)
if (!isCustodialAccountSelected && accountBalance > 0) {
if (formValues.interestDepositAccount.balance > 0) {
payment = yield payment.build()
yield put(A.setPaymentSuccess(payment.value()))
} else {
Expand All @@ -230,30 +231,26 @@ export default ({ api, coreSagas, networks }: { api: APIType; coreSagas: any; ne
}
break
case 'interestDepositAccount':
let custodialBalances: SBBalancesType | undefined
let depositPayment: PaymentValue

yield put(A.setPaymentLoading())
yield put(actions.form.change(DEPOSIT_FORM, 'depositAmount', undefined))
// focus amount to ensure deposit amount validation will be triggered
yield put(actions.form.focus(DEPOSIT_FORM, 'depositAmount'))

// custodial account selected
if (isCustodialAccountSelected) {
custodialBalances = (yield select(
const custodialBalances: SBBalancesType = (yield select(
selectors.components.simpleBuy.getSBBalances
)).getOrFail('Failed to get balance')

depositPayment = yield call(createPayment, {
...formValues.interestDepositAccount
})
yield call(createLimits, undefined, custodialBalances)
yield put(A.setPaymentSuccess())
} else {
depositPayment = yield call(createPayment, {
// noncustodial account selected
const depositPayment: PaymentValue = yield call(createPayment, {
...formValues.interestDepositAccount,
address: getAccountIndexOrAccount(coin, formValues.interestDepositAccount)
})
yield call(createLimits, depositPayment)
yield put(A.setPaymentSuccess(depositPayment))
}

yield call(createLimits, depositPayment, custodialBalances)
yield put(A.setPaymentSuccess(depositPayment))
break
default:
// do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export const getInterestAccountBalance = (state: RootState) =>

export const getCoinType = (state: RootState) => state.components.interest.coin

export const getCoinDisplay = (state: RootState) => state.components.interest.isCoinDisplayed
export const getIsAmountDisplayedInCrypto = (state: RootState) =>
state.components.interest.isAmountDisplayedInCrypto

export const getInterestEligible = (state: RootState) => state.components.interest.interestEligible

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export interface InterestState {
interestEligible: RemoteDataType<string, InterestEligibleType>
interestLimits: RemoteDataType<string, InterestLimitsType>
interestRate: RemoteDataType<string, InterestRateType['rates']>
isCoinDisplayed: boolean
isAmountDisplayedInCrypto: boolean
// make this optional here. places where ts doesnt like it, check, custodial
payment?: RemoteDataType<string, PaymentValue | undefined>
step: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const getData = (state: RootState) => {
const interestLimitsR = selectors.components.interest.getInterestLimits(state)
const interestRateR = selectors.components.interest.getInterestRate(state)
const depositLimits = selectors.components.interest.getDepositLimits(state)
const displayCoin = selectors.components.interest.getCoinDisplay(state)
const displayCoin = selectors.components.interest.getIsAmountDisplayedInCrypto(state)
const ethRatesR = selectors.core.data.misc.getRatesSelector('ETH', state)
const paymentR = selectors.components.interest.getPayment(state)
const walletCurrencyR = selectors.core.settings.getCurrency(state) as RemoteDataType<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { selectors } from 'data'

const getData = (state) => {
const coin = selectors.components.interest.getCoinType(state)
const displayCoin = selectors.components.interest.getCoinDisplay(state)
const displayCoin = selectors.components.interest.getIsAmountDisplayedInCrypto(state)
const accountBalancesR = selectors.components.interest.getInterestAccountBalance(state)
const ratesR = selectors.components.interest.getRates(state)
const withdrawalMinimumsR = selectors.components.interest.getWithdrawalMinimums(state)
Expand Down

0 comments on commit 4561667

Please sign in to comment.