Giveth / vaultcontract

Timelock vault for securing the payments
GNU General Public License v3.0
57 stars 30 forks source link

Move checkings to constant functions #29

Closed adria0 closed 7 years ago

adria0 commented 7 years ago

Since js-DApp should be able to check certain conditions to be met to show a button to execute a method contract, consider to add those checkings in a constant functions. This allows the js DApp to not implement the logic again.

for instance:

function allowedCollectAuthorizedPayment(uint _idPayment)
         constant (returns bool) {

    // Check that the `_idPayment` has been added to the payments struct
    if (_idPayment >= authorizedPayments.length) false;;

    Payment p = authorizedPayments[_idPayment];

    Spender spender = spenders[p.spender];
    if (spender.idx == 0) return false;

    // Checking for reasons not to execute the payment
    if (msg.sender != p.recipient) false;
    if (now < p.earliestPayTime) false;
    if (p.canceled) false;
    if (p.paid) false;
    if (getBalance() < p.amount) false;

    return true;
}

function collectAuthorizedPayment(uint _idPayment) {

    if (!allowedCollectAuthorizedPayment(_idPayment)) throw;

    Payment p = authorizedPayments[_idPayment];
    Spender spender = spenders[p.spender];

    p.paid = true; // Set the payment to being paid
    transfer(p.recipient, p.amount);// Make the payment

    totalAuthorizedToBeSpent -= p.amount;
    totalSpent += p.amount;
    PaymentExecuted(_idPayment, p.recipient, p.amount);
 }