2017-08-18 16:47:22 +00:00
|
|
|
const LiquidPledgingAbi = require('../build/LiquidPledging.sol').LiquidPledgingAbi;
|
|
|
|
const LiquidPledgingCode = require('../build/LiquidPledging.sol').LiquidPledgingByteCode;
|
|
|
|
const LiquidPledgingMockAbi = require('../build/LiquidPledgingMock.sol').LiquidPledgingMockAbi;
|
|
|
|
const LiquidPledgingMockCode = require('../build/LiquidPledgingMock.sol').LiquidPledgingMockByteCode;
|
2017-09-22 18:31:42 +00:00
|
|
|
const generateClass = require('eth-contract-class').default;
|
2017-07-04 23:13:24 +00:00
|
|
|
|
2017-07-13 17:12:45 +00:00
|
|
|
module.exports = (test) => {
|
2017-09-16 04:05:50 +00:00
|
|
|
const $abi = (test) ? LiquidPledgingMockAbi : LiquidPledgingAbi;
|
|
|
|
const $byteCode = (test) ? LiquidPledgingMockCode : LiquidPledgingCode;
|
2017-07-13 17:12:45 +00:00
|
|
|
|
2017-09-20 15:07:34 +00:00
|
|
|
const LiquidPledging = generateClass($abi, $byteCode);
|
|
|
|
|
2017-10-03 13:15:05 +00:00
|
|
|
LiquidPledging.prototype.$getPledge = function (idPledge) {
|
|
|
|
const pledge = {
|
2017-09-20 15:07:34 +00:00
|
|
|
delegates: [],
|
|
|
|
};
|
2017-09-21 21:48:56 +00:00
|
|
|
|
2017-10-03 13:15:05 +00:00
|
|
|
return this.getPledge(idPledge)
|
2017-09-21 21:48:56 +00:00
|
|
|
.then((res) => {
|
2017-10-03 13:15:05 +00:00
|
|
|
pledge.amount = res.amount;
|
|
|
|
pledge.owner = res.owner;
|
2017-09-21 21:48:56 +00:00
|
|
|
|
2017-10-04 23:27:23 +00:00
|
|
|
if (res.intendedProject) {
|
|
|
|
pledge.intendedProject = res.intendedProject;
|
2017-10-03 13:15:05 +00:00
|
|
|
pledge.commmitTime = res.commitTime;
|
2017-09-21 21:48:56 +00:00
|
|
|
}
|
2017-10-03 13:15:05 +00:00
|
|
|
if (res.oldPledge) {
|
|
|
|
pledge.oldPledge = res.oldPledge;
|
2017-09-21 21:48:56 +00:00
|
|
|
}
|
|
|
|
if (res.paymentState === '0') {
|
2017-10-04 08:29:41 +00:00
|
|
|
pledge.paymentState = 'Pledged';
|
2017-09-21 21:48:56 +00:00
|
|
|
} else if (res.paymentState === '1') {
|
2017-10-03 13:15:05 +00:00
|
|
|
pledge.paymentState = 'Paying';
|
2017-09-21 21:48:56 +00:00
|
|
|
} else if (res.paymentState === '2') {
|
2017-10-03 13:15:05 +00:00
|
|
|
pledge.paymentState = 'Paid';
|
2017-09-21 21:48:56 +00:00
|
|
|
} else {
|
2017-10-03 13:15:05 +00:00
|
|
|
pledge.paymentState = 'Unknown';
|
2017-09-21 21:48:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const promises = [];
|
2017-10-02 20:20:56 +00:00
|
|
|
for (let i = 1; i <= res.nDelegates; i += 1) {
|
2017-09-21 21:48:56 +00:00
|
|
|
promises.push(
|
2017-10-03 13:15:05 +00:00
|
|
|
this.getPledgeDelegate(idPledge, i)
|
2017-09-21 21:48:56 +00:00
|
|
|
.then(r => ({
|
2017-10-02 20:20:56 +00:00
|
|
|
id: r.idDelegate,
|
2017-09-21 21:48:56 +00:00
|
|
|
addr: r.addr,
|
|
|
|
name: r.name,
|
2017-10-04 09:40:26 +00:00
|
|
|
url: r.url
|
2017-09-21 21:48:56 +00:00
|
|
|
})),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(promises);
|
|
|
|
})
|
|
|
|
.then((delegates) => {
|
2017-10-03 13:15:05 +00:00
|
|
|
pledge.delegates = delegates;
|
|
|
|
return pledge;
|
2017-09-21 21:48:56 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-04 08:24:35 +00:00
|
|
|
LiquidPledging.prototype.$getAdmin = function (idAdmin) {
|
|
|
|
const admin = {};
|
|
|
|
return this.getPledgeAdmin(idAdmin)
|
2017-09-21 21:48:56 +00:00
|
|
|
.then((res) => {
|
2017-10-04 08:24:35 +00:00
|
|
|
if (res.adminType === '0') {
|
|
|
|
admin.type = 'Giver';
|
|
|
|
} else if (res.adminType === '1') {
|
|
|
|
admin.type = 'Delegate';
|
|
|
|
} else if (res.adminType === '2') {
|
2017-10-04 23:27:23 +00:00
|
|
|
admin.type = 'Project';
|
2017-09-21 21:48:56 +00:00
|
|
|
} else {
|
2017-10-04 08:24:35 +00:00
|
|
|
admin.type = 'Unknown';
|
2017-09-21 21:48:56 +00:00
|
|
|
}
|
2017-10-04 08:24:35 +00:00
|
|
|
admin.addr = res.addr;
|
|
|
|
admin.name = res.name;
|
2017-10-04 09:40:26 +00:00
|
|
|
admin.url = res.url;
|
2017-10-04 08:24:35 +00:00
|
|
|
admin.commitTime = res.commitTime;
|
2017-10-04 23:27:23 +00:00
|
|
|
if (admin.paymentState === 'Project') {
|
|
|
|
admin.parentProject = res.parentProject;
|
2017-10-04 08:24:35 +00:00
|
|
|
admin.canceled = res.canceled;
|
2017-09-21 21:48:56 +00:00
|
|
|
}
|
2017-10-04 08:24:35 +00:00
|
|
|
admin.plugin = res.plugin;
|
|
|
|
admin.canceled = res.canceled;
|
|
|
|
return admin;
|
2017-09-21 21:48:56 +00:00
|
|
|
});
|
2017-09-20 15:07:34 +00:00
|
|
|
};
|
2017-08-18 16:47:22 +00:00
|
|
|
|
2017-09-21 21:48:56 +00:00
|
|
|
LiquidPledging.prototype.getState = function () {
|
2017-10-03 13:15:05 +00:00
|
|
|
const getPledges = () => this.numberOfPledges()
|
|
|
|
.then((nPledges) => {
|
2017-09-21 21:48:56 +00:00
|
|
|
const promises = [];
|
2017-10-03 13:15:05 +00:00
|
|
|
for (let i = 1; i <= nPledges; i += 1) {
|
|
|
|
promises.push(this.$getPledge(i));
|
2017-09-21 21:48:56 +00:00
|
|
|
}
|
|
|
|
return Promise.all(promises);
|
|
|
|
});
|
2017-09-20 15:07:34 +00:00
|
|
|
|
2017-10-04 08:24:35 +00:00
|
|
|
const getAdmins = () => this.numberOfPledgeAdmins()
|
|
|
|
.then((nAdmins) => {
|
2017-09-21 21:48:56 +00:00
|
|
|
const promises = [];
|
2017-10-04 08:24:35 +00:00
|
|
|
for (let i = 1; i <= nAdmins; i += 1) {
|
|
|
|
promises.push(this.$getAdmin(i));
|
2017-09-21 21:48:56 +00:00
|
|
|
}
|
2017-08-18 16:47:22 +00:00
|
|
|
|
2017-09-21 21:48:56 +00:00
|
|
|
return Promise.all(promises);
|
|
|
|
});
|
|
|
|
|
2017-10-04 08:24:35 +00:00
|
|
|
return Promise.all([getPledges(), getAdmins()])
|
|
|
|
.then(([pledges, admins]) => ({
|
2017-10-03 13:15:05 +00:00
|
|
|
pledges: [null, ...pledges],
|
2017-10-04 08:24:35 +00:00
|
|
|
admins: [null, ...admins],
|
2017-09-21 21:48:56 +00:00
|
|
|
}));
|
|
|
|
};
|
2017-08-18 16:47:22 +00:00
|
|
|
|
2017-10-03 13:15:05 +00:00
|
|
|
LiquidPledging.prototype.generateGiversState = function () {
|
|
|
|
const giversState = [];
|
2017-08-18 16:47:22 +00:00
|
|
|
|
2017-10-03 13:15:05 +00:00
|
|
|
const getGiver = (idPledge) => {
|
|
|
|
let pledge = this.pledges[idPledge];
|
|
|
|
while (pledge.oldNode) pledge = this.pledges[idPledge];
|
|
|
|
return pledge.owner;
|
2017-09-20 15:07:34 +00:00
|
|
|
};
|
2017-08-18 16:47:22 +00:00
|
|
|
|
2017-10-03 13:15:05 +00:00
|
|
|
// Add a giver structure to the list
|
|
|
|
const addGiver = (_list, idGiver) => {
|
2017-09-20 15:07:34 +00:00
|
|
|
const list = _list;
|
2017-10-03 13:15:05 +00:00
|
|
|
if (!list[idGiver]) {
|
|
|
|
list[idGiver] = {
|
|
|
|
idGiver,
|
2017-09-20 15:07:34 +00:00
|
|
|
notAssigned: {
|
2017-10-03 12:42:21 +00:00
|
|
|
pledges: [],
|
2017-09-20 15:07:34 +00:00
|
|
|
delegates: [],
|
|
|
|
},
|
2017-10-04 23:27:23 +00:00
|
|
|
precommitedProjects: [],
|
|
|
|
commitedProjects: [],
|
2017-09-20 15:07:34 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2017-08-18 16:47:22 +00:00
|
|
|
|
2017-09-20 15:07:34 +00:00
|
|
|
// Add a delegate structure to the list
|
|
|
|
const addDelegate = (_list, idDelegate) => {
|
|
|
|
const list = _list;
|
|
|
|
if (!list[idDelegate]) {
|
|
|
|
list[idDelegate] = {
|
|
|
|
idDelegate,
|
2017-10-04 08:24:35 +00:00
|
|
|
name: this.admins[idDelegate].name,
|
2017-10-04 09:40:26 +00:00
|
|
|
url: this.admins[idDelegate].url,
|
2017-10-03 13:15:05 +00:00
|
|
|
pledges: [],
|
2017-09-20 15:07:34 +00:00
|
|
|
delegtes: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-04 23:27:23 +00:00
|
|
|
const addProject = (_list, idProject) => {
|
2017-09-20 15:07:34 +00:00
|
|
|
const list = _list;
|
2017-10-04 23:27:23 +00:00
|
|
|
if (!list[idProject]) {
|
|
|
|
list[idProject] = {
|
|
|
|
idProject,
|
2017-10-03 13:15:05 +00:00
|
|
|
pledges: [],
|
2017-10-04 23:27:23 +00:00
|
|
|
commitedProjects: [],
|
|
|
|
name: this.admins[idProject].name,
|
|
|
|
url: this.admins[idProject].url,
|
|
|
|
commitTime: this.admins[idProject].commitTime,
|
|
|
|
owner: this.admins[idProject].owner,
|
|
|
|
parentProject: this.admins[idProject].parentProject,
|
2017-09-20 15:07:34 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-03 13:15:05 +00:00
|
|
|
const addDelegatePledge = (stGiver, idPledge) => {
|
|
|
|
const pledge = this.pledges[idPledge];
|
|
|
|
stGiver.notAssigned.pledges.push(idPledge);
|
|
|
|
let list = stGiver.notAssigned.delegates;
|
|
|
|
for (let i = 0; i < pledge.delegationChain.length; i += 1) {
|
|
|
|
const idDelegate = pledge.delegationChain[i];
|
2017-09-20 15:07:34 +00:00
|
|
|
addDelegate(list, idDelegate);
|
|
|
|
list = list[idDelegate].delegates;
|
|
|
|
}
|
|
|
|
};
|
2017-08-18 16:47:22 +00:00
|
|
|
|
2017-10-04 23:27:23 +00:00
|
|
|
const addProjectPledge = (stGiver, idPledge) => {
|
2017-10-03 13:15:05 +00:00
|
|
|
const pledge = this.pledges[idPledge];
|
2017-08-18 16:47:22 +00:00
|
|
|
|
2017-10-04 23:27:23 +00:00
|
|
|
const crojectList = [];
|
2017-10-03 13:15:05 +00:00
|
|
|
let n = pledge;
|
2017-09-20 15:07:34 +00:00
|
|
|
while (n.oldNode) {
|
2017-10-04 23:27:23 +00:00
|
|
|
crojectList.unshift(n.owner);
|
2017-10-03 13:15:05 +00:00
|
|
|
n = this.pledges[n.oldNode];
|
2017-08-18 16:47:22 +00:00
|
|
|
}
|
2017-07-11 11:01:00 +00:00
|
|
|
|
2017-10-04 23:27:23 +00:00
|
|
|
let list = stGiver.commitedProjects;
|
|
|
|
for (let j = 0; j < crojectList.length; j += 1) {
|
|
|
|
addProject(list, crojectList[j]);
|
|
|
|
list[crojectList[j]].pledges.push(idPledge);
|
|
|
|
list = list[crojectList[j]].commitedProjects;
|
2017-09-20 15:07:34 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-03 13:15:05 +00:00
|
|
|
for (let i = 0; i < this.pledges; i += 1) {
|
|
|
|
const idPledge = this.pledges[i];
|
|
|
|
const idGiver = getGiver(idPledge);
|
|
|
|
addGiver(giversState, idGiver);
|
|
|
|
const stGiver = giversState[idGiver];
|
|
|
|
const pledge = this.pledges[idPledge];
|
2017-10-04 23:27:23 +00:00
|
|
|
if ((pledge.owner === idGiver) && (pledge.precommitedProject === 0)) {
|
2017-10-03 13:15:05 +00:00
|
|
|
addDelegatePledge(stGiver, idPledge);
|
2017-10-04 23:27:23 +00:00
|
|
|
} else if ((pledge.owner === idGiver) && (pledge.precommitedProject !== 0)) {
|
|
|
|
addProject(stGiver.precommitedProjects, pledge.precommitedProject);
|
|
|
|
stGiver.precommitedProjects[pledge.precommitedProject].pledges.push(idPledge);
|
2017-09-20 15:07:34 +00:00
|
|
|
} else {
|
2017-10-04 23:27:23 +00:00
|
|
|
addProjectPledge(stGiver, idPledge);
|
2017-09-20 15:07:34 +00:00
|
|
|
}
|
2017-07-11 11:01:00 +00:00
|
|
|
}
|
2017-09-16 04:05:50 +00:00
|
|
|
|
2017-10-03 13:15:05 +00:00
|
|
|
this.giversState = giversState;
|
2017-07-13 17:12:45 +00:00
|
|
|
};
|
2017-09-20 15:07:34 +00:00
|
|
|
|
|
|
|
return LiquidPledging;
|
2017-07-11 11:01:00 +00:00
|
|
|
};
|