Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
49b16cf
Basic idea
eternauta1337 Feb 4, 2018
fc7af3c
Fine tuning idea
eternauta1337 Feb 5, 2018
d54f799
Add comments / tidy up Crowdsale base class
eternauta1337 Feb 5, 2018
f48c150
fixed TimedCrowdsale constructor
fiiiu Feb 7, 2018
3f5680b
added simple crowdsale test
fiiiu Feb 7, 2018
a8a14df
added HODL directory under home to store unused contracts. ugly hack …
fiiiu Feb 7, 2018
469a999
Capped no longer inherits from Timed, added capReached() method (repl…
fiiiu Feb 8, 2018
90f0973
added SafeMath in TimedCrowdsale for safety, CHECK whether it is inhe…
fiiiu Feb 8, 2018
3ffe518
several fixes related to separating Capped from Timed. functions rena…
fiiiu Feb 8, 2018
a5aaf94
added TimedCrowdsaleImpl.sol, TimedCrowdsale tests, passed
fiiiu Feb 8, 2018
8a3cfb9
added Whitelisted implementation and test, passed.
fiiiu Feb 8, 2018
6343246
removed unnecessary super constructor call in WhitelistedCrowdsale, r…
fiiiu Feb 8, 2018
2c22337
renamed UserCappedCrowdsale to IndividuallyCappedCrowdsale, implement…
fiiiu Feb 8, 2018
8c8fed1
homogeneized use of using SafeMath for uint256 across validation crow…
fiiiu Feb 8, 2018
962b5bc
adding questions.md where I track questions, bugs and progress
fiiiu Feb 8, 2018
bbb2dfa
modified VariablePriceCrowdsale, added Impl.
fiiiu Feb 9, 2018
3fdb6da
finished VariablePrice, fixed sign, added test, passing.
fiiiu Feb 9, 2018
53ec3cc
changed VariablePrice to IncreasingPrice, added corresponding require()
fiiiu Feb 14, 2018
2be5806
MintedCrowdsale done, mock implemented, test passing
fiiiu Feb 14, 2018
b814a05
PremintedCrowdsale done, mocks, tests passing
fiiiu Feb 14, 2018
dd05925
checked FinalizableCrowdsale
fiiiu Feb 14, 2018
120d277
PostDeliveryCrowdsale done, mock, tests passing.
fiiiu Feb 14, 2018
e677e33
RefundableCrowdsale done. Detached Vault. modified mock and test, pas…
fiiiu Feb 14, 2018
5873f8e
renamed crowdsale-refactor to crowdsale in contracts and test
fiiiu Feb 14, 2018
2fe239c
deleted HODL old contracts
fiiiu Feb 15, 2018
bf5e9dd
polished variable names in tests
fiiiu Feb 15, 2018
cd0ba80
fixed typos and removed comments in tests
fiiiu Feb 15, 2018
0487d25
Renamed 'crowdsale-refactor' to 'crowdsale' in all imports
eternauta1337 Feb 15, 2018
c4a2f9c
Fix minor param naming issues in Crowdsale functions and added docume…
eternauta1337 Feb 15, 2018
3ef55bc
Added documentation to Crowdsale extensions
eternauta1337 Feb 15, 2018
c1a41ae
removed residual comments and progress tracking files
fiiiu Feb 15, 2018
b455000
added docs for validation crowdsales
fiiiu Feb 15, 2018
a841691
Made user promises in PostDeliveryCrowdsale public so that users can …
eternauta1337 Feb 15, 2018
3d4d41a
added docs for distribution crowdsales
fiiiu Feb 15, 2018
8b6b342
renamed PremintedCrowdsale to AllowanceCrowdsale
fiiiu Feb 16, 2018
1a2a5ec
added allowance check function and corresponding test. fixed filename…
fiiiu Feb 16, 2018
1680a18
spilt Crowdsale _postValidatePurchase in _postValidatePurchase and _u…
fiiiu Feb 16, 2018
344bec6
polished tests for linter, salve Travis
fiiiu Feb 16, 2018
7d4035a
polished IncreasingPriceCrowdsale.sol for linter.
fiiiu Feb 16, 2018
bcd0464
renamed and polished for linter WhitelistedCrowdsale test.
fiiiu Feb 16, 2018
51c0954
fixed indentation in IncreasingPriceCrowdsaleImpl.sol for linter
fiiiu Feb 16, 2018
cd15b41
fixed ignoring token.mint return value in MintedCrowdsale.sol
fiiiu Feb 17, 2018
e716a22
expanded docs throughout, fixed minor issues
fiiiu Feb 19, 2018
7134f0d
extended test coverage for IndividuallyCappedCrowdsale
fiiiu Feb 19, 2018
f56116e
Extended WhitelistedCrwodsale test coverage
fiiiu Feb 19, 2018
2a6dd9f
roll back decoupling of RefundVault in RefundableCrowdsale
fiiiu Feb 19, 2018
8e8e8cc
moved cap exceedance checks in Capped and IndividuallyCapped crowdsal…
fiiiu Feb 19, 2018
fa055a6
revert name change, IndividuallyCapped to UserCapped
fiiiu Feb 19, 2018
56e83b9
extended docs.
fiiiu Feb 19, 2018
1bf30f9
added crowd whitelisting with tests
fiiiu Feb 19, 2018
a70e3ed
added group capping, plus tests
fiiiu Feb 19, 2018
5f4fc83
added modifiers in TimedCrowdsale and WhitelistedCrowdsale
fiiiu Feb 19, 2018
47ce79f
polished tests for linter
fiiiu Feb 19, 2018
5dab495
moved check of whitelisted to modifier, mainly for testing coverage
fiiiu Feb 19, 2018
dd338f1
fixed minor ordering/polishingafter review
fiiiu Feb 19, 2018
8376baa
modified TimedCrowdsale modifier/constructor ordering
fiiiu Feb 19, 2018
8ad6a10
unchanged truffle-config.js
fiiiu Feb 20, 2018
107fdc4
changed indentation of visibility modifier in mocks
fiiiu Feb 20, 2018
530d85d
changed naming of modifier and function to use Open/Closed for TimedC…
fiiiu Feb 20, 2018
9fe61b8
changed ordering of constructor calls in SampleCrowdsale
fiiiu Feb 20, 2018
de8e88f
changed startTime and endTime to openingTime and closingTime throughout
fiiiu Feb 20, 2018
6c38f46
fixed exceeding line lenght for linter
fiiiu Feb 20, 2018
33839c1
Merge branch 'master' into feature/crowdsale-refactor
fiiiu Feb 20, 2018
df03099
renamed _emitTokens to _deliverTokens
fiiiu Feb 20, 2018
c6def0e
renamed addCrowdToWhitelist to addManyToWhitelist
fiiiu Feb 20, 2018
c177f01
renamed UserCappedCrowdsale to IndividuallyCappedCrowdsale
fiiiu Feb 20, 2018
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
Basic idea
  • Loading branch information
eternauta1337 committed Feb 15, 2018
commit 49b16cf64bd38b18d25154f4ef45e73b09c3ffbd
109 changes: 109 additions & 0 deletions contracts/crowdsale-refactor/Crowdsale.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
pragma solidity ^0.4.18;

import "../token/ERC20/MintableToken.sol";
import "../math/SafeMath.sol";

contract Crowdsale {
using SafeMath for uint256;

ERC20 public token;

uint256 public startTime;
uint256 public endTime;

address public wallet;
uint256 public rate;
uint256 public weiRaised;

event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);

function Crowdsale(uint256 _startTime, uint256 _endTime, uint256 _rate, address _wallet, ERC20 _token) public {
require(_startTime >= now);
require(_endTime >= _startTime);
require(_rate > 0);
require(_wallet != address(0));
require(_token != address(0));

startTime = _startTime;
endTime = _endTime;
rate = _rate;
wallet = _wallet;
token = _token;
}

// -----------------------------------------
// Crowdsale external interface
// -----------------------------------------

function () external payable {
buyTokens(msg.sender);
}

function buyTokens(address beneficiary) public payable {

uint256 weiAmount = msg.value;
preValidatePurchase(beneficiary, weiAmount);

// calculate token amount to be created
uint256 tokens = getTokenAmount(weiAmount);

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

processPurchase(beneficiary, tokens);
TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);

forwardFunds();
postValidatePurchase(beneficiary, weiAmount);
}

