Merge pull request #2 from perissology/master

update to use web3 1.0
This commit is contained in:
Jordi Baylina 2017-10-04 10:01:36 +02:00 committed by GitHub
commit 0de789ef30
4 changed files with 313 additions and 286 deletions

View File

@ -1,198 +1,215 @@
/* 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;
const LiquidPledgingMockCode = require('../build/LiquidPledgingMock.sol').LiquidPledgingMockByteCode; const LiquidPledgingMockCode = require('../build/LiquidPledgingMock.sol').LiquidPledgingMockByteCode;
const runethtx = require('runethtx'); const generateClass = require('eth-contract-class').default;
module.exports = (test) => { module.exports = (test) => {
const LiquidPledgingContract = test ? const $abi = (test) ? LiquidPledgingMockAbi : LiquidPledgingAbi;
runethtx.generateClass(LiquidPledgingMockAbi, LiquidPledgingMockCode) : const $byteCode = (test) ? LiquidPledgingMockCode : LiquidPledgingCode;
runethtx.generateClass(LiquidPledgingAbi, LiquidPledgingCode);
return class LiquidPledging extends LiquidPledgingContract { const LiquidPledging = generateClass($abi, $byteCode);
constructor(web3, address) {
super(web3, address);
this.pledges = [];
this.managers = [];
this.b = 'xxxxxxx b xxxxxxxx';
}
async $getPledge(idPledge) { LiquidPledging.prototype.$getPledge = function (idPledge) {
const pledge = { const pledge = {
delegates: [], delegates: [],
}; };
const res = await this.getPledge(idPledge);
pledge.amount = res[0];
pledge.owner = res[1];
for (let i = 1; i <= res[2].toNumber(); i += 1) {
const delegate = {};
const resd = await this.getPledgeDelegate(idPledge, i);
delegate.id = resd[0].toNumber();
delegate.addr = resd[1];
delegate.name = resd[2];
pledge.delegates.push(delegate);
}
if (res[3].toNumber()) {
pledge.proposedCampaign = res[3].toNumber();
pledge.commmitTime = res[4].toNumber();
}
if (res[5].toNumber()) {
pledge.oldCampaign = res[5].toNumber();
}
if (res[6].toNumber() === 0) {
pledge.paymentState = 'NotPaid';
} else if (res[6].toNumber() === 1) {
pledge.paymentState = 'Paying';
} else if (res[6].toNumber() === 2) {
pledge.paymentState = 'Paid';
} else {
pledge.paymentState = 'Unknown';
}
return pledge;
}
async $getManager(idManager) { return this.getPledge(idPledge)
const manager = {}; .then((res) => {
const res = await this.getPledgeManager(idManager); pledge.amount = res.amount;
if (res[0].toNumber() === 0) { pledge.owner = res.owner;
manager.paymentState = 'Giver';
} else if (res[0].toNumber() === 1) {
manager.paymentState = 'Delegate';
} else if (res[0].toNumber() === 2) {
manager.paymentState = 'Campaign';
} else {
manager.paymentState = 'Unknown';
}
manager.addr = res[1];
manager.name = res[2];
manager.commitTime = res[3].toNumber();
if (manager.paymentState === 'Campaign') {
manager.parentCampaign = res[4];
manager.canceled = res[5];
}
return manager;
}
async getState() { if (res.proposedCampaign) {
const st = { pledge.proposedCampaign = res.proposedCampaign;
pledges: [null], pledge.commmitTime = res.commitTime;
managers: [null], }
}; if (res.oldPledge) {
const nPledges = await this.numberOfPledges(); pledge.oldPledge = res.oldPledge;
for (let i = 1; i <= nPledges; i += 1) { }
const pledge = await this.$getPledge(i); if (res.paymentState === '0') {
st.pledges.push(pledge); pledge.paymentState = 'NotPaid';
} } else if (res.paymentState === '1') {
pledge.paymentState = 'Paying';
} else if (res.paymentState === '2') {
pledge.paymentState = 'Paid';
} else {
pledge.paymentState = 'Unknown';
}
const nManagers = await this.numberOfPledgeManagers(); const promises = [];
for (let i = 1; i <= nManagers; i += 1) { for (let i = 1; i <= res.nDelegates; i += 1) {
const manager = await this.$getManager(i); promises.push(
st.managers.push(manager); this.getPledgeDelegate(idPledge, i)
} .then(r => ({
return st; id: r.idDelegate,
} addr: r.addr,
name: r.name,
})),
);
}
generateGiversState() { return Promise.all(promises);
const giversState = []; })
.then((delegates) => {
pledge.delegates = delegates;
return pledge;
});
};
const getGiver = (idPledge) => { LiquidPledging.prototype.$getManager = function (idManager) {
let pledge = this.pledges[idPledge]; const manager = {};
while (pledge.oldNode) pledge = this.pledges[idPledge]; return this.getPledgeManager(idManager)
return pledge.owner; .then((res) => {
}; if (res.managerType === '0') {
manager.type = 'Giver';
} else if (res.managerType === '1') {
manager.type = 'Delegate';
} else if (res.managerType === '2') {
manager.type = 'Campaign';
} else {
manager.type = 'Unknown';
}
manager.addr = res.addr;
manager.name = res.name;
manager.commitTime = res.commitTime;
if (manager.paymentState === 'Campaign') {
manager.parentCampaign = res.parentCampaign;
manager.canceled = res.canceled;
}
manager.plugin = res.plugin;
manager.canceled = res.canceled;
return manager;
});
};
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 getManagers = () => this.numberOfPledgeManagers()
.then((nManagers) => {
const promises = [];
for (let i = 1; i <= nManagers; i += 1) {
promises.push(this.$getManager(i));
}
return Promise.all(promises);
});
return Promise.all([getPledges(), getManagers()])
.then(([pledges, managers]) => ({
pledges: [null, ...pledges],
managers: [null, ...managers],
}));
};
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 // Add a giver structure to the list
const addGiver = (_list, idGiver) => { const addGiver = (_list, idGiver) => {
const list = _list; const list = _list;
if (!list[idGiver]) { if (!list[idGiver]) {
list[idGiver] = { list[idGiver] = {
idGiver, idGiver,
notAssigned: { notAssigned: {
pledges: [], pledges: [],
delegates: [], delegates: [],
}, },
precommitedCampaigns: [], precommitedCampaigns: [],
commitedCampaigns: [], commitedCampaigns: [],
}; };
} }
}; };
// Add a delegate structure to the list // Add a delegate structure to the list
const addDelegate = (_list, idDelegate) => { const addDelegate = (_list, idDelegate) => {
const list = _list; const list = _list;
if (!list[idDelegate]) { if (!list[idDelegate]) {
list[idDelegate] = { list[idDelegate] = {
idDelegate, idDelegate,
name: this.managers[idDelegate].name, name: this.managers[idDelegate].name,
pledges: [], pledges: [],
delegtes: [], delegtes: [],
}; };
} }
}; };
const addCampaign = (_list, idCampaign) => { const addCampaign = (_list, idCampaign) => {
const list = _list; const list = _list;
if (!list[idCampaign]) { if (!list[idCampaign]) {
list[idCampaign] = { list[idCampaign] = {
idCampaign, idCampaign,
pledges: [], pledges: [],
commitedCampaigns: [], commitedCampaigns: [],
name: this.managers[idCampaign].name, name: this.managers[idCampaign].name,
commitTime: this.managers[idCampaign].commitTime, commitTime: this.managers[idCampaign].commitTime,
owner: this.managers[idCampaign].owner, owner: this.managers[idCampaign].owner,
parentCampaign: this.managers[idCampaign].parentCampaign, parentCampaign: this.managers[idCampaign].parentCampaign,
}; };
} }
}; };
const addDelegatePledge = (stGiver, idPledge) => { const addDelegatePledge = (stGiver, idPledge) => {
const pledge = this.pledges[idPledge]; const pledge = this.pledges[idPledge];
stGiver.notAssigned.pledges.push(idPledge); stGiver.notAssigned.pledges.push(idPledge);
let list = stGiver.notAssigned.delegates; let list = stGiver.notAssigned.delegates;
for (let i = 0; i < pledge.delegationChain.length; i += 1) { for (let i = 0; i < pledge.delegationChain.length; i += 1) {
const idDelegate = pledge.delegationChain[i]; const idDelegate = pledge.delegationChain[i];
addDelegate(list, idDelegate); addDelegate(list, idDelegate);
list = list[idDelegate].delegates; list = list[idDelegate].delegates;
} }
}; };
const addCampaignPledge = (stGiver, idPledge) => { const addCampaignPledge = (stGiver, idPledge) => {
const pledge = this.pledges[idPledge]; const pledge = this.pledges[idPledge];
const campaignList = []; const campaignList = [];
let n = pledge; let n = pledge;
while (n.oldNode) { while (n.oldNode) {
campaignList.unshift(n.owner); campaignList.unshift(n.owner);
n = this.pledges[n.oldNode]; n = this.pledges[n.oldNode];
}
let list = stGiver.commitedCampaigns;
for (let j = 0; j < campaignList.length; j += 1) {
addCampaign(list, campaignList[j]);
list[campaignList[j]].pledges.push(idPledge);
list = list[campaignList[j]].commitedCampaigns;
}
};
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.precommitedCampaign === 0)) {
addDelegatePledge(stGiver, idPledge);
} else if ((pledge.owner === idGiver) && (pledge.precommitedCampaign !== 0)) {
addCampaign(stGiver.precommitedCampaigns, pledge.precommitedCampaign);
stGiver.precommitedCampaigns[pledge.precommitedCampaign].pledges.push(idPledge);
} else {
addCampaignPledge(stGiver, idPledge);
}
} }
this.giverssState = giversState; let list = stGiver.commitedCampaigns;
for (let j = 0; j < campaignList.length; j += 1) {
addCampaign(list, campaignList[j]);
list[campaignList[j]].pledges.push(idPledge);
list = list[campaignList[j]].commitedCampaigns;
}
};
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.precommitedCampaign === 0)) {
addDelegatePledge(stGiver, idPledge);
} else if ((pledge.owner === idGiver) && (pledge.precommitedCampaign !== 0)) {
addCampaign(stGiver.precommitedCampaigns, pledge.precommitedCampaign);
stGiver.precommitedCampaigns[pledge.precommitedCampaign].pledges.push(idPledge);
} else {
addCampaignPledge(stGiver, idPledge);
}
} }
this.giversState = giversState;
}; };
return LiquidPledging;
}; };

View File

@ -1,5 +1,5 @@
const VaultAbi = require("../build/Vault.sol").VaultAbi; const VaultAbi = require('../build/Vault.sol').VaultAbi;
const VaultByteCode = require("../build/Vault.sol").VaultByteCode; const VaultByteCode = require('../build/Vault.sol').VaultByteCode;
const runethtx = require("runethtx"); const generateClass = require('eth-contract-class').default;
module.exports = runethtx.generateClass(VaultAbi, VaultByteCode); module.exports = generateClass(VaultAbi, VaultByteCode);

View File

@ -34,16 +34,17 @@
"eslint-plugin-import": "^2.6.0", "eslint-plugin-import": "^2.6.0",
"eslint-plugin-jsx-a11y": "^6.0.2", "eslint-plugin-jsx-a11y": "^6.0.2",
"eslint-plugin-react": "^7.1.0", "eslint-plugin-react": "^7.1.0",
"ethereumjs-testrpc": "^3.0.5", "ethereumjs-testrpc": "git://github.com/perissology/testrpc.git#81216dbc",
"lerna": "^2.2.0",
"random-bytes": "^1.0.0", "random-bytes": "^1.0.0",
"mocha": "^3.5.0", "mocha": "^3.5.0",
"solcpiler": "0.0.4" "solcpiler": "0.0.4",
"web3": "git://github.com/perissology/web3.js.git#all_fixes"
}, },
"homepage": "https://github.com/Giveth/liquidpledging#readme", "homepage": "https://github.com/Giveth/liquidpledging#readme",
"dependencies": { "dependencies": {
"async": "^2.4.0", "async": "^2.4.0",
"ethconnector": "0.0.25", "chai": "^4.1.0",
"runethtx": "0.1.1", "eth-contract-class": "0.0.4"
"chai": "^4.1.0"
} }
} }

View File

@ -1,31 +1,33 @@
/* eslint-env mocha */ /* eslint-env mocha */
/* eslint-disable no-await-in-loop */ /* eslint-disable no-await-in-loop */
const ethConnector = require('ethconnector'); const TestRPC = require('ethereumjs-testrpc');
const Web3 = require('web3');
const chai = require('chai'); const chai = require('chai');
const getBalance = require('runethtx').getBalance;
const liquidpledging = require('../index.js'); const liquidpledging = require('../index.js');
const assertFail = require('./helpers/assertFail'); const assertFail = require('./helpers/assertFail');
const { utils } = Web3;
const LiquidPledging = liquidpledging.LiquidPledging(true); const LiquidPledging = liquidpledging.LiquidPledging(true);
const Vault = liquidpledging.Vault; const Vault = liquidpledging.Vault;
const assert = chai.assert; const assert = chai.assert;
const printState = async(liquidPledging) => { const printState = async (liquidPledging) => {
console.log(liquidPledging.b); console.log(liquidPledging.b);
const st = await liquidPledging.getState(); const st = await liquidPledging.getState();
console.log(JSON.stringify(st, null, 2)); console.log(JSON.stringify(st, null, 2));
}; };
const printBalances = async(liquidPledging) => { const printBalances = async (liquidPledging) => {
const st = await liquidPledging.getState(); const st = await liquidPledging.getState();
assert.equal(st.pledges.length, 13); assert.equal(st.pledges.length, 13);
for (let i = 1; i <= 12; i += 1) { for (let i = 1; i <= 12; i += 1) {
console.log(i, ethConnector.web3.fromWei(st.pledges[i].amount).toNumber()); console.log(i, Web3.utils.fromWei(st.pledges[i].amount));
} }
}; };
const readTest = async(liquidPledging) => { const readTest = async (liquidPledging) => {
const t1 = await liquidPledging.test1(); const t1 = await liquidPledging.test1();
const t2 = await liquidPledging.test2(); const t2 = await liquidPledging.test2();
const t3 = await liquidPledging.test3(); const t3 = await liquidPledging.test3();
@ -48,19 +50,24 @@ describe('LiquidPledging test', () => {
let adminCampaign2; let adminCampaign2;
let adminCampaign2a; let adminCampaign2a;
let delegate2; let delegate2;
before((done) => { before(async () => {
ethConnector.init('testrpc', { gasLimit: 5200000 }, () => { const testrpc = TestRPC.server({
web3 = ethConnector.web3; ws: true,
accounts = ethConnector.accounts; gasLimit: 5200000,
giver1 = accounts[1]; total_accounts: 10,
delegate1 = accounts[2];
adminCampaign1 = accounts[3];
adminCampaign2 = accounts[4];
adminCampaign2a = accounts[5];
delegate2 = accounts[6];
giver2 = accounts[7];
done();
}); });
testrpc.listen(8546, '127.0.0.1');
web3 = new Web3('ws://localhost:8546');
accounts = await web3.eth.getAccounts();
giver1 = accounts[1];
delegate1 = accounts[2];
adminCampaign1 = accounts[3];
adminCampaign2 = accounts[4];
adminCampaign2a = accounts[5];
delegate2 = accounts[6];
giver2 = accounts[7];
}); });
it('Should deploy LiquidPledging contract', async () => { it('Should deploy LiquidPledging contract', async () => {
vault = await Vault.new(web3); vault = await Vault.new(web3);
@ -78,9 +85,9 @@ describe('LiquidPledging test', () => {
assert.equal(res[3], 86400); assert.equal(res[3], 86400);
}).timeout(6000); }).timeout(6000);
it('Should make a donation', async () => { it('Should make a donation', async () => {
await liquidPledging.donate(1, 1, { from: giver1, value: web3.toWei(1) }); await liquidPledging.donate(1, 1, { from: giver1, value: utils.toWei(1) });
const nPledges = await liquidPledging.numberOfPledges(); const nPledges = await liquidPledging.numberOfPledges();
assert.equal(nPledges.toNumber(), 1); assert.equal(nPledges, 1);
await liquidPledging.getPledge(1); await liquidPledging.getPledge(1);
}).timeout(6000); }).timeout(6000);
it('Should create a delegate', async () => { it('Should create a delegate', async () => {
@ -93,14 +100,14 @@ describe('LiquidPledging test', () => {
assert.equal(res[2], 'Delegate1'); assert.equal(res[2], 'Delegate1');
}).timeout(6000); }).timeout(6000);
it('Giver should delegate on the delegate', async () => { it('Giver should delegate on the delegate', async () => {
await liquidPledging.transfer(1, 1, web3.toWei(0.5), 2, { from: giver1 }); await liquidPledging.transfer(1, 1, utils.toWei(0.5), 2, { from: giver1 });
const nPledges = await liquidPledging.numberOfPledges(); const nPledges = await liquidPledging.numberOfPledges();
assert.equal(nPledges.toNumber(), 2); assert.equal(nPledges, 2);
const res1 = await liquidPledging.getPledge(1); const res1 = await liquidPledging.getPledge(1);
assert.equal(res1[0].toNumber(), web3.toWei(0.5)); assert.equal(res1[0], utils.toWei(0.5));
const res2 = await liquidPledging.getPledge(2); const res2 = await liquidPledging.getPledge(2);
assert.equal(res2[0].toNumber(), web3.toWei(0.5)); assert.equal(res2[0], utils.toWei(0.5));
assert.equal(res2[1].toNumber(), 1); // One delegate assert.equal(res2[1], 1); // One delegate
const d = await liquidPledging.getPledgeDelegate(2, 1); const d = await liquidPledging.getPledgeDelegate(2, 1);
assert.equal(d[0], 2); assert.equal(d[0], 2);
@ -132,77 +139,77 @@ describe('LiquidPledging test', () => {
assert.equal(res4[4], 0); assert.equal(res4[4], 0);
assert.equal(res4[5], false); assert.equal(res4[5], false);
}).timeout(6000); }).timeout(6000);
it('Delegate should assign to campaign2', async () => { it('Delegate should assign to campaign1', async () => {
const n = Math.floor(new Date().getTime() / 1000); const n = Math.floor(new Date().getTime() / 1000);
await liquidPledging.transfer(2, 2, web3.toWei(0.2), 3, { from: delegate1 }); await liquidPledging.transfer(2, 2, utils.toWei(0.2), 3, { from: delegate1 });
const nPledges = await liquidPledging.numberOfPledges(); const nPledges = await liquidPledging.numberOfPledges();
assert.equal(nPledges.toNumber(), 3); assert.equal(nPledges, 3);
const res3 = await liquidPledging.getPledge(3); const res3 = await liquidPledging.getPledge(3);
assert.equal(res3[0].toNumber(), web3.toWei(0.2)); assert.equal(res3[0], utils.toWei(0.2));
assert.equal(res3[1].toNumber(), 1); // Owner assert.equal(res3[1], 1); // Owner
assert.equal(res3[2].toNumber(), 1); // Delegates assert.equal(res3[2], 1); // Delegates
assert.equal(res3[3].toNumber(), 3); // Proposed Campaign assert.equal(res3[3], 3); // Proposed Campaign
assert.isAbove(res3[4].toNumber(), n + 86000); assert.isAbove(utils.toDecimal(res3[4]), n + 86000);
assert.equal(res3[5].toNumber(), 0); // Old Node assert.equal(res3[5], 0); // Old Node
assert.equal(res3[6].toNumber(), 0); // Not Paid assert.equal(res3[6], 0); // Not Paid
}).timeout(6000); }).timeout(6000);
it('Giver should change his mind and assign half of it to campaign2', async () => { it('Giver should change his mind and assign half of it to campaign2', async () => {
await liquidPledging.transfer(1, 3, web3.toWei(0.1), 4, { from: giver1 }); await liquidPledging.transfer(1, 3, utils.toWei(0.1), 4, { from: giver1 });
const nPledges = await liquidPledging.numberOfPledges(); const nPledges = await liquidPledging.numberOfPledges();
assert.equal(nPledges.toNumber(), 4); assert.equal(nPledges, 4);
const res3 = await liquidPledging.getPledge(3); const res3 = await liquidPledging.getPledge(3);
assert.equal(res3[0].toNumber(), web3.toWei(0.1)); assert.equal(res3[0], utils.toWei(0.1));
const res4 = await liquidPledging.getPledge(4); const res4 = await liquidPledging.getPledge(4);
assert.equal(res4[1].toNumber(), 4); // Owner assert.equal(res4[1], 4); // Owner
assert.equal(res4[2].toNumber(), 0); // Delegates assert.equal(res4[2], 0); // Delegates
assert.equal(res4[3].toNumber(), 0); // Proposed Campaign assert.equal(res4[3], 0); // Proposed Campaign
assert.equal(res4[4], 0); assert.equal(res4[4], 0);
assert.equal(res4[5].toNumber(), 2); // Old Node assert.equal(res4[5], 2); // Old Node
assert.equal(res4[6].toNumber(), 0); // Not Paid assert.equal(res4[6], 0); // Not Paid
}).timeout(6000); }).timeout(6000);
it('After the time, the campaign1 should be able to spend part of it', async () => { it('After the time, the campaign1 should be able to spend part of it', async () => {
const n = Math.floor(new Date().getTime() / 1000); const n = Math.floor(new Date().getTime() / 1000);
await liquidPledging.setMockedTime(n + 86401); await liquidPledging.setMockedTime(n + 86401);
await liquidPledging.withdraw(3, web3.toWei(0.05), { from: adminCampaign1 }); await liquidPledging.withdraw(3, utils.toWei(0.05), { from: adminCampaign1 });
const nPledges = await liquidPledging.numberOfPledges(); const nPledges = await liquidPledging.numberOfPledges();
assert.equal(nPledges.toNumber(), 6); assert.equal(nPledges, 6);
const res5 = await liquidPledging.getPledge(5); const res5 = await liquidPledging.getPledge(5);
assert.equal(res5[0].toNumber(), web3.toWei(0.05)); assert.equal(res5[0], utils.toWei(0.05));
assert.equal(res5[1].toNumber(), 3); // Owner assert.equal(res5[1], 3); // Owner
assert.equal(res5[2].toNumber(), 0); // Delegates assert.equal(res5[2], 0); // Delegates
assert.equal(res5[3].toNumber(), 0); // Proposed Campaign assert.equal(res5[3], 0); // Proposed Campaign
assert.equal(res5[4], 0); // commit time assert.equal(res5[4], 0); // commit time
assert.equal(res5[5].toNumber(), 2); // Old Node assert.equal(res5[5], 2); // Old Node
assert.equal(res5[6].toNumber(), 0); // Not Paid assert.equal(res5[6], 0); // Not Paid
const res6 = await liquidPledging.getPledge(6); const res6 = await liquidPledging.getPledge(6);
assert.equal(res6[0].toNumber(), web3.toWei(0.05)); assert.equal(res6[0], utils.toWei(0.05));
assert.equal(res6[1].toNumber(), 3); // Owner assert.equal(res6[1], 3); // Owner
assert.equal(res6[2].toNumber(), 0); // Delegates assert.equal(res6[2], 0); // Delegates
assert.equal(res6[3].toNumber(), 0); // Proposed Campaign assert.equal(res6[3], 0); // Proposed Campaign
assert.equal(res6[4], 0); // commit time assert.equal(res6[4], 0); // commit time
assert.equal(res6[5].toNumber(), 2); // Old Node assert.equal(res6[5], 2); // Old Node
assert.equal(res6[6].toNumber(), 1); // Peinding paid Paid assert.equal(res6[6], 1); // Peinding paid Paid
}).timeout(6000); }).timeout(6000);
it('Should collect the Ether', async () => { it('Should collect the Ether', async () => {
const initialBalance = await getBalance(web3, adminCampaign1); const initialBalance = await web3.eth.getBalance(adminCampaign1);
await vault.confirmPayment(0); await vault.confirmPayment(0);
const finalBalance = await getBalance(web3, adminCampaign1); const finalBalance = await web3.eth.getBalance(adminCampaign1);
const collected = web3.fromWei(finalBalance.sub(initialBalance)).toNumber(); const collected = utils.fromWei(utils.toBN(finalBalance).sub(utils.toBN(initialBalance)));
assert.equal(collected, 0.05); assert.equal(collected, 0.05);
const nPledges = await liquidPledging.numberOfPledges(); const nPledges = await liquidPledging.numberOfPledges();
assert.equal(nPledges.toNumber(), 7); assert.equal(nPledges, 7);
const res7 = await liquidPledging.getPledge(7); const res7 = await liquidPledging.getPledge(7);
assert.equal(res7[0].toNumber(), web3.toWei(0.05)); assert.equal(res7[0], utils.toWei(0.05));
assert.equal(res7[1].toNumber(), 3); // Owner assert.equal(res7[1], 3); // Owner
assert.equal(res7[2].toNumber(), 0); // Delegates assert.equal(res7[2], 0); // Delegates
assert.equal(res7[3].toNumber(), 0); // Proposed Campaign assert.equal(res7[3], 0); // Proposed Campaign
assert.equal(res7[4], 0); // commit time assert.equal(res7[4], 0); // commit time
assert.equal(res7[5].toNumber(), 2); // Old Node assert.equal(res7[5], 2); // Old Node
assert.equal(res7[6].toNumber(), 2); // Peinding paid Paid assert.equal(res7[6], 2); // Peinding paid Paid
}).timeout(6000); }).timeout(6000);
it('Admin of the campaign1 should be able to cancel campaign1', async () => { it('Admin of the campaign1 should be able to cancel campaign1', async () => {
await liquidPledging.cancelCampaign(3, { from: adminCampaign1 }); await liquidPledging.cancelCampaign(3, { from: adminCampaign1 });
@ -211,29 +218,30 @@ describe('LiquidPledging test', () => {
}).timeout(6000); }).timeout(6000);
it('Should not allow to withdraw from a canceled campaign', async () => { it('Should not allow to withdraw from a canceled campaign', async () => {
const st = await liquidPledging.getState(liquidPledging); const st = await liquidPledging.getState(liquidPledging);
assert.equal(web3.fromWei(st.pledges[5].amount).toNumber(), 0.05); assert.equal(utils.fromWei(st.pledges[5].amount), 0.05);
await assertFail(async () => { await assertFail(async () => {
await liquidPledging.withdraw(5, web3.toWei(0.01), { from: adminCampaign1 }); await liquidPledging.withdraw(5, utils.toWei(0.01), { from: adminCampaign1 });
}); });
}).timeout(6000); }).timeout(6000);
it('Delegate should send part of this ETH to campaign2', async () => { it('Delegate should send part of this ETH to campaign2', async () => {
await liquidPledging.transfer(2, 5, web3.toWei(0.03), 4, await liquidPledging.transfer(2, 5, utils.toWei(0.03), 4, {
{ $extraGas: 100000 }, $extraGas: 100000,
{ from: delegate1 }); from: delegate1,
});
const st = await liquidPledging.getState(liquidPledging); const st = await liquidPledging.getState(liquidPledging);
assert.equal(st.pledges.length, 9); assert.equal(st.pledges.length, 9);
assert.equal(web3.fromWei(st.pledges[8].amount).toNumber(), 0.03); assert.equal(utils.fromWei(st.pledges[8].amount), 0.03);
assert.equal(st.pledges[8].owner, 1); assert.equal(st.pledges[8].owner, 1);
assert.equal(st.pledges[8].delegates.length, 1); assert.equal(st.pledges[8].delegates.length, 1);
assert.equal(st.pledges[8].delegates[0].id, 2); assert.equal(st.pledges[8].delegates[0].id, 2);
assert.equal(st.pledges[8].proposedCampaign, 4); assert.equal(st.pledges[8].proposedCampaign, 4);
}).timeout(6000); }).timeout(6000);
it('Giver should be able to send the remaining to campaign2', async () => { it('Giver should be able to send the remaining to campaign2', async () => {
await liquidPledging.transfer(1, 5, web3.toWei(0.02), 4, { from: giver1 }); await liquidPledging.transfer(1, 5, utils.toWei(0.02), 4, { from: giver1, $extraGas: 100000 });
const st = await liquidPledging.getState(liquidPledging); const st = await liquidPledging.getState(liquidPledging);
assert.equal(st.pledges.length, 9); assert.equal(st.pledges.length, 9);
assert.equal(web3.fromWei(st.pledges[5].amount).toNumber(), 0); assert.equal(utils.fromWei(st.pledges[5].amount), 0);
assert.equal(web3.fromWei(st.pledges[4].amount).toNumber(), 0.12); assert.equal(utils.fromWei(st.pledges[4].amount), 0.12);
}).timeout(6000); }).timeout(6000);
it('A subcampaign 2a and a delegate2 is created', async () => { it('A subcampaign 2a and a delegate2 is created', async () => {
await liquidPledging.addCampaign('Campaign2a', adminCampaign2a, 4, 86400, 0, { from: adminCampaign2 }); await liquidPledging.addCampaign('Campaign2a', adminCampaign2a, 4, 86400, 0, { from: adminCampaign2 });
@ -242,28 +250,28 @@ describe('LiquidPledging test', () => {
assert.equal(nManagers, 6); assert.equal(nManagers, 6);
}).timeout(6000); }).timeout(6000);
it('Campaign 2 delegate in delegate2', async () => { it('Campaign 2 delegate in delegate2', async () => {
await liquidPledging.transfer(4, 4, web3.toWei(0.02), 6, { from: adminCampaign2 }); await liquidPledging.transfer(4, 4, utils.toWei(0.02), 6, { from: adminCampaign2 });
const st = await liquidPledging.getState(liquidPledging); const st = await liquidPledging.getState(liquidPledging);
assert.equal(st.pledges.length, 10); assert.equal(st.pledges.length, 10);
assert.equal(web3.fromWei(st.pledges[9].amount).toNumber(), 0.02); assert.equal(utils.fromWei(st.pledges[9].amount), 0.02);
assert.equal(web3.fromWei(st.pledges[4].amount).toNumber(), 0.1); assert.equal(utils.fromWei(st.pledges[4].amount), 0.1);
}).timeout(6000); }).timeout(6000);
it('delegate2 assigns to projec2a', async () => { it('delegate2 assigns to projec2a', async () => {
await liquidPledging.transfer(6, 9, web3.toWei(0.01), 5, { from: delegate2 }); await liquidPledging.transfer(6, 9, utils.toWei(0.01), 5, { from: delegate2 });
const st = await liquidPledging.getState(liquidPledging); const st = await liquidPledging.getState(liquidPledging);
assert.equal(st.pledges.length, 11); assert.equal(st.pledges.length, 11);
assert.equal(web3.fromWei(st.pledges[9].amount).toNumber(), 0.01); assert.equal(utils.fromWei(st.pledges[9].amount), 0.01);
assert.equal(web3.fromWei(st.pledges[10].amount).toNumber(), 0.01); assert.equal(utils.fromWei(st.pledges[10].amount), 0.01);
}).timeout(4000); }).timeout(4000);
it('campaign2a authorize to spend a litle', async () => { it('campaign2a authorize to spend a litle', async () => {
const n = Math.floor(new Date().getTime() / 1000); const n = Math.floor(new Date().getTime() / 1000);
await liquidPledging.setMockedTime(n + (86401 * 3)); await liquidPledging.setMockedTime(n + (86401 * 3));
await liquidPledging.withdraw(10, web3.toWei(0.005), { from: adminCampaign2a }); await liquidPledging.withdraw(10, utils.toWei(0.005), { from: adminCampaign2a });
const st = await liquidPledging.getState(liquidPledging); const st = await liquidPledging.getState(liquidPledging);
assert.equal(st.pledges.length, 13); assert.equal(st.pledges.length, 13);
assert.equal(web3.fromWei(st.pledges[10].amount).toNumber(), 0); assert.equal(utils.fromWei(st.pledges[10].amount), 0);
assert.equal(web3.fromWei(st.pledges[11].amount).toNumber(), 0.005); assert.equal(utils.fromWei(st.pledges[11].amount), 0.005);
assert.equal(web3.fromWei(st.pledges[12].amount).toNumber(), 0.005); assert.equal(utils.fromWei(st.pledges[12].amount), 0.005);
}).timeout(4000); }).timeout(4000);
it('campaign2 is canceled', async () => { it('campaign2 is canceled', async () => {
await liquidPledging.cancelCampaign(4, { from: adminCampaign2 }); await liquidPledging.cancelCampaign(4, { from: adminCampaign2 });
@ -275,41 +283,42 @@ describe('LiquidPledging test', () => {
}).timeout(6000); }).timeout(6000);
it('Should not be able to withdraw it', async () => { it('Should not be able to withdraw it', async () => {
await assertFail(async () => { await assertFail(async () => {
await liquidPledging.withdraw(12, web3.toWei(0.005), { from: giver1 }); await liquidPledging.withdraw(12, utils.toWei(0.005), { from: giver1 });
}); });
}).timeout(6000); }).timeout(6000);
it('Should not be able to cancel payment', async () => { it('Should be able to cancel payment', async () => {
await vault.cancelPayment(1); // bug somewhere which will throw invalid op_code if we don't provide gas or extraGas
const st = await liquidPledging.getState(liquidPledging); await vault.cancelPayment(1, { $extraGas: 100000 });
const st = await liquidPledging.getState();
assert.equal(st.pledges.length, 13); assert.equal(st.pledges.length, 13);
assert.equal(web3.fromWei(st.pledges[2].amount).toNumber(), 0.31); assert.equal(utils.fromWei(st.pledges[2].amount), 0.31);
assert.equal(web3.fromWei(st.pledges[11].amount).toNumber(), 0); assert.equal(utils.fromWei(st.pledges[11].amount), 0);
assert.equal(web3.fromWei(st.pledges[12].amount).toNumber(), 0); assert.equal(utils.fromWei(st.pledges[12].amount), 0);
}).timeout(6000); }).timeout(6000);
it('original owner should recover the remaining funds', async () => { it('original owner should recover the remaining funds', async () => {
await liquidPledging.withdraw(1, web3.toWei(0.5), { from: giver1 }); await liquidPledging.withdraw(1, utils.toWei(0.5), { from: giver1 });
await liquidPledging.withdraw(2, web3.toWei(0.31), { from: giver1 }); await liquidPledging.withdraw(2, utils.toWei(0.31), { from: giver1 });
await liquidPledging.withdraw(4, web3.toWei(0.1), { from: giver1 }); await liquidPledging.withdraw(4, utils.toWei(0.1), { $extraGas: 100000, from: giver1 });
await liquidPledging.withdraw(8, web3.toWei(0.03), { $extraGas: 100000 }, { from: giver1 }); await liquidPledging.withdraw(8, utils.toWei(0.03), { $extraGas: 100000, from: giver1 });
await liquidPledging.withdraw(9, web3.toWei(0.01), { from: giver1 }); await liquidPledging.withdraw(9, utils.toWei(0.01), { $extraGas: 100000, from: giver1 });
const initialBalance = await getBalance(web3, giver1); const initialBalance = await web3.eth.getBalance(giver1);
await vault.multiConfirm([2, 3, 4, 5, 6]); await vault.multiConfirm([2, 3, 4, 5, 6]);
const finalBalance = await getBalance(web3, giver1); const finalBalance = await web3.eth.getBalance(giver1);
const collected = web3.fromWei(finalBalance.sub(initialBalance)).toNumber(); const collected = utils.fromWei(utils.toBN(finalBalance).sub(utils.toBN(initialBalance)));
assert.equal(collected, 0.95); assert.equal(collected, 0.95);
}).timeout(8000); }).timeout(10000);
it('Should make a donation and create giver', async () => { it('Should make a donation and create giver', async () => {
const oldNPledges = await liquidPledging.numberOfPledges(); const oldNPledges = await liquidPledging.numberOfPledges();
const oldNManagers = await liquidPledging.numberOfPledgeManagers(); const oldNManagers = await liquidPledging.numberOfPledgeManagers();
await liquidPledging.donate(0, 1, { from: giver2, value: web3.toWei(1) }); await liquidPledging.donate(0, 1, { from: giver2, value: utils.toWei(1) });
const nPledges = await liquidPledging.numberOfPledges(); const nPledges = await liquidPledging.numberOfPledges();
assert.equal(nPledges.toNumber(), oldNPledges.toNumber() + 1); assert.equal(utils.toDecimal(nPledges), utils.toDecimal(oldNPledges) + 1);
const nManagers = await liquidPledging.numberOfPledgeManagers(); const nManagers = await liquidPledging.numberOfPledgeManagers();
assert.equal(nManagers.toNumber(), oldNManagers.toNumber() + 1); assert.equal(utils.toDecimal(nManagers), utils.toDecimal(oldNManagers) + 1);
const res = await liquidPledging.getPledgeManager(nManagers); const res = await liquidPledging.getPledgeManager(nManagers);
assert.equal(res[0], 0); // Giver assert.equal(res[0], 0); // Giver
assert.equal(res[1], giver2); assert.equal(res[1], giver2);