EscrowUpgradeable.sol 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. __Ownable_init_unchained();
  26. }
  27. function __Escrow_init_unchained() internal onlyInitializing {
  28. }
  29. using AddressUpgradeable for address payable;
  30. event Deposited(address indexed payee, uint256 weiAmount);
  31. event Withdrawn(address indexed payee, uint256 weiAmount);
  32. mapping(address => uint256) private _deposits;
  33. function depositsOf(address payee) public view returns (uint256) {
  34. return _deposits[payee];
  35. }
  36. /**
  37. * @dev Stores the sent amount as credit to be withdrawn.
  38. * @param payee The destination address of the funds.
  39. */
  40. function deposit(address payee) public payable virtual onlyOwner {
  41. uint256 amount = msg.value;
  42. _deposits[payee] += amount;
  43. emit Deposited(payee, amount);
  44. }
  45. /**
  46. * @dev Withdraw accumulated balance for a payee, forwarding all gas to the
  47. * recipient.
  48. *
  49. * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
  50. * Make sure you trust the recipient, or are either following the
  51. * checks-effects-interactions pattern or using {ReentrancyGuard}.
  52. *
  53. * @param payee The address whose funds will be withdrawn and transferred to.
  54. */
  55. function withdraw(address payable payee) public virtual onlyOwner {
  56. uint256 payment = _deposits[payee];
  57. _deposits[payee] = 0;
  58. payee.sendValue(payment);
  59. emit Withdrawn(payee, payment);
  60. }
  61. /**
  62. * @dev This empty reserved space is put in place to allow future versions to add new
  63. * variables without shifting down storage in the inheritance chain.
  64. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  65. */
  66. uint256[49] private __gap;
  67. }