Skip to content

Commit

Permalink
feat(borrow): fetch loan txs info
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip London committed Mar 6, 2020
1 parent 63a672a commit 5fad3d5
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 7 deletions.
Original file line number Diff line number Diff line change
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_TRANSACTIONS = '@EVENT.FETCH_LOAN_TRANSACTIONS'
export const FETCH_LOAN_TRANSACTIONS_FAILURE =
'@EVENT.FETCH_LOAN_TRANSACTIONS_FAILURE'
export const FETCH_LOAN_TRANSACTIONS_LOADING =
'@EVENT.FETCH_LOAN_TRANSACTIONS_LOADING'
export const FETCH_LOAN_TRANSACTIONS_SUCCESS =
'@EVENT.FETCH_LOAN_TRANSACTIONS_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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BorrowActionTypes, BorrowMinMaxType, PaymentValue } from './types'
import {
CoinType,
LoanFinancialsType,
LoanTransactionsType,
LoanType,
OfferType
} from 'blockchain-wallet-v4/src/types'
Expand Down Expand Up @@ -41,6 +42,30 @@ export const fetchBorrowOffersSuccess = (offers): BorrowActionTypes => ({
}
})

export const fetchLoanTransactions = () => ({
type: AT.FETCH_LOAN_TRANSACTIONS
})

export const fetchLoanTransactionsLoading = (): BorrowActionTypes => ({
type: AT.FETCH_LOAN_TRANSACTIONS_LOADING
})

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

export const fetchLoanTransactionsSuccess = (
transactions: Array<LoanTransactionsType>
): BorrowActionTypes => ({
type: AT.FETCH_LOAN_TRANSACTIONS_SUCCESS,
payload: {
transactions
}
})

