Merge branch 'master' into veto_delegation

This commit is contained in:
perissology 2017-11-10 07:33:12 -08:00
commit 1ed9c88a14
17 changed files with 347 additions and 170 deletions

1
.soliumignore Normal file
View File

@ -0,0 +1 @@
node_modules

22
.soliumrc.json Normal file
View File

@ -0,0 +1,22 @@
{
"custom-rules-filename": null,
"rules": {
"imports-on-top": true,
"variable-declarations": true,
"array-declarations": true,
"operator-whitespace": true,
"lbrace": true,
"mixedcase": true,
"camelcase": true,
"uppercase": true,
"no-with": true,
"no-empty-blocks": true,
"no-unused-vars": true,
"double-quotes": true,
"blank-lines": true,
"indentation": true,
"whitespace": true,
"deprecated-suicide": true,
"pragma-on-top": true
}
}

View File

@ -1,6 +1,6 @@
/* This is an autogenerated file. DO NOT EDIT MANUALLY */
exports.ILiquidPledgingPluginAbi = [{"constant":false,"inputs":[{"name":"pledgeManager","type":"uint64"},{"name":"pledgeFrom","type":"uint64"},{"name":"pledgeTo","type":"uint64"},{"name":"context","type":"uint64"},{"name":"amount","type":"uint256"}],"name":"afterTransfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"pledgeManager","type":"uint64"},{"name":"pledgeFrom","type":"uint64"},{"name":"pledgeTo","type":"uint64"},{"name":"context","type":"uint64"},{"name":"amount","type":"uint256"}],"name":"beforeTransfer","outputs":[{"name":"maxAllowed","type":"uint256"}],"payable":false,"type":"function"}]
exports.ILiquidPledgingPluginAbi = [{"constant":false,"inputs":[{"name":"pledgeManager","type":"uint64"},{"name":"pledgeFrom","type":"uint64"},{"name":"pledgeTo","type":"uint64"},{"name":"context","type":"uint64"},{"name":"amount","type":"uint256"}],"name":"afterTransfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"pledgeManager","type":"uint64"},{"name":"pledgeFrom","type":"uint64"},{"name":"pledgeTo","type":"uint64"},{"name":"context","type":"uint64"},{"name":"amount","type":"uint256"}],"name":"beforeTransfer","outputs":[{"name":"maxAllowed","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]
exports.ILiquidPledgingPluginByteCode = "0x"
exports._solcVersion = "0.4.15+commit.bbb8e64f.Emscripten.clang"
exports._solcVersion = "0.4.18+commit.9cf6e910.Emscripten.clang"
exports._sha256 = "0xdca1f6e8e45033d2b165904df14f39c39500c42395eabcc79e156346438f3259"

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -646,11 +646,13 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
idPledge = normalizePledge(idPledge);
Pledge storage n = findPledge(idPledge);
require(n.oldPledge != 0);
PledgeAdmin storage m = findAdmin(n.owner);
checkAdminOwner(m);
doTransfer(idPledge, n.oldPledge, amount);
uint64 oldPledge = getOldestPledgeNotCanceled(n.oldPledge);
doTransfer(idPledge, oldPledge, amount);
}

View File

@ -646,11 +646,13 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
idPledge = normalizePledge(idPledge);
Pledge storage n = findPledge(idPledge);
require(n.oldPledge != 0);
PledgeAdmin storage m = findAdmin(n.owner);
checkAdminOwner(m);
doTransfer(idPledge, n.oldPledge, amount);
uint64 oldPledge = getOldestPledgeNotCanceled(n.oldPledge);
doTransfer(idPledge, oldPledge, amount);
}

View File

@ -1,6 +1,6 @@
/* This is an autogenerated file. DO NOT EDIT MANUALLY */
exports.OwnedAbi = [{"constant":false,"inputs":[],"name":"acceptOwnership","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_newOwner","type":"address"}],"name":"changeOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"newOwner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}]
exports.OwnedByteCode = "0x6060604052341561000f57600080fd5b5b60008054600160a060020a03191633600160a060020a03161790555b5b6101d18061003c6000396000f300606060405263ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166379ba5097811461005e5780638da5cb5b14610073578063a6f9dae1146100a2578063d4ee1d90146100c3575b600080fd5b341561006957600080fd5b6100716100f2565b005b341561007e57600080fd5b61008661013f565b604051600160a060020a03909116815260200160405180910390f35b34156100ad57600080fd5b610071600160a060020a036004351661014e565b005b34156100ce57600080fd5b610086610196565b604051600160a060020a03909116815260200160405180910390f35b60015433600160a060020a0390811691161461010d57600080fd5b6001546000805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a039092169190911790555b565b600054600160a060020a031681565b60005433600160a060020a0390811691161461016957600080fd5b6001805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0383161790555b5b50565b600154600160a060020a0316815600a165627a7a72305820e68440031004354b3db9d740fb2b8896f5bedd56653beb65edd62e94e414a7ad0029"
exports._solcVersion = "0.4.15+commit.bbb8e64f.Emscripten.clang"
exports.OwnedAbi = [{"constant":false,"inputs":[],"name":"acceptOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_newOwner","type":"address"}],"name":"changeOwner","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"newOwner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]
exports.OwnedByteCode = "0x6060604052341561000f57600080fd5b60008054600160a060020a033316600160a060020a03199091161790556101bc8061003b6000396000f3006060604052600436106100615763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166379ba509781146100665780638da5cb5b1461007b578063a6f9dae1146100aa578063d4ee1d90146100c9575b600080fd5b341561007157600080fd5b6100796100dc565b005b341561008657600080fd5b61008e610128565b604051600160a060020a03909116815260200160405180910390f35b34156100b557600080fd5b610079600160a060020a0360043516610137565b34156100d457600080fd5b61008e610181565b60015433600160a060020a039081169116146100f757600080fd5b6001546000805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a03909216919091179055565b600054600160a060020a031681565b60005433600160a060020a0390811691161461015257600080fd5b6001805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0392909216919091179055565b600154600160a060020a0316815600a165627a7a7230582093915e2078cf23efe265b372c026c6cbc79ddd13f04e6e31d52efe53c70380790029"
exports._solcVersion = "0.4.18+commit.9cf6e910.Emscripten.clang"
exports._sha256 = "0xf61824cbb7f4765beceba9899e284825b9c60abba4179558d6b40e6976076b6b"

