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,15 +70,15 @@ 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(() => {

View File

@ -22,19 +22,25 @@ 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()
}), })
]
if (toPledge) {
args.push(
toPledge.prepareUpdate(pledge => { toPledge.prepareUpdate(pledge => {
pledge.amount = (BigInt(pledge.amount) + BigInt(amount)).toString() pledge.amount = (BigInt(pledge.amount) + BigInt(amount)).toString()
}) })
) )
} }
await this.batch(...args)
}
@action async updateFields(newPledge) { @action async updateFields(newPledge) {
const { amount, nDelegates, pledgeState, blockNumber } = newPledge const { amount, nDelegates, pledgeState, blockNumber } = newPledge