From d462929d3405044286bec0f9e6fbef1a1975dc24 Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Thu, 18 Apr 2019 17:40:04 -0400 Subject: [PATCH] add pledge internal transfer after back-project --- app/components/projects/BackProject.jsx | 68 +++++++++++++------------ app/model/pledge.js | 16 ++++-- 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/app/components/projects/BackProject.jsx b/app/components/projects/BackProject.jsx index 2b2eae4..6f5028a 100644 --- a/app/components/projects/BackProject.jsx +++ b/app/components/projects/BackProject.jsx @@ -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 (
{profiles && profiles.length === 0 && - - Please create a Delegate profile before backing - - Delegate creation page - } + + Please create a Delegate profile before backing - + Delegate creation page + } {filteredPledges && + 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 => ( {`Pledge no: ${pledge.idPledge} - Amount: ${toEther(pledge.pledgeData.amount)} ${getTokenLabel(pledge.pledgeData.token)}`} @@ -147,18 +149,18 @@ const SubmissionSection = ({ classes, profiles, delegatePledges, projectId, open ))} } {values.delegatePledge && } {values.amount && } diff --git a/app/model/pledge.js b/app/model/pledge.js index e22a3e6..21e3a53 100644 --- a/app/model/pledge.js +++ b/app/model/pledge.js @@ -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) {