function hasEnded() public view returns (bool) {
return now > endTime;
}

// -----------------------------------------
// Purchase validation
// -----------------------------------------

function preValidatePurchase(address _beneficiary, uint256 _weiAmount) internal {
require(_beneficiary != address(0));
require(now >= startTime && now <= endTime);
require(_weiAmount != 0);
}

function postValidatePurchase(address beneficiary, uint256 weiAmount) internal {
// optional override
}

// -----------------------------------------
// Emission
// -----------------------------------------

function emitTokens(address _beneficiary, uint256 _tokenAmount) internal {
token.transfer(_beneficiary, _tokenAmount);
}

// -----------------------------------------
// Token distribution
// -----------------------------------------

function processPurchase(address _beneficiary, uint256 _tokenAmount) internal {
emitTokens(_beneficiary, _tokenAmount);
}

// -----------------------------------------
// Price calculation
// -----------------------------------------

function getTokenAmount(uint256 weiAmount) internal view returns (uint256) {
return weiAmount.mul(rate);
}

// -----------------------------------------
// Funds distribution
// -----------------------------------------

function forwardFunds() internal {
wallet.transfer(msg.value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO: overrides getTokenAmount()
41 changes: 41 additions & 0 deletions contracts/crowdsale-refactor/distribution/FinalizableCrowdsale.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
pragma solidity ^0.4.18;

import "../../math/SafeMath.sol";
import "../../ownership/Ownable.sol";
import "../CrowdsaleBase.sol";


/**
* @title FinalizableCrowdsale
* @dev Extension of Crowdsale where an owner can do extra work
* after finishing.
*/
contract FinalizableCrowdsale is CrowdsaleBase, Ownable {
using SafeMath for uint256;

bool public isFinalized = false;

event Finalized();

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

finalization();
Finalized();

isFinalized = true;
}

/**
* @dev Can be overridden to add finalization logic. The overriding function
* should call super.finalization() to ensure the chain of finalization is
* executed entirely.
*/
function finalization() internal {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pragma solidity ^0.4.18;

import "../CrowdsaleBase.sol";
import "../../token/ERC20/ERC20.sol";

contract PostDeliveryCrowdsale is CrowdsaleBase {

mapping(address => uint256) promised;

function processPurchase(address _beneficiary, uint256 _tokenAmount) internal {
promises[_beneficiary] = promises[_beneficiary].add(_tokenAmount);
}

function withdrawTokens() public {
require(hasEnded());
uint256 amount = promises[msg.sender];
require(amount > 0);
promises[msg.sender] = 0;
emitTokens(msg.sender, amount);
}
}
60 changes: 60 additions & 0 deletions contracts/crowdsale-refactor/distribution/RefundableCrowdsale.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
pragma solidity ^0.4.18;


import "../../math/SafeMath.sol";
import "./FinalizableCrowdsale.sol";
import "./utils/RefundVault.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.
*/
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;

function RefundableCrowdsale(uint256 _goal) public {
require(_goal > 0);
vault = new RefundVault(wallet);
goal = _goal;
}

// if crowdsale is unsuccessful, investors can claim refunds here
function claimRefund() public {
require(isFinalized);
require(!goalReached());

vault.refund(msg.sender);
}

function goalReached() public view returns (bool) {
return weiRaised >= goal;
}

// vault finalization task, called when owner calls finalize()
function finalization() internal {
if (goalReached()) {
vault.close();
} else {
vault.enableRefunds();
}

super.finalization();
}

// We're overriding the fund forwarding from Crowdsale.
// In addition to sending the funds, we want to call
// the RefundVault deposit function
function forwardFunds() internal {
vault.deposit.value(msg.value)(msg.sender);
}

}
57 changes: 57 additions & 0 deletions contracts/crowdsale-refactor/distribution/utils/RefundVault.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
pragma solidity ^0.4.18;

import "../../../math/SafeMath.sol";
import "../../../ownership/Ownable.sol";


/**
* @title RefundVault
* @dev This contract is used for storing funds while a crowdsale
* is in progress. Supports refunding the money if crowdsale fails,
* and forwarding it if crowdsale is successful.
*/
contract RefundVault is Ownable {
using SafeMath for uint256;

enum State { Active, Refunding, Closed }

mapping (address => uint256) public deposited;
address public wallet;
State public state;

event Closed();
event RefundsEnabled();
event Refunded(address indexed beneficiary, uint256 weiAmount);

function RefundVault(address _wallet) public {
require(_wallet != address(0));
wallet = _wallet;
state = State.Active;
}

function deposit(address investor) onlyOwner public payable {
require(state == State.Active);
deposited[investor] = deposited[investor].add(msg.value);
}

function close() onlyOwner public {
require(state == State.Active);
state = State.Closed;
Closed();
wallet.transfer(this.balance);
}

function enableRefunds() onlyOwner public {
require(state == State.Active);
state = State.Refunding;
RefundsEnabled();
}

function refund(address investor) public {
require(state == State.Refunding);
uint256 depositedValue = deposited[investor];
deposited[investor] = 0;
investor.transfer(depositedValue);
Refunded(investor, depositedValue);
}
}
23 changes: 23 additions & 0 deletions contracts/crowdsale-refactor/emission/ApprovedCrowdsale.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pragma solidity ^0.4.18;

import "../Crowdsale.sol";
import "../../token/ERC20/ERC20.sol";

contract ApprovedCrowdsale is Crowdsale {

ERC20 public token;
address public tokenOwner;

function ApprovedCrowdsale(ERC20 _token, address _tokenOwner) {
require(_token != address(0));
require(_tokenOwner != address(0));
token = _token;
tokenOwner = _tokenOwner;
}

// TODO: consider querying approval left and end crowdsale if depleted

function emitTokens(address _beneficiary, uint256 _tokenAmount) internal {
token.transferFrom(tokenOwner, _beneficiary, _tokenAmount);
}
}
18 changes: 18 additions & 0 deletions contracts/crowdsale-refactor/emission/MintedCrowdsale.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pragma solidity ^0.4.18;

import "../CrowdsaleBase.sol";
import "../../token/ERC20/MintableToken.sol";

contract MintedCrowdsale is CrowdsaleBase {

MintableToken token;

function MintedCrowdsale(MintableToken _token) {
require(_token != address(0));
token = _token;
}

function emitTokens(address _beneficiary, uint256 _tokenAmount) internal {
token.mint(_beneficiary, _tokenAmount);
}
}
26 changes: 26 additions & 0 deletions contracts/crowdsale-refactor/validation/CappedCrowdsale.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pragma solidity ^0.4.18;

import "../../math/SafeMath.sol";
import "../CrowdsaleBase.sol";

contract CappedCrowdsale is CrowdsaleBase {
using SafeMath for uint256;

uint256 public cap;

function CappedCrowdsale(uint256 _cap) public {
require(_cap > 0);
cap = _cap;
}

function hasEnded() public view returns (bool) {
bool capReached = weiRaised >= cap;
return capReached || super.hasEnded();
}

function postValidatePurchase(address _beneficiary, uint256 _weiAmount) internal {
super.postValidatePurchase(_beneficiary, _weiAmount);
require(weiRaised <= cap);
}

}
28 changes: 28 additions & 0 deletions contracts/crowdsale-refactor/validation/UserCappedCrowdsale.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pragma solidity ^ 0.4.18;

import "../CrowdsaleBase.sol";
import "../../ownership/Ownable.sol";

contract UserCappedCrowdsale is CrowdsaleBase, Ownable {

mapping(address => uint256) public contributions;
mapping(address => uint256) public caps;

function setUserCap(address _beneficiary, uint256 _cap) external onlyOwner {
caps[_beneficiary] = _cap;
}

function getUserCap(address _beneficiary) public view returns (uint256) {
return caps[_beneficiary];
}

function getUserContribution(address _beneficiary) public view returns (uint256) {
return contributions[_beneficiary];
}

function postValidatePurchase(address _beneficiary, uint256 _weiAmount) internal {
super.postValidatePurchase(_beneficiary, _weiAmount);
contributions[_beneficiary] = contributions[_beneficiary].add(_weiAmount);
require(contributions[_beneficiary] <= caps[_beneficiary]);
}
}
Loading