Escrow.sol 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. pragma solidity ^0.6.0;
  2. import "../../math/SafeMath.sol";
  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 SafeMath for uint256;
  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. function deposit(address payee) public virtual payable onlyOwner {
  32. uint256 amount = msg.value;
  33. _deposits[payee] = _deposits[payee].add(amount);
  34. emit Deposited(payee, amount);
  35. }
  36. /**
  37. * @dev Withdraw accumulated balance for a payee, forwarding all gas to the
  38. * recipient.
  39. *
  40. * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
  41. * Make sure you trust the recipient, or are either following the
  42. * checks-effects-interactions pattern or using {ReentrancyGuard}.
  43. *
  44. * @param payee The address whose funds will be withdrawn and transferred to.
  45. */
  46. function withdraw(address payable payee) public virtual onlyOwner {
  47. uint256 payment = _deposits[payee];
  48. _deposits[payee] = 0;
  49. payee.sendValue(payment);
  50. emit Withdrawn(payee, payment);
  51. }
  52. }