Skip to content

Instantly share code, notes, and snippets.

@kn1g
Created December 17, 2017 17:44
Show Gist options
  • Save kn1g/2b82ad2678e75aca405483dc45a804bb to your computer and use it in GitHub Desktop.
Save kn1g/2b82ad2678e75aca405483dc45a804bb to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.18;
import './MyToken.sol';
contract Crowdsale{
using SafeMath for uint256;
// The token being sold
MintableToken public token = new MyToken();
// weather crowdsale is stopped/paused or live
bool public stopped = false;
// address where funds are collected
address public wallet;
// how many token units a buyer gets per wei in each tier
mapping (uint256 => uint256) public rate;
// max cap in eacht tier
mapping (uint256 => uint256) public cap;
// the current tier we are in
uint256 public currentTier;
// amount of token raised in "wei"
uint256 public tokenRaised = 0;
// amount of raised money in wei
uint256 public weiRaised = 0;
address public owner = 0;
event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
function Crowdsale() public {
// set token address
owner = msg.sender;
// set rates
rate[1] = 23000;
rate[2] = 22000;
rate[3] = 21000;
rate[4] = 20000;
// set caps in Wei
cap[1] = 100000000000000000;
cap[2] = 200000000000000000;
cap[3] = 300000000000000000;
cap[4] = 1000000000000000000;
// the current tier
currentTier = 1;
wallet = msg.sender;
}
// fallback function can be used to buy tokens
function () external payable {
buyTokens(msg.sender);
}
// token purchase function
function buyTokens(address beneficiary) public payable {
require(beneficiary != address(0));
require(!stopped);
require(msg.value <= 100000000000000000);
require(msg.value > 0);
uint256 weiAmount = msg.value;
uint256 tokens;
// in case that the cap is exceeded
if(weiRaised.add(weiAmount) > cap[currentTier]){
// split the fund
uint256 overFunded = weiRaised.add(weiAmount).sub(cap[currentTier]);
weiAmount = msg.value.sub(overFunded);
// update state
// calculate token amount to be created
tokens = weiAmount.mul(rate[currentTier]);
weiRaised = weiRaised.add(weiAmount);
tokenRaised = tokenRaised.add(tokens);
// first fraction of token
token.mint(beneficiary, tokens);
TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
forwardFunds(weiAmount);
// go into next tier
currentTier = currentTier+1;
if(currentTier > 4){
// Refund overspend wei
msg.sender.transfer(overFunded);
// stop token sale
stopped = true;
} else {
// fund the remaining
tokens = overFunded.mul(rate[currentTier]);
weiRaised = weiRaised.add(overFunded);
tokenRaised = tokenRaised.add(tokens);
// second fraction of token
token.mint(beneficiary, tokens);
TokenPurchase(msg.sender, beneficiary, overFunded, tokens);
forwardFunds(overFunded);
}
} else {
// calculate token amount to be created
tokens = weiAmount.mul(rate[currentTier]);
weiRaised = weiRaised.add(weiAmount);
tokenRaised = tokenRaised.add(tokens);
token.mint(beneficiary, tokens);
TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
forwardFunds(msg.value);
}
}
function forwardFunds(uint256 _amount) internal {
wallet.transfer(_amount);
}
function withdrawFunds() public{
require(msg.sender == owner);
require(this.balance > 0);
msg.sender.transfer(this.balance);
}
function shutThatShitDown() public{
require(msg.sender == owner);
selfdestruct(msg.sender);
}
}
pragma solidity ^0.4.18;
import './MintableToken.sol';
contract MyToken is MintableToken{
string public name = "MyToken";
string public symbol = "MYT";
uint256 public decimals = 18;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment