Escrow.sol 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. pragma solidity ^0.4.24;
  2. import "../math/SafeMath.sol";
  3. import "../ownership/Secondary.sol";
  4. /**
  5. * @title Escrow
  6. * @dev Base escrow contract, holds funds destinated to a payee until they
  7. * withdraw them. The contract that uses the escrow as its payment method
  8. * should be its primary, and provide public methods redirecting to the escrow's
  9. * deposit and withdraw.
  10. */
  11. contract Escrow is Secondary {
  12. using SafeMath for uint256;
  13. event Deposited(address indexed payee, uint256 weiAmount);
  14. event Withdrawn(address indexed payee, uint256 weiAmount);
  15. mapping(address => uint256) private _deposits;
  16. function depositsOf(address payee) public view returns (uint256) {
  17. return _deposits[payee];
  18. }
  19. /**
  20. * @dev Stores the sent amount as credit to be withdrawn.
  21. * @param payee The destination address of the funds.
  22. */
  23. function deposit(address payee) public onlyPrimary payable {
  24. uint256 amount = msg.value;
  25. _deposits[payee] = _deposits[payee].add(amount);
  26. emit Deposited(payee, amount);
  27. }
  28. /**
  29. * @dev Withdraw accumulated balance for a payee.
  30. * @param payee The address whose funds will be withdrawn and transferred to.
  31. */
  32. function withdraw(address payee) public onlyPrimary {
  33. uint256 payment = _deposits[payee];
  34. _deposits[payee] = 0;
  35. payee.transfer(payment);
  36. emit Withdrawn(payee, payment);
  37. }
  38. }