Merge branch 'escapable' into v1

This commit is contained in:
perissology 2017-12-19 16:43:44 -10:00
commit 6bd39d0eba
13 changed files with 173 additions and 79 deletions

View File

@ -12,7 +12,7 @@
"no-with": true,
"no-empty-blocks": true,
"no-unused-vars": true,
"double-quotes": true,
"quotes": true,
"blank-lines": true,
"indentation": true,
"whitespace": true,

View File

@ -27,7 +27,7 @@ pragma solidity ^0.4.11;
/// be a safe place to store funds equipped with optional variable time delays
/// to allow for an optional escapeHatch to be implemented in case of issues;
/// future versions of this contract will be enabled for tokens
import "./Owned.sol";// TODO IMPORT ESCAPABLE :-D...............................???????????????????????????/
import "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.
@ -37,9 +37,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 {// TODO NEEDS TO BE ESCAPABLE!!! AND WE NEED TO ADD A `WITHDRAW PARTIAL` FUNCTION????????????????
contract LPVault is Escapable {
LiquidPledging public liquidPledging; // LiquidPledging contract's address
bool public autoPay; // If false, payments will take 2 txs to be completed
@ -62,21 +62,21 @@ contract LPVault is Owned {// TODO NEEDS TO BE ESCAPABLE!!! AND WE NEED TO ADD A
// @dev An array that contains all the payments for this LPVault
Payment[] public payments;
function LPVault(address _escapeHatchCaller, address _escapeHatchDestination)
Escapable(_escapeHatchCaller, _escapeHatchDestination) public
{
}
/// @dev The attached `LiquidPledging` contract is the only address that can
/// call a function with this modifier
modifier onlyLiquidPledging() {
require(msg.sender == address(liquidPledging));
_;
}
/// @dev Used for testing... SHOULD BE REMOVED FOR MAINNET LAUNCH OR MAYBE NOT????????????????????....................
function VaultMock() public pure {
}
/// @dev The fall back function allows ETH to be deposited into the LPVault
/// through a simple send
function () public payable {
}
function () public payable {}
/// @notice `onlyOwner` used to attach a specific liquidPledging instance
/// to this LPvault; keep in mind that once a liquidPledging contract is
@ -108,7 +108,9 @@ contract LPVault is Owned {// TODO NEEDS TO BE ESCAPABLE!!! AND WE NEED TO ADD A
function authorizePayment(
bytes32 _ref,
address _dest,
uint _amount ) public onlyLiquidPledging returns (uint) {
uint _amount
) public onlyLiquidPledging returns (uint)
{
uint idPayment = payments.length;
payments.length ++;
payments[idPayment].state = PaymentStatus.Pending;
@ -147,7 +149,7 @@ contract LPVault is Owned {// TODO NEEDS TO BE ESCAPABLE!!! AND WE NEED TO ADD A
p.dest.transfer(p.amount); // Transfers ETH denominated in wei
ConfirmPayment(_idPayment);
ConfirmPayment(_idPayment, p.ref);
}
/// @notice When `autopay` is `false` and after a payment has been authorized
@ -168,7 +170,7 @@ contract LPVault is Owned {// TODO NEEDS TO BE ESCAPABLE!!! AND WE NEED TO ADD A
liquidPledging.cancelPayment(uint64(p.ref), p.amount);
CancelPayment(_idPayment);
CancelPayment(_idPayment, p.ref);
}
@ -193,9 +195,31 @@ contract LPVault is Owned {// TODO NEEDS TO BE ESCAPABLE!!! AND WE NEED TO ADD A
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 AutoPaySet();
event ConfirmPayment(uint indexed idPayment);
event CancelPayment(uint indexed idPayment);
event EscapeFundsCalled(address token, uint amount);
event ConfirmPayment(uint indexed idPayment, bytes32 indexed ref);
event CancelPayment(uint indexed idPayment, bytes32 indexed ref);
event AuthorizePayment(
uint indexed idPayment,
bytes32 indexed ref,

View File

@ -37,7 +37,12 @@ contract LiquidPledging is LiquidPledgingBase {
/// @dev This constructor also calls the constructor
/// for `LiquidPledgingBase`
/// @param _vault The vault where ETH backing this pledge is stored
function LiquidPledging(address _vault) LiquidPledgingBase(_vault) {
function LiquidPledging(
address _vault,
address _escapeHatchCaller,
address _escapeHatchDestination
) LiquidPledgingBase(_vault, _escapeHatchCaller, _escapeHatchDestination) {
}
/// @notice This is how value enters the system and how pledges are created;
@ -518,7 +523,7 @@ contract LiquidPledging is LiquidPledgingBase {
) internal {
Pledge storage p = findPledge(idPledge);
require(getPledgeLevel(p) < MAX_SUBPROJECT_LEVEL);
require(getPledgeLevel(p) < MAX_INTERPROJECT_LEVEL);
require(!isProjectCanceled(idReceiver));
uint64 toPledge = findOrCreatePledge(
@ -605,7 +610,7 @@ contract LiquidPledging is LiquidPledgingBase {
p = findPledge(idPledge);
}
toPledge = getOldestPledgeNotCanceled(idPledge);// TODO toPledge is pledge defined
toPledge = getOldestPledgeNotCanceled(idPledge);
if (toPledge != idPledge) {
doTransfer(idPledge, toPledge, p.amount);
}
@ -734,11 +739,11 @@ contract LiquidPledging is LiquidPledgingBase {
/// @notice `callPlugins` calls `callPluginsPledge` once for the transfer
/// context and once for the receiving context. The aggregated
/// allowed amount is then returned.
/// @param before This toggle determines whether the plugin call is occuring
/// @param before This toggle determines whether the plugin call is occurring
/// before or after a transfer.
/// @param fromPledge This is the Id from which value is being transfered.
/// @param toPledge This is the Id that value is being transfered to.
/// @param amount The amount of value that is being transfered.
/// @param fromPledge This is the Id from which value is being transferred.
/// @param toPledge This is the Id that value is being transferred to.
/// @param amount The amount of value that is being transferred.
function callPlugins(
bool before,
uint64 fromPledge,

View File

@ -19,7 +19,7 @@ pragma solidity ^0.4.11;
*/
import "./ILiquidPledgingPlugin.sol";
import "../node_modules/giveth-common-contracts/contracts/Owned.sol";
import "giveth-common-contracts/contracts/Escapable.sol";
/// @dev This is an interface for `LPVault` which serves as a secure storage for
/// the ETH that backs the Pledges, only after `LiquidPledging` authorizes
@ -32,10 +32,10 @@ interface LPVault {
/// @dev `LiquidPledgingBase` is the base level contract used to carry out
/// liquidPledging's most basic functions, mostly handling and searching the
/// data structures
contract LiquidPledgingBase is Owned {
contract LiquidPledgingBase is Escapable {
// Limits inserted to prevent large loops that could prevent canceling
uint constant MAX_DELEGATES = 20;
uint constant MAX_DELEGATES = 10;
uint constant MAX_SUBPROJECT_LEVEL = 20;
uint constant MAX_INTERPROJECT_LEVEL = 20;
@ -99,7 +99,11 @@ contract LiquidPledgingBase is Owned {
/// @notice The Constructor creates `LiquidPledgingBase` on the blockchain
/// @param _vault The vault where the ETH backing the pledges is stored
function LiquidPledgingBase(address _vault) {
function LiquidPledgingBase(
address _vault,
address _escapeHatchCaller,
address _escapeHatchDestination
) Escapable(_escapeHatchCaller, _escapeHatchDestination) public {
admins.length = 1; // we reserve the 0 admin
pledges.length = 1; // we reserve the 0 pledge
vault = LPVault(_vault); // Assigns the specified vault

View File

@ -29,7 +29,11 @@ contract LiquidPledgingMock is LiquidPledging {
/// @dev `LiquidPledgingMock` creates a standard `LiquidPledging`
/// instance and sets the mocked time to the current blocktime.
/// @param _vault The vault where ETH backing this pledge is stored
function LiquidPledgingMock(address _vault) LiquidPledging(_vault) {
function LiquidPledgingMock(
address _vault,
address _escapeHatchCaller,
address _escapeHatchDestination
) LiquidPledging(_vault, _escapeHatchCaller, _escapeHatchDestination) {
mock_time = now;
}

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

@ -44,7 +44,7 @@
"lerna": "^2.2.0",
"random-bytes": "^1.0.0",
"mocha": "^3.5.0",
"solcpiler": "0.0.7",
"solcpiler": "0.0.10",
"web3": "1.0.0-beta.24"
},
"homepage": "https://github.com/Giveth/liquidpledging#readme",

View File

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

View File

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

View File

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

View File

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

View File

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

98
test/Vault.js Normal file
View File

@ -0,0 +1,98 @@
/* 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.LPVault;
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, escapeHatchCaller, escapeHatchDestination, { 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);
});
});