Skip to content
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

Merged
merged 15 commits into from
Jul 3, 2018
Merged

Escrows #1014

Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions contracts/crowdsale/distribution/RefundableCrowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@ pragma solidity ^0.4.24;

import "../../math/SafeMath.sol";
import "./FinalizableCrowdsale.sol";
import "./utils/RefundVault.sol";
import "../../payment/RefundEscrow.sol";
Copy link
Contributor

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?

Copy link
Contributor Author

@nventuro nventuro Jun 18, 2018

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.



/**
* @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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}

Expand All @@ -38,7 +38,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale {
require(isFinalized);
require(!goalReached());

vault.refund(msg.sender);
escrow.refund(msg.sender);
}

/**
Expand All @@ -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);
}

}
66 changes: 0 additions & 66 deletions contracts/crowdsale/distribution/utils/RefundVault.sol

This file was deleted.

18 changes: 18 additions & 0 deletions contracts/mocks/ConditionalEscrowMock.sol
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];
}
}
22 changes: 22 additions & 0 deletions contracts/payment/ConditionalEscrow.sol
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);
}
}
46 changes: 46 additions & 0 deletions contracts/payment/Escrow.sol
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add Deposited and Withdrawn events. (I'm open to other names.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would make the Refunded event a bit redundant (since it will also emit an identical Withdrawn): do we want to remove it?

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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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 Escrow, when we can easily treat deposit(0) as a no-op. Let's just remove the line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deposit(0) would indeed do nothing: I thought it better to protect the caller from using the API wrong, though the argument could be made that this would make the contract more expensive for all other users. It's more of a library-wide decision, IMO. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is deposit(0) is not a wrong use of the API, it's just a no-op. It's not a matter of gas efficiency, it's about not forcing users of Escrow to add code to treat 0 specially.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more of an assert than a require.


deposits[_payee] = 0;

_payee.transfer(payment);
}
}
35 changes: 17 additions & 18 deletions contracts/payment/PullPayment.sol
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";


/**
Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

& β†’ *

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Copy link
Contributor

@frangio frangio Jun 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The word transfer is quite overloaded but I feel like this is an asyncTransfer more than an asyncSend, in analogy with send vs transfer because of the lack of return value.

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this when updating PullPayment, I agree that transfer is indeed the better term.

escrow.deposit.value(_amount)(_dest);
}
}
91 changes: 91 additions & 0 deletions contracts/payment/RefundEscrow.sol
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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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 RefundableCrowdsale.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, unique is just plain wrong. I think it could just say 'for a beneficiary' and the meaning would remain clear.

I'm not sure about the RefundableCrowdsale part though: supporting refunds in a payment system doesn't scream 'crowdsale' to me. I actually see the inverse of that being true: the RefundableCrowdsale is nothing more than a crowdsale, the escrow, and glue code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's true that RefundEscrow is not necessarily crowdsale-specific. I didn't mean to say that. I'm just interested in cross-linking related contracts. Perhaps this information should be automatically generated by our documentation generator though.

*/
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you decide to use a separate invest function instead of deposit? From what we discussed offline, I think it was because conceptually the argument to deposit is the payee and in this case the investor is not necessarily the payee. I'm not convinced by that. The concept of investment feels foreign to the escrow. I would stick to the base Escrow interface and use deposit with the additional state constraint.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed on deposit being better than invest. Maybe depositFor(_payee) to make it explicit that you're not specifically depositing for yourself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree on invest being foreign to the escrow itself. I wanted to make the distinction clear between the base escrow, where you deposit a payment to someone, and the refundable, where you deposit a payment to the beneficiary, but specify the refund address. Maybe we should call them refundees instead of investors?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refundee is better.

The error may also be in using the term payee in the base Escrow, but I can't think of a better name right now.

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Escrow#withdraw(address) has the same name but it does something different. Let's name this one something different. beneficiaryWithdraw? I don't know.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. On a related note, withdraw and refund do the same thing, but withdraw emits no events. Should we prevent the user from calling the base function (i.e. override and revert)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I hadn't noticed that. Hm... I think we should keep withdraw instead of adding a new refund function. In the case of RefundableCrowdsale, the user will use RefundableCrowdsale#claimRefund anyway.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use super here.

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;
}
}
Loading