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.pledgeState = pledgeState
|
||||
pledge.intendedProject = Number(intendedProject)
|
||||
pledge.profile.id = profile.id
|
||||
//pledge.profile.id = profile.id
|
||||
pledge.profile.set(profile)
|
||||
}
|
||||
|
||||
const pledgesCollection = database.collections.get('pledges')
|
||||
|
|
|
@ -12,9 +12,9 @@ const FundsManagement = ({ open }) => {
|
|||
const WebkitTransition = 'all 0.25s ease-out 0s'
|
||||
return (
|
||||
<FundingContext.Consumer>
|
||||
{({ allPledges, appendPledges, appendFundProfile, transferPledgeAmounts, fundProfiles }) =>
|
||||
{({ appendPledges, appendFundProfile }) =>
|
||||
<div style={{ maxWidth, WebkitTransition }}>
|
||||
<PledgesTable data={allPledges} transferPledgeAmounts={transferPledgeAmounts} fundProfiles={fundProfiles} />
|
||||
<PledgesTable />
|
||||
<FunderProfilesTable />
|
||||
<AddFunder appendFundProfile={appendFundProfile} />
|
||||
<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 withObservables from '@nozbe/with-observables'
|
||||
import { withDatabase } from '@nozbe/watermelondb/DatabaseProvider'
|
||||
|
@ -14,46 +14,66 @@ const pledgeStateMap = {
|
|||
1: 'Paying',
|
||||
2: 'Paid'
|
||||
}
|
||||
const convertToDatetime = field => {
|
||||
const convertToDatetime = async field => {
|
||||
const { commitTime } = field
|
||||
const profile = field.profile
|
||||
const profile = await field.profile.fetch()
|
||||
if (!profile || Number(commitTime) === 0) return 0
|
||||
const time = Number(commitTime) + Number(profile.commitTime)
|
||||
const date = new Date(time * 1000)
|
||||
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`
|
||||
}
|
||||
const formatField = field => ({
|
||||
const formatField = async field => ({
|
||||
...field.getFields(),
|
||||
commitTime: convertToDatetime(field),
|
||||
commitTime: await convertToDatetime(field),
|
||||
amount: toEther(field.amount),
|
||||
token: getTokenLabel(field.token),
|
||||
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 = {
|
||||
data: [],
|
||||
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 => {
|
||||
this.setState({ row });
|
||||
this.setState({ row })
|
||||
}
|
||||
|
||||
handleClose = () => {
|
||||
this.setState({ row: false });
|
||||
this.setState({ row: false })
|
||||
}
|
||||
|
||||
clearRowData = () => this.setState({ rowData: null })
|
||||
|
||||
render() {
|
||||
const { pledges, transferPledgeAmounts } = this.props
|
||||
const { row, rowData } = this.state
|
||||
const { data, row, rowData } = this.state
|
||||
return (
|
||||
<Fragment>
|
||||
<TransferDialog
|
||||
row={row}
|
||||
handleClose={this.handleClose}
|
||||
transferPledgeAmounts={transferPledgeAmounts}
|
||||
/>
|
||||
<MaterialTable
|
||||
columns={[
|
||||
|
@ -66,7 +86,7 @@ class PledgesTable extends PureComponent {
|
|||
{ title: 'Intended Project', field: 'intendedProject' },
|
||||
{ title: 'Pledge State', field: 'pledgeState' },
|
||||
]}
|
||||
data={pledges.map(formatField)}
|
||||
data={data}
|
||||
title="Pledges"
|
||||
options={{ showEmptyDataSourceMessage: true }}
|
||||
actions={[
|
||||
|
@ -94,5 +114,5 @@ class PledgesTable extends PureComponent {
|
|||
}
|
||||
|
||||
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))
|
||||
|
|
|
@ -10,34 +10,42 @@ import DialogContentText from '@material-ui/core/DialogContentText'
|
|||
import DialogTitle from '@material-ui/core/DialogTitle'
|
||||
import { getTokenLabel } from '../utils/currencies'
|
||||
import { toWei } from '../utils/conversions'
|
||||
import { FundingContext } from '../context'
|
||||
|
||||
const { transfer } = LiquidPledging.methods
|
||||
|
||||
const TransferDialog = ({ row, handleClose, transferPledgeAmounts }) => (
|
||||
const TransferDialog = ({ row, handleClose }) => (
|
||||
<Formik
|
||||
initialValues={{}}
|
||||
onSubmit={async (values, { setSubmitting, resetForm, setStatus }) => {
|
||||
const { id } = row
|
||||
const { idSender, amount, idReceiver } = values
|
||||
const args = [idSender, id, toWei(amount.toString()), idReceiver]
|
||||
const toSend = transfer(...args);
|
||||
const estimatedGas = await toSend.estimateGas();
|
||||
onSubmit={async (values, { setSubmitting, resetForm, setStatus }) => {
|
||||
const { pledgeId, pledge } = row
|
||||
const { idSender, amount, idReceiver } = values
|
||||
const args = [idSender, pledgeId, toWei(amount.toString()), idReceiver]
|
||||
const toSend = transfer(...args)
|
||||
const estimatedGas = await toSend.estimateGas()
|
||||
|
||||
toSend.send({gas: estimatedGas + 1000})
|
||||
.then(res => {
|
||||
console.log({res})
|
||||
const { events: { Transfer: { returnValues } } } = res
|
||||
transferPledgeAmounts(returnValues)
|
||||
})
|
||||
.catch(e => {
|
||||
console.log({e})
|
||||
})
|
||||
.finally(() => {
|
||||
handleClose()
|
||||
resetForm()
|
||||
})
|
||||
}}
|
||||
toSend
|
||||
.send({gas: estimatedGas + 1000})
|
||||
.then(async res => {
|
||||
console.log({res})
|
||||
const { events: { Transfer } } = res
|
||||
if (Array.isArray(Transfer)) {
|
||||
Transfer.forEach(async t => {
|
||||
const { to, amount } = t.returnValues
|
||||
await pledge.transferTo(to, amount)
|
||||
})
|
||||
} else {
|
||||
const { to, amount } = Transfer.returnValues
|
||||
await pledge.transferTo(to, amount)
|
||||
}
|
||||
})
|
||||
.catch(e => {
|
||||
console.log({e})
|
||||
})
|
||||
.finally(() => {
|
||||
handleClose()
|
||||
resetForm()
|
||||
})
|
||||
}}
|
||||
>
|
||||
{({
|
||||
values,
|
||||
|
@ -60,7 +68,7 @@ const TransferDialog = ({ row, handleClose, transferPledgeAmounts }) => (
|
|||
<DialogTitle id="form-dialog-title">Transfer Funds</DialogTitle>
|
||||
<DialogContent>
|
||||
<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>
|
||||
<TextField
|
||||
autoFocus
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { action, field, relation } from '@nozbe/watermelondb/decorators'
|
||||
import { Q } from '@nozbe/watermelondb'
|
||||
import { LiquidModel } from '../utils/models'
|
||||
|
||||
|
||||
|
@ -18,4 +19,18 @@ export default class Pledge extends LiquidModel {
|
|||
@field('pledge_state') pledgeState
|
||||
@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