-
Notifications
You must be signed in to change notification settings - Fork 11.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Escrows #1014
Escrows #1014
Changes from 13 commits
2d2075a
b879f4c
48b64ce
3600240
fe63f2e
3a33ffb
d389fd3
e01edb3
5dfb9ce
6166248
cd84c37
dabb3d7
4422858
be8d65f
597aba7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pragma solidity ^0.4.24; | ||
|
||
|
||
import "../payment/ConditionalEscrow.sol"; | ||
|
||
|
||
// mock class using ConditionalEscrow | ||
contract ConditionalEscrowMock is ConditionalEscrow { | ||
mapping(address => bool) public allowed; | ||
|
||
function setAllowed(address _payee, bool _allowed) public { | ||
allowed[_payee] = _allowed; | ||
} | ||
|
||
function withdrawalAllowed(address _payee) public view returns (bool) { | ||
return allowed[_payee]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
pragma solidity ^0.4.23; | ||
|
||
import "./Escrow.sol"; | ||
|
||
|
||
/** | ||
* @title ConditionalEscrow | ||
* @dev Base abstract escrow to only allow withdrawal if a condition is met. | ||
*/ | ||
contract ConditionalEscrow is Escrow { | ||
/** | ||
* @dev Returns whether an address is allowed to withdraw their funds. To be | ||
* implemented by derived contracts. | ||
* @param _payee The destination address of the funds. | ||
*/ | ||
function withdrawalAllowed(address _payee) public view returns (bool); | ||
|
||
function withdraw(address _payee) public { | ||
require(withdrawalAllowed(_payee)); | ||
super.withdraw(_payee); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
pragma solidity ^0.4.23; | ||
|
||
import "../math/SafeMath.sol"; | ||
|
||
|
||
/** | ||
* @title Escrow | ||
* @dev Base escrow contract, holds funds destinated to a payee until they | ||
* withdraw them. | ||
*/ | ||
contract Escrow { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would make the |
||
using SafeMath for uint256; | ||
|
||
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 Called by the payer to store the sent amount as credit to be pulled. | ||
* @param _payee The destination address of the funds. | ||
*/ | ||
function deposit(address _payee) payable public { | ||
uint256 amount = msg.value; | ||
deposits[_payee] = deposits[_payee].add(amount); | ||
|
||
emit Deposited(_payee, amount); | ||
} | ||
|
||
/** | ||
* @dev Withdraw accumulated balance for a payee. Any address can trigger a | ||
* withdrawal. | ||
* @param _payee The address whose funds will be withdrawn and transferred to. | ||
*/ | ||
function withdraw(address _payee) public { | ||
uint256 payment = deposits[_payee]; | ||
assert(address(this).balance >= payment); | ||
|
||
deposits[_payee] = 0; | ||
|
||
_payee.transfer(payment); | ||
|
||
emit Withdrawn(_payee, payment); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,42 @@ | ||
pragma solidity ^0.4.24; | ||
|
||
|
||
import "../math/SafeMath.sol"; | ||
import "./Escrow.sol"; | ||
|
||
|
||
/** | ||
* @title PullPayment | ||
* @dev Base contract supporting async send for pull payments. Inherit from this | ||
* contract and use asyncSend instead of send or transfer. | ||
* contract and use asyncTransfer instead of send or transfer. | ||
*/ | ||
contract PullPayment { | ||
using SafeMath for uint256; | ||
Escrow private escrow; | ||
|
||
mapping(address => uint256) public payments; | ||
uint256 public totalPayments; | ||
constructor() public { | ||
escrow = new Escrow(); | ||
} | ||
|
||
/** | ||
* @dev Withdraw accumulated balance, called by payee. | ||
*/ | ||
function withdrawPayments() public { | ||
address payee = msg.sender; | ||
uint256 payment = payments[payee]; | ||
|
||
require(payment != 0); | ||
require(address(this).balance >= payment); | ||
|
||
totalPayments = totalPayments.sub(payment); | ||
payments[payee] = 0; | ||
escrow.withdraw(payee); | ||
} | ||
|
||
payee.transfer(payment); | ||
/** | ||
* @dev Returns the credit owed to an address. | ||
* @param _dest The creditor's address. | ||
*/ | ||
function payments(address _dest) public view returns (uint256) { | ||
return escrow.depositsOf(_dest); | ||
} | ||
|
||
/** | ||
* @dev Called by the payer to store the sent amount as credit to be pulled. | ||
* @param dest The destination address of the funds. | ||
* @param amount The amount to transfer. | ||
* @param _dest The destination address of the funds. | ||
* @param _amount The amount to transfer. | ||
*/ | ||
function asyncSend(address dest, uint256 amount) internal { | ||
payments[dest] = payments[dest].add(amount); | ||
totalPayments = totalPayments.add(amount); | ||
function asyncTransfer(address _dest, uint256 _amount) internal { | ||
escrow.deposit.value(_amount)(_dest); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
RefundEscrow
is very much crowdsale-specific so it should be with the crowdsale stuff. Potentially in this same file. Thoughts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disagree: a
RefundEscrow
is a payment method, a crowdsale typically using it doesn't make it crowdsale-specific. As an example, platforms like Amazon or eBay also offer refunds and act as an escrow themselves, and could extend their payment systems to add support for multiple payers.