Escrow.sol 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.7.0) (utils/escrow/Escrow.sol)
  3. pragma solidity ^0.8.0;
  4. import "../../access/Ownable.sol";
  5. import "../Address.sol";
  6. /**
  7. * @title Escrow
  8. * @dev Base escrow contract, holds funds designated for a payee until they
  9. * withdraw them.
  10. *
  11. * Intended usage: This contract (and derived escrow contracts) should be a
  12. * standalone contract, that only interacts with the contract that instantiated
  13. * it. That way, it is guaranteed that all Ether will be handled according to
  14. * the `Escrow` rules, and there is no need to check for payable functions or
  15. * transfers in the inheritance tree. The contract that uses the escrow as its
  16. * payment method should be its owner, and provide public methods redirecting
  17. * to the escrow's deposit and withdraw.
  18. */
  19. contract Escrow is Ownable {
  20. using Address for address payable;
  21. event Deposited(address indexed payee, uint256 weiAmount);
  22. event Withdrawn(address indexed payee, uint256 weiAmount);
  23. mapping(address => uint256) private _deposits;
  24. function depositsOf(address payee) public view returns (uint256) {
  25. return _deposits[payee];
  26. }
  27. /**
  28. * @dev Stores the sent amount as credit to be withdrawn.
  29. * @param payee The destination address of the funds.
  30. *
  31. * Emits a {Deposited} event.
  32. */
  33. function deposit(address payee) public payable virtual onlyOwner {
  34. uint256 amount = msg.value;
  35. _deposits[payee] += amount;
  36. emit Deposited(payee, amount);
  37. }
  38. /**
  39. * @dev Withdraw accumulated balance for a payee, forwarding all gas to the
  40. * recipient.
  41. *
  42. * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
  43. * Make sure you trust the recipient, or are either following the
  44. * checks-effects-interactions pattern or using {ReentrancyGuard}.
  45. *
  46. * @param payee The address whose funds will be withdrawn and transferred to.
  47. *
  48. * Emits a {Withdrawn} event.
  49. */
  50. function withdraw(address payable payee) public virtual onlyOwner {
  51. uint256 payment = _deposits[payee];
  52. _deposits[payee] = 0;
  53. payee.sendValue(payment);
  54. emit Withdrawn(payee, payment);
  55. }
  56. }