From 396dab932078727cd95d3fd45031a924ab51a861 Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Wed, 19 Dec 2018 09:27:09 -0500 Subject: [PATCH] display funded withdraw net summary per token --- app/components/dashboard/FundingSummary.jsx | 31 +++++++++++++-------- app/dapp.js | 3 +- app/selectors/pledging.js | 27 ++++++++++++++---- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/app/components/dashboard/FundingSummary.jsx b/app/components/dashboard/FundingSummary.jsx index 1361698..c8914f5 100644 --- a/app/components/dashboard/FundingSummary.jsx +++ b/app/components/dashboard/FundingSummary.jsx @@ -5,8 +5,7 @@ import Card from '@material-ui/core/Card' import CardContent from '@material-ui/core/CardContent' import Typography from '@material-ui/core/Typography' import { FundingContext } from '../../context' -import { getDepositsTotal } from '../../selectors/pledging' -import { getAuthorizations } from '../../selectors/vault' +import { getDepositWithdrawTotals } from '../../selectors/pledging' const styles = { card: { @@ -30,21 +29,31 @@ function SimpleCard(props) { return ( - {({ allPledges, allLpEvents }) => + {({ allPledges, allLpEvents, vaultEvents }) => {title} {!!allLpEvents && - Object.entries(getDepositsTotal({ allLpEvents, allPledges })).map(deposit => { - const [name, amount] = deposit - return ( - - Total Funded: {amount} {name} - - ) - })} + Object.entries(getDepositWithdrawTotals({ allLpEvents, allPledges, vaultEvents })) + .map(token => { + const [name, amounts] = token + const { deposits, withdraws } = amounts + return ( +
+ + Funded: {deposits} {name} + + + Withdrawn: {withdraws} {name} + + + Net: {Number(deposits) - Number(withdraws)} {name} + +
+ ) + })}
} diff --git a/app/dapp.js b/app/dapp.js index 6170509..4f58f89 100644 --- a/app/dapp.js +++ b/app/dapp.js @@ -21,7 +21,8 @@ class App extends React.Component { allPledges: [], needsInit: true, transfers: [], - allLpEvents: [] + allLpEvents: [], + vaultEvents: [] }; componentDidMount(){ diff --git a/app/selectors/pledging.js b/app/selectors/pledging.js index 8f807d1..572a18b 100644 --- a/app/selectors/pledging.js +++ b/app/selectors/pledging.js @@ -5,6 +5,9 @@ import { getTokenLabel } from '../utils/currencies' export const getTransfers = state => state.allLpEvents.filter( obj => obj.event === 'Transfer' ) +const getWithdraws = state => state.vaultEvents.filter( + event => event.event === 'AuthorizePayment' +) export const getPledges = state => state.allPledges export const getTransfersMemo = createSelector( @@ -26,25 +29,37 @@ export const sumDeposits = deposits => deposits.reduce( BigInt(0) ).toString() -const formatAndSumDeposits = (deposits, pledges) => { +const formatAndSumDepositWithdraws = (deposits, pledges, withdraws) => { const tokens = {} deposits.forEach(deposit => { const { amount, to } = deposit.returnValues const { token } = pledges.find(p => Number(p.id) === Number(to)) const tokenName = getTokenLabel(token) - if (tokens[tokenName]) tokens[tokenName] = BigInt(tokens[tokenName]) + BigInt(amount) - else tokens[tokenName] = BigInt(amount) + if (tokens[tokenName]) tokens[tokenName]['deposits'] = BigInt(tokens[tokenName]['deposits']) + 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 .entries(tokens) .forEach(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 } -export const getDepositsTotal = createSelector( +export const getDepositWithdrawTotals = createSelector( getDepositsSelector, getPledges, - formatAndSumDeposits + getWithdraws, + formatAndSumDepositWithdraws )