PullPayment.sol 928 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 {
  9. using SafeMath for uint;
  10. mapping(address => uint) public payments;
  11. uint public totalPayments;
  12. // store sent amount as credit to be pulled, called by payer
  13. function asyncSend(address dest, uint amount) internal {
  14. payments[dest] = payments[dest].add(amount);
  15. totalPayments = totalPayments.add(amount);
  16. }
  17. // withdraw accumulated balance, called by payee
  18. function withdrawPayments() {
  19. address payee = msg.sender;
  20. uint payment = payments[payee];
  21. if (payment == 0) {
  22. throw;
  23. }
  24. if (this.balance < payment) {
  25. throw;
  26. }
  27. totalPayments = totalPayments.sub(payment);
  28. payments[payee] = 0;
  29. if (!payee.send(payment)) {
  30. throw;
  31. }
  32. }
  33. }