liquid-funding/js/liquidPledgingState.js

110 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-10-14 10:46:04 +00:00
class LiquidPledgingState {
constructor(liquidPledging) {
this.$lp = liquidPledging;
}
$getPledge(idPledge) {
const pledge = {
delegates: [],
};
return this.$lp.getPledge(idPledge)
.then((res) => {
pledge.amount = res.amount;
pledge.owner = res.owner;
pledge.token = res.token;
2017-10-14 10:46:04 +00:00
if (res.intendedProject) {
pledge.intendedProject = res.intendedProject;
pledge.commmitTime = res.commitTime;
}
if (res.oldPledge) {
pledge.oldPledge = res.oldPledge;
}
2017-12-05 20:42:59 +00:00
if (res.pledgeState === '0') {
pledge.pledgeState = 'Pledged';
} else if (res.pledgeState === '1') {
pledge.pledgeState = 'Paying';
} else if (res.pledgeState === '2') {
pledge.pledgeState = 'Paid';
2017-10-14 10:46:04 +00:00
} else {
2017-12-05 20:42:59 +00:00
pledge.pledgeState = 'Unknown';
2017-10-14 10:46:04 +00:00
}
const promises = [];
2018-02-12 22:55:11 +00:00
for (let i = 1; i <= res.nDelegates; i += 1) {
2017-10-14 10:46:04 +00:00
promises.push(
2018-02-12 22:55:11 +00:00
this.$lp.getPledgeDelegate(idPledge, i)
2017-10-14 10:46:04 +00:00
.then(r => ({
id: r.idDelegate,
addr: r.addr,
name: r.name,
url: r.url,
})),
);
}
return Promise.all(promises);
})
.then((delegates) => {
pledge.delegates = delegates;
return pledge;
});
}
$getAdmin(idAdmin) {
const admin = {};
return this.$lp.getPledgeAdmin(idAdmin)
.then((res) => {
if (res.adminType === '0') {
admin.type = 'Giver';
} else if (res.adminType === '1') {
admin.type = 'Delegate';
} else if (res.adminType === '2') {
admin.type = 'Project';
} else {
admin.type = 'Unknown';
}
admin.addr = res.addr;
admin.name = res.name;
admin.url = res.url;
admin.commitTime = res.commitTime;
if (admin.type === 'Project') {
2017-10-14 10:46:04 +00:00
admin.parentProject = res.parentProject;
admin.canceled = res.canceled;
}
admin.plugin = res.plugin;
return admin;
});
}
getState() {
const getPledges = () => this.$lp.numberOfPledges()
.then((nPledges) => {
const promises = [];
for (let i = 1; i <= nPledges; i += 1) {
promises.push(this.$getPledge(i));
}
return Promise.all(promises);
});
const getAdmins = () => this.$lp.numberOfPledgeAdmins()
.then((nAdmins) => {
const promises = [];
for (let i = 1; i <= nAdmins; i += 1) {
promises.push(this.$getAdmin(i));
}
return Promise.all(promises);
});
return Promise.all([getPledges(), getAdmins()])
.then(([pledges, admins]) => ({
pledges: [null, ...pledges],
admins: [null, ...admins],
}));
}
}
module.exports = LiquidPledgingState;