Semarate mock class

This commit is contained in:
Jordi Baylina 2017-10-14 12:46:04 +02:00
parent fe85890e4d
commit 436be31e7f
No known key found for this signature in database
GPG Key ID: 7480C80C1BE43112
8 changed files with 148 additions and 274 deletions

View File

@ -1,3 +1,5 @@
exports.LiquidPledging = require('./js/liquidPledging.js');
exports.LiquidPledgingMock = require('./js/liquidPledgingMock.js');
exports.LiquidPledgingState = require('./js/liquidPledgingState.js');
exports.Vault = require('./js/vault.js');

View File

@ -1,219 +1,6 @@
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;
const generateClass = require('eth-contract-class').default;
module.exports = (test) => {
const $abi = (test) ? LiquidPledgingMockAbi : LiquidPledgingAbi;
const $byteCode = (test) ? LiquidPledgingMockCode : LiquidPledgingCode;
module.exports = generateClass(LiquidPledgingAbi, LiquidPledgingCode);
const LiquidPledging = generateClass($abi, $byteCode);
LiquidPledging.prototype.$getPledge = function (idPledge) {
const pledge = {
delegates: [],
};
return this.getPledge(idPledge)
.then((res) => {
pledge.amount = res.amount;
pledge.owner = res.owner;
if (res.intendedProject) {
pledge.intendedProject = res.intendedProject;
pledge.commmitTime = res.commitTime;
}
if (res.oldPledge) {
pledge.oldPledge = res.oldPledge;
}
if (res.paymentState === '0') {
pledge.paymentState = 'Pledged';
} else if (res.paymentState === '1') {
pledge.paymentState = 'Paying';
} else if (res.paymentState === '2') {
pledge.paymentState = 'Paid';
} else {
pledge.paymentState = 'Unknown';
}
const promises = [];
for (let i = 1; i <= res.nDelegates; i += 1) {
promises.push(
this.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;
});
};
LiquidPledging.prototype.$getAdmin = function (idAdmin) {
const admin = {};
return this.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.paymentState === 'Project') {
admin.parentProject = res.parentProject;
admin.canceled = res.canceled;
}
admin.plugin = res.plugin;
admin.canceled = res.canceled;
return admin;
});
};
LiquidPledging.prototype.getState = function () {
const getPledges = () => this.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.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],
}));
};
LiquidPledging.prototype.generateGiversState = function () {
const giversState = [];
const getGiver = (idPledge) => {
let pledge = this.pledges[idPledge];
while (pledge.oldNode) pledge = this.pledges[idPledge];
return pledge.owner;
};
// Add a giver structure to the list
const addGiver = (_list, idGiver) => {
const list = _list;
if (!list[idGiver]) {
list[idGiver] = {
idGiver,
notAssigned: {
pledges: [],
delegates: [],
},
precommitedProjects: [],
commitedProjects: [],
};
}
};
// Add a delegate structure to the list
const addDelegate = (_list, idDelegate) => {
const list = _list;
if (!list[idDelegate]) {
list[idDelegate] = {
idDelegate,
name: this.admins[idDelegate].name,
url: this.admins[idDelegate].url,
pledges: [],
delegtes: [],
};
}
};
const addProject = (_list, idProject) => {
const list = _list;
if (!list[idProject]) {
list[idProject] = {
idProject,
pledges: [],
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,
};
}
};
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];
addDelegate(list, idDelegate);
list = list[idDelegate].delegates;
}
};
const addProjectPledge = (stGiver, idPledge) => {
const pledge = this.pledges[idPledge];
const crojectList = [];
let n = pledge;
while (n.oldNode) {
crojectList.unshift(n.owner);
n = this.pledges[n.oldNode];
}
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;
}
};
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];
if ((pledge.owner === idGiver) && (pledge.precommitedProject === 0)) {
addDelegatePledge(stGiver, idPledge);
} else if ((pledge.owner === idGiver) && (pledge.precommitedProject !== 0)) {
addProject(stGiver.precommitedProjects, pledge.precommitedProject);
stGiver.precommitedProjects[pledge.precommitedProject].pledges.push(idPledge);
} else {
addProjectPledge(stGiver, idPledge);
}
}
this.giversState = giversState;
};
return LiquidPledging;
};

5
js/liquidPledgingMock.js Normal file
View File

@ -0,0 +1,5 @@
const LiquidPledgingMockAbi = require('../build/LiquidPledgingMock.sol').LiquidPledgingMockAbi;
const LiquidPledgingMockCode = require('../build/LiquidPledgingMock.sol').LiquidPledgingMockByteCode;
const generateClass = require('eth-contract-class').default;
module.exports = generateClass(LiquidPledgingMockAbi, LiquidPledgingMockCode);

109
js/liquidPledgingState.js Normal file
View File

