PullPayment.sol 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. pragma solidity ^0.4.11;
  2. import '../SafeMath.sol';
  3. /**
  4. * @title PullPayment
  5. * @dev Base contract supporting async send for pull payments. Inherit from this
  6. * contract and use asyncSend instead of send.
  7. */
  8. contract PullPayment {
  9. using SafeMath for uint256;
  10. mapping(address => uint256) public payments;
  11. uint256 public totalPayments;
  12. /**
  13. * @dev Called by the payer to store the sent amount as credit to be pulled.
  14. * @param dest The destination address of the funds.
  15. * @param amount The amount to transfer.
  16. */
  17. function asyncSend(address dest, uint256 amount) internal {
  18. payments[dest] = payments[dest].add(amount);
  19. totalPayments = totalPayments.add(amount);
  20. }
  21. /**
  22. * @dev withdraw accumulated balance, called by payee.
  23. */
  24. function withdrawPayments() {
  25. address payee = msg.sender;
  26. uint256 payment = payments[payee];
  27. if (payment == 0) {
  28. throw;
  29. }
  30. if (this.balance < payment) {
  31. throw;
  32. }
  33. totalPayments = totalPayments.sub(payment);
  34. payments[payee] = 0;
  35. if (!payee.send(payment)) {
  36. throw;
  37. }
  38. }
  39. }