removed await from liquidPledging.js

This commit is contained in:
perissology 2017-09-21 17:48:56 -04:00
parent ebf53f885a
commit bb14e679f0
2 changed files with 90 additions and 71 deletions

View File

@ -1,4 +1,3 @@
/* eslint-disable no-await-in-loop */
const LiquidPledgingAbi = require('../build/LiquidPledging.sol').LiquidPledgingAbi; const LiquidPledgingAbi = require('../build/LiquidPledging.sol').LiquidPledgingAbi;
const LiquidPledgingCode = require('../build/LiquidPledging.sol').LiquidPledgingByteCode; const LiquidPledgingCode = require('../build/LiquidPledging.sol').LiquidPledgingByteCode;
const LiquidPledgingMockAbi = require('../build/LiquidPledgingMock.sol').LiquidPledgingMockAbi; const LiquidPledgingMockAbi = require('../build/LiquidPledgingMock.sol').LiquidPledgingMockAbi;
@ -11,21 +10,19 @@ module.exports = (test) => {
const LiquidPledging = generateClass($abi, $byteCode); const LiquidPledging = generateClass($abi, $byteCode);
async function $getNote(idNote) { LiquidPledging.prototype.$toDecimal = function (val) { return this.$web3.utils.toDecimal(val); };
LiquidPledging.prototype.$toNumber = function (val) { this.$web3.utils.toBN(val); };
LiquidPledging.prototype.$getNote = function (idNote) {
const note = { const note = {
delegates: [], delegates: [],
}; };
const res = await this.getNote(idNote);
return this.getNote(idNote)
.then((res) => {
note.amount = this.$toNumber(res.amount); note.amount = this.$toNumber(res.amount);
note.owner = res.owner; note.owner = res.owner;
for (let i = 1; i <= this.$toDecimal(res.nDelegates); i += 1) {
const delegate = {};
const resd = await this.getNoteDelegate(idNote, i);
delegate.id = this.$toDecimal(resd.idDelegate);
delegate.addr = resd.addr;
delegate.name = resd.name;
note.delegates.push(delegate);
}
if (res.proposedProject) { if (res.proposedProject) {
note.proposedProject = this.$toDecimal(res.proposedProject); note.proposedProject = this.$toDecimal(res.proposedProject);
note.commmitTime = this.$toDecimal(res.commitTime); note.commmitTime = this.$toDecimal(res.commitTime);
@ -42,14 +39,31 @@ module.exports = (test) => {
} else { } else {
note.paymentState = 'Unknown'; note.paymentState = 'Unknown';
} }
return note;
const promises = [];
for (let i = 1; i <= this.$toDecimal(res.nDelegates); i += 1) {
promises.push(
this.getNoteDelegate(idNote, i)
.then(r => ({
id: this.$toDecimal(r.idDelegate),
addr: r.addr,
name: r.name,
})),
);
} }
LiquidPledging.prototype.$getNote = $getNote; return Promise.all(promises);
})
.then((delegates) => {
note.delegates = delegates;
return note;
});
};
async function $getManager(idManager) { LiquidPledging.prototype.$getManager = function (idManager) {
const manager = {}; const manager = {};
const res = await this.getNoteManager(idManager); return this.getNoteManager(idManager)
.then((res) => {
if (res.managerType === '0') { if (res.managerType === '0') {
manager.type = 'Donor'; manager.type = 'Donor';
} else if (res.managerType === '1') { } else if (res.managerType === '1') {
@ -67,30 +81,35 @@ module.exports = (test) => {
manager.canceled = res.canceled; manager.canceled = res.canceled;
} }
return manager; return manager;
});
}; };
LiquidPledging.prototype.$getManager = $getManager; LiquidPledging.prototype.getState = function () {
const getNotes = () => this.numberOfNotes()
async function getState() { .then((nNotes) => {
const st = { const promises = [];
notes: [null],
managers: [null],
};
const nNotes = await this.numberOfNotes();
for (let i = 1; i <= nNotes; i += 1) { for (let i = 1; i <= nNotes; i += 1) {
const note = await this.$getNote(i); promises.push(this.$getNote(i));
st.notes.push(note);
} }
return Promise.all(promises);
});
const nManagers = await this.numberOfNoteManagers(); const getManagers = () => this.numberOfNoteManagers()
.then((nManagers) => {
const promises = [];
for (let i = 1; i <= nManagers; i += 1) { for (let i = 1; i <= nManagers; i += 1) {
const manager = await this.$getManager(i); promises.push(this.$getManager(i));
st.managers.push(manager);
} }
return st;
};
LiquidPledging.prototype.getState = getState; return Promise.all(promises);
});
return Promise.all([getNotes(), getManagers()])
.then(([notes, managers]) => ({
notes: [null, ...notes],
managers: [null, ...managers],
}));
};
LiquidPledging.prototype.generateDonorsState = function () { LiquidPledging.prototype.generateDonorsState = function () {
const donorsState = []; const donorsState = [];