Skip to content

Commit

Permalink
RefundableCrowdsale now uses a RefundEscrow, removed RefundVault.
Browse files Browse the repository at this point in the history
  • Loading branch information
nventuro committed Jun 15, 2018
1 parent fe63f2e commit 9be72d5
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 231 deletions.
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";


/**
* @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.
*/
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;

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

12 changes: 6 additions & 6 deletions contracts/payment/Escrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import "../math/SafeMath.sol";
contract Escrow {
using SafeMath for uint256;

mapping(address => uint256) private _deposits;
mapping(address => uint256) private deposits;

function deposits(address _payee) public view returns (uint256) {
return _deposits[_payee];
function depositsOf(address _payee) public view returns (uint256) {
return deposits[_payee];
}

/**
Expand All @@ -25,7 +25,7 @@ contract Escrow {
uint256 amount = msg.value;
require(amount > 0);

_deposits[_payee] = _deposits[_payee].add(amount);
deposits[_payee] = deposits[_payee].add(amount);
}

/**
Expand All @@ -34,12 +34,12 @@ contract Escrow {
* @param _payee The address whose funds will be withdrawn and transferred to.
*/
function withdraw(address _payee) public {
uint256 payment = _deposits[_payee];
uint256 payment = deposits[_payee];

require(payment != 0);
require(address(this).balance >= payment);

_deposits[_payee] = 0;
deposits[_payee] = 0;

_payee.transfer(payment);
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/payment/PullPayment.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ contract PullPayment {
& @param _dest The creditor's address.
*/
function payments(address _dest) public view returns (uint256) {
return escrow.deposits(_dest);
return escrow.depositsOf(_dest);
}

/**
Expand Down
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.
*/
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 {
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 {
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);
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;
}
}
65 changes: 0 additions & 65 deletions contracts/payment/RefundableEscrow.sol

This file was deleted.

Loading

0 comments on commit 9be72d5

Please sign in to comment.