PullPayment.sol 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. pragma solidity ^0.4.18;
  2. import "../math/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 withdraw accumulated balance, called by payee.
  14. */
  15. function withdrawPayments() public {
  16. address payee = msg.sender;
  17. uint256 payment = payments[payee];
  18. require(payment != 0);
  19. require(this.balance >= payment);
  20. totalPayments = totalPayments.sub(payment);
  21. payments[payee] = 0;
  22. assert(payee.send(payment));
  23. }
  24. /**
  25. * @dev Called by the payer to store the sent amount as credit to be pulled.
  26. * @param dest The destination address of the funds.
  27. * @param amount The amount to transfer.
  28. */
  29. function asyncSend(address dest, uint256 amount) internal {
  30. payments[dest] = payments[dest].add(amount);
  31. totalPayments = totalPayments.add(amount);
  32. }
  33. }