liquid-funding/lib/liquidPledgingState.js

100 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-11-29 20:52:24 +00:00
class LiquidPledgingState {
constructor(liquidPledging) {
2018-03-16 14:28:04 +00:00
this.$lp = liquidPledging;
}
2018-11-29 20:52:24 +00:00
$getPledge(idPledge) {
const pledge = {
delegates: []
};
return this.$lp.getPledge(idPledge).then(res => {
pledge.amount = res.amount;
pledge.owner = res.owner;
pledge.token = res.token;
if (res.intendedProject) {
pledge.intendedProject = res.intendedProject;
pledge.commmitTime = res.commitTime;
}
if (res.oldPledge) {
pledge.oldPledge = res.oldPledge;
}
if (res.pledgeState === '0') {
pledge.pledgeState = 'Pledged';
} else if (res.pledgeState === '1') {
pledge.pledgeState = 'Paying';
} else if (res.pledgeState === '2') {
pledge.pledgeState = 'Paid';
} else {
pledge.pledgeState = 'Unknown';
}
const promises = [];
for (let i = 1; i <= res.nDelegates; i += 1) {
promises.push(this.$lp.getPledgeDelegate(idPledge, i).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;
});
}
2018-03-16 14:28:04 +00:00
2018-11-29 20:52:24 +00:00
$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') {
admin.parentProject = res.parentProject;
admin.canceled = res.canceled;
}
admin.plugin = res.plugin;
return admin;
});
}
2018-03-16 14:28:04 +00:00
2018-11-29 20:52:24 +00:00
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]
}));
}
}
2018-03-16 14:28:04 +00:00
module.exports = LiquidPledgingState;