Skip to content

Commit

Permalink
fix(Borrow): set limit type
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip London committed Jan 29, 2020
1 parent 7312a9e commit 5ff69cf
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export const INITIALIZE_BORROW = '@EVENT.INITIALIZE_BORROW'

export const MAX_COLLATERAL_CLICK = '@EVENT.MAX_COLLATERAL_CLICK'

export const SET_LIMITS = '@DATA.SET_LIMITS'

export const SET_OFFER = '@DATA.SET_OFFER'

export const SET_PAYMENT_FAILURE = '@EVENT.SET_PAYMENT_FAILURE'
export const SET_PAYMENT_LOADING = '@EVENT.SET_PAYMENT_LOADING'
export const SET_PAYMENT_SUCCESS = '@EVENT.SET_PAYMENT_SUCCESS'
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as AT from './actionTypes'
import { BorrowActionTypes } from './types'
import { BorrowActionTypes, BorrowMinMaxType, OfferType } from './types'
import { CoinType } from 'blockchain-wallet-v4/src/types'

export const createBorrow = () => ({
Expand Down Expand Up @@ -56,10 +56,25 @@ export const handleMaxCollateralClick = () => ({
type: AT.MAX_COLLATERAL_CLICK
})

export const initializeBorrow = (coin: CoinType) => ({
export const initializeBorrow = (coin: CoinType, offer: OfferType) => ({
type: AT.INITIALIZE_BORROW,
payload: {
coin
coin,
offer
}
})

export const setLimits = (limits: BorrowMinMaxType): BorrowActionTypes => ({
type: AT.SET_LIMITS,
payload: {
limits
}
})

export const setOffer = (offer: OfferType | null): BorrowActionTypes => ({
type: AT.SET_OFFER,
payload: {
offer
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import Remote from 'blockchain-wallet-v4/src/remote/remote'
const INITIAL_STATE: BorrowState = {
borrowHistory: Remote.NotAsked,
coin: 'BTC',
limits: {
maxFiat: 0,
minFiat: 0,
maxCrypto: 0,
minCrypto: 0
},
offer: null,
offers: Remote.NotAsked,
payment: Remote.NotAsked
}
Expand Down Expand Up @@ -49,6 +56,16 @@ export function borrowReducer (
...state,
coin: action.payload.coin
}
case AT.SET_LIMITS:
return {
...state,
limits: action.payload.limits
}
case AT.SET_OFFER:
return {
...state,
offer: action.payload.offer
}
case AT.SET_PAYMENT_FAILURE:
return {
...state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { FormAction, initialize } from 'redux-form'
import { NO_OFFER_EXISTS } from './model'
import { nth } from 'ramda'
import { promptForSecondPassword } from 'services/SagaService'
import BigNumber from 'bignumber.js'

export default ({
api,
Expand All @@ -34,7 +35,9 @@ export default ({

const offersR = S.getOffers(yield select())
const offers = offersR.getOrFail(NO_OFFER_EXISTS)
const offer = offers[0]
const coin = S.getCoinType(yield select())
const offer = offers.find(offer => offer.terms.collateralCcy === coin)
if (!offer) throw new Error(NO_OFFER_EXISTS)

const loan = yield call(api.createLoan, offer.id, {
symbol: offer.terms.principalCcy,
Expand Down Expand Up @@ -68,20 +71,48 @@ export default ({
}
}

const _createMaxCounter = function * (payment: PaymentType) {
const coin = S.getCoinType(yield select())
const ratesR = yield select(S.getRates)
const rates = ratesR.getOrElse({})
const balance = payment.value().effectiveBalance
const _createLimits = function * (payment: PaymentType) {
try {
const coin = S.getCoinType(yield select())
const offer = S.getOffer(yield select())
const ratesR = yield select(S.getRates)
const rates = ratesR.getOrElse({})
const balance = payment.value().effectiveBalance

switch (coin) {
case 'BTC':
return Exchange.convertBtcToFiat({
value: balance,
fromUnit: 'SAT',
toCurrency: 'USD',
rates: rates
}).value
if (!offer) throw new Error(NO_OFFER_EXISTS)

let adjustedBalance = new BigNumber(balance)
.dividedBy(offer.terms.collateralRatio)
.toNumber()

let maxFiat
let maxCrypto
switch (coin) {
case 'BTC':
maxFiat = Exchange.convertBtcToFiat({
value: adjustedBalance,
fromUnit: 'SAT',
toCurrency: 'USD',
rates: rates
}).value
maxCrypto = Exchange.convertBtcToBtc({
value: adjustedBalance,
fromUnit: 'SAT',
toUnit: 'SAT',
rates: rates
}).value
}

yield put(
A.setLimits({
maxFiat: Number(maxFiat),
maxCrypto: Number(maxCrypto),
minFiat: 0,
minCrypto: 0
})
)
} catch (e) {
yield put(A.setPaymentFailure(e))
}
}

Expand Down Expand Up @@ -136,7 +167,7 @@ export default ({
case 'BTC':
payment = yield call(_createPayment, action.payload.index)
}
const maxCollateralCounter = yield call(_createMaxCounter, payment)
const maxCollateralCounter = yield call(_createLimits, payment)

yield put(
actions.form.change(
Expand All @@ -155,6 +186,7 @@ export default ({
let defaultAccountR
let payment: PaymentType = <PaymentType>{}
yield put(A.setPaymentLoading())
yield put(A.setOffer(payload.offer))

try {
switch (payload.coin) {
Expand All @@ -170,14 +202,11 @@ export default ({
break
}

const maxCollateralCounter = yield call(_createMaxCounter, payment)
const offersR = S.getOffers(yield select())
const offers = offersR.getOrFail(NO_OFFER_EXISTS)
const maxCollateralCounter = yield call(_createLimits, payment)

const initialValues = {
collateral: defaultAccountR.getOrElse(),
maxCollateralCounter,
offer: offers[0]
maxCollateralCounter
}

yield put(initialize('borrowForm', initialValues))
Expand All @@ -188,17 +217,9 @@ export default ({
}

const maxCollateralClick = function * () {
const values: BorrowFormValuesType = yield select(
selectors.form.getFormValues('borrowForm')
)

yield put(
actions.form.change(
'borrowForm',
'principal',
values.maxCollateralCounter
)
)
const limits = S.getLimits(yield select())

yield put(actions.form.change('borrowForm', 'principal', limits.maxFiat))
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ export const getCoinType = (state: RootState) => state.components.borrow.coin

export const getPayment = (state: RootState) => state.components.borrow.payment

export const getLimits = (state: RootState) => state.components.borrow.limits

export const getOffers = (state: RootState) => state.components.borrow.offers

export const getOffer = (state: RootState) => state.components.borrow.offer

export const getBorrowHistory = (state: RootState) =>
state.components.borrow.borrowHistory

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export type BorrowFormValuesType = {
principal: string
}

export type BorrowMinMaxType = {
maxCrypto: number
maxFiat: number
minCrypto: number
minFiat: number
}

// TODO: move to ticker
export type RatesType = {
[key in string]: {
Expand Down Expand Up @@ -95,6 +102,8 @@ export type PaymentType = {
export interface BorrowState {
borrowHistory: RemoteDataType<NabuApiErrorType, any>
coin: CoinType
limits: BorrowMinMaxType
offer: OfferType | null
offers: RemoteDataType<NabuApiErrorType, Array<OfferType>>
payment: RemoteDataType<string | Error, PaymentType>
}
Expand Down Expand Up @@ -135,22 +144,37 @@ interface FetchUserBorrowHistorySuccessAction {
interface InitializeBorrowAction {
payload: {
coin: CoinType
offer: OfferType
}
type: typeof AT.INITIALIZE_BORROW
}

interface SetPaymentFailure {
interface SetLimitsAction {
payload: {
limits: BorrowMinMaxType
}
type: typeof AT.SET_LIMITS
}

interface SetOfferAction {
payload: {
offer: OfferType | null
}
type: typeof AT.SET_OFFER
}

interface SetPaymentFailureAction {
payload: {
error: string | Error
}
type: typeof AT.SET_PAYMENT_FAILURE
}

interface SetPaymentLoading {
interface SetPaymentLoadingAction {
type: typeof AT.SET_PAYMENT_LOADING
}

interface SetPaymentSuccess {
interface SetPaymentSuccessAction {
payload: {
payment: PaymentType
}
Expand All @@ -165,6 +189,8 @@ export type BorrowActionTypes =
| FetchUserBorrowHistoryLoadingAction
| FetchUserBorrowHistorySuccessAction
| InitializeBorrowAction
| SetPaymentFailure
| SetPaymentLoading
| SetPaymentSuccess
| SetLimitsAction
| SetOfferAction
| SetPaymentFailureAction
| SetPaymentLoadingAction
| SetPaymentSuccessAction
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { actions } from 'data'
import { bindActionCreators, compose, Dispatch } from 'redux'
import { BorrowMinMaxType, OfferType, PaymentType, RatesType } from 'data/types'
import { CoinType, RemoteDataType, SupportedCoinsType } from 'core/types'
import { connect } from 'react-redux'
import { getData } from './selectors'
import { OfferType, PaymentType, RatesType } from 'data/types'
import DataError from 'components/DataError'
import Loading from './template.loading'
import React, { Component } from 'react'
import Success from './template.success'

export type OwnProps = {
offer: OfferType
}

export type LinkDispatchPropsType = {
borrowActions: typeof actions.components.borrow
}

export type SuccessStateType = {
coin: CoinType
offers: Array<OfferType>
limits: BorrowMinMaxType
payment: PaymentType
rates: RatesType
supportedCoins: SupportedCoinsType
Expand All @@ -25,17 +29,21 @@ type LinkStatePropsType = {
data: RemoteDataType<string | Error, SuccessStateType>
}

type Props = LinkDispatchPropsType & LinkStatePropsType
type Props = OwnProps & LinkDispatchPropsType & LinkStatePropsType

export class BorrowForm extends Component<Props> {
state = {}

componentDidMount () {
this.props.borrowActions.initializeBorrow('BTC')
this.props.borrowActions.initializeBorrow('BTC', this.props.offer)
}

componentWillUnmount () {
this.props.borrowActions.setOffer(null)
}

handleRefresh = () => {
this.props.borrowActions.initializeBorrow('BTC')
this.props.borrowActions.initializeBorrow('BTC', this.props.offer)
}

handleSubmit = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { selectors } from 'data'

export const getData = state => {
const coin = selectors.components.borrow.getCoinType(state)
const offersR = selectors.components.borrow.getOffers(state)
const limits = selectors.components.borrow.getLimits(state)
const paymentR = selectors.components.borrow.getPayment(state)
const ratesR = selectors.components.borrow.getRates(state)
const supportedCoinsR = selectors.core.walletOptions.getSupportedCoins(state)

return lift((offers, payment, rates, supportedCoins) => ({
return lift((payment, rates, supportedCoins) => ({
coin,
offers,
limits,
payment,
rates,
supportedCoins
}))(offersR, paymentR, ratesR, supportedCoinsR)
}))(paymentR, ratesR, supportedCoinsR)
}

0 comments on commit 5ff69cf

Please sign in to comment.