PullPayment.sol 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. * TIP: If you would like to learn more about reentrancy and alternative ways
  14. * to protect against it, check out our blog post
  15. * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
  16. *
  17. * To use, derive from the `PullPayment` contract, and use {_asyncTransfer}
  18. * instead of Solidity's `transfer` function. Payees can query their due
  19. * payments with {payments}, and retrieve them with {withdrawPayments}.
  20. */
  21. contract PullPayment {
  22. Escrow private _escrow;
  23. constructor () internal {
  24. _escrow = new Escrow();
  25. }
  26. /**
  27. * @dev Withdraw accumulated payments.
  28. *
  29. * Note that _any_ account can call this function, not just the `payee`.
  30. * This means that contracts unaware of the `PullPayment` protocol can still
  31. * receive funds this way, by having a separate account call
  32. * {withdrawPayments}.
  33. *
  34. * NOTE: This function has been deprecated, use {withdrawPaymentsWithGas}
  35. * instead. Calling contracts with fixed gas limits is an anti-pattern and
  36. * may break contract interactions in network upgrades (hardforks).
  37. * https://diligence.consensys.net/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more.]
  38. *
  39. * @param payee Whose payments will be withdrawn.
  40. */
  41. function withdrawPayments(address payable payee) public {
  42. _escrow.withdraw(payee);
  43. }
  44. /**
  45. * @dev Same as {withdrawPayments}, but forwarding all gas to the recipient.
  46. *
  47. * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
  48. * Make sure you trust the recipient, or are either following the
  49. * checks-effects-interactions pattern or using {ReentrancyGuard}.
  50. *
  51. * _Available since v2.4.0._
  52. */
  53. function withdrawPaymentsWithGas(address payable payee) external {
  54. _escrow.withdrawWithGas(payee);
  55. }
  56. /**
  57. * @dev Returns the payments owed to an address.
  58. * @param dest The creditor's address.
  59. */
  60. function payments(address dest) public view returns (uint256) {
  61. return _escrow.depositsOf(dest);
  62. }
  63. /**
  64. * @dev Called by the payer to store the sent amount as credit to be pulled.
  65. * Funds sent in this way are stored in an intermediate {Escrow} contract, so
  66. * there is no danger of them being spent before withdrawal.
  67. *
  68. * @param dest The destination address of the funds.
  69. * @param amount The amount to transfer.
  70. */
  71. function _asyncTransfer(address dest, uint256 amount) internal {
  72. _escrow.deposit.value(amount)(dest);
  73. }
  74. }