Escrow.sol 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. pragma solidity ^0.5.0;
  2. import "../../math/SafeMath.sol";
  3. import "../../ownership/Secondary.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 primary, and provide public methods redirecting
  16. * to the escrow's deposit and withdraw.
  17. */
  18. contract Escrow is Secondary {
  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 onlyPrimary payable {
  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 2300 gas (a
  38. * Solidity `transfer`).
  39. *
  40. * NOTE: This function has been deprecated, use {withdrawWithGas} instead.
  41. * Calling contracts with fixed-gas limits is an anti-pattern and may break
  42. * contract interactions in network upgrades (hardforks).
  43. * https://diligence.consensys.net/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more.]
  44. *
  45. * @param payee The address whose funds will be withdrawn and transferred to.
  46. */
  47. function withdraw(address payable payee) public onlyPrimary {
  48. uint256 payment = _deposits[payee];
  49. _deposits[payee] = 0;
  50. payee.transfer(payment);
  51. emit Withdrawn(payee, payment);
  52. }
  53. /**
  54. * @dev Same as {withdraw}, but forwarding all gas to the recipient.
  55. *
  56. * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
  57. * Make sure you trust the recipient, or are either following the
  58. * checks-effects-interactions pattern or using {ReentrancyGuard}.
  59. */
  60. function withdrawWithGas(address payable payee) public onlyPrimary {
  61. uint256 payment = _deposits[payee];
  62. _deposits[payee] = 0;
  63. payee.sendValue(payment);
  64. emit Withdrawn(payee, payment);
  65. }
  66. }