Escrow.sol 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. pragma solidity ^0.5.0;
  2. import "../../math/SafeMath.sol";
  3. import "../../ownership/Secondary.sol";
  4. /**
  5. * @title Escrow
  6. * @dev Base escrow contract, holds funds designated for a payee until they
  7. * withdraw them.
  8. *
  9. * Intended usage: This contract (and derived escrow contracts) should be a
  10. * standalone contract, that only interacts with the contract that instantiated
  11. * it. That way, it is guaranteed that all Ether will be handled according to
  12. * the `Escrow` rules, and there is no need to check for payable functions or
  13. * transfers in the inheritance tree. The contract that uses the escrow as its
  14. * payment method should be its primary, and provide public methods redirecting
  15. * to the escrow's deposit and withdraw.
  16. */
  17. contract Escrow is Secondary {
  18. using SafeMath for uint256;
  19. event Deposited(address indexed payee, uint256 weiAmount);
  20. event Withdrawn(address indexed payee, uint256 weiAmount);
  21. mapping(address => uint256) private _deposits;
  22. function depositsOf(address payee) public view returns (uint256) {
  23. return _deposits[payee];
  24. }
  25. /**
  26. * @dev Stores the sent amount as credit to be withdrawn.
  27. * @param payee The destination address of the funds.
  28. */
  29. function deposit(address payee) public onlyPrimary payable {
  30. uint256 amount = msg.value;
  31. _deposits[payee] = _deposits[payee].add(amount);
  32. emit Deposited(payee, amount);
  33. }
  34. /**
  35. * @dev Withdraw accumulated balance for a payee.
  36. * @param payee The address whose funds will be withdrawn and transferred to.
  37. */
  38. function withdraw(address payable payee) public onlyPrimary {
  39. uint256 payment = _deposits[payee];
  40. _deposits[payee] = 0;
  41. payee.transfer(payment);
  42. emit Withdrawn(payee, payment);
  43. }
  44. }