2018-02-13 00:24:19 +00:00
|
|
|
pragma solidity ^0.4.18;
|
2017-06-26 17:54:28 +00:00
|
|
|
|
2017-12-04 01:21:09 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017, Jordi Baylina
|
|
|
|
Contributors: RJ Ewing, Griff Green, Arthur Lunn
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// @dev This contract holds ether securely for liquid pledging systems; for
|
|
|
|
/// this iteration the funds will come often be escaped to the Giveth Multisig
|
|
|
|
/// (safety precaution), but once fully tested and optimized this contract will
|
2017-09-29 10:16:53 +00:00
|
|
|
/// be a safe place to store funds equipped with optional variable time delays
|
2017-12-05 21:55:14 +00:00
|
|
|
/// to allow for an optional escapeHatch to be implemented in case of issues;
|
|
|
|
/// future versions of this contract will be enabled for tokens
|
2018-02-10 14:14:52 +00:00
|
|
|
import "./EscapableApp.sol";
|
2018-02-20 16:12:01 +00:00
|
|
|
import "./LiquidPledgingACLHelpers.sol";
|
2018-02-16 21:44:44 +00:00
|
|
|
import "giveth-common-contracts/contracts/ERC20.sol";
|
2017-12-05 19:58:20 +00:00
|
|
|
|
2017-11-01 21:22:41 +00:00
|
|
|
/// @dev `LiquidPledging` is a basic interface to allow the `LPVault` contract
|
2017-10-31 21:00:34 +00:00
|
|
|
/// to confirm and cancel payments in the `LiquidPledging` contract.
|
2018-02-10 14:14:52 +00:00
|
|
|
contract ILiquidPledging {
|
2017-12-05 20:47:38 +00:00
|
|
|
function confirmPayment(uint64 idPledge, uint amount) public;
|
|
|
|
function cancelPayment(uint64 idPledge, uint amount) public;
|
2017-07-13 17:12:45 +00:00
|
|
|
}
|
2017-06-26 17:54:28 +00:00
|
|
|
|
2017-12-03 14:53:12 +00:00
|
|
|
/// @dev `LPVault` is a higher level contract built off of the `Escapable`
|
2017-10-04 11:02:19 +00:00
|
|
|
/// contract that holds funds for the liquid pledging system.
|
2018-02-20 16:12:01 +00:00
|
|
|
contract LPVault is EscapableApp, LiquidPledgingACLHelpers {
|
2017-06-26 17:54:28 +00:00
|
|
|
|
2018-02-12 22:55:11 +00:00
|
|
|
bytes32 constant public CONFIRM_PAYMENT_ROLE = keccak256("CONFIRM_PAYMENT_ROLE");
|
|
|
|
bytes32 constant public CANCEL_PAYMENT_ROLE = keccak256("CANCEL_PAYMENT_ROLE");
|
|
|
|
bytes32 constant public AUTHORIZE_PAYMENT_ROLE = keccak256("AUTHORIZE_PAYMENT_ROLE");
|
|
|
|
bytes32 constant public SET_AUTOPAY_ROLE = keccak256("SET_AUTOPAY_ROLE");
|
2018-02-10 14:14:52 +00:00
|
|
|
|
|
|
|
event AutoPaySet(bool autoPay);
|
|
|
|
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,
|
|
|
|
address indexed dest,
|
2018-02-16 21:44:44 +00:00
|
|
|
address token,
|
2018-02-10 14:14:52 +00:00
|
|
|
uint amount
|
|
|
|
);
|
2017-06-26 17:54:28 +00:00
|
|
|
|
2017-10-04 11:02:19 +00:00
|
|
|
enum PaymentStatus {
|
2017-12-04 01:21:09 +00:00
|
|
|
Pending, // When the payment is awaiting confirmation
|
2017-12-05 21:47:01 +00:00
|
|
|
Paid, // When the payment has been sent
|
2017-12-04 01:21:09 +00:00
|
|
|
Canceled // When the payment will never be sent
|
2017-06-26 17:54:28 +00:00
|
|
|
}
|
2018-02-10 14:14:52 +00:00
|
|
|
|
2017-09-29 10:16:53 +00:00
|
|
|
/// @dev `Payment` is a public structure that describes the details of
|
|
|
|
/// each payment the `ref` param makes it easy to track the movements of
|
|
|
|
/// funds transparently by its connection to other `Payment` structs
|
2017-06-26 17:54:28 +00:00
|
|
|
struct Payment {
|
2017-09-29 10:16:53 +00:00
|
|
|
bytes32 ref; // an input that references details from other contracts
|
|
|
|
address dest; // recipient of the ETH
|
2018-02-16 21:44:44 +00:00
|
|
|
PaymentStatus state; // Pending, Paid or Canceled
|
|
|
|
address token;
|
2017-09-29 10:16:53 +00:00
|
|
|
uint amount; // amount of ETH (in wei) to be sent
|
2017-06-26 17:54:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-10 14:14:52 +00:00
|
|
|
bool public autoPay; // If false, payments will take 2 txs to be completed
|
|
|
|
|
2017-11-01 21:22:41 +00:00
|
|
|
// @dev An array that contains all the payments for this LPVault
|
2017-06-26 17:54:28 +00:00
|
|
|
Payment[] public payments;
|
2018-02-10 14:14:52 +00:00
|
|
|
ILiquidPledging public liquidPledging;
|
2017-12-03 14:53:12 +00:00
|
|
|
|
2017-12-04 01:21:09 +00:00
|
|
|
/// @dev The attached `LiquidPledging` contract is the only address that can
|
|
|
|
/// call a function with this modifier
|
2017-06-26 17:54:28 +00:00
|
|
|
modifier onlyLiquidPledging() {
|
|
|
|
require(msg.sender == address(liquidPledging));
|
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2018-02-12 22:55:11 +00:00
|
|
|
function initialize(address _escapeHatchDestination) onlyInit public {
|
2018-02-10 14:14:52 +00:00
|
|
|
require(false); // overload the EscapableApp
|
2018-02-13 00:24:19 +00:00
|
|
|
_escapeHatchDestination;
|
2018-02-10 14:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @param _liquidPledging
|
|
|
|
/// @param _escapeHatchDestination The address of a safe location (usu a
|
|
|
|
/// Multisig) to send the ether held in this contract; if a neutral address
|
|
|
|
/// is required, the WHG Multisig is an option:
|
|
|
|
/// 0x8Ff920020c8AD673661c8117f2855C384758C572
|
|
|
|
function initialize(address _liquidPledging, address _escapeHatchDestination) onlyInit external {
|
2018-02-12 22:55:11 +00:00
|
|
|
super.initialize(_escapeHatchDestination);
|
2018-02-10 14:14:52 +00:00
|
|
|
|
2018-02-12 22:55:11 +00:00
|
|
|
require(_liquidPledging != 0x0);
|
2018-02-10 14:14:52 +00:00
|
|
|
liquidPledging = ILiquidPledging(_liquidPledging);
|
|
|
|
}
|
|
|
|
|
2017-12-04 01:21:09 +00:00
|
|
|
/// @notice Used to decentralize, toggles whether the LPVault will
|
|
|
|
/// automatically confirm a payment after the payment has been authorized
|
|
|
|
/// @param _automatic If true, payments will confirm instantly, if false
|
|
|
|
/// the training wheels are put on and the owner must manually approve
|
|
|
|
/// every payment
|
2018-02-20 16:12:01 +00:00
|
|
|
function setAutopay(bool _automatic) external authP(SET_AUTOPAY_ROLE, arr(_automatic)) {
|
2017-06-26 17:54:28 +00:00
|
|
|
autoPay = _automatic;
|
2018-02-10 14:14:52 +00:00
|
|
|
AutoPaySet(autoPay);
|
2017-06-26 17:54:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-10 14:14:52 +00:00
|
|
|
/// @notice If `autoPay == true` the transfer happens automatically `else` the `owner`
|
2017-12-04 01:21:09 +00:00
|
|
|
/// must call `confirmPayment()` for a transfer to occur (training wheels);
|
|
|
|
/// either way, a new payment is added to `payments[]`
|
2017-12-05 21:29:10 +00:00
|
|
|
/// @param _ref References the payment will normally be the pledgeID
|
2017-12-04 01:21:09 +00:00
|
|
|
/// @param _dest The address that payments will be sent to
|
|
|
|
/// @param _amount The amount that the payment is being authorized for
|
|
|
|
/// @return idPayment The id of the payment (needed by the owner to confirm)
|
2017-11-01 21:03:03 +00:00
|
|
|
function authorizePayment(
|
|
|
|
bytes32 _ref,
|
|
|
|
address _dest,
|
2018-02-16 21:44:44 +00:00
|
|
|
address _token,
|
2017-12-05 19:58:20 +00:00
|
|
|
uint _amount
|
2018-02-10 14:14:52 +00:00
|
|
|
) external authP(AUTHORIZE_PAYMENT_ROLE, arr(_dest, _amount)) returns (uint)
|
2017-12-05 19:58:20 +00:00
|
|
|
{
|
2017-06-26 17:54:28 +00:00
|
|
|
uint idPayment = payments.length;
|
|
|
|
payments.length ++;
|
2017-10-04 11:02:19 +00:00
|
|
|
payments[idPayment].state = PaymentStatus.Pending;
|
2017-06-26 17:54:28 +00:00
|
|
|
payments[idPayment].ref = _ref;
|
|
|
|
payments[idPayment].dest = _dest;
|
2018-02-16 21:44:44 +00:00
|
|
|
payments[idPayment].token = _token;
|
2017-06-26 17:54:28 +00:00
|
|
|
payments[idPayment].amount = _amount;
|
|
|
|
|
2018-02-16 21:44:44 +00:00
|
|
|
AuthorizePayment(idPayment, _ref, _dest, _token, _amount);
|
2017-06-26 17:54:28 +00:00
|
|
|
|
2017-12-03 04:04:36 +00:00
|
|
|
if (autoPay) {
|
2018-02-13 00:24:19 +00:00
|
|
|
_doConfirmPayment(idPayment);
|
2017-12-03 04:04:36 +00:00
|
|
|
}
|
2017-06-26 17:54:28 +00:00
|
|
|
|
|
|
|
return idPayment;
|
|
|
|
}
|
|
|
|
|
2017-12-04 01:21:09 +00:00
|
|
|
/// @notice Allows the owner to confirm payments; since
|
|
|
|
/// `authorizePayment` is the only way to populate the `payments[]` array
|
2017-10-31 21:00:34 +00:00
|
|
|
/// this is generally used when `autopay` is `false` after a payment has
|
2017-12-04 01:21:09 +00:00
|
|
|
/// has been authorized
|
2017-10-31 21:00:34 +00:00
|
|
|
/// @param _idPayment Array lookup for the payment.
|
2018-02-10 14:14:52 +00:00
|
|
|
function confirmPayment(uint _idPayment) public {
|
2018-02-13 00:24:19 +00:00
|
|
|
_doConfirmPayment(_idPayment);
|
2017-06-26 17:54:28 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 20:49:18 +00:00
|
|
|
/// @notice When `autopay` is `false` and after a payment has been authorized
|
2017-10-31 21:00:34 +00:00
|
|
|
/// to allow the owner to cancel a payment instead of confirming it.
|
|
|
|
/// @param _idPayment Array lookup for the payment.
|
2018-02-10 14:14:52 +00:00
|
|
|
function cancelPayment(uint _idPayment) public {
|
2018-02-13 00:24:19 +00:00
|
|
|
_doCancelPayment(_idPayment);
|
2017-06-26 17:54:28 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 01:21:09 +00:00
|
|
|
/// @notice `onlyOwner` An efficient way to confirm multiple payments
|
2017-10-31 21:00:34 +00:00
|
|
|
/// @param _idPayments An array of multiple payment ids
|
2018-02-10 14:14:52 +00:00
|
|
|
function multiConfirm(uint[] _idPayments) external {
|
2017-10-25 17:37:21 +00:00
|
|
|
for (uint i = 0; i < _idPayments.length; i++) {
|
2018-02-13 00:24:19 +00:00
|
|
|
_doConfirmPayment(_idPayments[i]);
|
2017-06-26 17:54:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 01:21:09 +00:00
|
|
|
/// @notice `onlyOwner` An efficient way to cancel multiple payments
|
2017-10-31 21:00:34 +00:00
|
|
|
/// @param _idPayments An array of multiple payment ids
|
2018-02-10 14:14:52 +00:00
|
|
|
function multiCancel(uint[] _idPayments) external {
|
2017-10-25 17:37:21 +00:00
|
|
|
for (uint i = 0; i < _idPayments.length; i++) {
|
2018-02-13 00:24:19 +00:00
|
|
|
_doCancelPayment(_idPayments[i]);
|
2017-06-26 17:54:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-16 21:44:44 +00:00
|
|
|
/// Transfer tokens to the escapeHatchDestination.
|
2017-12-03 14:53:12 +00:00
|
|
|
/// Used as a safety mechanism to prevent the vault from holding too much value
|
|
|
|
/// before being thoroughly battle-tested.
|
2018-02-16 21:44:44 +00:00
|
|
|
/// @param _token to transfer
|
2017-12-03 14:53:12 +00:00
|
|
|
/// @param _amount to transfer
|
2018-02-10 14:14:52 +00:00
|
|
|
function escapeFunds(address _token, uint _amount) public authP(ESCAPE_HATCH_CALLER_ROLE, arr(_token)) {
|
2018-02-16 21:44:44 +00:00
|
|
|
require(_token != 0x0);
|
2017-12-03 14:53:12 +00:00
|
|
|
ERC20 token = ERC20(_token);
|
|
|
|
uint balance = token.balanceOf(this);
|
|
|
|
require(balance >= _amount);
|
|
|
|
require(token.transfer(escapeHatchDestination, _amount));
|
|
|
|
EscapeFundsCalled(_token, _amount);
|
|
|
|
}
|
|
|
|
|
2018-02-10 14:14:52 +00:00
|
|
|
/// @return The total number of payments that have ever been authorized
|
|
|
|
function nPayments() public view returns (uint) {
|
|
|
|
return payments.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @notice Transfers ETH according to the data held within the specified
|
|
|
|
/// payment id (internal function)
|
|
|
|
/// @param _idPayment id number for the payment about to be fulfilled
|
2018-02-13 00:24:19 +00:00
|
|
|
function _doConfirmPayment(uint _idPayment) internal {
|
2018-02-10 14:14:52 +00:00
|
|
|
require(_idPayment < payments.length);
|
|
|
|
Payment storage p = payments[_idPayment];
|
|
|
|
require(p.state == PaymentStatus.Pending);
|
|
|
|
require(canPerform(msg.sender, CONFIRM_PAYMENT_ROLE, arr(_idPayment, p.amount)));
|
|
|
|
|
|
|
|
p.state = PaymentStatus.Paid;
|
|
|
|
liquidPledging.confirmPayment(uint64(p.ref), p.amount);
|
|
|
|
|
2018-02-16 21:44:44 +00:00
|
|
|
ERC20 token = ERC20(p.token);
|
|
|
|
require(token.transfer(p.dest, p.amount)); // Transfers token to dest
|
2018-02-10 14:14:52 +00:00
|
|
|
|
|
|
|
ConfirmPayment(_idPayment, p.ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @notice Cancels a pending payment (internal function)
|
|
|
|
/// @param _idPayment id number for the payment
|
2018-02-13 00:24:19 +00:00
|
|
|
function _doCancelPayment(uint _idPayment) internal authP(CANCEL_PAYMENT_ROLE, arr(_idPayment)) {
|
2018-02-10 14:14:52 +00:00
|
|
|
require(_idPayment < payments.length);
|
|
|
|
Payment storage p = payments[_idPayment];
|
|
|
|
require(p.state == PaymentStatus.Pending);
|
|
|
|
|
|
|
|
p.state = PaymentStatus.Canceled;
|
|
|
|
|
|
|
|
liquidPledging.cancelPayment(uint64(p.ref), p.amount);
|
|
|
|
|
|
|
|
CancelPayment(_idPayment, p.ref);
|
|
|
|
}
|
2017-12-04 01:21:09 +00:00
|
|
|
}
|