1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- // SPDX-License-Identifier: MIT
- pragma solidity ^0.7.0;
- import "../../math/SafeMath.sol";
- import "../../access/Ownable.sol";
- import "../../utils/Address.sol";
- /**
- * @title Escrow
- * @dev Base escrow contract, holds funds designated for a payee until they
- * withdraw them.
- *
- * Intended usage: This contract (and derived escrow contracts) should be a
- * standalone contract, that only interacts with the contract that instantiated
- * it. That way, it is guaranteed that all Ether will be handled according to
- * the `Escrow` rules, and there is no need to check for payable functions or
- * transfers in the inheritance tree. The contract that uses the escrow as its
- * payment method should be its owner, and provide public methods redirecting
- * to the escrow's deposit and withdraw.
- */
- contract Escrow is Ownable {
- using SafeMath for uint256;
- using Address for address payable;
- event Deposited(address indexed payee, uint256 weiAmount);
- event Withdrawn(address indexed payee, uint256 weiAmount);
- mapping(address => uint256) private _deposits;
- function depositsOf(address payee) public view returns (uint256) {
- return _deposits[payee];
- }
- /**
- * @dev Stores the sent amount as credit to be withdrawn.
- * @param payee The destination address of the funds.
- */
- function deposit(address payee) public virtual payable onlyOwner {
- uint256 amount = msg.value;
- _deposits[payee] = _deposits[payee].add(amount);
- emit Deposited(payee, amount);
- }
- /**
- * @dev Withdraw accumulated balance for a payee, forwarding all gas to the
- * recipient.
- *
- * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
- * Make sure you trust the recipient, or are either following the
- * checks-effects-interactions pattern or using {ReentrancyGuard}.
- *
- * @param payee The address whose funds will be withdrawn and transferred to.
- */
- function withdraw(address payable payee) public virtual onlyOwner {
- uint256 payment = _deposits[payee];
- _deposits[payee] = 0;
- payee.sendValue(payment);
- emit Withdrawn(payee, payment);
- }
- }
|