PullPayment.sol 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. pragma solidity ^0.5.0;
  2. import "./escrow/Escrow.sol";
  3. /**
  4. * @dev Simple implementation of a
  5. * https://consensys.github.io/smart-contract-best-practices/recommendations/#favor-pull-over-push-for-external-calls[pull-payment]
  6. * strategy, where the paying contract doesn't interact directly with the
  7. * receiver account, which must withdraw its payments itself.
  8. *
  9. * Pull-payments are often considered the best practice when it comes to sending
  10. * Ether, security-wise. It prevents recipients from blocking execution, and
  11. * eliminates reentrancy concerns.
  12. *
  13. * To use, derive from the `PullPayment` contract, and use {_asyncTransfer}
  14. * instead of Solidity's `transfer` function. Payees can query their due
  15. * payments with {payments}, and retrieve them with {withdrawPayments}.
  16. */
  17. contract PullPayment {
  18. Escrow private _escrow;
  19. constructor () internal {
  20. _escrow = new Escrow();
  21. }
  22. /**
  23. * @dev Withdraw accumulated payments.
  24. * @param payee Whose payments will be withdrawn.
  25. *
  26. * Note that _any_ account can call this function, not just the `payee`.
  27. * This means that contracts unaware of the `PullPayment` protocol can still
  28. * receive funds this way, by having a separate account call
  29. * {withdrawPayments}.
  30. */
  31. function withdrawPayments(address payable payee) public {
  32. _escrow.withdraw(payee);
  33. }
  34. /**
  35. * @dev Returns the payments owed to an address.
  36. * @param dest The creditor's address.
  37. */
  38. function payments(address dest) public view returns (uint256) {
  39. return _escrow.depositsOf(dest);
  40. }
  41. /**
  42. * @dev Called by the payer to store the sent amount as credit to be pulled.
  43. * @param dest The destination address of the funds.
  44. * @param amount The amount to transfer.
  45. *
  46. * Funds sent in this way are stored in an intermediate {Escrow} contract, so
  47. * there is no danger of them being spent before withdrawal.
  48. */
  49. function _asyncTransfer(address dest, uint256 amount) internal {
  50. _escrow.deposit.value(amount)(dest);
  51. }
  52. }