add withdraws to transfer graph to map funds exiting

This commit is contained in:
Barry Gitarts 2018-12-15 05:49:08 -05:00
parent cac3d81559
commit 61a3405a9b
3 changed files with 28 additions and 9 deletions

View File

@ -1,9 +1,10 @@
import Cytoscape from 'cytoscape' import Cytoscape from 'cytoscape'
import dagre from 'cytoscape-dagre' import dagre from 'cytoscape-dagre'
import React, { Fragment } from 'react' import React, { Fragment, memo } from 'react'
import CytoscapeComponent from 'react-cytoscapejs' import CytoscapeComponent from 'react-cytoscapejs'
import { uniq } from 'ramda' import { uniq } from 'ramda'
import { toEther } from '../utils/conversions' import { toEther } from '../utils/conversions'
import { getTokenLabel } from '../utils/currencies'
Cytoscape.use(dagre) Cytoscape.use(dagre)
const layout = { name: 'dagre' } const layout = { name: 'dagre' }
@ -32,9 +33,11 @@ const stylesheet = [
} }
] ]
const createElements = transfers => { const getAuthorizations = events => events.filter(event => event.event === 'AuthorizePayment')
const createElements = (transfers, vaultEvents) => {
const nodes = [] const nodes = []
const edges = [] const edges = []
const authorizations = getAuthorizations(vaultEvents)
transfers.forEach(transfer => { transfers.forEach(transfer => {
const { returnValues: { amount, from, to } } = transfer const { returnValues: { amount, from, to } } = transfer
nodes.push({ nodes.push({
@ -47,17 +50,29 @@ const createElements = transfers => {
data: { source: from === '0' ? 'Create Funding' : from, target: to, label: toEther(amount) } data: { source: from === '0' ? 'Create Funding' : from, target: to, label: toEther(amount) }
}) })
}) })
authorizations.forEach(auth => {
const { returnValues: { amount, dest, token, ref } } = auth
const reference = Number(ref.slice(2)).toString()
if (!isNaN(reference)) {
nodes.push({
data: { id: dest, label: dest }
})
edges.push({
data: { source: reference, target: dest, label: `Withdraw ${toEther(amount)} ${getTokenLabel(token)}`}
})
}
})
return [ return [
...uniq(nodes), ...uniq(nodes),
...edges ...edges
] ]
} }
const TransfersGraph = ({ transfers }) => { const TransfersGraph = ({ transfers, vaultEvents }) => {
return ( return (
<Fragment> <Fragment>
<CytoscapeComponent <CytoscapeComponent
elements={createElements(transfers)} elements={createElements(transfers, vaultEvents)}
style={ { width: '100%', height: '600px', fontSize: '14px' } } style={ { width: '100%', height: '600px', fontSize: '14px' } }
stylesheet={stylesheet} stylesheet={stylesheet}
layout={layout} layout={layout}
@ -66,4 +81,4 @@ const TransfersGraph = ({ transfers }) => {
) )
} }
export default TransfersGraph export default memo(TransfersGraph)

View File

@ -10,7 +10,7 @@ import CreateFunding from './components/CreateFunding';
import FunderProfilesTable from './components/FunderProfilesTable' import FunderProfilesTable from './components/FunderProfilesTable'
import PledgesTable from './components/PledgesTable' import PledgesTable from './components/PledgesTable'
import { initVaultAndLP, vaultPledgingNeedsInit, standardTokenApproval, getLpAllowance } from './utils/initialize' import { initVaultAndLP, vaultPledgingNeedsInit, standardTokenApproval, getLpAllowance } from './utils/initialize'
import { getAllLPEvents, getProfileEvents, formatFundProfileEvent, getAuthorizedPayments } from './utils/events' import { getAllLPEvents, getAllVaultEvents, getProfileEvents, formatFundProfileEvent, getAuthorizedPayments } from './utils/events'
import { getAllPledges, appendToExistingPledges, transferBetweenPledges } from './utils/pledges'; import { getAllPledges, appendToExistingPledges, transferBetweenPledges } from './utils/pledges';
import { FundingContext } from './context' import { FundingContext } from './context'
import { cancelProfile } from './utils/fundProfiles' import { cancelProfile } from './utils/fundProfiles'
@ -40,6 +40,7 @@ class App extends React.Component {
const authorizedPayments = await getAuthorizedPayments() const authorizedPayments = await getAuthorizedPayments()
const account = await web3.eth.getCoinbase() const account = await web3.eth.getCoinbase()
const allLpEvents = await getAllLPEvents() const allLpEvents = await getAllLPEvents()
const allVaultEvents = await getAllVaultEvents()
const transfers = allLpEvents.filter(obj => obj.event === 'Transfer') const transfers = allLpEvents.filter(obj => obj.event === 'Transfer')
this.setState({ this.setState({
account, account,
@ -51,6 +52,7 @@ class App extends React.Component {
allPledges, allPledges,
authorizedPayments, authorizedPayments,
allLpEvents, allLpEvents,
allVaultEvents,
transfers transfers
}) })
} }
@ -83,13 +85,13 @@ class App extends React.Component {
} }
render() { render() {
const { account, needsInit, lpAllowance, fundProfiles, allPledges, authorizedPayments, transfers } = this.state const { account, needsInit, lpAllowance, fundProfiles, allPledges, authorizedPayments, transfers, allVaultEvents } = this.state
const { appendFundProfile, appendPledges, transferPledgeAmounts, cancelFundProfile } = this const { appendFundProfile, appendPledges, transferPledgeAmounts, cancelFundProfile } = this
const fundingContext = { account, transferPledgeAmounts, authorizedPayments } const fundingContext = { account, transferPledgeAmounts, authorizedPayments }
return ( return (
<FundingContext.Provider value={fundingContext}> <FundingContext.Provider value={fundingContext}>
<div> <div>
{transfers && <TransfersGraph transfers={transfers} />} {transfers && <TransfersGraph transfers={transfers} vaultEvents={allVaultEvents} />}
{!!allPledges.length && <PledgesTable data={allPledges} transferPledgeAmounts={transferPledgeAmounts} fundProfiles={fundProfiles} />} {!!allPledges.length && <PledgesTable data={allPledges} transferPledgeAmounts={transferPledgeAmounts} fundProfiles={fundProfiles} />}
{!!fundProfiles.length && <FunderProfilesTable data={fundProfiles} cancelFundProfile={cancelFundProfile}/>} {!!fundProfiles.length && <FunderProfilesTable data={fundProfiles} cancelFundProfile={cancelFundProfile}/>}
<AddFunder appendFundProfile={appendFundProfile} /> <AddFunder appendFundProfile={appendFundProfile} />

View File

@ -30,12 +30,13 @@ const formatVaultEvent = async event => {
} }
} }
const getPastVaultEvents = async event => { const getPastVaultEvents = async (event, raw = false) => {
const events = await LPVault.getPastEvents(event, { const events = await LPVault.getPastEvents(event, {
addr: await web3.eth.getCoinbase(), addr: await web3.eth.getCoinbase(),
fromBlock: 0, fromBlock: 0,
toBlock: 'latest' toBlock: 'latest'
}) })
if (raw) return events
const formattedEvents = await Promise.all( const formattedEvents = await Promise.all(
events.map(formatVaultEvent) events.map(formatVaultEvent)
) )
@ -86,6 +87,7 @@ export const getDelegateProfiles = async () => await getPastEvents(DELEGATE_ADDE
export const getProjectProfiles = async () => await getPastEvents(PROJECT_ADDED) export const getProjectProfiles = async () => await getPastEvents(PROJECT_ADDED)
export const getAllLPEvents = async () => await getPastEvents(ALL_EVENTS, true) export const getAllLPEvents = async () => await getPastEvents(ALL_EVENTS, true)
export const getAuthorizedPayments = async () => getPastVaultEvents(AUTHORIZE_PAYMENT) export const getAuthorizedPayments = async () => getPastVaultEvents(AUTHORIZE_PAYMENT)
export const getAllVaultEvents = async () => getPastVaultEvents(ALL_EVENTS,true)
export const getProfileEvents = async () => { export const getProfileEvents = async () => {
const [ funderProfiles, delegateProfiles, projectProfiles] const [ funderProfiles, delegateProfiles, projectProfiles]
= await Promise.all([getFunderProfiles(), getDelegateProfiles(), getProjectProfiles()]) = await Promise.all([getFunderProfiles(), getDelegateProfiles(), getProjectProfiles()])