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

View File

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