PullPayment.sol 927 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. pragma solidity ^0.4.8;
  2. import '../SafeMath.sol';
  3. /*
  4. * PullPayment
  5. * Base contract supporting async send for pull payments.
  6. * Inherit from this contract and use asyncSend instead of send.
  7. */
  8. contract PullPayment is SafeMath {
  9. mapping(address => uint) public payments;
  10. uint public totalPayments;
  11. // store sent amount as credit to be pulled, called by payer
  12. function asyncSend(address dest, uint amount) internal {
  13. payments[dest] = safeAdd(payments[dest], amount);
  14. totalPayments = safeAdd(totalPayments, amount);
  15. }
  16. // withdraw accumulated balance, called by payee
  17. function withdrawPayments() {
  18. address payee = msg.sender;
  19. uint payment = payments[payee];
  20. if (payment == 0) {
  21. throw;
  22. }
  23. if (this.balance < payment) {
  24. throw;
  25. }
  26. totalPayments = safeSub(totalPayments, payment);
  27. payments[payee] = 0;
  28. if (!payee.send(payment)) {
  29. throw;
  30. }
  31. }
  32. }