Skip to content

Commit

Permalink
feat(interest): simplify/fix logic for deposit send from custodial on…
Browse files Browse the repository at this point in the history
…ly coins
  • Loading branch information
schnogz committed Jul 22, 2021
1 parent 4561667 commit 6834094
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { concat, isEmpty, isNil, last, prop } from 'ramda'
import { FormAction, initialize } from 'redux-form'
import { call, delay, put, select, take } from 'redux-saga/effects'

import { Remote } from 'blockchain-wallet-v4/src'
import { Exchange, Remote } from 'blockchain-wallet-v4/src'
import { APIType } from 'blockchain-wallet-v4/src/network/api'
import {
AccountTypes,
Expand Down Expand Up @@ -390,31 +390,40 @@ export default ({ api, coreSagas, networks }: { api: APIType; coreSagas: any; ne
const formValues: InterestDepositFormType = yield select(
selectors.form.getFormValues(DEPOSIT_FORM)
)
const isCustodialDeposit = prop('type', formValues.interestDepositAccount) === 'CUSTODIAL'
const isCustodialDeposit = formValues.interestDepositAccount.type === 'CUSTODIAL'
const coin = S.getCoinType(yield select())
const paymentR = S.getPayment(yield select())
const payment = yield getOrUpdateProvisionalPaymentForCoin(
coin,
paymentR as RemoteDataType<string, any>
)

// custodial account deposit
if (isCustodialDeposit) {
const { amount } = payment.value()
if (amount === null || amount === undefined) {
throw Error('Deposit amount unknown')
}
// BTC/BCH amounts from payments are returned as objects
const amountString = typeof amount === 'object' ? amount[0].toString() : amount.toString()
const { depositAmount } = formValues
const isAmountDisplayedInCrypto = S.getIsAmountDisplayedInCrypto(yield select())
const userCurrency = (yield select(selectors.core.settings.getCurrency)).getOrFail(
'Failed to get user currency'
)
const rates = S.getRates(yield select()).getOrElse({} as RatesType)
const rate = rates[userCurrency].last
const baseCrypto = Exchange.convertCoinToCoin({
baseToStandard: false,
coin,
value: isAmountDisplayedInCrypto
? new BigNumber(depositAmount).toNumber()
: new BigNumber(depositAmount).dividedBy(rate).toNumber()
})

// custodial deposit
yield call(api.initiateCustodialTransfer, {
amount: amountString as string,
amount: new BigNumber(baseCrypto).integerValue(BigNumber.ROUND_DOWN).toString(),
currency: coin,
destination: 'SAVINGS',
origin: 'SIMPLEBUY'
})
} else {
// non-custodial deposit
// non-custodial account deposit
// get payment
const paymentR = S.getPayment(yield select())
const payment = yield getOrUpdateProvisionalPaymentForCoin(
coin,
paymentR as RemoteDataType<string, any>
)
// fetch deposit address
yield put(A.fetchInterestAccount(coin))
yield take([
Expand Down
6 changes: 2 additions & 4 deletions packages/blockchain-wallet-v4/src/exchange/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { BigNumber } from 'bignumber.js'
import { path, prop } from 'ramda'

import { CoinType, FiatType, RatesType, WalletCurrencyType, WalletFiatType } from 'core/types'
import { CoinType, FiatType, RatesType, WalletFiatType } from 'core/types'

import Currencies, { FiatCurrenciesType } from './currencies'
import { formatCoin, getLang } from './utils'
Expand Down Expand Up @@ -66,9 +66,7 @@ const convertCoinToFiat = ({
? new BigNumber(value)
: new BigNumber(value).dividedBy(Math.pow(10, coinfig.precision))

const fiatAmt = amt.times(last).toFixed(2)

return fiatAmt
return amt.times(last).toFixed(2)
}

const convertFiatToCoin = ({
Expand Down

0 comments on commit 6834094

Please sign in to comment.