Escrow.sol 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.7.0;
  3. import "../../math/SafeMath.sol";
  4. import "../../access/Ownable.sol";
  5. import "../../utils/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 SafeMath for uint256;
  21. using Address for address payable;
  22. event Deposited(address indexed payee, uint256 weiAmount);
  23. event Withdrawn(address indexed payee, uint256 weiAmount);
  24. mapping(address => uint256) private _deposits;
  25. function depositsOf(address payee) public view returns (uint256) {
  26. return _deposits[payee];
  27. }
  28. /**
  29. * @dev Stores the sent amount as credit to be withdrawn.
  30. * @param payee The destination address of the funds.
  31. */
  32. function deposit(address payee) public payable virtual onlyOwner {
  33. uint256 amount = msg.value;
  34. _deposits[payee] = _deposits[payee].add(amount);
  35. emit Deposited(payee, amount);
  36. }
  37. /**
  38. * @dev Withdraw accumulated balance for a payee, forwarding all gas to the
  39. * recipient.
  40. *
  41. * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
  42. * Make sure you trust the recipient, or are either following the
  43. * checks-effects-interactions pattern or using {ReentrancyGuard}.
  44. *
  45. * @param payee The address whose funds will be withdrawn and transferred to.
  46. */
  47. function withdraw(address payable payee) public virtual onlyOwner {
  48. uint256 payment = _deposits[payee];
  49. _deposits[payee] = 0;
  50. payee.sendValue(payment);
  51. emit Withdrawn(payee, payment);
  52. }
  53. }