-
Notifications
You must be signed in to change notification settings - Fork 12k
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 6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,31 +3,31 @@ pragma solidity ^0.4.24; | |
|
||
import "../../math/SafeMath.sol"; | ||
import "./FinalizableCrowdsale.sol"; | ||
import "./utils/RefundVault.sol"; | ||
import "../../payment/RefundEscrow.sol"; | ||
|
||
|
||
/** | ||
* @title RefundableCrowdsale | ||
* @dev Extension of Crowdsale contract that adds a funding goal, and | ||
* the possibility of users getting a refund if goal is not met. | ||
* Uses a RefundVault as the crowdsale's vault. | ||
* Uses a RefundEscrow as the crowdsale's 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. This line is an implementation detail. Let's remove it. |
||
*/ | ||
contract RefundableCrowdsale is FinalizableCrowdsale { | ||
using SafeMath for uint256; | ||
|
||
// minimum amount of funds to be raised in weis | ||
uint256 public goal; | ||
|
||
// refund vault used to hold funds while crowdsale is running | ||
RefundVault public vault; | ||
// refund escrow used to hold funds while crowdsale is running | ||
RefundEscrow public 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. Please make this state variable private as well. |
||
|
||
/** | ||
* @dev Constructor, creates RefundVault. | ||
* @dev Constructor, creates RefundEscrow. | ||
* @param _goal Funding goal | ||
*/ | ||
constructor(uint256 _goal) public { | ||
require(_goal > 0); | ||
vault = new RefundVault(wallet); | ||
escrow = new RefundEscrow(wallet); | ||
goal = _goal; | ||
} | ||
|
||
|
@@ -38,7 +38,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale { | |
require(isFinalized); | ||
require(!goalReached()); | ||
|
||
vault.refund(msg.sender); | ||
escrow.refund(msg.sender); | ||
} | ||
|
||
/** | ||
|
@@ -50,23 +50,24 @@ contract RefundableCrowdsale is FinalizableCrowdsale { | |
} | ||
|
||
/** | ||
* @dev vault finalization task, called when owner calls finalize() | ||
* @dev escrow finalization task, called when owner calls finalize() | ||
*/ | ||
function finalization() internal { | ||
if (goalReached()) { | ||
vault.close(); | ||
escrow.close(); | ||
escrow.withdraw(); | ||
} else { | ||
vault.enableRefunds(); | ||
escrow.enableRefunds(); | ||
} | ||
|
||
super.finalization(); | ||
} | ||
|
||
/** | ||
* @dev Overrides Crowdsale fund forwarding, sending funds to vault. | ||
* @dev Overrides Crowdsale fund forwarding, sending funds to escrow. | ||
*/ | ||
function _forwardFunds() internal { | ||
vault.deposit.value(msg.value)(msg.sender); | ||
escrow.invest.value(msg.value)(msg.sender); | ||
} | ||
|
||
} |
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,46 @@ | ||
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; | ||
|
||
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; | ||
require(amount > 0); | ||
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 line doesn't add any value (ha!). It will unnecessarily propagate the special case to the users of 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.
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. The thing is We've discussed this before, though I can't remember where, and the decision was to not revert. |
||
|
||
deposits[_payee] = deposits[_payee].add(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]; | ||
|
||
require(payment != 0); | ||
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 feels unnecessary as well. I would remove it. |
||
require(address(this).balance >= payment); | ||
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 is more of an |
||
|
||
deposits[_payee] = 0; | ||
|
||
_payee.transfer(payment); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
pragma solidity ^0.4.24; | ||
|
||
|
||
import "../math/SafeMath.sol"; | ||
import "./Escrow.sol"; | ||
|
||
|
||
/** | ||
|
@@ -10,34 +9,34 @@ import "../math/SafeMath.sol"; | |
* contract and use asyncSend instead of send or transfer. | ||
*/ | ||
contract PullPayment { | ||
using SafeMath for uint256; | ||
Escrow public 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. Please make this state variable private. |
||
|
||
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. | ||
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.
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. I blame Stroustrup |
||
*/ | ||
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 asyncSend(address _dest, uint256 _amount) internal { | ||
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. The word Thoughts? 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. I missed this when updating |
||
escrow.deposit.value(_amount)(_dest); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
pragma solidity ^0.4.23; | ||
|
||
import "./ConditionalEscrow.sol"; | ||
import "../ownership/Ownable.sol"; | ||
|
||
|
||
/** | ||
* @title RefundEscrow | ||
* @dev Escrow that holds investor funds for a unique benefitiary, and allows for | ||
* either withdrawal by the benefiatiary, or refunds to the investors. | ||
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. Typo twice here: "benefitiary" β "beneficiary". Also, "unique" doesn't sound right... "single" is the right word, but it feels redundant to me anyway. This description doesn't explain the condition according to which either withdrawal or refunding is allowed. We should also explain that this contract was written as the escrow for a 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. Yeah, I'm not sure about the 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. It's true that |
||
*/ | ||
contract RefundEscrow is ConditionalEscrow, Ownable { | ||
enum State { Active, Refunding, Closed } | ||
|
||
event Closed(); | ||
event RefundsEnabled(); | ||
event Refunded(address indexed investor, uint256 weiAmount); | ||
|
||
State public state; | ||
address public beneficiary; | ||
|
||
/** | ||
* @dev Constructor. | ||
* @param _beneficiary The beneficiary of the investments. | ||
*/ | ||
constructor(address _beneficiary) public { | ||
require(_beneficiary != address(0)); | ||
beneficiary = _beneficiary; | ||
state = State.Active; | ||
} | ||
|
||
/** | ||
* @dev Stores funds that may later be refunded. | ||
* @param _investor The address funds will be sent to if a refund occurs. | ||
*/ | ||
function invest(address _investor) payable public { | ||
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. Why did you decide to use a separate 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. agreed on 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. Agree on 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.
The error may also be in using the term |
||
require(state == State.Active); | ||
super.deposit(_investor); | ||
} | ||
|
||
/** | ||
* @dev Disable the base deposit function, use invest instead. | ||
*/ | ||
function deposit(address _payee) payable public { | ||
revert(); | ||
} | ||
|
||
/** | ||
* @dev Allows for the beneficiary to withdraw their funds, rejecting | ||
* further investments. | ||
*/ | ||
function close() onlyOwner public { | ||
require(state == State.Active); | ||
state = State.Closed; | ||
emit Closed(); | ||
} | ||
|
||
/** | ||
* @dev Allows for refunds to take place, rejecting further investments. | ||
*/ | ||
function enableRefunds() onlyOwner public { | ||
require(state == State.Active); | ||
state = State.Refunding; | ||
emit RefundsEnabled(); | ||
} | ||
|
||
/** | ||
* @dev Withdraws the beneficiary's funds. | ||
*/ | ||
function withdraw() public { | ||
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.
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. Agreed. On a related note, 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. Oh, I hadn't noticed that. Hm... I think we should keep |
||
require(state == State.Closed); | ||
beneficiary.transfer(address(this).balance); | ||
} | ||
|
||
/** | ||
* @dev Refunds an investor. | ||
* @param _investor The address to refund. | ||
*/ | ||
function refund(address _investor) public { | ||
uint256 amount = depositsOf(_investor); | ||
super.withdraw(_investor); | ||
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. No need to use |
||
emit Refunded(_investor, amount); | ||
} | ||
|
||
/** | ||
* @dev Returns whether investors can withdraw their investments (be refunded). | ||
*/ | ||
function withdrawalAllowed(address _payee) public view returns (bool) { | ||
return state == State.Refunding; | ||
} | ||
} |
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.