EscrowUpgradeable.sol 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (utils/escrow/Escrow.sol)
  3. pragma solidity ^0.8.0;
  4. import "../../access/OwnableUpgradeable.sol";
  5. import "../AddressUpgradeable.sol";
  6. import "../../proxy/utils/Initializable.sol";
  7. /**
  8. * @title Escrow
  9. * @dev Base escrow contract, holds funds designated for a payee until they
  10. * withdraw them.
  11. *
  12. * Intended usage: This contract (and derived escrow contracts) should be a
  13. * standalone contract, that only interacts with the contract that instantiated
  14. * it. That way, it is guaranteed that all Ether will be handled according to
  15. * the `Escrow` rules, and there is no need to check for payable functions or
  16. * transfers in the inheritance tree. The contract that uses the escrow as its
  17. * payment method should be its owner, and provide public methods redirecting
  18. * to the escrow's deposit and withdraw.
  19. */
  20. contract EscrowUpgradeable is Initializable, OwnableUpgradeable {
  21. function initialize() public virtual initializer {
  22. __Escrow_init();
  23. }
  24. function __Escrow_init() internal onlyInitializing {
  25. __Context_init_unchained();
  26. __Ownable_init_unchained();
  27. __Escrow_init_unchained();
  28. }
  29. function __Escrow_init_unchained() internal onlyInitializing {
  30. }
  31. using AddressUpgradeable for address payable;
  32. event Deposited(address indexed payee, uint256 weiAmount);
  33. event Withdrawn(address indexed payee, uint256 weiAmount);
  34. mapping(address => uint256) private _deposits;
  35. function depositsOf(address payee) public view returns (uint256) {
  36. return _deposits[payee];
  37. }
  38. /**
  39. * @dev Stores the sent amount as credit to be withdrawn.
  40. * @param payee The destination address of the funds.
  41. */
  42. function deposit(address payee) public payable virtual onlyOwner {
  43. uint256 amount = msg.value;
  44. _deposits[payee] += amount;
  45. emit Deposited(payee, amount);
  46. }
  47. /**
  48. * @dev Withdraw accumulated balance for a payee, forwarding all gas to the
  49. * recipient.
  50. *
  51. * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
  52. * Make sure you trust the recipient, or are either following the
  53. * checks-effects-interactions pattern or using {ReentrancyGuard}.
  54. *
  55. * @param payee The address whose funds will be withdrawn and transferred to.
  56. */
  57. function withdraw(address payable payee) public virtual onlyOwner {
  58. uint256 payment = _deposits[payee];
  59. _deposits[payee] = 0;
  60. payee.sendValue(payment);
  61. emit Withdrawn(payee, payment);
  62. }
  63. uint256[49] private __gap;
  64. }