2017-12-03 14:53:12 +00:00
|
|
|
/* eslint-env mocha */
|
|
|
|
/* eslint-disable no-await-in-loop */
|
2018-01-25 23:09:58 +00:00
|
|
|
const TestRPC = require("ganache-cli");
|
2017-12-03 14:53:12 +00:00
|
|
|
const Web3 = require('web3');
|
|
|
|
const chai = require('chai');
|
|
|
|
const assertFail = require('./helpers/assertFail');
|
2018-02-12 22:55:11 +00:00
|
|
|
const contracts = require("../build/contracts.js");
|
2017-12-03 14:53:12 +00:00
|
|
|
|
2018-02-12 22:55:11 +00:00
|
|
|
const LiquidPledgingState = require('../index').LiquidPledgingState;
|
2017-12-03 14:53:12 +00:00
|
|
|
const assert = chai.assert;
|
|
|
|
|
|
|
|
describe('Vault test', function () {
|
|
|
|
this.timeout(0);
|
|
|
|
|
|
|
|
let testrpc;
|
|
|
|
let web3;
|
|
|
|
let accounts;
|
|
|
|
let liquidPledging;
|
|
|
|
let liquidPledgingState;
|
|
|
|
let vault;
|
|
|
|
let vaultOwner;
|
|
|
|
let escapeHatchCaller;
|
|
|
|
let escapeHatchDestination;
|
|
|
|
let giver1;
|
|
|
|
let adminProject1;
|
2018-02-12 22:55:11 +00:00
|
|
|
let restrictedPaymentsConfirmer;
|
2018-02-16 21:44:44 +00:00
|
|
|
let token;
|
2017-12-03 14:53:12 +00:00
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
testrpc = TestRPC.server({
|
2018-01-22 19:00:30 +00:00
|
|
|
gasLimit: 6700000,
|
2017-12-03 14:53:12 +00:00
|
|
|
total_accounts: 10,
|
|
|
|
});
|
|
|
|
|
2018-02-12 23:24:26 +00:00
|
|
|
testrpc.listen(8545, '127.0.0.1');
|
2017-12-03 14:53:12 +00:00
|
|
|
|
2018-02-12 23:24:26 +00:00
|
|
|
web3 = new Web3('http://localhost:8545');
|
2017-12-03 14:53:12 +00:00
|
|
|
accounts = await web3.eth.getAccounts();
|
|
|
|
giver1 = accounts[ 1 ];
|
|
|
|
adminProject1 = accounts[ 2 ];
|
|
|
|
vaultOwner = accounts[ 3 ];
|
|
|
|
escapeHatchDestination = accounts[ 4 ];
|
|
|
|
escapeHatchCaller = accounts[ 5 ];
|
2018-02-12 22:55:11 +00:00
|
|
|
restrictedPaymentsConfirmer = accounts[ 6 ];
|
2017-12-03 14:53:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after((done) => {
|
|
|
|
testrpc.close();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should deploy Vault contract', async function () {
|
2018-02-12 22:55:11 +00:00
|
|
|
const baseVault = await contracts.LPVault.new(web3);
|
|
|
|
const baseLP = await contracts.LiquidPledgingMock.new(web3);
|
|
|
|
lpFactory = await contracts.LPFactory.new(web3, baseVault.$address, baseLP.$address);
|
2018-01-22 19:00:30 +00:00
|
|
|
|
2018-02-12 22:55:11 +00:00
|
|
|
const r = await lpFactory.newLP(accounts[0], escapeHatchDestination);
|
2018-01-22 19:00:30 +00:00
|
|
|
|
2018-02-12 22:55:11 +00:00
|
|
|
const vaultAddress = r.events.DeployVault.returnValues.vault;
|
|
|
|
vault = new contracts.LPVault(web3, vaultAddress);
|
|
|
|
|
|
|
|
const lpAddress = r.events.DeployLiquidPledging.returnValues.liquidPledging;
|
|
|
|
liquidPledging = new contracts.LiquidPledgingMock(web3, lpAddress);
|
2018-01-22 19:00:30 +00:00
|
|
|
|
2017-12-03 14:53:12 +00:00
|
|
|
liquidPledgingState = new LiquidPledgingState(liquidPledging);
|
|
|
|
|
2018-02-12 22:55:11 +00:00
|
|
|
// set permissions
|
|
|
|
const kernel = new contracts.Kernel(web3, await liquidPledging.kernel());
|
|
|
|
acl = new contracts.ACL(web3, await kernel.acl());
|
|
|
|
await acl.createPermission(accounts[0], vault.$address, await vault.CANCEL_PAYMENT_ROLE(), accounts[0], { $extraGas: 200000 });
|
|
|
|
await acl.createPermission(accounts[0], vault.$address, await vault.CONFIRM_PAYMENT_ROLE(), accounts[0], { $extraGas: 200000 });
|
|
|
|
await acl.grantPermission(escapeHatchCaller, vault.$address, await vault.ESCAPE_HATCH_CALLER_ROLE(), {$extraGas: 200000});
|
|
|
|
await acl.revokePermission(accounts[0], vault.$address, await vault.ESCAPE_HATCH_CALLER_ROLE(), {$extraGas: 200000});
|
|
|
|
|
|
|
|
await liquidPledging.addGiver('Giver1', '', 0, '0x0', { from: giver1, $extraGas: 100000 });
|
|
|
|
await liquidPledging.addProject('Project1', '', adminProject1, 0, 0, '0x0', { from: adminProject1, $extraGas: 100000 });
|
2017-12-03 14:53:12 +00:00
|
|
|
|
|
|
|
const nAdmins = await liquidPledging.numberOfPledgeAdmins();
|
|
|
|
assert.equal(nAdmins, 2);
|
2018-02-16 21:44:44 +00:00
|
|
|
|
|
|
|
token = await contracts.StandardToken.new(web3);
|
|
|
|
await token.mint(giver1, web3.utils.toWei('1000'));
|
|
|
|
await token.approve(liquidPledging.$address, "0xFFFFFFFFFFFFFFFF", {from: giver1});
|
2017-12-03 14:53:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should hold funds from liquidPledging', async function () {
|
2018-02-16 21:44:44 +00:00
|
|
|
await liquidPledging.addGiverAndDonate(2, token.$address, 10000, { from: giver1, $extraGas: 100000 });
|
2017-12-03 14:53:12 +00:00
|
|
|
|
2018-02-16 21:44:44 +00:00
|
|
|
const balance = await token.balanceOf(vault.$address);
|
2017-12-03 14:53:12 +00:00
|
|
|
assert.equal(10000, balance);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('escapeFunds should fail', async function () {
|
|
|
|
// only vaultOwner can escapeFunds
|
2018-01-25 23:09:58 +00:00
|
|
|
await assertFail(vault.escapeFunds(0x0, 1000, {gas: 4000000}));
|
2017-12-03 14:53:12 +00:00
|
|
|
|
|
|
|
// can't send more then the balance
|
2018-01-25 23:09:58 +00:00
|
|
|
await assertFail(vault.escapeFunds(0x0, 11000, { from: vaultOwner, gas: 4000000 }));
|
2017-12-03 14:53:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('escapeFunds should send funds to escapeHatchDestination', async function () {
|
2018-02-16 21:44:44 +00:00
|
|
|
const preBalance = await token.balanceOf(escapeHatchDestination);
|
2017-12-03 14:53:12 +00:00
|
|
|
|
2018-02-16 21:44:44 +00:00
|
|
|
assertFail(vault.escapeFunds(0x0, 1000, { from: escapeHatchCaller, gas: 1000000}));
|
2017-12-03 14:53:12 +00:00
|
|
|
|
2018-02-16 21:44:44 +00:00
|
|
|
await vault.escapeFunds(token.$address, 1000, { from: escapeHatchCaller, $extraGas: 200000 });
|
|
|
|
|
|
|
|
const vaultBalance = await token.balanceOf(vault.$address);
|
2017-12-03 14:53:12 +00:00
|
|
|
assert.equal(9000, vaultBalance);
|
|
|
|
|
2018-01-25 23:09:58 +00:00
|
|
|
const expected = web3.utils.toBN(preBalance).add(web3.utils.toBN('1000')).toString();
|
2018-02-16 21:44:44 +00:00
|
|
|
const postBalance = await token.balanceOf(escapeHatchDestination);
|
2017-12-03 14:53:12 +00:00
|
|
|
|
|
|
|
assert.equal(expected, postBalance);
|
2018-02-12 22:55:11 +00:00
|
|
|
|
2018-02-16 21:44:44 +00:00
|
|
|
await token.transfer(vault.$address, 1000, {from: escapeHatchDestination, $extraGas: 200000});
|
2018-02-12 22:55:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should restrict confirm payment to payments under specified amount', async function () {
|
|
|
|
await liquidPledging.withdraw(2, 300, {from: adminProject1, $extraGas: 200000});
|
|
|
|
await liquidPledging.withdraw(2, 700, {from: adminProject1, $extraGas: 200000});
|
|
|
|
|
|
|
|
// set permission for 2nd param (p.amount) <= 300
|
|
|
|
await acl.grantPermissionP(restrictedPaymentsConfirmer, vault.$address, await vault.CONFIRM_PAYMENT_ROLE(), ["0x010600000000000000000000000000000000000000000000000000000000012c"], {$extraGas: 200000});
|
|
|
|
|
|
|
|
assertFail(vault.confirmPayment(1, { from: restrictedPaymentsConfirmer, gas: 4000000 }));
|
|
|
|
await vault.confirmPayment(0, { from: restrictedPaymentsConfirmer, $extraGas: 200000 });
|
2017-12-03 14:53:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|