export const fetchUserBorrowHistory = () => ({
type: AT.FETCH_USER_BORROW_HISTORY
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const INITIAL_STATE: BorrowState = {
},
offer: undefined,
loan: undefined,
loanTransactions: Remote.NotAsked,
offers: Remote.NotAsked,
payment: Remote.NotAsked,
step: 'CHECKOUT'
Expand All @@ -38,6 +39,21 @@ export function borrowReducer (
...state,
offers: Remote.Success(action.payload.offers)
}
case AT.FETCH_LOAN_TRANSACTIONS_LOADING:
return {
...state,
loanTransactions: Remote.Loading
}
case AT.FETCH_LOAN_TRANSACTIONS_FAILURE:
return {
...state,
loanTransactions: Remote.Failure(action.payload.error)
}
case AT.FETCH_LOAN_TRANSACTIONS_SUCCESS:
return {
...state,
loanTransactions: Remote.Success(action.payload.transactions)
}
case AT.FETCH_USER_BORROW_HISTORY_LOADING:
return {
...state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ 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_TRANSACTIONS,
borrowSagas.fetchLoanTransactions
)
yield takeLatest(
AT.FETCH_USER_BORROW_HISTORY,
borrowSagas.fetchUserBorrowHistory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ export default ({
}
}

const fetchLoanTransactions = function * () {
try {
const loan = S.getLoan(yield select())
if (!loan) throw NO_LOAN_EXISTS
yield put(A.fetchLoanTransactionsLoading())
const { transactions } = yield call(api.getLoanTransactions, loan.loanId)
yield put(A.fetchLoanTransactionsSuccess(transactions))
} catch (e) {
yield put(A.fetchLoanTransactionsFailure(e))
}
}

const fetchUserBorrowHistory = function * () {
try {
yield put(A.fetchUserBorrowHistoryLoading())
Expand Down Expand Up @@ -421,6 +433,7 @@ export default ({
createBorrow,
destroyBorrow,
fetchBorrowOffers,
fetchLoanTransactions,
fetchUserBorrowHistory,
formChanged,
initializeBorrow,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const getLimits = (state: RootState) => state.components.borrow.limits

export const getLoan = (state: RootState) => state.components.borrow.loan

export const getLoanTransactions = (state: RootState) =>
state.components.borrow.loanTransactions

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

export const getOffer = (state: RootState) => state.components.borrow.offer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as AT from './actionTypes'
import {
AccountTypes,
CoinType,
LoanTransactionsType,
LoanType,
NabuApiErrorType,
OfferType,
Expand Down Expand Up @@ -114,6 +115,10 @@ export interface BorrowState {
coin: CoinType
limits: BorrowMinMaxType
loan?: LoanType
loanTransactions: RemoteDataType<
NabuApiErrorType,
Array<LoanTransactionsType>
>
offer?: OfferType
offers: RemoteDataType<NabuApiErrorType, Array<OfferType>>
payment: RemoteDataType<string | Error, PaymentValue>
Expand All @@ -137,6 +142,22 @@ interface FetchBorrowOffersSuccessAction {
}
type: typeof AT.FETCH_BORROW_OFFERS_SUCCESS
}
interface FetchLoanTransactionsFailureAction {
payload: {
error: NabuApiErrorType
}
type: typeof AT.FETCH_LOAN_TRANSACTIONS_FAILURE
}

interface FetchLoanTransactionsLoadingAction {
type: typeof AT.FETCH_LOAN_TRANSACTIONS_LOADING
}
interface FetchLoanTransactionsSuccessAction {
payload: {
transactions: Array<LoanTransactionsType>
}
type: typeof AT.FETCH_LOAN_TRANSACTIONS_SUCCESS
}
interface FetchUserBorrowHistoryFailureAction {
payload: {
error: NabuApiErrorType
Expand Down Expand Up @@ -217,6 +238,9 @@ export type BorrowActionTypes =
| FetchBorrowOffersFailureAction
| FetchBorrowOffersLoadingAction
| FetchBorrowOffersSuccessAction
| FetchLoanTransactionsFailureAction
| FetchLoanTransactionsLoadingAction
| FetchLoanTransactionsSuccessAction
| FetchUserBorrowHistoryFailureAction
| FetchUserBorrowHistoryLoadingAction
| FetchUserBorrowHistorySuccessAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { bindActionCreators, compose, Dispatch } from 'redux'
import { BorrowMinMaxType, PaymentType, RatesType } from 'data/types'
import {
CoinType,
LoanTransactionsType,
LoanType,
OfferType,
RemoteDataType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LoanType, OfferType } from 'core/types'
import { LoanType, OfferType, SupportedCoinsType } from 'core/types'
import { model } from 'data'
import { SuccessStateType } from '..'
import { RatesType } from 'data/types'
import { Text } from 'blockchain-info-components'
import React from 'react'
import styled from 'styled-components'
Expand Down Expand Up @@ -53,10 +53,12 @@ const CurrentBackground = styled(Current)`
left: 0;
`

type Props = SuccessStateType & {
type Props = {
loan: LoanType
offer: OfferType
rates: RatesType
showPercentages?: boolean
supportedCoins: SupportedCoinsType
}

const PADDING = 0.363636
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { bindActionCreators, Dispatch } from 'redux'
import { connect } from 'react-redux'
import { getData } from './selectors'
import {
LoanTransactionsType,
LoanType,
OfferType,
RemoteDataType,
Expand All @@ -20,6 +21,7 @@ export type OwnProps = {
offer: OfferType
}
export type SuccessStateType = {
loanTransactions: Array<LoanTransactionsType>
rates: RatesType
supportedCoins: SupportedCoinsType
}
Expand All @@ -36,6 +38,7 @@ class BorrowDetails extends PureComponent<Props> {

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

render () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import { RootState } from 'data/rootReducer'
import { selectors } from 'data'

export const getData = (state: RootState) => {
const loanTransactionsR = selectors.components.borrow.getLoanTransactions(
state
)
const ratesR = selectors.components.borrow.getRates(state)
const supportedCoinsR = selectors.core.walletOptions.getSupportedCoins(state)

const transform = (rates, supportedCoins) => ({
const transform = (loanTransactions, rates, supportedCoins) => ({
loanTransactions,
rates,
supportedCoins
})

return lift(transform)(ratesR, supportedCoinsR)
return lift(transform)(loanTransactionsR, ratesR, supportedCoinsR)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BorrowMinMaxType, PaymentType, RatesType } from 'data/types'
import { connect } from 'react-redux'
import { getData } from './selectors'
import {
LoanTransactionsType,
LoanType,
OfferType,
RemoteDataType,
Expand Down
11 changes: 10 additions & 1 deletion packages/blockchain-wallet-v4/src/network/api/borrow/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CoinType } from 'core/types'
import { LoanType, MoneyType, OfferType } from './types'
import { LoanTransactionsType, LoanType, MoneyType, OfferType } from './types'

export default ({ nabuUrl, authorizedGet, authorizedPost }) => {
const closeLoanWithPrincipal = (
Expand Down Expand Up @@ -37,6 +37,14 @@ export default ({ nabuUrl, authorizedGet, authorizedPost }) => {
endPoint: `/user/loans/${loanId}/financials`
})

const getLoanTransactions = (
loanId: string
): { transactions: Array<LoanTransactionsType> } =>
authorizedGet({
url: nabuUrl,
endPoint: `/user/loans/${loanId}/transactions`
})

const getOffers = (): Array<OfferType> =>
authorizedGet({
url: nabuUrl,
Expand Down Expand Up @@ -72,6 +80,7 @@ export default ({ nabuUrl, authorizedGet, authorizedPost }) => {
closeLoanWithPrincipal,
createLoan,
getLoanFinancials,
getLoanTransactions,
getOffers,
getUserBorrowHistory,
notifyLoanDeposit
Expand Down
28 changes: 27 additions & 1 deletion packages/blockchain-wallet-v4/src/network/api/borrow/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CoinType } from 'core/types'
import { CoinType, NabuApiErrorType, RemoteDataType } from 'core/types'

export type MoneyType = {
amount: string
Expand All @@ -12,6 +12,32 @@ export type LoanFinancialsType = {
owedInterest: Array<MoneyType>
}

export type LoanTransactionsType = {
notes: string
principalTakenAsInterest: Array<MoneyType>
request: {
amount: MoneyType
dstAddress: string
}
status: 'CONFIRMED' | 'REQUESTED' | 'UNCONFIRMED' | 'FAILED'
type:
| 'DEPOSIT_COLLATERAL'
| 'WITHDRAW_PRINCIPAL'
| 'TOPUP_COLLATERAL'
| 'REFUND_COLLATERAL'
| 'DEPOSIT_PRINCIPAL_AND_INTEREST'
| 'WITHDRAW_COLLATERAL'
| 'WITHDRAW_COLLATERAL_AND_CLOSE'
| 'LIQUIDATED_PRINCIPAL'
| 'LIQUIDATED_COLLATERAL'
| 'LIQUIDATION_FEE'
| 'INTEREST_TAKEN'
value: {
amount: MoneyType
txHash?: string
}
}

export type LoanType = {
borrowerId: string
collateral: {
Expand Down

0 comments on commit 5fad3d5

Please sign in to comment.