liquid-funding/contracts/LPVault.sol

202 lines
8.2 KiB
Solidity
Raw Normal View History

2017-06-26 17:54:28 +00:00
pragma solidity ^0.4.11;
2017-11-01 21:22:41 +00:00
/// @title LPVault
2017-09-29 10:16:53 +00:00
/// @author Jordi Baylina
/// @notice This contract holds ether securely for liquid pledging systems. For
/// this iteration the funds will come straight from the Giveth Multisig as a
/// 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 "../node_modules/giveth-common-contracts/contracts/Escapable.sol";
2017-07-13 17:12:45 +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.
2017-07-13 17:12:45 +00:00
contract LiquidPledging {
2017-10-25 17:37:21 +00:00
function confirmPayment(uint64 idNote, uint amount) public;
function cancelPayment(uint64 idNote, uint amount) public;
2017-07-13 17:12:45 +00:00
}
2017-06-26 17:54:28 +00:00
2017-09-29 10:16:53 +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.
contract LPVault is Escapable {
2017-06-26 17:54:28 +00:00
2017-09-29 10:16:53 +00:00
LiquidPledging public liquidPledging; // liquidPledging contract's address
bool public autoPay; // if false, payments will take 2 txs to be completed
2017-06-26 17:54:28 +00:00
2017-10-04 11:02:19 +00:00
enum PaymentStatus {
2017-09-29 10:16:53 +00:00
Pending, // means the payment is awaiting confirmation
2017-10-04 11:02:19 +00:00
Paid, // means the payment has been sent
2017-09-29 10:16:53 +00:00
Canceled // means the payment will never be sent
2017-06-26 17:54:28 +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-10-04 11:02:19 +00:00
PaymentStatus state; //
2017-09-29 10:16:53 +00:00
bytes32 ref; // an input that references details from other contracts
address dest; // recipient of the ETH
uint amount; // amount of ETH (in wei) to be sent
2017-06-26 17:54:28 +00:00
}
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;
function Vault(address _escapeHatchCaller, address _escapeHatchDestination)
Escapable(_escapeHatchCaller, _escapeHatchDestination) public {
}
2017-10-25 17:37:21 +00:00
/// @dev `liquidPledging` 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));
_;
}
function () public payable {}
2017-06-26 17:54:28 +00:00
2017-10-31 21:00:34 +00:00
/// @notice `setLiquidPledging` is used to attach a specific liquid pledging
2017-11-01 21:22:41 +00:00
/// instance to this LPvault. Keep in mind this isn't a single pledge but
2017-10-31 21:00:34 +00:00
/// instead an entire liquid pledging contract.
/// @param _newLiquidPledging A full liquid pledging contract
2017-10-25 17:37:21 +00:00
function setLiquidPledging(address _newLiquidPledging) public onlyOwner {
2017-09-29 10:16:53 +00:00
require(address(liquidPledging) == 0x0);
2017-06-26 17:54:28 +00:00
liquidPledging = LiquidPledging(_newLiquidPledging);
}
2017-11-01 21:22:41 +00:00
/// @notice `setAutopay` is used to toggle whether the LPvault will
2017-10-31 21:00:34 +00:00
/// automatically confirm a payment after the payment has been authorized.
/// @param _automatic If true payments will confirm automatically
2017-10-25 17:37:21 +00:00
function setAutopay(bool _automatic) public onlyOwner {
2017-06-26 17:54:28 +00:00
autoPay = _automatic;
}
2017-10-31 21:00:34 +00:00
/// @notice `authorizePayment` is used in order to approve a payment
/// from the liquid pledging contract. Whenever a project or other address
2017-11-01 21:03:03 +00:00
/// needs to receive a payment it needs to be authorized with this contract.
2017-10-31 21:00:34 +00:00
/// @param _ref This parameter is used to reference details about the
2017-11-01 21:03:03 +00:00
/// payment from another contract.
2017-10-31 21:00:34 +00:00
/// @param _dest This is the address that payments will end up being sent to
/// @param _amount This is the amount that the payment is being authorized
/// for.
2017-11-01 21:03:03 +00:00
function authorizePayment(
bytes32 _ref,
address _dest,
uint _amount ) public onlyLiquidPledging returns (uint) {
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;
payments[idPayment].amount = _amount;
AuthorizePayment(idPayment, _ref, _dest, _amount);
2017-06-26 17:54:28 +00:00
if (autoPay) {
2017-10-25 17:37:21 +00:00
doConfirmPayment(idPayment);
}
2017-06-26 17:54:28 +00:00
return idPayment;
}
2017-10-31 21:00:34 +00:00
/// @notice `confirmPayment` is a basic function used to allow the
/// owner of the vault to initiate a payment confirmation. Since
/// `authorizePayment` is the only pay to populate the `payments` array
/// this is generally used when `autopay` is `false` after a payment has
/// has been authorized.
/// @param _idPayment Array lookup for the payment.
2017-10-25 17:37:21 +00:00
function confirmPayment(uint _idPayment) public onlyOwner {
2017-06-26 17:54:28 +00:00
doConfirmPayment(_idPayment);
}
2017-10-31 21:00:34 +00:00
/// @notice `doConfirmPayment` is used to actually initiate a payment
/// to the final destination. All of the payment information should be
/// set before calling this function.
/// @param _idPayment Array lookup for the payment.
2017-06-26 17:54:28 +00:00
function doConfirmPayment(uint _idPayment) internal {
require(_idPayment < payments.length);
2017-07-13 17:12:45 +00:00
Payment storage p = payments[_idPayment];
2017-10-04 11:02:19 +00:00
require(p.state == PaymentStatus.Pending);
2017-06-26 17:54:28 +00:00
2017-10-04 11:02:19 +00:00
p.state = PaymentStatus.Paid;
2017-06-26 17:54:28 +00:00
liquidPledging.confirmPayment(uint64(p.ref), p.amount);
2017-10-06 18:21:20 +00:00
p.dest.transfer(p.amount); // only ETH denominated in wei
2017-06-26 17:54:28 +00:00
ConfirmPayment(_idPayment);
}
2017-10-31 21:00:34 +00:00
/// @notice `cancelPayment` is used when `autopay` is `false` in order
/// to allow the owner to cancel a payment instead of confirming it.
/// @param _idPayment Array lookup for the payment.
2017-10-25 17:37:21 +00:00
function cancelPayment(uint _idPayment) public onlyOwner {
2017-06-26 17:54:28 +00:00
doCancelPayment(_idPayment);
}
2017-10-31 21:00:34 +00:00
/// @notice `doCancelPayment` This carries out the task of actually
/// canceling a payment instead of confirming it.
/// @param _idPayment Array lookup for the payment.
2017-06-26 17:54:28 +00:00
function doCancelPayment(uint _idPayment) internal {
require(_idPayment < payments.length);
2017-07-13 17:12:45 +00:00
Payment storage p = payments[_idPayment];
2017-10-04 11:02:19 +00:00
require(p.state == PaymentStatus.Pending);
2017-06-26 17:54:28 +00:00
2017-10-04 11:02:19 +00:00
p.state = PaymentStatus.Canceled;
2017-06-26 17:54:28 +00:00
liquidPledging.cancelPayment(uint64(p.ref), p.amount);
CancelPayment(_idPayment);
}
2017-10-31 21:00:34 +00:00
/// @notice `multiConfirm` allows for more efficient confirmation of
/// multiple payments.
/// @param _idPayments An array of multiple payment ids
2017-10-25 17:37:21 +00:00
function multiConfirm(uint[] _idPayments) public onlyOwner {
for (uint i = 0; i < _idPayments.length; i++) {
2017-06-26 17:54:28 +00:00
doConfirmPayment(_idPayments[i]);
}
}
2017-10-31 21:00:34 +00:00
/// @notice `multiCancel` allows for more efficient cancellation of
/// multiple payments.
/// @param _idPayments An array of multiple payment ids
2017-10-25 17:37:21 +00:00
function multiCancel(uint[] _idPayments) public onlyOwner {
for (uint i = 0; i < _idPayments.length; i++) {
2017-06-26 17:54:28 +00:00
doCancelPayment(_idPayments[i]);
}
}
2017-10-31 21:00:34 +00:00
/// @notice `nPayments` Basic getter to return the number of payments
/// currently held in the system. Since payments are not removed from
/// the array this represents all payments over all time.
2017-10-25 17:37:21 +00:00
function nPayments() constant public returns (uint) {
2017-06-26 17:54:28 +00:00
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);
}
2017-06-26 17:54:28 +00:00
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);
2017-10-25 17:37:21 +00:00
}