Skip to content

Commit

Permalink
fix(Coinify): buy quote refreshing
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Welber committed Jun 1, 2018
1 parent e7b8a13 commit d01804a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ export const OrderDetails = ({ quoteR, onRefreshQuote, type, medium }) => (
? <Text size='13px' weight={300}><FormattedMessage id='orderdetails.amounttopurchase' defaultMessage='BTC Amount to Purchase' /></Text>
: <Text size='13px' weight={300}><FormattedMessage id='orderdetails.amounttosell' defaultMessage='BTC Amount to Sell' /></Text>
}
<Text size='13px' weight={300}>{quoteR.map(q => reviewOrder.renderSummary(q, type, medium)).data.firstRow}</Text>
<Text size='13px' weight={300}>{quoteR.map(q => reviewOrder.renderFirstRow(q, type, medium)).getOrElse('~')}</Text>
</OrderDetailsRow>
<OrderDetailsRow>
<Text size='13px' weight={300}><FormattedMessage id='orderdetails.tradingfee' defaultMessage='Trading Fee' /></Text>
<Text size='13px' weight={300}>{quoteR.map(q => reviewOrder.renderSummary(q, type, medium)).data.fee}</Text>
<Text size='13px' weight={300}>{quoteR.map(q => reviewOrder.renderFeeRow(q, type, medium)).getOrElse('~')}</Text>
</OrderDetailsRow>
<OrderDetailsRow>
{
type === 'buy'
? <Text size='13px' weight={300}><FormattedMessage id='orderdetails.totalcost' defaultMessage='Total Cost' /></Text>
: <Text size='13px' weight={300}><FormattedMessage id='orderdetails.totaltobereceived' defaultMessage='Total to be Received' /></Text>
}
<Text size='13px' weight={300} color='success'>{quoteR.map(q => reviewOrder.renderSummary(q, type, medium)).data.total}</Text>
<Text size='13px' weight={300} color='success'>{quoteR.map(q => reviewOrder.renderTotalRow(q, type, medium)).getOrElse('~')}</Text>
</OrderDetailsRow>
</OrderDetailsTable>
{quoteR.map((q) => (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { gt, has, slice, toUpper, equals } from 'ramda'
import { gt, has, slice, toUpper, equals, path } from 'ramda'
import { FormattedMessage } from 'react-intl'

export const getLimits = (limits, curr, effectiveBalance) => {
Expand Down Expand Up @@ -66,47 +66,34 @@ export const mockedLimits = {
export const reviewOrder = {
baseBtc: (q) => q.baseCurrency === 'BTC',
hasMedium: (paymentMediums, medium) => {
if (medium) return medium
if (paymentMediums && has(medium, paymentMediums)) {
return medium
} else {
return medium === 'bank' ? 'card' : 'bank'
}
},
renderSummary: (q, type, medium) => {
renderFirstRow: (q, type, medium) => {
const qAmt = Math.abs(q.quoteAmount)
const bAmt = Math.abs(q.baseAmount)
if (reviewOrder.baseBtc(q)) return `${bAmt / 1e8} BTC (${currencySymbolMap[q.quoteCurrency]}${qAmt.toFixed(2)})`
else return `${qAmt / 1e8} BTC (${currencySymbolMap[q.baseCurrency]}${bAmt.toFixed(2)})`
},
renderFeeRow: (q, type, medium) => {
const med = medium
const fee = path(['paymentMediums', med], q) && Math.abs(q.paymentMediums[med]['fee'])
if (!fee) return `~`
if (reviewOrder.baseBtc(q)) return `${currencySymbolMap[q.quoteCurrency]}${fee && fee.toFixed(2)}`
else return `${currencySymbolMap[q.baseCurrency]}${fee && fee.toFixed(2)}`
},
renderTotalRow: (q, type, medium) => {
const qAmt = Math.abs(q.quoteAmount)
const med = reviewOrder.hasMedium(q.paymentMediums, medium)
const fee = Math.abs(q.paymentMediums[med]['fee'])
const totalBase = Math.abs((q.paymentMediums[med]['total']).toFixed(2))
if (type === 'buy') {
if (reviewOrder.baseBtc(q)) {
return {
firstRow: `${bAmt / 1e8} BTC (${currencySymbolMap[q.quoteCurrency]}${qAmt.toFixed(2)})`,
fee: `${currencySymbolMap[q.quoteCurrency]}${fee.toFixed(2)}`,
total: `${currencySymbolMap[q.quoteCurrency]}${(qAmt + fee).toFixed(2)}`
}
} else {
return {
firstRow: `${qAmt / 1e8} BTC (${currencySymbolMap[q.baseCurrency]}${bAmt.toFixed(2)})`,
fee: `${currencySymbolMap[q.baseCurrency]}${fee.toFixed(2)}`,
total: `${currencySymbolMap[q.baseCurrency]}${totalBase}`
}
}
} else { // type = sell
if (reviewOrder.baseBtc(q)) {
return {
firstRow: `${bAmt / 1e8} BTC (${currencySymbolMap[q.quoteCurrency]}${qAmt.toFixed(2)})`,
fee: `${currencySymbolMap[q.quoteCurrency]}${fee.toFixed(2)}`,
total: `${currencySymbolMap[q.quoteCurrency]}${(qAmt + fee).toFixed(2)}`
}
} else {
return {
firstRow: `${qAmt / 1e8} BTC (${currencySymbolMap[q.baseCurrency]}${bAmt.toFixed(2)})`,
fee: `${currencySymbolMap[q.baseCurrency]}${fee.toFixed(2)}`,
total: `${currencySymbolMap[q.baseCurrency]}${totalBase}`
}
}
}
const fee = path(['paymentMediums', med], q) && Math.abs(q.paymentMediums[med]['fee'])
const totalBase = path(['paymentMediums', med], q) && Math.abs((q.paymentMediums[med]['total']).toFixed(2))
if (!fee) return `~`
if (reviewOrder.baseBtc(q)) return `${currencySymbolMap[q.quoteCurrency]}${(qAmt + (fee || 0)).toFixed(2)}`
else return `${currencySymbolMap[q.baseCurrency]}${totalBase}`
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as AT from './actionTypes'

export const fetchQuote = (data) => ({ type: AT.COINIFY_FETCH_QUOTE, payload: data })
export const fetchQuote = (data) => ({ type: AT.COINIFY_FETCH_QUOTE, data })
export const fetchQuoteLoading = () => ({ type: AT.COINIFY_FETCH_QUOTE_LOADING })
export const fetchQuoteSuccess = (data) => ({ type: AT.COINIFY_FETCH_QUOTE_SUCCESS, payload: data })
export const fetchQuoteFailure = (error) => ({ type: AT.COINIFY_FETCH_QUOTE_FAILURE, payload: error })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ export default ({ api, options }) => {
yield takeLatest(AT.COINIFY_BUY, coinifySagas.buy)
yield takeLatest(AT.COINIFY_SELL, coinifySagas.sell)
yield takeLatest(AT.GET_KYCS, coinifySagas.getKYCs)
yield takeLatest(AT.COINIFY_REFRESH_BUY_QUOTE, coinifySagas.refreshBuyQuote)
}
}
31 changes: 23 additions & 8 deletions packages/blockchain-wallet-v4/src/redux/data/coinify/sagas.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import ExchangeDelegate from '../../../exchange/delegate'
import { apply, call, put, select, fork, take } from 'redux-saga/effects'
import { apply, call, put, select } from 'redux-saga/effects'
import * as A from './actions'
import * as S from './selectors'
import * as AT from './actionTypes'
import * as buySellSelectors from '../../kvStore/buySell/selectors'
import { coinifyService } from '../../../exchange/service'
import * as buySellA from '../../kvStore/buySell/actions'
import { prop, sort } from 'ramda'
import { prop, sort, path } from 'ramda'

export default ({ api, options }) => {
const getCoinify = function * () {
Expand Down Expand Up @@ -67,16 +66,30 @@ export default ({ api, options }) => {
const quote = yield apply(coinify.data, getQuote,
[Math.floor(amount), baseCurrency, quoteCurrency])
yield put(A.fetchQuoteSuccess(quote))
yield fork(waitForRefreshQuote, data.quote)
return quote
} catch (e) {
yield put(A.fetchQuoteFailure(e))
}
}

const waitForRefreshQuote = function * (quotePayload) {
yield take(AT.COINIFY_REFRESH_BUY_QUOTE)
yield put(A.fetchQuote(quotePayload))
const refreshBuyQuote = function * () {
try {
const quote = yield select(S.getQuote)
const qData = path(['data'], quote)

let quotePayload = {
baseCurrency: qData.baseCurrency,
quoteCurrency: qData.quoteCurrency,
type: 'buy'
}

if (qData.baseCurrency !== 'BTC') quotePayload['amount'] = (qData.baseAmount * -1) * 100
else quotePayload['amount'] = (qData.baseAmount * -1) / 1e8
const refreshedQuote = yield call(fetchQuote, {quote: quotePayload})
yield call(getPaymentMediums, {payload: refreshedQuote})
} catch (e) {

}
}

const fetchQuoteAndMediums = function * (data) {
Expand Down Expand Up @@ -128,6 +141,7 @@ export default ({ api, options }) => {
yield put(A.getPaymentMediumsLoading())
const mediums = yield apply(quote, quote.getPaymentMediums)
yield put(A.getPaymentMediumsSuccess(mediums))
return mediums
} catch (e) {
yield put(A.getPaymentMediumsFailure(e))
}
Expand Down Expand Up @@ -295,6 +309,7 @@ export default ({ api, options }) => {
cancelTrade,
triggerKYC,
getKYCs,
kycAsTrade
kycAsTrade,
refreshBuyQuote
}
}

0 comments on commit d01804a

Please sign in to comment.