Skip to content

Instantly share code, notes, and snippets.

@mvaz
Last active December 14, 2017 22:30
Show Gist options
  • Save mvaz/63441561c14071713b8bfd0b012dd604 to your computer and use it in GitHub Desktop.
Save mvaz/63441561c14071713b8bfd0b012dd604 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.18;
// File: node_modules/zeppelin-solidity/contracts/ownership/Ownable.sol
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
* account.
*/
function Ownable() public {
owner = msg.sender;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
*/
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
// File: node_modules/zeppelin-solidity/contracts/lifecycle/Pausable.sol
/**
* @title Pausable
* @dev Base contract which allows children to implement an emergency stop mechanism.
*/
contract Pausable is Ownable {
event Pause();
event Unpause();
bool public paused = false;
/**
* @dev Modifier to make a function callable only when the contract is not paused.
*/
modifier whenNotPaused() {
require(!paused);
_;
}
/**
* @dev Modifier to make a function callable only when the contract is paused.
*/
modifier whenPaused() {
require(paused);
_;
}
/**
* @dev called by the owner to pause, triggers stopped state
*/
function pause() onlyOwner whenNotPaused public {
paused = true;
Pause();
}
/**
* @dev called by the owner to unpause, returns to normal state
*/
function unpause() onlyOwner whenPaused public {
paused = false;
Unpause();
}
}
// File: node_modules/zeppelin-solidity/contracts/math/SafeMath.sol
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
assert(c / a == b);
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}
// File: contracts/DryRunCrowdsale.sol
contract FxRates {
function getEurRate() public view returns(bytes32);
}
/**
* @title DryRunCrowdsale
* @dev This crowdsale contract filters investments made according to:
* - time
* - amount invested (in Wei)
* - eventually a whitelist of addresses?
*/
contract DryRunCrowdsale is Pausable {
using SafeMath for uint256;
/*// The token being sold
ComplexToken public token;*/
// start and end timestamps where investments are allowed (both inclusive)
uint256 public startTime; // Donnerstag morgen
uint256 public endTime; // Freitag abend
// address where funds are collected
address public wallet; // TODO wallet
// track which users made which investment
mapping(address => uint256) public investments;
// how many token units a buyer gets per wei
/* uint256 public rate; */
// amount of raised money in wei
uint256 public weiRaised;
uint256 public minWeiInvestment;
uint256 public maxWeiInvestment;
FxRates public fxRates;
/**
* event for token purchase logging
* @param purchaser who paid for the tokens
* @param beneficiary who got the tokens
* @param value weis paid for purchase
*/
event Investment(address indexed purchaser,
address indexed beneficiary,
uint256 value,
bytes32 fxRate);
function DryRunCrowdsale(uint256 _startTime, uint256 _endTime,
uint256 _minWeiInvestment, uint256 _maxWeiInvestment,
address _fxRates,
address _wallet) public {
require(_startTime >= now);
require(_endTime >= _startTime);
require(_minWeiInvestment > 0);
require(_maxWeiInvestment > _minWeiInvestment);
require(_fxRates != address(0));
require(_wallet != address(0));
startTime = _startTime;
endTime = _endTime;
minWeiInvestment = _minWeiInvestment;
maxWeiInvestment = _maxWeiInvestment;
fxRates = FxRates(_fxRates);
wallet = _wallet;
}
// fallback function can be used to buy tokens
function () external payable {
buyTokens(msg.sender);
}
// low level token purchase function
function buyTokens(address beneficiary) whenNotPaused public payable {
require(beneficiary != address(0));
require(validPurchase());
uint256 weiAmount = msg.value;
// update state
weiRaised = weiRaised.add(weiAmount);
bytes32 euroRate = fxRates.getEurRate();
Investment(msg.sender, beneficiary, weiAmount, euroRate);
forwardFunds();
// track how much was transfered
investments[beneficiary] = investments[beneficiary].add(weiAmount);
}
// send ether to the fund collection wallet
// override to create custom fund forwarding mechanisms
function forwardFunds() internal {
// TODO eventually here a refundable wallet
wallet.transfer(msg.value);
}
// overriding Crowdsale#validPurchase to add extra cap logic
// @return true if investors can buy at the moment
function validPurchase() internal view returns (bool) {
if (msg.value < minWeiInvestment || msg.value > maxWeiInvestment) {
return false;
}
bool withinPeriod = now >= startTime && now <= endTime;
bool nonZeroPurchase = msg.value != 0;
return withinPeriod && nonZeroPurchase;
}
// overriding Crowdsale#hasEnded to add cap logic
// @return true if crowdsale event has ended
function hasStarted() public view returns (bool) {
return now > startTime;
}
// overriding Crowdsale#hasEnded to add cap logic
// @return true if crowdsale event has ended
function hasEnded() public view returns (bool) {
return now > endTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment