Skip to content

Commit

Permalink
feat(borrow): set up getLoanFinancials
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip London committed Feb 26, 2020
1 parent 49a8060 commit bcc7b24
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 6 deletions.
Expand Up @@ -12,6 +12,14 @@ export const FETCH_BORROW_OFFERS_FAILURE = '@EVENT.FETCH_BORROW_OFFERS_FAILURE'
export const FETCH_BORROW_OFFERS_LOADING = '@EVENT.FETCH_BORROW_OFFERS_LOADING'
export const FETCH_BORROW_OFFERS_SUCCESS = '@EVENT.FETCH_BORROW_OFFERS_SUCCESS'

export const FETCH_LOAN_FINANCIALS = '@EVENT.FETCH_LOAN_FINANCIALS'
export const FETCH_LOAN_FINANCIALS_FAILURE =
'@EVENT.FETCH_LOAN_FINANCIALS_FAILURE'
export const FETCH_LOAN_FINANCIALS_LOADING =
'@EVENT.FETCH_LOAN_FINANCIALS_LOADING'
export const FETCH_LOAN_FINANCIALS_SUCCESS =
'@EVENT.FETCH_LOAN_FINANCIALS_SUCCESS'

export const FETCH_USER_BORROW_HISTORY = '@EVENT.FETCH_USER_BORROW_HISTORY'
export const FETCH_USER_BORROW_HISTORY_FAILURE =
'@EVENT.FETCH_USER_BORROW_HISTORY_FAILURE'
Expand Down
@@ -1,6 +1,11 @@
import * as AT from './actionTypes'
import { BorrowActionTypes, BorrowMinMaxType } from './types'
import { CoinType, LoanType, OfferType } from 'blockchain-wallet-v4/src/types'
import {
CoinType,
LoanFinancialsType,
LoanType,
OfferType
} from 'blockchain-wallet-v4/src/types'

