Skip to content

Commit

Permalink
feat(savings): add interest balances to total
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip London committed May 14, 2020
1 parent 05cb2ad commit 5230c57
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 88 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,41 @@ import { add, lift, pathOr, prop, reduce } from 'ramda'
import { createDeepEqualSelector } from 'services/ReselectHelper'
import { Exchange, Remote } from 'blockchain-wallet-v4/src'
import { formatFiat } from 'core/exchange/currency'
import { RemoteDataType, SBBalancesType } from 'core/types'
import {
InterestAccountBalanceType,
RemoteDataType,
SBBalancesType
} from 'core/types'
import { selectors } from 'data'
import BigNumber from 'bignumber.js'

export const getBtcBalance = createDeepEqualSelector(
[
selectors.core.wallet.getSpendableContext,
selectors.core.data.btc.getAddresses,
selectors.components.interest.getInterestAccountBalance,
selectors.components.simpleBuy.getSBBalances
],
(context, addressesR, sbBalancesR) => {
(context, addressesR, interestAccountBalanceR, sbBalancesR) => {
const contextToBalances = (
context,
balances,
interestAccountBalance: InterestAccountBalanceType,
sbBalances: SBBalancesType
): Array<number> => {
const walletBalances: Array<number> = context.map(a =>
pathOr(0, [a, 'final_balance'], balances)
)
const interestBalance = interestAccountBalance.BTC
? interestAccountBalance.BTC.balance
: 0
const sbBalance = Number(sbBalances.BTC ? sbBalances.BTC.available : 0)
return walletBalances.concat(sbBalance)
return walletBalances.concat(sbBalance).concat(interestBalance)
}
const balancesR = lift(contextToBalances)(
Remote.of(context),
addressesR,
interestAccountBalanceR,
sbBalancesR
)
return balancesR.map(reduce<number, number>(add, 0))
Expand All @@ -37,23 +47,29 @@ export const getBchBalance = createDeepEqualSelector(
[
selectors.core.kvStore.bch.getSpendableContext,
selectors.core.data.bch.getAddresses,
selectors.components.interest.getInterestAccountBalance,
selectors.components.simpleBuy.getSBBalances
],
(context, addressesR, sbBalancesR) => {
(context, addressesR, interestAccountBalanceR, sbBalancesR) => {
const contextToBalances = (
context,
balances,
interestAccountBalance: InterestAccountBalanceType,
sbBalances: SBBalancesType
) => {
const walletBalances: Array<number> = context.map(a =>
pathOr(0, [a, 'final_balance'], balances)
)
const interestBalance = interestAccountBalance.BCH
? interestAccountBalance.BCH.balance
: 0
const sbBalance = Number(sbBalances.BCH ? sbBalances.BCH.available : 0)
return walletBalances.concat(sbBalance)
return walletBalances.concat(sbBalance).concat(interestBalance)
}
const balancesR = lift(contextToBalances)(
Remote.of(context),
addressesR,
interestAccountBalanceR,
sbBalancesR
)
return balancesR.map(reduce<number, number>(add, 0))
Expand All @@ -64,16 +80,22 @@ export const getEthBalance = createDeepEqualSelector(
[
selectors.core.kvStore.eth.getContext,
selectors.core.data.eth.getAddresses,
selectors.components.interest.getInterestAccountBalance,
selectors.components.simpleBuy.getSBBalances
],
(
context,
addressesR,
interestAccountBalanceR: RemoteDataType<string, InterestAccountBalanceType>,
sbBalancesR: RemoteDataType<string, SBBalancesType>
) => {
const contextToBalances = (context, balances) =>
context.map(a => pathOr(0, [a, 'balance'], balances))

const interestEthBalance = interestAccountBalanceR.getOrElse({
ETH: { balance: 0 }
}).ETH
const interestBalance = interestEthBalance ? interestEthBalance.balance : 0
const sbEthBalance = sbBalancesR.getOrElse({ ETH: { available: '0' } }).ETH
const sbBalance = sbEthBalance ? sbEthBalance.available : '0'

Expand All @@ -82,22 +104,37 @@ export const getEthBalance = createDeepEqualSelector(
addressesR
)
return balancesR.map(b => {
return new BigNumber(b.getOrElse(0)).plus(new BigNumber(sbBalance))
return new BigNumber(b.getOrElse(0))
.plus(new BigNumber(sbBalance))
.plus(new BigNumber(interestBalance))
})
}
)

export const getPaxBalance = createDeepEqualSelector(
[
state => selectors.core.data.eth.getErc20Balance(state, 'pax'),
selectors.components.interest.getInterestAccountBalance,
selectors.components.simpleBuy.getSBBalances
],
(balanceR, sbBalancesR) => {
(
balanceR,
interestAccountBalanceR: RemoteDataType<string, InterestAccountBalanceType>,
sbBalancesR: RemoteDataType<string, SBBalancesType>
) => {
const interestPaxBalance = interestAccountBalanceR.getOrElse({
PAX: { balance: '0' }
}).PAX
const interestBalance = interestPaxBalance
? interestPaxBalance.balance
: '0'
const sbPaxBalance = sbBalancesR.getOrElse({ PAX: { available: '0' } }).PAX
const sbBalance = sbPaxBalance ? sbPaxBalance.available : '0'

return Remote.of(
new BigNumber(balanceR.getOrElse(0)).plus(new BigNumber(sbBalance))
new BigNumber(balanceR.getOrElse(0))
.plus(new BigNumber(sbBalance))
.plus(new BigNumber(interestBalance))
)
}
)
Expand All @@ -110,14 +147,27 @@ export const getXlmBalance = createDeepEqualSelector(
.map(accountId =>
selectors.core.data.xlm.getBalance(state, accountId).getOrElse(0)
),
selectors.components.interest.getInterestAccountBalance,
selectors.components.simpleBuy.getSBBalances
],
(balanceR, sbBalancesR: RemoteDataType<string, SBBalancesType>) => {
(
balanceR,
interestAccountBalanceR: RemoteDataType<string, InterestAccountBalanceType>,
sbBalancesR: RemoteDataType<string, SBBalancesType>
) => {
const interestXlmBalance = interestAccountBalanceR.getOrElse({
PAX: { balance: '0' }
}).PAX
const interestBalance = interestXlmBalance
? interestXlmBalance.balance
: '0'
const sbXlmBalance = sbBalancesR.getOrElse({ XLM: { available: '0' } }).XLM
const sbBalance = sbXlmBalance ? sbXlmBalance.available : '0'

return Remote.of(
new BigNumber(balanceR.getOrElse(0)).plus(new BigNumber(sbBalance))
new BigNumber(balanceR.getOrElse(0))
.plus(new BigNumber(sbBalance))
.plus(new BigNumber(interestBalance))
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default () => {
yield put(actions.core.data.eth.fetchData())
yield put(actions.core.data.xlm.fetchData())
yield put(actions.core.data.eth.fetchErc20Data('pax'))
yield put(actions.components.interest.fetchInterestAccountBalance())
yield put(actions.components.simpleBuy.fetchSBBalances())
yield put(actions.components.simpleBuy.fetchSBOrders())
// Rates
Expand Down

0 comments on commit 5230c57

Please sign in to comment.