make vault Escapable & add escapeFunds method

This commit is contained in:
perissology 2017-12-03 06:53:12 -08:00
parent 4039a7173b
commit 41f0eeb155
8 changed files with 135 additions and 56 deletions

View File

@ -7,7 +7,7 @@ pragma solidity ^0.4.11;
/// safety precaution, but once fully tested and optimized this contract will
/// be a safe place to store funds equipped with optional variable time delays
/// to allow for an optional escape hatch to be implemented
import "./Owned.sol";
import "../node_modules/giveth-common-contracts/contracts/Escapable.sol";
/// @dev `LiquidPledging` is a basic interface to allow the `LPVault` contract
/// to confirm and cancel payments in the `LiquidPledging` contract.
@ -17,9 +17,9 @@ contract LiquidPledging {
}
/// @dev `LPVault` is a higher level contract built off of the `Owned`
/// @dev `LPVault` is a higher level contract built off of the `Escapable`
/// contract that holds funds for the liquid pledging system.
contract LPVault is Owned {
contract LPVault is Escapable {
LiquidPledging public liquidPledging; // liquidPledging contract's address
bool public autoPay; // if false, payments will take 2 txs to be completed
@ -42,20 +42,18 @@ contract LPVault is Owned {
// @dev An array that contains all the payments for this LPVault
Payment[] public payments;
function Vault(address _escapeHatchCaller, address _escapeHatchDestination)
Escapable(_escapeHatchCaller, _escapeHatchDestination) public {
}
/// @dev `liquidPledging` is the only address that can call a function with
/// this modifier
modifier onlyLiquidPledging() {
require(msg.sender == address(liquidPledging));
_;
}
/// @dev USED FOR TESTING???
function VaultMock() public pure {
}
function () public payable {
}
function () public payable {}
/// @notice `setLiquidPledging` is used to attach a specific liquid pledging
/// instance to this LPvault. Keep in mind this isn't a single pledge but
@ -176,7 +174,29 @@ contract LPVault is Owned {
return payments.length;
}
/// Transfer eth or tokens to the escapeHatchDestination.
/// Used as a safety mechanism to prevent the vault from holding too much value
/// before being thoroughly battle-tested.
/// @param _token to transfer, use 0x0 for ether
/// @param _amount to transfer
function escapeFunds(address _token, uint _amount) public onlyOwner {
/// @dev Logic for ether
if (_token == 0x0) {
require(this.balance >= _amount);
escapeHatchDestination.transfer(_amount);
EscapeHatchCalled(_token, _amount);
return;
}
/// @dev Logic for tokens
ERC20 token = ERC20(_token);
uint balance = token.balanceOf(this);
require(balance >= _amount);
require(token.transfer(escapeHatchDestination, _amount));
EscapeFundsCalled(_token, _amount);
}
event ConfirmPayment(uint indexed idPayment);
event CancelPayment(uint indexed idPayment);
event AuthorizePayment(uint indexed idPayment, bytes32 indexed ref, address indexed dest, uint amount);
event EscapeFundsCalled(address _token, uint _amount);
}

View File

@ -1,41 +0,0 @@
pragma solidity ^0.4.11;
/// @dev `Owned` is a base level contract that assigns an `owner` that can be
/// later changed
contract Owned {
/// @dev `owner` is the only address that can call a function with this
/// modifier
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
address public owner;
/// @notice The Constructor assigns the account deploying the contract to be
/// the `owner`
function Owned() public {
owner = msg.sender;
}
address public newOwner;
/// @notice `owner` can step down and assign some other address to this role
/// but after this function is called the current owner still has ownership
/// powers in this contract; change of ownership is a 2 step process
/// @param _newOwner The address of the new owner. A simple contract with
/// the ability to accept ownership but the inability to do anything else
/// can be used to create an unowned contract to achieve decentralization
function changeOwner(address _newOwner) public onlyOwner {
newOwner = _newOwner;
}
/// @notice `newOwner` can accept ownership over this contract
function acceptOwnership() public {
require(msg.sender == newOwner);
owner = newOwner;
}
}

View File

@ -54,7 +54,7 @@ describe('LiquidPledging plugins test', function () {
});
it('Should deploy LiquidPledging contract', async function() {
vault = await Vault.new(web3);
vault = await Vault.new(web3, accounts[0], accounts[1]);
liquidPledging = await LiquidPledging.new(web3, vault.$address, { gas: 6500000 });
await vault.setLiquidPledging(liquidPledging.$address);
liquidPledgingState = new LiquidPledgingState(liquidPledging);

View File

@ -51,7 +51,7 @@ describe('LiquidPledging cancelPledge normal scenario', function () {
});
it('Should deploy LiquidPledging contract', async () => {
vault = await Vault.new(web3);
vault = await Vault.new(web3, accounts[0], accounts[1]);
liquidPledging = await LiquidPledging.new(web3, vault.$address, { gas: 5800000 });
await vault.setLiquidPledging(liquidPledging.$address);
liquidPledgingState = new LiquidPledgingState(liquidPledging);

View File

@ -57,7 +57,7 @@ describe('DelegationChain test', function () {
});
it('Should deploy LiquidPledging contract', async () => {
vault = await Vault.new(web3);
vault = await Vault.new(web3, accounts[0], accounts[1]);
liquidPledging = await LiquidPledging.new(web3, vault.$address, { gas: 5800000 });
await vault.setLiquidPledging(liquidPledging.$address);
liquidPledgingState = new LiquidPledgingState(liquidPledging);

View File

@ -62,7 +62,7 @@ describe('LiquidPledging test', function () {
});
it('Should deploy LiquidPledging contract', async () => {
vault = await LPVault.new(web3);
vault = await LPVault.new(web3, accounts[0], accounts[1]);
liquidPledging = await LiquidPledging.new(web3, vault.$address, { gas: 5800000 });
await vault.setLiquidPledging(liquidPledging.$address);
liquidPledgingState = new LiquidPledgingState(liquidPledging);

View File

@ -57,7 +57,7 @@ describe('NormalizePledge test', function () {
});
it('Should deploy LiquidPledging contract', async () => {
vault = await Vault.new(web3);
vault = await Vault.new(web3, accounts[0], accounts[1]);
liquidPledging = await LiquidPledging.new(web3, vault.$address, { gas: 5800000 });
await vault.setLiquidPledging(liquidPledging.$address);
liquidPledgingState = new LiquidPledgingState(liquidPledging);

100
test/Vault.js Normal file
View File

@ -0,0 +1,100 @@
/* 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;
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;
before(async () => {
testrpc = TestRPC.server({
ws: true,
gasLimit: 6500000,
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 ];
vaultOwner = accounts[ 3 ];
escapeHatchDestination = accounts[ 4 ];
escapeHatchCaller = accounts[ 5 ];
});
after((done) => {
testrpc.close();
done();
});
it('Should deploy Vault contract', async function () {
vault = await Vault.new(web3, escapeHatchCaller, escapeHatchDestination, { from: vaultOwner });
liquidPledging = await LiquidPledging.new(web3, vault.$address, { gas: 6500000 });
await vault.setLiquidPledging(liquidPledging.$address, { from: vaultOwner });
liquidPledgingState = new LiquidPledgingState(liquidPledging);
await liquidPledging.addGiver('Giver1', '', 0, '0x0', { from: giver1 });
await liquidPledging.addProject('Project1', '', adminProject1, 0, 0, '0x0', { from: adminProject1 });
const nAdmins = await liquidPledging.numberOfPledgeAdmins();
assert.equal(nAdmins, 2);
});
it('Should hold funds from liquidPledging', async function () {
await liquidPledging.donate(0, 2, { from: giver1, value: 10000 });
const balance = await web3.eth.getBalance(vault.$address);
assert.equal(10000, balance);
});
it('escapeFunds should fail', async function () {
// only vaultOwner can escapeFunds
await assertFail(async () => {
await vault.escapeFunds(0x0, 1000);
});
// can't send more then the balance
await assertFail(async () => {
await vault.escapeFunds(0x0, 11000, { from: vaultOwner });
});
});
it('escapeFunds should send funds to escapeHatchDestination', async function () {
const preBalance = await web3.eth.getBalance(escapeHatchDestination);
await vault.escapeFunds(0x0, 1000, { from: vaultOwner });
const vaultBalance = await web3.eth.getBalance(vault.$address);
assert.equal(9000, vaultBalance);
const expected = web3.utils.toBN(preBalance).add(web3.utils.toBN('1000'));
const postBalance = await web3.eth.getBalance(escapeHatchDestination);
assert.equal(expected, postBalance);
});
it('should')
});