2017-06-26 17:54:28 +00:00
|
|
|
pragma solidity ^0.4.11;
|
|
|
|
|
2017-09-29 10:16:53 +00:00
|
|
|
/// @title Vault
|
|
|
|
/// @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
|
2017-06-26 17:54:28 +00:00
|
|
|
import "./Owned.sol";
|
2017-07-13 17:12:45 +00:00
|
|
|
|
2017-09-29 10:16:53 +00:00
|
|
|
/// @dev This is declares a few functions from `LiquidPledging` so that the
|
|
|
|
/// `Vault` contract can interface with the `LiquidPledging` contract
|
2017-07-13 17:12:45 +00:00
|
|
|
contract LiquidPledging {
|
|
|
|
function confirmPayment(uint64 idNote, uint amount);
|
|
|
|
function cancelPayment(uint64 idNote, uint amount);
|
|
|
|
}
|
2017-06-26 17:54:28 +00:00
|
|
|
|
2017-09-29 10:16:53 +00:00
|
|
|
|
|
|
|
/// @dev `Vault` is a higher level contract built off of the `Owned`
|
2017-10-04 11:02:19 +00:00
|
|
|
/// contract that holds funds for the liquid pledging system.
|
2017-06-26 17:54:28 +00:00
|
|
|
contract Vault is Owned {
|
|
|
|
|
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-09-29 10:16:53 +00:00
|
|
|
// @dev An array that contains all the payments for this Vault
|
2017-06-26 17:54:28 +00:00
|
|
|
Payment[] public payments;
|
|
|
|
|
2017-09-29 10:16:53 +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));
|
|
|
|
_;
|
|
|
|
}
|
2017-09-29 10:16:53 +00:00
|
|
|
/// @dev USED FOR TESTING???
|
2017-06-26 17:54:28 +00:00
|
|
|
function VaultMock() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function () payable {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function setLiquidPledging(address _newLiquidPledging) onlyOwner {
|
2017-09-29 10:16:53 +00:00
|
|
|
require(address(liquidPledging) == 0x0);
|
2017-06-26 17:54:28 +00:00
|
|
|
liquidPledging = LiquidPledging(_newLiquidPledging);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setAutopay(bool _automatic) onlyOwner {
|
|
|
|
autoPay = _automatic;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function authorizePayment(bytes32 _ref, address _dest, uint _amount) onlyLiquidPledging returns (uint) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (autoPay) doConfirmPayment(idPayment);
|
|
|
|
|
|
|
|
return idPayment;
|
|
|
|
}
|
|
|
|
|
|
|
|
function confirmPayment(uint _idPayment) onlyOwner {
|
|
|
|
doConfirmPayment(_idPayment);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
function cancelPayment(uint _idPayment) onlyOwner {
|
|
|
|
doCancelPayment(_idPayment);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function multiConfirm(uint[] _idPayments) onlyOwner {
|
|
|
|
for (uint i=0; i < _idPayments.length; i++) {
|
|
|
|
doConfirmPayment(_idPayments[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function multiCancel(uint[] _idPayments) onlyOwner {
|
|
|
|
for (uint i=0; i < _idPayments.length; i++) {
|
|
|
|
doCancelPayment(_idPayments[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function nPayments() constant returns (uint) {
|
|
|
|
return payments.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
event ConfirmPayment(uint indexed idPayment);
|
|
|
|
event CancelPayment(uint indexed idPayment);
|
|
|
|
event AuthorizePayment(uint indexed idPayment, bytes32 indexed ref, address indexed dest, uint amount);
|
|
|
|
}
|