display funded withdraw net summary per token

This commit is contained in:
Barry Gitarts 2018-12-19 09:27:09 -05:00
parent 40022d102f
commit 396dab9320
3 changed files with 43 additions and 18 deletions

View File

@ -5,8 +5,7 @@ import Card from '@material-ui/core/Card'
import CardContent from '@material-ui/core/CardContent' import CardContent from '@material-ui/core/CardContent'
import Typography from '@material-ui/core/Typography' import Typography from '@material-ui/core/Typography'
import { FundingContext } from '../../context' import { FundingContext } from '../../context'
import { getDepositsTotal } from '../../selectors/pledging' import { getDepositWithdrawTotals } from '../../selectors/pledging'
import { getAuthorizations } from '../../selectors/vault'
const styles = { const styles = {
card: { card: {
@ -30,21 +29,31 @@ function SimpleCard(props) {
return ( return (
<FundingContext.Consumer> <FundingContext.Consumer>
{({ allPledges, allLpEvents }) => {({ allPledges, allLpEvents, vaultEvents }) =>
<Card className={classes.card}> <Card className={classes.card}>
<CardContent> <CardContent>
<Typography variant="h5" component="h2"> <Typography variant="h5" component="h2">
{title} {title}
</Typography> </Typography>
{!!allLpEvents && {!!allLpEvents &&
Object.entries(getDepositsTotal({ allLpEvents, allPledges })).map(deposit => { Object.entries(getDepositWithdrawTotals({ allLpEvents, allPledges, vaultEvents }))
const [name, amount] = deposit .map(token => {
return ( const [name, amounts] = token
<Typography key={name} className={classes.pos} color="textSecondary"> const { deposits, withdraws } = amounts
Total Funded: {amount} {name} return (
</Typography> <div key={name}>
) <Typography key={name + 'withdraw'} className={classes.pos} color="textSecondary">
})} Funded: {deposits} {name}
</Typography>
<Typography key={name + 'deposit'} className={classes.pos} color="textSecondary">
Withdrawn: {withdraws} {name}
</Typography>
<Typography key={name + 'total'} className={classes.pos} color="textSecondary">
Net: {Number(deposits) - Number(withdraws)} {name}
</Typography>
</div>
)
})}
</CardContent> </CardContent>
</Card> </Card>
} }

View File

@ -21,7 +21,8 @@ class App extends React.Component {
allPledges: [], allPledges: [],
needsInit: true, needsInit: true,
transfers: [], transfers: [],
allLpEvents: [] allLpEvents: [],
vaultEvents: []
}; };
componentDidMount(){ componentDidMount(){

View File

@ -5,6 +5,9 @@ import { getTokenLabel } from '../utils/currencies'
export const getTransfers = state => state.allLpEvents.filter( export const getTransfers = state => state.allLpEvents.filter(
obj => obj.event === 'Transfer' obj => obj.event === 'Transfer'
) )
const getWithdraws = state => state.vaultEvents.filter(
event => event.event === 'AuthorizePayment'
)
export const getPledges = state => state.allPledges export const getPledges = state => state.allPledges
export const getTransfersMemo = createSelector( export const getTransfersMemo = createSelector(
@ -26,25 +29,37 @@ export const sumDeposits = deposits => deposits.reduce(
BigInt(0) BigInt(0)
).toString() ).toString()
const formatAndSumDeposits = (deposits, pledges) => { const formatAndSumDepositWithdraws = (deposits, pledges, withdraws) => {
const tokens = {} const tokens = {}
deposits.forEach(deposit => { deposits.forEach(deposit => {
const { amount, to } = deposit.returnValues const { amount, to } = deposit.returnValues
const { token } = pledges.find(p => Number(p.id) === Number(to)) const { token } = pledges.find(p => Number(p.id) === Number(to))
const tokenName = getTokenLabel(token) const tokenName = getTokenLabel(token)
if (tokens[tokenName]) tokens[tokenName] = BigInt(tokens[tokenName]) + BigInt(amount) if (tokens[tokenName]) tokens[tokenName]['deposits'] = BigInt(tokens[tokenName]['deposits']) + BigInt(amount)
else tokens[tokenName] = BigInt(amount) else tokens[tokenName] = { 'deposits': BigInt(amount) }
}) })
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)
})
Object Object
.entries(tokens) .entries(tokens)
.forEach(token => { .forEach(token => {
const [key, value] = token const [key, value] = token
tokens[key] = toEther(value.toString()) tokens[key]['deposits'] = toEther(value['deposits'].toString())
if (tokens[key]['withdraws']) tokens[key]['withdraws'] = toEther(value['withdraws'].toString())
}) })
return tokens return tokens
} }
export const getDepositsTotal = createSelector( export const getDepositWithdrawTotals = createSelector(
getDepositsSelector, getDepositsSelector,
getPledges, getPledges,
formatAndSumDeposits getWithdraws,
formatAndSumDepositWithdraws
) )