add pledge internal transfer after back-project

This commit is contained in:
Barry Gitarts 2019-04-18 17:40:04 -04:00 committed by Barry G
parent 0adbc380ed
commit d462929d34
2 changed files with 46 additions and 38 deletions

View File

@ -55,8 +55,10 @@ const SubmissionSection = ({ classes, profiles, delegatePledges, projectId, open
initialValues={{ amount: '', delegateProfile: '', delegatePledge: '' }} initialValues={{ amount: '', delegateProfile: '', delegatePledge: '' }}
onSubmit={async(values, { resetForm }) => { onSubmit={async(values, { resetForm }) => {
const { amount, delegateProfile, delegatePledge } = values const { amount, delegateProfile, delegatePledge } = values
const dPledge = delegatePledges.find(d => d.idPledge === delegatePledge)
const pledge = await dPledge.pledge.fetch()
const args = [delegateProfile.idProfile, delegatePledge, toWei(amount), projectId] const args = [delegateProfile.idProfile, delegatePledge, toWei(amount), projectId]
console.log({values, args}) console.log({values, args, pledge, delegatePledge})
const toSend = transfer(...args) const toSend = transfer(...args)
const estimatedGas = await toSend.estimateGas() const estimatedGas = await toSend.estimateGas()
@ -68,22 +70,22 @@ const SubmissionSection = ({ classes, profiles, delegatePledges, projectId, open
if (Array.isArray(Transfer)) { if (Array.isArray(Transfer)) {
Transfer.forEach(async t => { Transfer.forEach(async t => {
const { to, amount } = t.returnValues const { to, amount } = t.returnValues
//await pledge.transferTo(to, amount) await pledge.transferTo(to, amount)
}) })
} else { } else {
const { to, amount } = Transfer.returnValues const { to, amount } = Transfer.returnValues
//await pledge.transferTo(to, amount) await pledge.transferTo(to, amount)
} }
}) })
.catch(e => { .catch(e => {
openSnackBar('error', e) openSnackBar('error', 'An error has occured with the transaction')
console.log({e}) console.log({e})
}) })
.finally(() => { .finally(() => {
openSnackBar('success', 'project backed!') openSnackBar('success', 'project backed!')
resetForm() resetForm()
}) })
}} }}
> >
{({ {({
values, values,
@ -103,10 +105,10 @@ const SubmissionSection = ({ classes, profiles, delegatePledges, projectId, open
return ( return (
<form onSubmit={handleSubmit} className={classes.submissionRoot}> <form onSubmit={handleSubmit} className={classes.submissionRoot}>
{profiles && profiles.length === 0 && {profiles && profiles.length === 0 &&
<Typography color="error"> <Typography color="error">
Please create a Delegate profile before backing - Please create a Delegate profile before backing -
<Link href="/#/funds-management"> Delegate creation page</Link> <Link href="/#/funds-management"> Delegate creation page</Link>
</Typography>} </Typography>}
<TextField <TextField
className={classes.textField} className={classes.textField}
id="delegateProfile" id="delegateProfile"
@ -128,18 +130,18 @@ const SubmissionSection = ({ classes, profiles, delegatePledges, projectId, open
))} ))}
</TextField> </TextField>
{filteredPledges && <TextField {filteredPledges && <TextField
className={classes.textField} className={classes.textField}
id="delegatePledge" id="delegatePledge"
name="delegatePledge" name="delegatePledge"
select select
label="Select Pledge for Funding" label="Select Pledge for Funding"
placeholder="Select Pledge for Funding" placeholder="Select Pledge for Funding"
margin="normal" margin="normal"
variant="outlined" variant="outlined"
onChange={handleChange} onChange={handleChange}
onBlur={handleBlur} onBlur={handleBlur}
value={values.delegatePledge || ''} value={values.delegatePledge || ''}
> >
{filteredPledges.map(pledge => ( {filteredPledges.map(pledge => (
<MenuItem style={{ display: 'flex', alignItems: 'center' }} key={pledge.idPledge} value={pledge.idPledge}> <MenuItem style={{ display: 'flex', alignItems: 'center' }} key={pledge.idPledge} value={pledge.idPledge}>
{`Pledge no: ${pledge.idPledge} - Amount: ${toEther(pledge.pledgeData.amount)} ${getTokenLabel(pledge.pledgeData.token)}`} {`Pledge no: ${pledge.idPledge} - Amount: ${toEther(pledge.pledgeData.amount)} ${getTokenLabel(pledge.pledgeData.token)}`}
@ -147,18 +149,18 @@ const SubmissionSection = ({ classes, profiles, delegatePledges, projectId, open
))} ))}
</TextField>} </TextField>}
{values.delegatePledge && <TextField {values.delegatePledge && <TextField
autoFocus autoFocus
margin="normal" margin="normal"
id="amount" id="amount"
name="amount" name="amount"
label="Amount to transfer" label="Amount to transfer"
placeholder="Amount to transfer" placeholder="Amount to transfer"
variant="outlined" variant="outlined"
autoComplete="off" autoComplete="off"
fullWidth fullWidth
onChange={handleChange} onChange={handleChange}
onBlur={handleBlur} onBlur={handleBlur}
value={values.amount || ''} value={values.amount || ''}
/>} />}
{values.amount && <Button type="submit" color="primary" variant="contained" style={{ height: '50px', width: '100%' }}>Submit for Funding</Button>} {values.amount && <Button type="submit" color="primary" variant="contained" style={{ height: '50px', width: '100%' }}>Submit for Funding</Button>}
</form> </form>

View File

@ -22,18 +22,24 @@ export default class Pledge extends LiquidModel {
@json('delegates', sanitizeValues) delegates @json('delegates', sanitizeValues) delegates
@action async transferTo(to, amount) { @action async transferTo(to, amount) {
//TODO check if exists then update or create.
const toPledgeQuery = await this.collections.get('pledges').query( const toPledgeQuery = await this.collections.get('pledges').query(
Q.where('pledge_id', to) Q.where('pledge_id', to)
).fetch() ).fetch()
const toPledge = toPledgeQuery[0] const toPledge = toPledgeQuery[0]
await this.batch( const args = [
this.prepareUpdate(pledge => { this.prepareUpdate(pledge => {
pledge.amount = (BigInt(pledge.amount) - BigInt(amount)).toString() pledge.amount = (BigInt(pledge.amount) - BigInt(amount)).toString()
}),
toPledge.prepareUpdate(pledge => {
pledge.amount = (BigInt(pledge.amount) + BigInt(amount)).toString()
}) })
) ]
if (toPledge) {
args.push(
toPledge.prepareUpdate(pledge => {
pledge.amount = (BigInt(pledge.amount) + BigInt(amount)).toString()
})
)
}
await this.batch(...args)
} }
@action async updateFields(newPledge) { @action async updateFields(newPledge) {