File diff suppressed because one or more lines are too long

View File

@ -22,7 +22,7 @@ contract LiquidPledging is LiquidPledgingBase {
/// @param idReceiver To whom it's transfered. Can be the same giver, another
/// giver, a delegate or a project
function donate(uint64 idGiver, uint64 idReceiver) payable {
function donate(uint64 idGiver, uint64 idReceiver) payable {
if (idGiver == 0) {
idGiver = addGiver('', '', 259200, ILiquidPledgingPlugin(0x0)); // default to 3 day commitTime
}
@ -180,7 +180,7 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
require(n.paymentState == PaymentState.Paying);
// Check the project is not canceled in the while.
require(getOldestPledgeNotCanceled(idPledge) == idPledge);
require(!isProjectCanceled(n.owner));
uint64 idNewPledge = findOrCreatePledge(
n.owner,
@ -232,11 +232,13 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
idPledge = normalizePledge(idPledge);
Pledge storage n = findPledge(idPledge);
require(n.oldPledge != 0);
PledgeAdmin storage m = findAdmin(n.owner);
checkAdminOwner(m);
doTransfer(idPledge, n.oldPledge, amount);
uint64 oldPledge = getOldestPledgeNotCanceled(n.oldPledge);
doTransfer(idPledge, oldPledge, amount);
}
@ -283,11 +285,9 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
}
}
function mNormalizePledge(uint[] pledges) returns(uint64) {
function mNormalizePledge(uint64[] pledges) {
for (uint i = 0; i < pledges.length; i++ ) {
uint64 idPledge = uint64( pledges[i] & (D64-1) );
normalizePledge(idPledge);
normalizePledge( pledges[i] );
}
}

View File

@ -92,10 +92,10 @@ contract Vault is Owned {
require(p.state == PaymentStatus.Pending);
p.state = PaymentStatus.Paid;
p.dest.transfer(p.amount); // only ETH denominated in wei
liquidPledging.confirmPayment(uint64(p.ref), p.amount);
p.dest.transfer(p.amount); // only ETH denominated in wei
ConfirmPayment(_idPayment);
}

89
test/CancelPledge.js Normal file
View File

@ -0,0 +1,89 @@
/* eslint-env mocha */
/* eslint-disable no-await-in-loop */
const TestRPC = require('ethereumjs-testrpc');
const Web3 = require('web3');
const chai = require('chai');
const liquidpledging = require('../index.js');
const assertFail = require('./helpers/assertFail');
const LiquidPledging = liquidpledging.LiquidPledgingMock;
const LiquidPledgingState = liquidpledging.LiquidPledgingState;
const Vault = liquidpledging.Vault;
const assert = chai.assert;
const printState = async (liquidPledgingState) => {
const st = await liquidPledgingState.getState();
console.log(JSON.stringify(st, null, 2));
};
describe('LiquidPledging cancelPledge normal scenario', function () {
this.timeout(0);
let testrpc;
let web3;
let accounts;
let liquidPledging;
let liquidPledgingState;
let vault;
let giver1;
let adminProject1;
let adminProject2;
before(async () => {
testrpc = TestRPC.server({
ws: true,
gasLimit: 5800000,
total_accounts: 10,
});
testrpc.listen(8546, '127.0.0.1');
web3 = new Web3('ws://localhost:8546');
accounts = await web3.eth.getAccounts();
giver1 = accounts[ 1 ];
adminProject1 = accounts[ 2 ];
adminProject2 = accounts[ 3 ];
});
after((done) => {
testrpc.close();
done();
});
it('Should deploy LiquidPledging contract', async () => {
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 add project and donate ', async () => {
await liquidPledging.addProject('Project1', 'URLProject1', adminProject1, 0, 0, '0x0', { from: adminProject1 });
await liquidPledging.donate(0, 1, { from: giver1, value: '1000', gas: 500000 });
const nAdmins = await liquidPledging.numberOfPledgeAdmins();
assert.equal(nAdmins, 2);
});
it('Should only allow pledge owner to cancel pledge', async () => {
await assertFail(async () => {
await liquidPledging.cancelPledge(2, 1000, { from: giver1, gas: 500000 });
});
});
it('Should cancel pledge and return to oldPledge', async () => {
await liquidPledging.cancelPledge(2, 1000, { from: adminProject1, gas: 500000 });
const st = await liquidPledgingState.getState();
assert.equal(st.pledges[1].amount, 1000);
assert.equal(st.pledges[2].amount, 0);
});
it('Should not allow to cancel pledge if oldPledge === 0', async () => {
await assertFail(async () => {
await liquidPledging.cancelPledge(1, 1000, { from: giver1, gas: 500000 });
});
})
});

View File

@ -16,7 +16,7 @@ const printState = async (liquidPledgingState) => {
console.log(JSON.stringify(st, null, 2));
};
describe('LiquidPledging test', function () {
describe('DelegationChain test', function () {
this.timeout(0);
let testrpc;
@ -64,8 +64,8 @@ describe('LiquidPledging test', function () {
});
it('Should add pledgeAdmins', async () => {
await liquidPledging.addGiver('Giver1', 'URLGiver1', 0, 0, { from: giver1 }); // pledgeAdmin 1
await liquidPledging.addDelegate('Delegate1', 'URLDelegate1', 0, 0, { from: delegate1 }); // pledgeAdmin 2
await liquidPledging.addGiver('Giver1', 'URLGiver1', 86400, 0, { from: giver1 }); // pledgeAdmin 1
await liquidPledging.addDelegate('Delegate1', 'URLDelegate1', 259200, 0, { from: delegate1 }); // pledgeAdmin 2
await liquidPledging.addDelegate('Delegate2', 'URLDelegate2', 0, 0, { from: delegate2 }); // pledgeAdmin 3
await liquidPledging.addDelegate('Delegate3', 'URLDelegate3', 0, 0, { from: delegate3 }); // pledgeAdmin 4
await liquidPledging.addProject('Project1', 'URLProject1', adminProject1, 0, 0, 0, { from: adminProject1 }); // pledgeAdmin 5
@ -76,11 +76,11 @@ describe('LiquidPledging test', function () {
});
it('Should allow previous delegate to transfer pledge', async () => {
await liquidPledging.donate(1, 2, {from: giver1, value: 1000});
await liquidPledging.donate(1, 2, {from: giver1, value: 1000, $extraGas: 50000});
// add delegate2 to chain
await liquidPledging.transfer(2, 2, 1000, 3, {from: delegate1});
await liquidPledging.transfer(2, 2, 1000, 3, {from: delegate1, $extraGas: 100000});
// delegate 1 transfer pledge back to self, thus undelegating delegate2
await liquidPledging.transfer(2, 3, 1000, 2, {from: delegate1, gas: 100000});
await liquidPledging.transfer(2, 3, 1000, 2, {from: delegate1, $extraGas: 100000});
const st = await liquidPledgingState.getState();
assert.equal(st.pledges[2].amount, 1000);
@ -149,20 +149,51 @@ describe('LiquidPledging test', function () {
assert.equal(st.pledges[2].delegates[0].id, 2);
});
it('delegation chain should remain the same when owner veto\'s delegation', async () => {
it('Should not append delegate on veto delegation', async () => {
// propose the delegation
await liquidPledging.transfer(2, 2, 1000, 5, { from: delegate1, $extraGas: 100000 });
const origPledge = await liquidPledging.getPledge(2);
assert.equal(origPledge.amount, '0');
// veto the delegation
await liquidPledging.transfer(1, 5, 1000, 2, { from: giver1, $extraGas: 100000 });
const currentPledge = await liquidPledging.getPledge(2);
assert.equal(currentPledge.amount, '1000');
assert.equal(currentPledge.nDelegates, 1);
});
it('Pledge should have longest commitTime in delegation chain', async () => {
// delegate1 add delegate2 to chain
await liquidPledging.transfer(2, 2, 1000, 3, {from: delegate1, $extraGas: 100000});
// set the time
const now = Math.floor(new Date().getTime() / 1000);
await liquidPledging.setMockedTime(now);
// propose project delegation
await liquidPledging.transfer(3, 3, 1000, 5, { from: delegate2, $extraGas: 100000 });
const pledge = await liquidPledging.getPledge(8);
assert.equal(pledge.commitTime, now + 259200); // 259200 is longest commitTime in delegationChain
});
it('delegation chain should remain the same when owner veto\'s delegation', async () => {
// delegate1 add delegate2 to chain
await liquidPledging.transfer(2, 2, 1000, 3, { from: delegate1, $extraGas: 100000 });
// delegate2 delegate to project1
await liquidPledging.transfer(3, 3, 1000, 5, {from: delegate2, $extraGas: 100000});
await liquidPledging.transfer(3, 3, 1000, 5, { from: delegate2, $extraGas: 100000 });
// owner veto delegation to project1
await liquidPledging.transfer(1, 8, 1000, 3, {from: giver1, $extraGas: 100000});
await liquidPledging.transfer(1, 8, 1000, 3, { from: giver1, $extraGas: 100000 });
printState(liquidPledgingState);
const st = await liquidPledgingState.getState();
assert.equal(st.pledges[8].amount, 0);
assert.equal(st.pledges[3].amount, 1000);
assert.equal(st.pledges[3].delegates.length, 2);
assert.equal(st.pledges[3].delegates[0].id, 2);
assert.equal(st.pledges[3].delegates[0].id, 3);
})
assert.equal(st.pledges[ 8 ].amount, 0);
assert.equal(st.pledges[ 3 ].amount, 1000);
assert.equal(st.pledges[ 3 ].delegates.length, 2);
assert.equal(st.pledges[ 3 ].delegates[ 0 ].id, 2);
assert.equal(st.pledges[ 3 ].delegates[ 0 ].id, 3);
});
});

View File

@ -295,12 +295,20 @@ describe('LiquidPledging test', function () {
assert.equal(utils.fromWei(st.pledges[12].amount), 0);
});
it('original owner should recover the remaining funds', async () => {
await liquidPledging.withdraw(1, utils.toWei(0.5), { from: giver1 });
await liquidPledging.withdraw(2, utils.toWei(0.31), { from: giver1 });
await liquidPledging.withdraw(4, utils.toWei(0.1), { $extraGas: 100000, from: giver1 });
const pledges = [
{ amount: utils.toWei(0.5), id: 1 },
{ amount: utils.toWei(0.31), id: 2 },
{ amount: utils.toWei(0.1), id: 4 },
{ amount: utils.toWei(0.03), id: 8 },
{ amount: utils.toWei(0.01), id: 9 },
];
await liquidPledging.withdraw(8, utils.toWei(0.03), { $extraGas: 100000, from: giver1 });
await liquidPledging.withdraw(9, utils.toWei(0.01), { $extraGas: 100000, from: giver1 });
// .substring is to remove the 0x prefix on the toHex result
const encodedPledges = pledges.map(p => {
return '0x' + utils.padLeft(utils.toHex(p.amount).substring(2), 48) + utils.padLeft(utils.toHex(p.id).substring(2), 16);
});
await liquidPledging.mWithdraw(encodedPledges, { from: giver1, $extraGas: 500000 });
const initialBalance = await web3.eth.getBalance(giver1);
await vault.multiConfirm([2, 3, 4, 5, 6]);

127
test/NormalizePledge.js Normal file
View File

@ -0,0 +1,127 @@
/* eslint-env mocha */
/* eslint-disable no-await-in-loop */
const TestRPC = require('ethereumjs-testrpc');
const Web3 = require('web3');
const chai = require('chai');
const liquidpledging = require('../index.js');
const LiquidPledging = liquidpledging.LiquidPledgingMock;
const LiquidPledgingState = liquidpledging.LiquidPledgingState;
const Vault = liquidpledging.Vault;
const assertFail = require('./helpers/assertFail');
const assert = chai.assert;
const printState = async (liquidPledgingState) => {
const st = await liquidPledgingState.getState();
console.log(JSON.stringify(st, null, 2));
};
describe('NormalizePledge test', function () {
this.timeout(0);
let testrpc;
let web3;
let accounts;
let liquidPledging;
let liquidPledgingState;
let vault;
let giver1;
let giver2;
let delegate1;
let delegate2;
let adminProject1;
let adminProject2;
before(async () => {
testrpc = TestRPC.server({
ws: true,
gasLimit: 5800000,
total_accounts: 10,
});
testrpc.listen(8546, '127.0.0.1');
web3 = new Web3('ws://localhost:8546');
accounts = await web3.eth.getAccounts();
giver1 = accounts[1];
delegate1 = accounts[2];
delegate2 = accounts[3];
adminProject1 = accounts[4];
adminProject2 = accounts[5];
giver2 = accounts[6];
});
after((done) => {
testrpc.close();
done();
});
it('Should deploy LiquidPledging contract', async () => {
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 add pledgeAdmins', async () => {
await liquidPledging.addGiver('Giver1', 'URLGiver1', 86400, 0, { from: giver1 }); // pledgeAdmin 1
await liquidPledging.addDelegate('Delegate1', 'URLDelegate1', 259200, 0, { from: delegate1 }); // pledgeAdmin 2
await liquidPledging.addDelegate('Delegate2', 'URLDelegate2', 0, 0, { from: delegate2 }); // pledgeAdmin 3
await liquidPledging.addProject('Project1', 'URLProject1', adminProject1, 0, 0, 0, { from: adminProject1 }); // pledgeAdmin 4
await liquidPledging.addProject('Project2', 'URLProject2', adminProject2, 0, 0, 0, { from: adminProject2 }); // pledgeAdmin 5
await liquidPledging.addGiver('Giver2', 'URLGiver2', 0, 0, { from: giver2 }); // pledgeAdmin 6
const nAdmins = await liquidPledging.numberOfPledgeAdmins();
assert.equal(nAdmins, 6);
});
it('Should commit pledges if commitTime has passed', async () => {
// commitTime 259200
await liquidPledging.donate(1, 2, {from: giver1, value: 1000, $extraGas: 50000});
// commitTime 86400
await liquidPledging.donate(1, 3, {from: giver1, value: 1000, $extraGas: 50000});
// commitTime 0
await liquidPledging.donate(6, 3, {from: giver2, value: 1000, $extraGas: 50000});
// set the time
const now = Math.floor(new Date().getTime() / 1000);
await liquidPledging.setMockedTime(now);
// delegate to project
await liquidPledging.transfer(2, 2, 1000, 4, {from: delegate1, $extraGas: 100000});
await liquidPledging.transfer(3, 3, 1000, 4, {from: delegate2, $extraGas: 100000});
await liquidPledging.transfer(3, 5, 1000, 4, {from: delegate2, $extraGas: 100000});
// advance the time
await liquidPledging.setMockedTime( now + 100000 );
await liquidPledging.mNormalizePledge([6, 7, 8], {$extraGas: 100000});
const st = await liquidPledgingState.getState();
assert.equal(st.pledges.length, 11);
assert.equal(st.pledges[6].amount, 1000);
assert.equal(st.pledges[9].amount, 1000);
assert.equal(st.pledges[9].owner, 4);
assert.equal(st.pledges[9].oldPledge, 3);
assert.equal(st.pledges[10].amount, 1000);
assert.equal(st.pledges[10].owner, 4);
assert.equal(st.pledges[10].oldPledge, 5);
});
it('Should transfer pledge to oldestPledgeNotCanceled', async () => {
await liquidPledging.transfer(4, 10, 1000, 5, {from: adminProject1, $extraGas: 100000});
// cancel projects
await liquidPledging.cancelProject(4, {from: adminProject1});
await liquidPledging.cancelProject(5, {from: adminProject2});
await liquidPledging.mNormalizePledge([9, 11], {$extraGas: 100000});
const st = await liquidPledgingState.getState();
assert.equal(st.pledges.length, 12);
assert.equal(st.pledges[3].amount, 1000);
assert.equal(st.pledges[5].amount, 1000);
assert.equal(st.pledges[9].amount, 0);
assert.equal(st.pledges[11].amount, 0);
})
});

View File

@ -1,105 +0,0 @@
/* eslint-env mocha */
/* eslint-disable no-await-in-loop */
const TestRPC = require('ethereumjs-testrpc');
const Web3 = require('web3');
const chai = require('chai');
const liquidpledging = require('../index.js');
const LiquidPledging = liquidpledging.LiquidPledgingMock;
const LiquidPledgingState = liquidpledging.LiquidPledgingState;
const Vault = liquidpledging.Vault;
const assert = chai.assert;
const printState = async (liquidPledgingState) => {
const st = await liquidPledgingState.getState();
console.log(JSON.stringify(st, null, 2));
};
describe('LiquidPledging test', function () {
this.timeout(0);
let testrpc;
let web3;
let accounts;
let liquidPledging;
let liquidPledgingState;
let vault;
let giver1;
let delegate1;
let adminProject1;
before(async () => {
testrpc = TestRPC.server({
ws: true,
gasLimit: 5800000,
total_accounts: 10,
});
testrpc.listen(8546, '127.0.0.1');
web3 = new Web3('ws://localhost:8546');
accounts = await web3.eth.getAccounts();
giver1 = accounts[1];
delegate1 = accounts[2];
adminProject1 = accounts[3];
});
after((done) => {
testrpc.close();
done();
});
it('Should deploy LiquidPledgin contract', async () => {
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 () => {
await liquidPledging.addDelegate('Delegate1', 'URLDelegate1', 0, 0, { from: delegate1 });
const nAdmins = await liquidPledging.numberOfPledgeAdmins();
assert.equal(nAdmins, 1);
const res = await liquidPledging.getPledgeAdmin(1);
assert.equal(res[0], 1); // Giver
assert.equal(res[1], delegate1);
assert.equal(res[2], 'Delegate1');
assert.equal(res[3], 'URLDelegate1');
assert.equal(res[4], 0);
});
it('Should make a donation and create giver', async () => {
await liquidPledging.donate(0, 1, { from: giver1, value: '1000', gas: 2000000 });
const nPledges = await liquidPledging.numberOfPledges();
assert.equal(nPledges, 2);
const nAdmins = await liquidPledging.numberOfPledgeAdmins();
assert.equal(nAdmins, 2);
const res = await liquidPledging.getPledgeAdmin(nAdmins);
assert.equal(res[0], 0); // Giver
assert.equal(res[1], giver1);
assert.equal(res[2], '');
assert.equal(res[3], '');
assert.equal(res[4], 259200); // default to 3 day commitTime
});
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 });
// await liquidPledging.transfer(1, 2, '1000', 5, { from: giver1, gas: 400000 });
const origPledge = await liquidPledging.getPledge(2);
assert.equal(origPledge.amount, '0');
// 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(liquidPledgingState);
assert.equal(currentPledge.amount, '1000');
assert.equal(currentPledge.nDelegates, 1);
});
});