Escrow.sol 1.8 KB

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