Escrow.sol 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. pragma solidity ^0.4.23;
  2. import "../math/SafeMath.sol";
  3. import "../ownership/Ownable.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 owner, and provide public methods redirecting to the escrow's
  9. * deposit and withdraw.
  10. */
  11. contract Escrow is Ownable {
  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 onlyOwner 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 onlyOwner {
  33. uint256 payment = deposits[_payee];
  34. assert(address(this).balance >= payment);
  35. deposits[_payee] = 0;
  36. _payee.transfer(payment);
  37. emit Withdrawn(_payee, payment);
  38. }
  39. }