Escrow.sol 2.1 KB

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