PullPayment.sol 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. *
  25. * Note that _any_ account can call this function, not just the `payee`.
  26. * This means that contracts unaware of the `PullPayment` protocol can still
  27. * receive funds this way, by having a separate account call
  28. * {withdrawPayments}.
  29. *
  30. * NOTE: This function has been deprecated, use {withdrawPaymentsWithGas}
  31. * instead. Calling contracts with fixed gas limits is an anti-pattern and
  32. * may break contract interactions in network upgrades (hardforks).
  33. * https://diligence.consensys.net/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more.]
  34. *
  35. * @param payee Whose payments will be withdrawn.
  36. */
  37. function withdrawPayments(address payable payee) public {
  38. _escrow.withdraw(payee);
  39. }
  40. /**
  41. * @dev Same as {withdrawPayments}, but forwarding all gas to the recipient.
  42. *
  43. * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
  44. * Make sure you trust the recipient, or are either following the
  45. * checks-effects-interactions pattern or using {ReentrancyGuard}.
  46. */
  47. function withdrawPaymentsWithGas(address payable payee) public {
  48. _escrow.withdrawWithGas(payee);
  49. }
  50. /**
  51. * @dev Returns the payments owed to an address.
  52. * @param dest The creditor's address.
  53. */
  54. function payments(address dest) public view returns (uint256) {
  55. return _escrow.depositsOf(dest);
  56. }
  57. /**
  58. * @dev Called by the payer to store the sent amount as credit to be pulled.
  59. * Funds sent in this way are stored in an intermediate {Escrow} contract, so
  60. * there is no danger of them being spent before withdrawal.
  61. *
  62. * @param dest The destination address of the funds.
  63. * @param amount The amount to transfer.
  64. */
  65. function _asyncTransfer(address dest, uint256 amount) internal {
  66. _escrow.deposit.value(amount)(dest);
  67. }
  68. }