2018-12-14 17:48:42 +00:00
|
|
|
import Cytoscape from 'cytoscape'
|
|
|
|
import dagre from 'cytoscape-dagre'
|
2018-12-15 20:31:24 +00:00
|
|
|
import React, { Fragment } from 'react'
|
2019-01-24 14:40:57 +00:00
|
|
|
import PropTypes from 'prop-types'
|
2018-12-14 17:48:42 +00:00
|
|
|
import CytoscapeComponent from 'react-cytoscapejs'
|
2019-01-24 14:40:57 +00:00
|
|
|
import withObservables from '@nozbe/with-observables'
|
|
|
|
import { Q } from '@nozbe/watermelondb'
|
|
|
|
import { withDatabase } from '@nozbe/watermelondb/DatabaseProvider'
|
2018-12-15 21:19:24 +00:00
|
|
|
import { uniq, isNil } from 'ramda'
|
2018-12-14 19:10:08 +00:00
|
|
|
import { toEther } from '../utils/conversions'
|
2018-12-15 10:49:08 +00:00
|
|
|
import { getTokenLabel } from '../utils/currencies'
|
2018-12-15 20:31:24 +00:00
|
|
|
import { FundingContext } from '../context'
|
2018-12-17 20:45:45 +00:00
|
|
|
import { getAuthorizations } from '../selectors/vault'
|
2018-12-14 17:48:42 +00:00
|
|
|
|
2018-12-15 21:19:24 +00:00
|
|
|
|
2018-12-14 17:48:42 +00:00
|
|
|
Cytoscape.use(dagre)
|
|
|
|
const layout = { name: 'dagre' }
|
|
|
|
|
|
|
|
const stylesheet = [
|
|
|
|
{
|
|
|
|
selector: 'node',
|
|
|
|
style: {
|
|
|
|
width: 5,
|
|
|
|
height: 5,
|
|
|
|
fontSize: '5px',
|
|
|
|
color: 'blue',
|
|
|
|
label: 'data(label)'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
selector: 'edge',
|
|
|
|
style: {
|
|
|
|
label: 'data(label)',
|
|
|
|
curveStyle: 'bezier',
|
|
|
|
targetArrowShape: 'triangle',
|
|
|
|
arrowScale: 0.5,
|
|
|
|
fontSize: '5px',
|
|
|
|
width: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-12-15 10:49:08 +00:00
|
|
|
const createElements = (transfers, vaultEvents) => {
|
2018-12-15 21:19:24 +00:00
|
|
|
if (isNil(transfers) || isNil(vaultEvents)) return []
|
2018-12-14 19:10:08 +00:00
|
|
|
const nodes = []
|
|
|
|
const edges = []
|
2018-12-15 10:49:08 +00:00
|
|
|
const authorizations = getAuthorizations(vaultEvents)
|
2018-12-14 19:10:08 +00:00
|
|
|
transfers.forEach(transfer => {
|
|
|
|
const { returnValues: { amount, from, to } } = transfer
|
|
|
|
nodes.push({
|
|
|
|
data: { id: from === '0' ? 'Create Funding' : from, label: `Pledge Id ${from}` }
|
|
|
|
})
|
|
|
|
nodes.push({
|
|
|
|
data: { id: to, label: `Pledge Id ${to}` }
|
|
|
|
})
|
|
|
|
edges.push({
|
|
|
|
data: { source: from === '0' ? 'Create Funding' : from, target: to, label: toEther(amount) }
|
|
|
|
})
|
|
|
|
})
|
2018-12-15 10:49:08 +00:00
|
|
|
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)}`}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2018-12-14 19:10:08 +00:00
|
|
|
return [
|
|
|
|
...uniq(nodes),
|
|
|
|
...edges
|
2018-12-14 17:48:42 +00:00
|
|
|
]
|
2018-12-14 19:10:08 +00:00
|
|
|
}
|
2018-12-14 17:48:42 +00:00
|
|
|
|
2019-01-24 14:40:57 +00:00
|
|
|
const TransfersGraph = ({ transfers }) => {
|
2018-12-14 17:48:42 +00:00
|
|
|
return (
|
2018-12-15 20:31:24 +00:00
|
|
|
<FundingContext.Consumer>
|
2019-01-24 14:40:57 +00:00
|
|
|
{({ vaultEvents }) =>
|
2018-12-15 20:31:24 +00:00
|
|
|
<Fragment>
|
|
|
|
<CytoscapeComponent
|
|
|
|
elements={createElements(transfers, vaultEvents)}
|
2018-12-15 21:19:24 +00:00
|
|
|
style={ { width: '800px', height: '100%', fontSize: '14px' } }
|
2018-12-15 20:31:24 +00:00
|
|
|
stylesheet={stylesheet}
|
|
|
|
layout={layout}
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
}
|
|
|
|
</FundingContext.Consumer>
|
2018-12-14 17:48:42 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-01-24 14:40:57 +00:00
|
|
|
TransfersGraph.propTypes = {
|
|
|
|
transfers: PropTypes.object.isRequired
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withDatabase(withObservables([], ({ database }) => ({
|
|
|
|
transfers: database.collections.get('lp_events').query(
|
|
|
|
Q.where('event', 'Transfer')
|
|
|
|
).observe()
|
|
|
|
}))(TransfersGraph))
|