2018-12-17 20:45:45 +00:00
|
|
|
import { createSelector } from 'reselect'
|
2018-12-18 19:30:50 +00:00
|
|
|
import { toEther } from '../utils/conversions'
|
2019-01-11 20:55:42 +00:00
|
|
|
import { getTokenLabel, getTokenAddress } from '../utils/currencies'
|
2018-12-17 20:45:45 +00:00
|
|
|
|
2018-12-18 19:30:50 +00:00
|
|
|
export const getTransfers = state => state.allLpEvents.filter(
|
|
|
|
obj => obj.event === 'Transfer'
|
|
|
|
)
|
2018-12-19 14:27:09 +00:00
|
|
|
const getWithdraws = state => state.vaultEvents.filter(
|
|
|
|
event => event.event === 'AuthorizePayment'
|
|
|
|
)
|
2018-12-18 19:30:50 +00:00
|
|
|
export const getPledges = state => state.allPledges
|
2019-01-24 13:32:11 +00:00
|
|
|
const pluckPledges = state => state.pledges
|
2018-12-18 19:30:50 +00:00
|
|
|
|
|
|
|
export const getTransfersMemo = createSelector(
|
|
|
|
getTransfers,
|
|
|
|
transfers => transfers
|
|
|
|
)
|
|
|
|
|
|
|
|
export const getDeposits = transfers => transfers.filter(
|
|
|
|
transfer => transfer.returnValues.from === '0'
|
|
|
|
)
|
|
|
|
|
|
|
|
const getDepositsSelector = createSelector(
|
2019-01-24 13:32:11 +00:00
|
|
|
({ transfers }) => transfers,
|
2018-12-18 19:30:50 +00:00
|
|
|
getDeposits
|
|
|
|
)
|
|
|
|
|
|
|
|
export const sumDeposits = deposits => deposits.reduce(
|
|
|
|
(pv,cv) => pv + BigInt(cv.returnValues.amount),
|
|
|
|
BigInt(0)
|
|
|
|
).toString()
|
|
|
|
|
2019-01-11 20:55:42 +00:00
|
|
|
const pledgesWaitingCommit = pledges => {
|
|
|
|
const date = Date.now()
|
|
|
|
return pledges.filter(p => Number(p.commitTime) * 1000 > date)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getPledgesWaitingCommit = createSelector(
|
2019-01-24 13:32:11 +00:00
|
|
|
pluckPledges,
|
2019-01-11 20:55:42 +00:00
|
|
|
pledgesWaitingCommit
|
|
|
|
)
|
|
|
|
|
2018-12-19 14:27:09 +00:00
|
|
|
const formatAndSumDepositWithdraws = (deposits, pledges, withdraws) => {
|
2018-12-18 19:30:50 +00:00
|
|
|
const tokens = {}
|
2019-01-28 17:13:03 +00:00
|
|
|
let incomplete = false
|
2018-12-18 19:30:50 +00:00
|
|
|
deposits.forEach(deposit => {
|
|
|
|
const { amount, to } = deposit.returnValues
|
2019-01-28 17:13:03 +00:00
|
|
|
const pledge = pledges.find(p => Number(p.pledgeId) === Number(to))
|
|
|
|
if (!pledge) {
|
|
|
|
incomplete = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const { token } = pledge
|
2018-12-18 19:30:50 +00:00
|
|
|
const tokenName = getTokenLabel(token)
|
2018-12-19 14:27:09 +00:00
|
|
|
if (tokens[tokenName]) tokens[tokenName]['deposits'] = BigInt(tokens[tokenName]['deposits']) + BigInt(amount)
|
|
|
|
else tokens[tokenName] = { 'deposits': BigInt(amount) }
|
2018-12-18 19:30:50 +00:00
|
|
|
})
|
2018-12-19 14:27:09 +00:00
|
|
|
|
2019-01-28 17:13:03 +00:00
|
|
|
if (incomplete) return {}
|
|
|
|
|
2018-12-19 14:27:09 +00:00
|
|
|
withdraws
|
|
|
|
.filter(w => !isNaN(Number(w.returnValues.ref.slice(2))))
|
|
|
|
.forEach(withdraw => {
|
|
|
|
const { returnValues: { amount, token } } = withdraw
|
|
|
|
const tokenName = getTokenLabel(token)
|
|
|
|
if (tokens[tokenName]['withdraws']) tokens[tokenName]['withdraws'] = BigInt(tokens[tokenName]['withdraws']) + BigInt(amount)
|
|
|
|
else tokens[tokenName]['withdraws'] = BigInt(amount)
|
|
|
|
})
|
|
|
|
|
2018-12-18 19:30:50 +00:00
|
|
|
Object
|
|
|
|
.entries(tokens)
|
|
|
|
.forEach(token => {
|
|
|
|
const [key, value] = token
|
2019-01-11 20:55:42 +00:00
|
|
|
const address = getTokenAddress(key)
|
2018-12-19 14:27:09 +00:00
|
|
|
tokens[key]['deposits'] = toEther(value['deposits'].toString())
|
|
|
|
if (tokens[key]['withdraws']) tokens[key]['withdraws'] = toEther(value['withdraws'].toString())
|
2018-12-18 19:30:50 +00:00
|
|
|
})
|
|
|
|
return tokens
|
|
|
|
}
|
2018-12-19 14:27:09 +00:00
|
|
|
export const getDepositWithdrawTotals = createSelector(
|
2018-12-18 19:30:50 +00:00
|
|
|
getDepositsSelector,
|
2019-01-24 13:32:11 +00:00
|
|
|
pluckPledges,
|
2018-12-19 14:27:09 +00:00
|
|
|
getWithdraws,
|
|
|
|
formatAndSumDepositWithdraws
|
2018-12-18 19:30:50 +00:00
|
|
|
)
|