@ -0,0 +1,109 @@
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;
if (res.intendedProject) {
pledge.intendedProject = res.intendedProject;
pledge.commmitTime = res.commitTime;
}
if (res.oldPledge) {
pledge.oldPledge = res.oldPledge;
}
if (res.paymentState === '0') {
pledge.paymentState = 'Pledged';
} else if (res.paymentState === '1') {
pledge.paymentState = 'Paying';
} else if (res.paymentState === '2') {
pledge.paymentState = 'Paid';
} else {
pledge.paymentState = '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;
});
}
$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.paymentState === 'Project') {
admin.parentProject = res.parentProject;
admin.canceled = res.canceled;
}
admin.plugin = res.plugin;
admin.canceled = res.canceled;
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;

4
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "liquidpledging",
"version": "0.0.8",
"version": "0.0.9",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -5521,7 +5521,7 @@
}
},
"web3": {
"version": "git://github.com/perissology/web3.js.git#8bf9615ef8420c70e68a20274a39bf9c3c315e6a",
"version": "git://github.com/perissology/web3.js.git#7b99cefbb6ec855441a1ab97087f03043bb6ec23",
"dev": true
},
"web3-core-promievent": {

View File

@ -1,6 +1,6 @@
{
"name": "liquidpledging",
"version": "0.0.9",
"version": "0.0.10",
"description": "Liquid Pledging Smart Contract",
"main": "index.js",
"directories": {

View File

@ -8,41 +8,23 @@ const assertFail = require('./helpers/assertFail');
const { utils } = Web3;
const LiquidPledging = liquidpledging.LiquidPledging(true);
const LiquidPledging = liquidpledging.LiquidPledgingMock;
const Vault = liquidpledging.Vault;
const LiquidPledgingState = liquidpledging.LiquidPledgingState;
const assert = chai.assert;
const printState = async (liquidPledging) => {
console.log(liquidPledging.b);
const st = await liquidPledging.getState();
const printState = async (liquidPledgingState) => {
const st = await liquidPledgingState.getState();
console.log(JSON.stringify(st, null, 2));
};
const printBalances = async (liquidPledging) => {
const st = await liquidPledging.getState();
assert.equal(st.pledges.length, 13);
for (let i = 1; i <= 12; i += 1) {
console.log(i, Web3.utils.fromWei(st.pledges[i].amount));
}
};
const readTest = async (liquidPledging) => {
const t1 = await liquidPledging.test1();
const t2 = await liquidPledging.test2();
const t3 = await liquidPledging.test3();
const t4 = await liquidPledging.test4();
console.log('t1: ', t1.toNumber());
console.log('t2: ', t2.toNumber());
console.log('t3: ', t3.toNumber());
console.log('t4: ', t4.toNumber());
};
describe('LiquidPledging test', () => {
let testrpc;
let web3;
let accounts;
let liquidPledging;
let liquidPledgingState;
let vault;
let giver1;
let giver2;
@ -81,6 +63,7 @@ describe('LiquidPledging test', () => {
vault = await Vault.new(web3);
liquidPledging = await LiquidPledging.new(web3, vault.$address, { gas: 5800000 });
await vault.setLiquidPledging(liquidPledging.$address);
liquidPledgingState = new LiquidPledgingState(liquidPledging);
}).timeout(6000);
it('Should create a giver', async () => {
await liquidPledging.addGiver('Giver1', 'URLGiver1', 86400, 0, { from: giver1 });
@ -226,11 +209,11 @@ describe('LiquidPledging test', () => {
}).timeout(6000);
it('Admin of the project1 should be able to cancel project1', async () => {
await liquidPledging.cancelProject(3, { from: adminProject1 });
const st = await liquidPledging.getState(liquidPledging);
const st = await liquidPledgingState.getState(liquidPledging);
assert.equal(st.admins[3].canceled, true);
}).timeout(6000);
it('Should not allow to withdraw from a canceled project', async () => {
const st = await liquidPledging.getState(liquidPledging);
const st = await liquidPledgingState.getState(liquidPledging);
assert.equal(utils.fromWei(st.pledges[5].amount), 0.05);
await assertFail(async () => {
await liquidPledging.withdraw(5, utils.toWei(0.01), { from: adminProject1 });
@ -241,7 +224,7 @@ describe('LiquidPledging test', () => {
$extraGas: 100000,
from: delegate1,
});
const st = await liquidPledging.getState(liquidPledging);
const st = await liquidPledgingState.getState(liquidPledging);
assert.equal(st.pledges.length, 9);
assert.equal(utils.fromWei(st.pledges[8].amount), 0.03);
assert.equal(st.pledges[8].owner, 1);
@ -251,7 +234,7 @@ describe('LiquidPledging test', () => {
}).timeout(6000);
it('Giver should be able to send the remaining to project2', async () => {
await liquidPledging.transfer(1, 5, utils.toWei(0.02), 4, { from: giver1, $extraGas: 100000 });
const st = await liquidPledging.getState(liquidPledging);
const st = await liquidPledgingState.getState(liquidPledging);
assert.equal(st.pledges.length, 9);
assert.equal(utils.fromWei(st.pledges[5].amount), 0);
assert.equal(utils.fromWei(st.pledges[4].amount), 0.12);
@ -264,14 +247,14 @@ describe('LiquidPledging test', () => {
}).timeout(6000);
it('Project 2 delegate in delegate2', async () => {
await liquidPledging.transfer(4, 4, utils.toWei(0.02), 6, { from: adminProject2 });
const st = await liquidPledging.getState(liquidPledging);
const st = await liquidPledgingState.getState(liquidPledging);
assert.equal(st.pledges.length, 10);
assert.equal(utils.fromWei(st.pledges[9].amount), 0.02);
assert.equal(utils.fromWei(st.pledges[4].amount), 0.1);
}).timeout(6000);
it('delegate2 assigns to projec2a', async () => {
await liquidPledging.transfer(6, 9, utils.toWei(0.01), 5, { from: delegate2 });
const st = await liquidPledging.getState(liquidPledging);
const st = await liquidPledgingState.getState(liquidPledging);
assert.equal(st.pledges.length, 11);
assert.equal(utils.fromWei(st.pledges[9].amount), 0.01);
assert.equal(utils.fromWei(st.pledges[10].amount), 0.01);
@ -280,7 +263,7 @@ describe('LiquidPledging test', () => {
const n = Math.floor(new Date().getTime() / 1000);
await liquidPledging.setMockedTime(n + (86401 * 3));
await liquidPledging.withdraw(10, utils.toWei(0.005), { from: adminProject2a });
const st = await liquidPledging.getState(liquidPledging);
const st = await liquidPledgingState.getState(liquidPledging);
assert.equal(st.pledges.length, 13);
assert.equal(utils.fromWei(st.pledges[10].amount), 0);
assert.equal(utils.fromWei(st.pledges[11].amount), 0.005);
@ -302,7 +285,7 @@ describe('LiquidPledging test', () => {
it('Should be able to cancel payment', async () => {
// bug somewhere which will throw invalid op_code if we don't provide gas or extraGas
await vault.cancelPayment(1, { $extraGas: 100000 });
const st = await liquidPledging.getState();
const st = await liquidPledgingState.getState();
assert.equal(st.pledges.length, 13);
assert.equal(utils.fromWei(st.pledges[2].amount), 0.31);
assert.equal(utils.fromWei(st.pledges[11].amount), 0);

View File

@ -4,36 +4,29 @@ const TestRPC = require('ethereumjs-testrpc');
const Web3 = require('web3');
const chai = require('chai');
const liquidpledging = require('../index.js');
const assertFail = require('./helpers/assertFail');
const { utils } = Web3;
const LiquidPledging = liquidpledging.LiquidPledging(true);
const LiquidPledging = liquidpledging.LiquidPledgingMock;
const LiquidPledgingState = liquidpledging.LiquidPledgingState;
const Vault = liquidpledging.Vault;
const assert = chai.assert;
const printState = async (liquidPledging) => {
const st = await liquidPledging.getState();
const printState = async (liquidPledgingState) => {
const st = await liquidPledgingState.getState();
console.log(JSON.stringify(st, null, 2));
};
describe('LiquidPledging test', function() {
this.timeout(0);
describe('LiquidPledging test', () => {
let testrpc;
let web3;
let accounts;
let liquidPledging;
let liquidPledgingState;
let vault;
let giver1;
let giver2;
let delegate1;
let adminProject1;
let adminProject2;
let adminProject2a;
let delegate2;
before(async () => {
testrpc = TestRPC.server({
ws: true,
gasLimit: 5800000,
@ -47,10 +40,6 @@ describe('LiquidPledging test', function() {
giver1 = accounts[1];
delegate1 = accounts[2];
adminProject1 = accounts[3];
adminProject2 = accounts[ 4 ];
adminProject2a = accounts[ 5 ];
delegate2 = accounts[ 6 ];
giver2 = accounts[ 7 ];
});
after((done) => {
@ -62,6 +51,7 @@ describe('LiquidPledging test', function() {
vault = await Vault.new(web3);
liquidPledging = await LiquidPledging.new(web3, vault.$address, { gas: 5800000 });
await vault.setLiquidPledging(liquidPledging.$address);
liquidPledgingState = new LiquidPledgingState(liquidPledging);
});
it('Should create a delegate', async () => {
@ -90,7 +80,7 @@ describe('LiquidPledging test', function() {
assert.equal(res[4], 259200); // default to 3 day commitTime
});
it('Should not append delegate on veto delegation', async function() {
it('Should not append delegate on veto delegation', async () => {
await liquidPledging.addProject('Project 1', 'url', adminProject1, 0, 0, 0);
// propose the delegation
await liquidPledging.transfer(1, 2, '1000', 3, { from: delegate1, gas: 400000 });
@ -99,17 +89,15 @@ describe('LiquidPledging test', function() {
const origPledge = await liquidPledging.getPledge(2);
assert.equal(origPledge.amount, '0');
// await printState(liquidPledging);
// await printState(liquidPledgingState);
// veto the delegation
await liquidPledging.transfer(2, 3, '1000', 1, { from: giver1, gas: 400000 });
const currentPledge = await liquidPledging.getPledge(2);
// await printState(liquidPledging);
// await printState(liquidPledgingState);
assert.equal(currentPledge.amount, '1000');
assert.equal(currentPledge.nDelegates, 1);
});
})
});