export const addCollateral = () => ({
type: AT.ADD_COLLATERAL
Expand Down Expand Up @@ -36,6 +41,33 @@ export const fetchBorrowOffersSuccess = (offers): BorrowActionTypes => ({
}
})

export const fetchLoanFinancials = (loan: LoanType) => ({
type: AT.FETCH_LOAN_FINANCIALS,
payload: {
loan
}
})

export const fetchLoanFinancialsLoading = (): BorrowActionTypes => ({
type: AT.FETCH_LOAN_FINANCIALS_LOADING
})

export const fetchLoanFinancialsFailure = (error): BorrowActionTypes => ({
type: AT.FETCH_LOAN_FINANCIALS_FAILURE,
payload: {
error
}
})

export const fetchLoanFinancialsSuccess = (
financials: LoanFinancialsType
): BorrowActionTypes => ({
type: AT.FETCH_LOAN_FINANCIALS_SUCCESS,
payload: {
financials
}
})

export const fetchUserBorrowHistory = () => ({
type: AT.FETCH_USER_BORROW_HISTORY
})
Expand Down
@@ -1,4 +1,5 @@
import * as AT from './actionTypes'
import { actions } from 'data'
import { BorrowActionTypes, BorrowState } from './types'
import Remote from 'blockchain-wallet-v4/src/remote/remote'

Expand Down Expand Up @@ -38,6 +39,42 @@ export function borrowReducer (
...state,
offers: Remote.Success(action.payload.offers)
}
case AT.FETCH_LOAN_FINANCIALS_LOADING:
return state.loan
? {
...state,
loan: {
...state.loan,
financials: Remote.Loading
}
}
: {
...state
}
case AT.FETCH_LOAN_FINANCIALS_FAILURE:
return state.loan
? {
...state,
loan: {
...state.loan,
financials: Remote.Failure(action.payload.error)
}
}
: {
...state
}
case AT.FETCH_LOAN_FINANCIALS_SUCCESS:
return state.loan
? {
...state,
loan: {
...state.loan,
financials: Remote.Success(action.payload.financials)
}
}
: {
...state
}
case AT.FETCH_USER_BORROW_HISTORY_LOADING:
return {
...state,
Expand Down
Expand Up @@ -15,6 +15,7 @@ export default ({ api, coreSagas, networks }) => {
yield takeLatest(AT.CREATE_BORROW, borrowSagas.createBorrow)
yield takeLatest(AT.DESTROY_BORROW, borrowSagas.destroyBorrow)
yield takeLatest(AT.FETCH_BORROW_OFFERS, borrowSagas.fetchBorrowOffers)
yield takeLatest(AT.FETCH_LOAN_FINANCIALS, borrowSagas.fetchLoanFinancials)
yield takeLatest(
AT.FETCH_USER_BORROW_HISTORY,
borrowSagas.fetchUserBorrowHistory
Expand Down
Expand Up @@ -15,7 +15,7 @@ import {
} from './model'
import { FormAction, initialize, touch } from 'redux-form'
import { head, nth } from 'ramda'
import { LoanType } from 'core/types'
import { LoanFinancialsType, LoanType } from 'core/types'

import { promptForSecondPassword } from 'services/SagaService'
import BigNumber from 'bignumber.js'
Expand Down Expand Up @@ -254,6 +254,22 @@ export default ({
}
}

const fetchLoanFinancials = function * ({
payload
}: ReturnType<typeof A.fetchLoanFinancials>) {
try {
const { loan } = payload
yield put(A.fetchLoanFinancialsLoading())
const financials: LoanFinancialsType = yield call(
api.getLoanFinancials,
loan.loanId
)
yield put(A.fetchLoanFinancialsSuccess(financials))
} catch (e) {
yield put(A.fetchBorrowOffersFailure(e))
}
}

const fetchUserBorrowHistory = function * () {
try {
yield put(A.fetchUserBorrowHistoryLoading())
Expand Down Expand Up @@ -432,6 +448,7 @@ export default ({
createBorrow,
destroyBorrow,
fetchBorrowOffers,
fetchLoanFinancials,
fetchUserBorrowHistory,
formChanged,
initializeBorrow,
Expand Down
@@ -1,6 +1,7 @@
import * as AT from './actionTypes'
import {
CoinType,
LoanFinancialsType,
LoanType,
NabuApiErrorType,
OfferType,
Expand Down Expand Up @@ -125,6 +126,22 @@ interface FetchBorrowOffersSuccessAction {
}
type: typeof AT.FETCH_BORROW_OFFERS_SUCCESS
}
interface FetchLoanFinancialsFailureAction {
payload: {
error: NabuApiErrorType
}
type: typeof AT.FETCH_LOAN_FINANCIALS_FAILURE
}

interface FetchLoanFinancialsLoadingAction {
type: typeof AT.FETCH_LOAN_FINANCIALS_LOADING
}
interface FetchLoanFinancialsSuccessAction {
payload: {
financials: LoanFinancialsType
}
type: typeof AT.FETCH_LOAN_FINANCIALS_SUCCESS
}
interface FetchUserBorrowHistoryFailureAction {
payload: {
error: NabuApiErrorType
Expand Down Expand Up @@ -205,6 +222,9 @@ export type BorrowActionTypes =
| FetchBorrowOffersFailureAction
| FetchBorrowOffersLoadingAction
| FetchBorrowOffersSuccessAction
| FetchLoanFinancialsFailureAction
| FetchLoanFinancialsLoadingAction
| FetchLoanFinancialsSuccessAction
| FetchUserBorrowHistoryFailureAction
| FetchUserBorrowHistoryLoadingAction
| FetchUserBorrowHistorySuccessAction
Expand Down
Expand Up @@ -36,6 +36,7 @@ class BorrowDetails extends PureComponent<Props> {

componentDidMount () {
this.props.borrowActions.setCoin(this.props.offer.terms.collateralCcy)
this.props.borrowActions.fetchLoanFinancials(this.props.loan)
}

render () {
Expand Down
@@ -1,7 +1,13 @@
import { CoinType } from 'core/types'
import { LoanType, OfferType } from './types'

export default ({ nabuUrl, authorizedGet, authorizedPost, authorizedPut }) => {
export default ({ nabuUrl, authorizedGet, authorizedPost }) => {
const getLoanFinancials = (loanId: string) =>
authorizedGet({
url: nabuUrl,
endPoint: `/user/loans/${loanId}/financials`
})

const getOffers = (): Array<OfferType> =>
authorizedGet({
url: nabuUrl,
Expand Down Expand Up @@ -43,6 +49,7 @@ export default ({ nabuUrl, authorizedGet, authorizedPost, authorizedPut }) => {
})

return {
getLoanFinancials,
getOffers,
getUserBorrowHistory,
closeLoanWithPrincipal,
Expand Down
10 changes: 9 additions & 1 deletion packages/blockchain-wallet-v4/src/network/api/borrow/types.ts
@@ -1,10 +1,17 @@
import { CoinType } from 'core/types'
import { CoinType, NabuApiErrorType, RemoteDataType } from 'core/types'

export type MoneyType = {
symbol: CoinType
value: string
}

export type LoanFinancialsType = {
collateralForInterest: Array<MoneyType>
onCloseCollateralRefund: Array<MoneyType>
onCloseCollateralTaken: Array<MoneyType>
owedInterest: Array<MoneyType>
}

export type LoanType = {
borrowerId: string
collateral: {
Expand All @@ -15,6 +22,7 @@ export type LoanType = {
}
collateralisationRatio: number
expiration: Date
financials: RemoteDataType<NabuApiErrorType, LoanFinancialsType>
loanId: string
offerId: string
openedAt: string
Expand Down
3 changes: 1 addition & 2 deletions packages/blockchain-wallet-v4/src/network/api/index.ts
Expand Up @@ -45,8 +45,7 @@ const api = ({
...borrow({
nabuUrl,
authorizedGet: authorizedHttp.get,
authorizedPost: authorizedHttp.post,
authorizedPut: authorizedHttp.put
authorizedPost: authorizedHttp.post
}),
...btc({ rootUrl, apiUrl, ...http }),
...coinify({ coinifyUrl, ...http }),
Expand Down

0 comments on commit bcc7b24

Please sign in to comment.