Last active
February 7, 2018 23:21
-
-
Save safead/8e8b4ff069fd4612fa541a1d88403f97 to your computer and use it in GitHub Desktop.
SAFE Pre-Sale Contract v 1.0
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.18; | |
/* SAFEPreSale contract v 1.1 */ | |
contract SAFEToken{ | |
function mint(address[], uint256[]) public pure returns (bool){} | |
} | |
contract SAFEPreSale{ | |
event PurchasedTokens(address indexed _purchaser, uint256 _ether, uint256 _tokens, uint256 _referralEther, uint256 _ourEther); | |
event WalletSet(address _addr); | |
event TokenWalletSet(address _addr, uint16 _perc); | |
event BountyWalletSet(address _addr, uint16 _perc); | |
event NewEarlyBuyer(address _addr, uint16 _discount, uint256 _minValue); | |
event DeletedEarlyBuyer(address _addr); | |
address owner; | |
SAFEToken token; | |
address tokenContract; | |
uint256 public tokensForOneEther; | |
uint16 public discount = 0; | |
uint16 public referralPercentage = 0; | |
uint16 public referredDiscount = 0; | |
bool public frozen = false; | |
address wallet; | |
address tokenWallet; | |
address bountyWallet; | |
uint16 bountyWalletPercentage = 0; | |
uint16 tokenWalletPercentage = 50; | |
struct earlyBuyer{ | |
uint16 _discount; | |
uint256 _minValue; | |
} | |
mapping(address => earlyBuyer) earlyBuyers; | |
struct refundApproval{ | |
uint256 _valueTokens; | |
uint256 _valueEther; | |
} | |
mapping(address => refundApproval) refundApprovals; | |
modifier onlyOwner{ | |
require(msg.sender == owner); | |
_; | |
} | |
modifier notFrozen{ | |
assert(frozen == false); | |
_; | |
} | |
function SAFEPreSale(address _tokenContract) public { | |
owner = msg.sender; | |
tokenContract = _tokenContract; | |
} | |
function checkDiscount(address _addr) public view returns (uint16 value, uint256 minAmount){ | |
if(earlyBuyers[_addr]._discount == 0) return (discount, 0); | |
return (earlyBuyers[_addr]._discount, earlyBuyers[_addr]._minValue); | |
} | |
function getDiscount(address _addr, uint256 _purchaseValue) private view returns (uint16 value){ | |
if(earlyBuyers[_addr]._discount > 0 && _purchaseValue / (1 ether) >= earlyBuyers[_addr]._minValue) return earlyBuyers[_addr]._discount; | |
return discount; | |
} | |
function purchaseTokens(address _referrerWallet) public payable notFrozen { | |
uint16 percentage = 1000 - tokenWalletPercentage - bountyWalletPercentage; | |
uint16 currentDiscount = getDiscount(msg.sender, msg.value); | |
uint256 value = msg.value; | |
uint256 valueToReferral = 0; | |
if( | |
currentDiscount == discount && | |
_referrerWallet != address(0) && | |
(referralPercentage > 0 || referredDiscount > 0) | |
){ | |
currentDiscount = currentDiscount + referredDiscount; | |
valueToReferral = value * referralPercentage / 1000; | |
value = value - valueToReferral; | |
} | |
uint256 tokensToBuyerValue = msg.value * tokensForOneEther * 1000 / (1000 - currentDiscount); | |
uint8 arrayLength = 1; | |
uint256 tokenWalletValue = tokensToBuyerValue * tokenWalletPercentage / percentage; | |
uint256 bountyWalletValue = tokensToBuyerValue * bountyWalletPercentage / percentage; | |
SAFEToken minter = SAFEToken(tokenContract); | |
if(tokenWalletValue > 0) arrayLength++; | |
if(bountyWalletValue > 0) arrayLength++; | |
address[] memory receivers = new address[](arrayLength); | |
uint256[] memory values = new uint256[](arrayLength); | |
receivers[0] = msg.sender; | |
values[0] = tokensToBuyerValue; | |
if(tokenWalletValue > 0){ | |
receivers[1] = tokenWallet; | |
values[1] = tokenWalletValue; | |
} | |
if(bountyWalletValue > 0){ | |
receivers[2] = bountyWallet; | |
values[2] = bountyWalletValue; | |
} | |
wallet.transfer(value); | |
minter.mint(receivers, values); | |
if(valueToReferral > 0) _referrerWallet.transfer(valueToReferral); | |
if(currentDiscount != discount) delete earlyBuyers[msg.sender]; | |
PurchasedTokens(msg.sender, msg.value, tokensToBuyerValue, valueToReferral, value); | |
} | |
function setWallet(address _addr) onlyOwner public { | |
require(_addr != address(0)); | |
wallet = _addr; | |
WalletSet(wallet); | |
} | |
function getWallet() public onlyOwner view returns (address acc){ | |
return (wallet); | |
} | |
function setTokenWallet(address _addr, uint16 _percentage) onlyOwner public { | |
require(_addr != address(0) && _percentage < 1001); | |
tokenWallet = _addr; | |
tokenWalletPercentage = _percentage; | |
TokenWalletSet(tokenWallet, tokenWalletPercentage); | |
} | |
function getTokenWallet() public onlyOwner view returns (address acc, uint16 perc){ | |
return (tokenWallet, tokenWalletPercentage); | |
} | |
function setBountyWallet(address _addr, uint16 _percentage) onlyOwner public { | |
require(_addr != address(0) && _percentage < 1001); | |
bountyWallet = _addr; | |
bountyWalletPercentage = _percentage; | |
BountyWalletSet(bountyWallet, bountyWalletPercentage); | |
} | |
function getBountyWallet() public onlyOwner view returns (address acc, uint16 perc){ | |
return (bountyWallet, bountyWalletPercentage); | |
} | |
function changeTokensForOneEther(uint256 _newtokensForOneEther) public onlyOwner { | |
require(_newtokensForOneEther != 0); | |
tokensForOneEther = _newtokensForOneEther; | |
} | |
function changeDiscount(uint16 _newDiscount) public onlyOwner { | |
require(_newDiscount < 1001); | |
discount = _newDiscount; | |
} | |
function changeReferralRules(uint16 _referralPercentage, uint16 _referredDiscount) public onlyOwner { | |
require(_referralPercentage < 100 && _referredDiscount < 100); | |
referralPercentage = _referralPercentage; | |
referredDiscount = _referredDiscount; | |
} | |
function emergencyToggle() public onlyOwner { | |
frozen = !frozen; | |
} | |
function addEarlyBuyer(address _addr, uint16 _discount, uint256 _minValue) public onlyOwner { | |
require(_discount > 0 && _discount < 1001); | |
earlyBuyers[_addr] = earlyBuyer(_discount, _minValue); | |
NewEarlyBuyer(_addr, _discount, _minValue); | |
} | |
function getEarlyBuyer(address _addr) public view onlyOwner returns(uint16 dis, uint256 minValue){ | |
require(earlyBuyers[_addr]._discount > 0); | |
return (earlyBuyers[_addr]._discount, earlyBuyers[_addr]._minValue); | |
} | |
function deleteEarlyBuyer(address _addr) public onlyOwner { | |
require(earlyBuyers[_addr]._discount > 0); | |
delete earlyBuyers[_addr]; | |
DeletedEarlyBuyer(_addr); | |
} | |
function changeOwner(address _newOwner) public onlyOwner { | |
require(_newOwner != 0); | |
owner = _newOwner; | |
} | |
function() public payable{ | |
purchaseTokens(address(0)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SAFE token contract: https://gist.github.com/safead/17f523bad49327ab5a706606757da51b
SAFE Pre-Sale truffle tests: https://gist.github.com/safead/5fb624547ed983770c5981a0662701c1