Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Improve encapsulation on Crowdsales
  • Loading branch information
Leo Arias committed Sep 3, 2018
commit 1b71ee07da1d130b9c449acc82011c8983fc8b3a
58 changes: 43 additions & 15 deletions contracts/crowdsale/Crowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,16 @@ contract Crowdsale {
using SafeERC20 for IERC20;

// The token being sold
IERC20 public token;
IERC20 private token_;

// Address where funds are collected
address public wallet;
address private wallet_;

// How many token units a buyer gets per wei.
// The rate is the conversion between wei and the smallest and indivisible token unit.
// So, if you are using a rate of 1 with a ERC20Detailed token with 3 decimals called TOK
// 1 wei will give you 1 unit, or 0.001 TOK.
uint256 public rate;
uint256 private rate_;

// Amount of wei raised
uint256 public weiRaised;
uint256 private weiRaised_;

/**
* Event for token purchase logging
Expand All @@ -52,6 +49,9 @@ contract Crowdsale {

/**
* @param _rate Number of token units a buyer gets per wei
* @dev The rate is the conversion between wei and the smallest and indivisible
* token unit. So, if you are using a rate of 1 with a ERC20Detailed token
* with 3 decimals called TOK, 1 wei will give you 1 unit, or 0.001 TOK.
* @param _wallet Address where collected funds will be forwarded to
* @param _token Address of the token being sold
*/
Expand All @@ -60,9 +60,9 @@ contract Crowdsale {
require(_wallet != address(0));
require(_token != address(0));

rate = _rate;
wallet = _wallet;
token = _token;
rate_ = _rate;
wallet_ = _wallet;
token_ = _token;
}

// -----------------------------------------
Expand All @@ -76,6 +76,34 @@ contract Crowdsale {
buyTokens(msg.sender);
}

/**
* @return the token being sold.
*/
function getToken() public view returns(IERC20) {
return token_;
}

/**
* @return the address where funds are collected.
*/
function getWallet() public view returns(address) {
return wallet_;
}

/**
* @return the number of token units a buyer gets per wei.
*/
function getRate() public view returns(uint256) {
return rate_;
}

/**
* @return the mount of wei raised.
*/
function getWeiRaised() public view returns (uint256) {
return weiRaised_;
}

/**
* @dev low level token purchase ***DO NOT OVERRIDE***
* @param _beneficiary Address performing the token purchase
Expand All @@ -89,7 +117,7 @@ contract Crowdsale {
uint256 tokens = _getTokenAmount(weiAmount);

// update state
weiRaised = weiRaised.add(weiAmount);
weiRaised_ = weiRaised_.add(weiAmount);

_processPurchase(_beneficiary, tokens);
emit TokensPurchased(
Expand All @@ -113,7 +141,7 @@ contract Crowdsale {
* @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met. Use `super` in contracts that inherit from Crowdsale to extend their validations.
* Example from CappedCrowdsale.sol's _preValidatePurchase method:
* super._preValidatePurchase(_beneficiary, _weiAmount);
* require(weiRaised.add(_weiAmount) <= cap);
* require(getWeiRaised().add(_weiAmount) <= cap);
* @param _beneficiary Address performing the token purchase
* @param _weiAmount Value in wei involved in the purchase
*/
Expand Down Expand Up @@ -152,7 +180,7 @@ contract Crowdsale {
)
internal
{
token.safeTransfer(_beneficiary, _tokenAmount);
token_.safeTransfer(_beneficiary, _tokenAmount);
}

/**
Expand Down Expand Up @@ -191,13 +219,13 @@ contract Crowdsale {
function _getTokenAmount(uint256 _weiAmount)
internal view returns (uint256)
{
return _weiAmount.mul(rate);
return _weiAmount.mul(rate_);
}

/**
* @dev Determines how ETH is stored/forwarded on purchases.
*/
function _forwardFunds() internal {
wallet.transfer(msg.value);
wallet_.transfer(msg.value);
}
}
13 changes: 10 additions & 3 deletions contracts/crowdsale/distribution/FinalizableCrowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,29 @@ import "../validation/TimedCrowdsale.sol";
contract FinalizableCrowdsale is Ownable, TimedCrowdsale {
using SafeMath for uint256;

bool public isFinalized = false;
bool private finalized_ = false;

event CrowdsaleFinalized();

/**
* @return true if the crowdsale is finalized, false otherwise.
*/
function isFinalized() public view returns(bool) {
return finalized_;
}

/**
* @dev Must be called after crowdsale ends, to do some extra finalization
* work. Calls the contract's finalization function.
*/
function finalize() public onlyOwner {
require(!isFinalized);
require(!finalized_);
require(hasClosed());

finalization();
emit CrowdsaleFinalized();

isFinalized = true;
finalized_ = true;
}

/**
Expand Down
15 changes: 11 additions & 4 deletions contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,26 @@ import "../../math/SafeMath.sol";
contract PostDeliveryCrowdsale is TimedCrowdsale {
using SafeMath for uint256;

mapping(address => uint256) public balances;
mapping(address => uint256) private balances_;

/**
* @dev Withdraw tokens only after crowdsale ends.
*/
function withdrawTokens() public {
require(hasClosed());
uint256 amount = balances[msg.sender];
uint256 amount = balances_[msg.sender];
require(amount > 0);
balances[msg.sender] = 0;
balances_[msg.sender] = 0;
_deliverTokens(msg.sender, amount);
}

/**
* @return the balance of an account.
*/
function getBalance(address _account) public view returns(uint256) {
return balances_[_account];
}

/**
* @dev Overrides parent by storing balances instead of issuing tokens right away.
* @param _beneficiary Token purchaser
Expand All @@ -36,7 +43,7 @@ contract PostDeliveryCrowdsale is TimedCrowdsale {
)
internal
{
balances[_beneficiary] = balances[_beneficiary].add(_tokenAmount);
balances_[_beneficiary] = balances_[_beneficiary].add(_tokenAmount);
}

}
17 changes: 12 additions & 5 deletions contracts/crowdsale/distribution/RefundableCrowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale {
using SafeMath for uint256;

// minimum amount of funds to be raised in weis
uint256 public goal;
uint256 private goal_;

// refund escrow used to hold funds while crowdsale is running
RefundEscrow private escrow_;
Expand All @@ -26,15 +26,22 @@ contract RefundableCrowdsale is FinalizableCrowdsale {
*/
constructor(uint256 _goal) public {
require(_goal > 0);
escrow_ = new RefundEscrow(wallet);
goal = _goal;
escrow_ = new RefundEscrow(getWallet());
goal_ = _goal;
}

/**
* @return minimum amount of funds to be raised in wei.
*/
function getGoal() public view returns(uint256) {
return goal_;
}

/**
* @dev Investors can claim refunds here if crowdsale is unsuccessful
*/
function claimRefund() public {
require(isFinalized);
require(isFinalized());
require(!goalReached());

escrow_.withdraw(msg.sender);
Expand All @@ -45,7 +52,7 @@ contract RefundableCrowdsale is FinalizableCrowdsale {
* @return Whether funding goal was reached
*/
function goalReached() public view returns (bool) {
return weiRaised >= goal;
return getWeiRaised() >= goal_;
}

/**
Expand Down
15 changes: 11 additions & 4 deletions contracts/crowdsale/emission/AllowanceCrowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,30 @@ contract AllowanceCrowdsale is Crowdsale {
using SafeMath for uint256;
using SafeERC20 for IERC20;

address public tokenWallet;
address private tokenWallet_;

/**
* @dev Constructor, takes token wallet address.
* @param _tokenWallet Address holding the tokens, which has approved allowance to the crowdsale
*/
constructor(address _tokenWallet) public {
require(_tokenWallet != address(0));
tokenWallet = _tokenWallet;
tokenWallet_ = _tokenWallet;
}

/**
* @return the address of the wallet that will hold the tokens.
*/
function getTokenWallet() public view returns(address) {
return tokenWallet_;
}

/**
* @dev Checks the amount of tokens left in the allowance.
* @return Amount of tokens left in the allowance
*/
function remainingTokens() public view returns (uint256) {
return token.allowance(tokenWallet, this);
return getToken().allowance(tokenWallet_, this);
}

/**
Expand All @@ -44,6 +51,6 @@ contract AllowanceCrowdsale is Crowdsale {
)
internal
{
token.safeTransferFrom(tokenWallet, _beneficiary, _tokenAmount);
getToken().safeTransferFrom(tokenWallet_, _beneficiary, _tokenAmount);
}
}
3 changes: 2 additions & 1 deletion contracts/crowdsale/emission/MintedCrowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ contract MintedCrowdsale is Crowdsale {
internal
{
// Potentially dangerous assumption about the type of the token.
require(ERC20Mintable(address(token)).mint(_beneficiary, _tokenAmount));
require(
ERC20Mintable(address(getToken())).mint(_beneficiary, _tokenAmount));
}
}
26 changes: 20 additions & 6 deletions contracts/crowdsale/price/IncreasingPriceCrowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import "../../math/SafeMath.sol";
contract IncreasingPriceCrowdsale is TimedCrowdsale {
using SafeMath for uint256;

uint256 public initialRate;
uint256 public finalRate;
uint256 private initialRate_;
uint256 private finalRate_;

/**
* @dev Constructor, takes initial and final rates of tokens received per wei contributed.
Expand All @@ -24,8 +24,22 @@ contract IncreasingPriceCrowdsale is TimedCrowdsale {
constructor(uint256 _initialRate, uint256 _finalRate) public {
require(_finalRate > 0);
require(_initialRate >= _finalRate);
initialRate = _initialRate;
finalRate = _finalRate;
initialRate_ = _initialRate;
finalRate_ = _finalRate;
}

/**
* @return the initial rate of the crowdsale.
*/
function getInitialRate() public view returns(uint256) {
return initialRate_;
}

/**
* @return the final rate of the crowdsale.
*/
function getFinalRate() public view returns (uint256) {
return finalRate_;
}

/**
Expand All @@ -37,8 +51,8 @@ contract IncreasingPriceCrowdsale is TimedCrowdsale {
// solium-disable-next-line security/no-block-members
uint256 elapsedTime = block.timestamp.sub(openingTime);
uint256 timeRange = closingTime.sub(openingTime);
uint256 rateRange = initialRate.sub(finalRate);
return initialRate.sub(elapsedTime.mul(rateRange).div(timeRange));
uint256 rateRange = initialRate_.sub(finalRate_);
return initialRate_.sub(elapsedTime.mul(rateRange).div(timeRange));
}

/**
Expand Down
15 changes: 11 additions & 4 deletions contracts/crowdsale/validation/CappedCrowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,30 @@ import "../Crowdsale.sol";
contract CappedCrowdsale is Crowdsale {
using SafeMath for uint256;

uint256 public cap;
uint256 private cap_;

/**
* @dev Constructor, takes maximum amount of wei accepted in the crowdsale.
* @param _cap Max amount of wei to be contributed
*/
constructor(uint256 _cap) public {
require(_cap > 0);
cap = _cap;
cap_ = _cap;
}

/**
* @return the cap of the crowdsale.
*/
function getCap() public view returns(uint256) {
return cap_;
}

/**
* @dev Checks whether the cap has been reached.
* @return Whether the cap was reached
*/
function capReached() public view returns (bool) {
return weiRaised >= cap;
return getWeiRaised() >= cap_;
}

/**
Expand All @@ -42,7 +49,7 @@ contract CappedCrowdsale is Crowdsale {
internal
{
super._preValidatePurchase(_beneficiary, _weiAmount);
require(weiRaised.add(_weiAmount) <= cap);
require(getWeiRaised().add(_weiAmount) <= cap_);
}

}
Loading