use pledge model for transfers in pledges
This commit is contained in:
parent
b7b39205d2
commit
c9ec430449
|
@ -14,7 +14,8 @@ const createPledge = (pledge, data, profiles) => {
|
||||||
pledge.nDelegates = Number(nDelegates)
|
pledge.nDelegates = Number(nDelegates)
|
||||||
pledge.pledgeState = pledgeState
|
pledge.pledgeState = pledgeState
|
||||||
pledge.intendedProject = Number(intendedProject)
|
pledge.intendedProject = Number(intendedProject)
|
||||||
pledge.profile.id = profile.id
|
//pledge.profile.id = profile.id
|
||||||
|
pledge.profile.set(profile)
|
||||||
}
|
}
|
||||||
|
|
||||||
const pledgesCollection = database.collections.get('pledges')
|
const pledgesCollection = database.collections.get('pledges')
|
||||||
|
|
|
@ -12,9 +12,9 @@ const FundsManagement = ({ open }) => {
|
||||||
const WebkitTransition = 'all 0.25s ease-out 0s'
|
const WebkitTransition = 'all 0.25s ease-out 0s'
|
||||||
return (
|
return (
|
||||||
<FundingContext.Consumer>
|
<FundingContext.Consumer>
|
||||||
{({ allPledges, appendPledges, appendFundProfile, transferPledgeAmounts, fundProfiles }) =>
|
{({ appendPledges, appendFundProfile }) =>
|
||||||
<div style={{ maxWidth, WebkitTransition }}>
|
<div style={{ maxWidth, WebkitTransition }}>
|
||||||
<PledgesTable data={allPledges} transferPledgeAmounts={transferPledgeAmounts} fundProfiles={fundProfiles} />
|
<PledgesTable />
|
||||||
<FunderProfilesTable />
|
<FunderProfilesTable />
|
||||||
<AddFunder appendFundProfile={appendFundProfile} />
|
<AddFunder appendFundProfile={appendFundProfile} />
|
||||||
<Divider variant="middle" />
|
<Divider variant="middle" />
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import React, { Fragment, PureComponent } from 'react'
|
import React, { Fragment, Component } from 'react'
|
||||||
import MaterialTable from 'material-table'
|
import MaterialTable from 'material-table'
|
||||||
import withObservables from '@nozbe/with-observables'
|
import withObservables from '@nozbe/with-observables'
|
||||||
import { withDatabase } from '@nozbe/watermelondb/DatabaseProvider'
|
import { withDatabase } from '@nozbe/watermelondb/DatabaseProvider'
|
||||||
|
@ -14,46 +14,66 @@ const pledgeStateMap = {
|
||||||
1: 'Paying',
|
1: 'Paying',
|
||||||
2: 'Paid'
|
2: 'Paid'
|
||||||
}
|
}
|
||||||
const convertToDatetime = field => {
|
const convertToDatetime = async field => {
|
||||||
const { commitTime } = field
|
const { commitTime } = field
|
||||||
const profile = field.profile
|
const profile = await field.profile.fetch()
|
||||||
if (!profile || Number(commitTime) === 0) return 0
|
if (!profile || Number(commitTime) === 0) return 0
|
||||||
const time = Number(commitTime) + Number(profile.commitTime)
|
const time = Number(commitTime) + Number(profile.commitTime)
|
||||||
const date = new Date(time * 1000)
|
const date = new Date(time * 1000)
|
||||||
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`
|
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`
|
||||||
}
|
}
|
||||||
const formatField = field => ({
|
const formatField = async field => ({
|
||||||
...field.getFields(),
|
...field.getFields(),
|
||||||
commitTime: convertToDatetime(field),
|
commitTime: await convertToDatetime(field),
|
||||||
amount: toEther(field.amount),
|
amount: toEther(field.amount),
|
||||||
token: getTokenLabel(field.token),
|
token: getTokenLabel(field.token),
|
||||||
intendedProject: projectText(field.intendedProject),
|
intendedProject: projectText(field.intendedProject),
|
||||||
pledgeState: pledgeStateMap[field.pledgeState]
|
pledgeState: pledgeStateMap[field.pledgeState],
|
||||||
|
transferTo: field.transferTo,
|
||||||
|
pledge: field
|
||||||
})
|
})
|
||||||
class PledgesTable extends PureComponent {
|
class PledgesTable extends Component {
|
||||||
state = {
|
state = {
|
||||||
|
data: [],
|
||||||
row: false,
|
row: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
this.setData()
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidUpdate() {
|
||||||
|
const { pledges } = this.props
|
||||||
|
const { data } = this.state
|
||||||
|
pledges.some((pledge, idx) => {
|
||||||
|
const current = data[idx]
|
||||||
|
if (toEther(pledge.amount) != current.amount || pledgeStateMap[pledge.pledgeState] != current.pledgeState) this.setData()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
setData = async () => {
|
||||||
|
const { pledges } = this.props
|
||||||
|
const data = await Promise.all(pledges.map(formatField))
|
||||||
|
this.setState({ data })
|
||||||
|
}
|
||||||
|
|
||||||
handleClickOpen = row => {
|
handleClickOpen = row => {
|
||||||
this.setState({ row });
|
this.setState({ row })
|
||||||
}
|
}
|
||||||
|
|
||||||
handleClose = () => {
|
handleClose = () => {
|
||||||
this.setState({ row: false });
|
this.setState({ row: false })
|
||||||
}
|
}
|
||||||
|
|
||||||
clearRowData = () => this.setState({ rowData: null })
|
clearRowData = () => this.setState({ rowData: null })
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { pledges, transferPledgeAmounts } = this.props
|
const { data, row, rowData } = this.state
|
||||||
const { row, rowData } = this.state
|
|
||||||
return (
|
return (
|
||||||
<Fragment>
|
<Fragment>
|
||||||
<TransferDialog
|
<TransferDialog
|
||||||
row={row}
|
row={row}
|
||||||
handleClose={this.handleClose}
|
handleClose={this.handleClose}
|
||||||
transferPledgeAmounts={transferPledgeAmounts}
|
|
||||||
/>
|
/>
|
||||||
<MaterialTable
|
<MaterialTable
|
||||||
columns={[
|
columns={[
|
||||||
|
@ -66,7 +86,7 @@ class PledgesTable extends PureComponent {
|
||||||
{ title: 'Intended Project', field: 'intendedProject' },
|
{ title: 'Intended Project', field: 'intendedProject' },
|
||||||
{ title: 'Pledge State', field: 'pledgeState' },
|
{ title: 'Pledge State', field: 'pledgeState' },
|
||||||
]}
|
]}
|
||||||
data={pledges.map(formatField)}
|
data={data}
|
||||||
title="Pledges"
|
title="Pledges"
|
||||||
options={{ showEmptyDataSourceMessage: true }}
|
options={{ showEmptyDataSourceMessage: true }}
|
||||||
actions={[
|
actions={[
|
||||||
|
@ -94,5 +114,5 @@ class PledgesTable extends PureComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default withDatabase(withObservables([], ({ database }) => ({
|
export default withDatabase(withObservables([], ({ database }) => ({
|
||||||
pledges: database.collections.get('pledges').query().observeWithColumns(['pledge_state']),
|
pledges: database.collections.get('pledges').query().observeWithColumns(['amount', 'pledge_state']),
|
||||||
}))(PledgesTable))
|
}))(PledgesTable))
|
||||||
|
|
|
@ -10,34 +10,42 @@ import DialogContentText from '@material-ui/core/DialogContentText'
|
||||||
import DialogTitle from '@material-ui/core/DialogTitle'
|
import DialogTitle from '@material-ui/core/DialogTitle'
|
||||||
import { getTokenLabel } from '../utils/currencies'
|
import { getTokenLabel } from '../utils/currencies'
|
||||||
import { toWei } from '../utils/conversions'
|
import { toWei } from '../utils/conversions'
|
||||||
import { FundingContext } from '../context'
|
|
||||||
|
|
||||||
const { transfer } = LiquidPledging.methods
|
const { transfer } = LiquidPledging.methods
|
||||||
|
|
||||||
const TransferDialog = ({ row, handleClose, transferPledgeAmounts }) => (
|
const TransferDialog = ({ row, handleClose }) => (
|
||||||
<Formik
|
<Formik
|
||||||
initialValues={{}}
|
initialValues={{}}
|
||||||
onSubmit={async (values, { setSubmitting, resetForm, setStatus }) => {
|
onSubmit={async (values, { setSubmitting, resetForm, setStatus }) => {
|
||||||
const { id } = row
|
const { pledgeId, pledge } = row
|
||||||
const { idSender, amount, idReceiver } = values
|
const { idSender, amount, idReceiver } = values
|
||||||
const args = [idSender, id, toWei(amount.toString()), idReceiver]
|
const args = [idSender, pledgeId, toWei(amount.toString()), idReceiver]
|
||||||
const toSend = transfer(...args);
|
const toSend = transfer(...args)
|
||||||
const estimatedGas = await toSend.estimateGas();
|
const estimatedGas = await toSend.estimateGas()
|
||||||
|
|
||||||
toSend.send({gas: estimatedGas + 1000})
|
toSend
|
||||||
.then(res => {
|
.send({gas: estimatedGas + 1000})
|
||||||
console.log({res})
|
.then(async res => {
|
||||||
const { events: { Transfer: { returnValues } } } = res
|
console.log({res})
|
||||||
transferPledgeAmounts(returnValues)
|
const { events: { Transfer } } = res
|
||||||
})
|
if (Array.isArray(Transfer)) {
|
||||||
.catch(e => {
|
Transfer.forEach(async t => {
|
||||||
console.log({e})
|
const { to, amount } = t.returnValues
|
||||||
})
|
await pledge.transferTo(to, amount)
|
||||||
.finally(() => {
|
})
|
||||||
handleClose()
|
} else {
|
||||||
resetForm()
|
const { to, amount } = Transfer.returnValues
|
||||||
})
|
await pledge.transferTo(to, amount)
|
||||||
}}
|
}
|
||||||
|
})
|
||||||
|
.catch(e => {
|
||||||
|
console.log({e})
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
handleClose()
|
||||||
|
resetForm()
|
||||||
|
})
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
{({
|
{({
|
||||||
values,
|
values,
|
||||||
|
@ -60,7 +68,7 @@ const TransferDialog = ({ row, handleClose, transferPledgeAmounts }) => (
|
||||||
<DialogTitle id="form-dialog-title">Transfer Funds</DialogTitle>
|
<DialogTitle id="form-dialog-title">Transfer Funds</DialogTitle>
|
||||||
<DialogContent>
|
<DialogContent>
|
||||||
<DialogContentText>
|
<DialogContentText>
|
||||||
{`Transfer ${values.amount || ''} ${values.amount ? getTokenLabel(row[6]) : ''} from Pledge ${row.id} ${values.idReceiver ? 'to Giver/Delegate/Project' : ''} ${values.idReceiver || ''}`}
|
{`Transfer ${values.amount || ''} ${values.amount ? getTokenLabel(row[6]) : ''} from Pledge ${row.pledgeId} ${values.idReceiver ? 'to Giver/Delegate/Project' : ''} ${values.idReceiver || ''}`}
|
||||||
</DialogContentText>
|
</DialogContentText>
|
||||||
<TextField
|
<TextField
|
||||||
autoFocus
|
autoFocus
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { action, field, relation } from '@nozbe/watermelondb/decorators'
|
import { action, field, relation } from '@nozbe/watermelondb/decorators'
|
||||||
|
import { Q } from '@nozbe/watermelondb'
|
||||||
import { LiquidModel } from '../utils/models'
|
import { LiquidModel } from '../utils/models'
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,4 +19,18 @@ export default class Pledge extends LiquidModel {
|
||||||
@field('pledge_state') pledgeState
|
@field('pledge_state') pledgeState
|
||||||
@relation('profiles', 'profile_id') profile
|
@relation('profiles', 'profile_id') profile
|
||||||
|
|
||||||
|
@action async transferTo(to, amount) {
|
||||||
|
const toPledgeQuery = await this.collections.get('pledges').query(
|
||||||
|
Q.where('pledge_id', to)
|
||||||
|
).fetch()
|
||||||
|
const toPledge = toPledgeQuery[0]
|
||||||
|
await this.batch(
|
||||||
|
this.prepareUpdate(pledge => {
|
||||||
|
pledge.amount = (BigInt(pledge.amount) - BigInt(amount)).toString()
|
||||||
|
}),
|
||||||
|
toPledge.prepareUpdate(pledge => {
|
||||||
|
pledge.amount = (BigInt(pledge.amount) + BigInt(amount)).toString()
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue