Created
September 2, 2018 17:14
-
-
Save cybercapitalinvest/7c862668e6f65fb711d9d4e3c1ac1a4f to your computer and use it in GitHub Desktop.
smart contract to buy CCI tokens on presale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.13; | |
import "./oraclizeAPI.sol"; | |
/** | |
* @title SafeMath | |
* Math operations with safety checks | |
*/ | |
library SafeMath { | |
function mul(uint256 a, uint256 b) internal returns (uint256) { | |
uint256 c = a * b; | |
assert(a == 0 || c / a == b); | |
return c; | |
} | |
function div(uint256 a, uint256 b) internal 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 returns (uint256) { | |
assert(b <= a); | |
return a - b; | |
} | |
function add(uint256 a, uint256 b) internal returns (uint256) { | |
uint256 c = a + b; | |
assert(c >= a); | |
return c; | |
} | |
function max64(uint64 a, uint64 b) internal constant returns (uint64) { | |
return a >= b ? a : b; | |
} | |
function min64(uint64 a, uint64 b) internal constant returns (uint64) { | |
return a < b ? a : b; | |
} | |
function max256(uint256 a, uint256 b) internal constant returns (uint256) { | |
return a >= b ? a : b; | |
} | |
function min256(uint256 a, uint256 b) internal constant returns (uint256) { | |
return a < b ? a : b; | |
} | |
} | |
/** | |
* @title Ownable | |
* Base contract with an owner. | |
* Provides onlyOwner modifier, which prevents function from running if it is called by anyone other than the owner. | |
*/ | |
contract Ownable { | |
address public owner; | |
function Ownable() { | |
owner = msg.sender; | |
} | |
modifier onlyOwner() { | |
require(msg.sender == owner); | |
_; | |
} | |
function transferOwnership(address newOwner) onlyOwner { | |
if (newOwner != address(0)) { | |
owner = newOwner; | |
} | |
} | |
} | |
/** | |
* @title Haltable | |
* Abstract contract that allows children to implement an | |
* emergency stop mechanism. Differs from Pausable by causing a throw when in halt mode. | |
*/ | |
contract Haltable is Ownable { | |
bool public halted; | |
modifier stopInEmergency { | |
require(!halted); | |
_; | |
} | |
modifier onlyInEmergency { | |
require(halted); | |
_; | |
} | |
// called by the owner on emergency, triggers stopped state | |
function halt() external onlyOwner { | |
halted = true; | |
} | |
// called by the owner on end of emergency, returns to normal state | |
function unhalt() external onlyOwner onlyInEmergency { | |
halted = false; | |
} | |
} | |
/** | |
* @title ERC20 | |
* ERC20 interface | |
*/ | |
contract ERC20 { | |
uint256 public totalSupply; | |
function balanceOf(address _owner) constant returns (uint balance); | |
function transfer(address _to, uint _value); | |
function transferFrom(address _from, address _to, uint _value); | |
function approve(address _spender, uint _value); | |
function allowance(address _owner, address _spender) constant returns (uint remaining); | |
event Transfer(address indexed _from, address indexed _to, uint _value); | |
event Approval(address indexed _owner, address indexed _spender, uint _value); | |
} | |
/** | |
* @title StandardToken | |
* Standard ERC20-compliant token | |
* https://github.com/ethereum/EIPs/issues/20 | |
*/ | |
contract StandardToken is ERC20 { | |
using SafeMath for uint256; | |
mapping(address => uint256) balances; | |
mapping(address => mapping(address => uint256)) allowed; | |
function balanceOf(address _owner) constant returns (uint256 balance) { | |
return balances[_owner]; | |
} | |
/** | |
* fix for ERC20 short address attack | |
*/ | |
modifier onlyPayloadSize(uint256 size) { | |
require(msg.data.length >= size + 4); | |
_; | |
} | |
function transfer(address _to, uint _value) onlyPayloadSize(2 * 32) { | |
balances[msg.sender] = balances[msg.sender].sub(_value); | |
balances[_to] = balances[_to].add(_value); | |
Transfer(msg.sender, _to, _value); | |
} | |
function transferFrom(address _from, address _to, uint _value) onlyPayloadSize(3 * 32) { | |
var _allowance = allowed[_from][msg.sender]; | |
balances[_to] = balances[_to].add(_value); | |
balances[_from] = balances[_from].sub(_value); | |
allowed[_from][msg.sender] = _allowance.sub(_value); | |
Transfer(_from, _to, _value); | |
} | |
function approve(address _spender, uint _value) { | |
/** | |
* Allowed amount should be first set to 0 | |
* by calling approve(_spender, 0) in order to avoid this: | |
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 | |
*/ | |
require(_value == 0 || allowed[msg.sender][_spender] == 0); | |
allowed[msg.sender][_spender] = _value; | |
Approval(msg.sender, _spender, _value); | |
} | |
function allowance(address _owner, address _spender) constant returns (uint256 remaining) { | |
return allowed[_owner][_spender]; | |
} | |
} | |
contract CyberCapitalInvestToken is StandardToken { | |
string public name = "Cyber Capital Invest Token"; | |
string public symbol = "CCI"; | |
uint256 public decimals = 18; | |
uint256 public INITIAL_SUPPLY = 250000000 * 1 ether; | |
/** | |
* All tokens are allocated to creator. | |
*/ | |
function CyberCapitalInvestToken() { | |
totalSupply = INITIAL_SUPPLY; | |
balances[msg.sender] = INITIAL_SUPPLY; | |
} | |
} | |
contract CCISale is Haltable, usingOraclize { | |
using SafeMath for uint; | |
string public name = "Cyber Capital Invest Token Sale"; | |
CyberCapitalInvestToken public token; | |
address public beneficiary; | |
uint public ETHUSD; | |
uint public lastUpdateTime; | |
bool public crowdsaleFinished = false; | |
function CCISale( | |
address _token, | |
address _beneficiary, | |
uint _defaultETHUSD | |
) public { | |
token = CyberCapitalInvestToken(_token); | |
beneficiary = _beneficiary; | |
ETHUSD = _defaultETHUSD; | |
lastUpdateTime = 0; | |
} | |
function () public payable stopInEmergency { | |
require(msg.value > 0.01 * 1 ether); | |
require(!crowdsaleFinished); | |
uint tokens = ETHUSD.mul(msg.value).div(10); // one token costs 0.1 USD | |
token.transfer(msg.sender, tokens); | |
} | |
/* Set crowdsale is finished. May be used by CyberCapitalInvest team in case | |
* the hard cap is reached in total (BTC + ETH), but not reached | |
* solely on this smart contract */ | |
function setCrowdsaleFinished() public onlyOwner { | |
crowdsaleFinished = true; | |
uint remainingTokens = token.balanceOf(this); | |
token.transfer(beneficiary, remainingTokens); | |
} | |
function setPriceInCents(uint priceInCents) public onlyOwner { | |
ETHUSD = priceInCents; | |
} | |
function withdraw() public onlyOwner { | |
require(beneficiary.send(this.balance)); | |
} | |
function __callback(bytes32 myid, string result, bytes proof) public { | |
require(msg.sender == oraclize_cbAddress()); | |
uint value = parseInt(result, 2); // save it as USD cents | |
if (value > 0) { | |
ETHUSD = value; | |
} | |
if (now - lastUpdateTime > 86000) { //just so it does not update too often | |
lastUpdateTime = now; | |
update(86400); // schedule another check in 24 hours 86400 | |
} | |
} | |
function update(uint delay) public payable { | |
oraclize_query(delay, "URL", | |
"json(https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD).USD"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment