Created
October 19, 2017 13:06
-
-
Save ateregulov/40b14dc611e65fdfe83f65c2f6666086 to your computer and use it in GitHub Desktop.
onedollar crowdsale
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.16; | |
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol"; | |
interface token { | |
function transfer(address receiver, uint amount); | |
} | |
contract Crowdsale is usingOraclize{ | |
address public beneficiary; | |
uint public fundingGoal; | |
uint public amountRaised; | |
uint public deadline; | |
uint public price; | |
token public tokenReward; | |
mapping(address => uint256) public balanceOf; | |
bool fundingGoalReached = false; | |
bool crowdsaleClosed = false; | |
uint public ETHUSD; | |
event newOraclizeQuery(string description); | |
event newKrakenPriceTicker(string price); | |
function KrakenPriceTicker() { | |
// FIXME: enable oraclize_setProof is production | |
// oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS); | |
update(0); | |
} | |
function __callback(bytes32 myid, string result, bytes proof) { | |
if (msg.sender != oraclize_cbAddress()) throw; | |
newKrakenPriceTicker(result); | |
ETHUSD = parseInt(result, 2); // save it in storage as $ cents | |
// do something with ETHUSD | |
// update(60); // FIXME: comment this out to enable recursive price updates | |
} | |
event GoalReached(address beneficiary, uint amountRaised); | |
event FundTransfer(address backer, uint amount, bool isContribution); | |
/** | |
* Constrctor function | |
* | |
* Setup the owner | |
*/ | |
function Crowdsale( | |
address ifSuccessfulSendTo, | |
uint fundingGoalInEthers, | |
uint durationInMinutes, | |
uint etherCostOfEachToken, | |
address addressOfTokenUsedAsReward | |
) { | |
beneficiary = ifSuccessfulSendTo; | |
fundingGoal = fundingGoalInEthers * 1 ether; | |
deadline = now + durationInMinutes * 1 minutes; | |
tokenReward = token(addressOfTokenUsedAsReward); | |
} | |
/** | |
* Fallback function | |
* | |
* The function without name is the default function that is called whenever anyone sends funds to a contract | |
*/ | |
function () payable { | |
update(0); | |
require(!crowdsaleClosed); | |
uint amount = msg.value; | |
balanceOf[msg.sender] += amount; | |
amountRaised += amount; | |
uint price = ETHUSD; | |
tokenReward.transfer(msg.sender, amount / ( 1 / price)); | |
FundTransfer(msg.sender, amount, true); | |
} | |
modifier afterDeadline() { if (now >= deadline) _; } | |
/** | |
* Check if goal was reached | |
* | |
* Checks if the goal or time limit has been reached and ends the campaign | |
*/ | |
function checkGoalReached() afterDeadline { | |
if (amountRaised >= fundingGoal){ | |
fundingGoalReached = true; | |
GoalReached(beneficiary, amountRaised); | |
} | |
crowdsaleClosed = true; | |
} | |
/** | |
* Withdraw the funds | |
* | |
* Checks to see if goal or time limit has been reached, and if so, and the funding goal was reached, | |
* sends the entire amount to the beneficiary. If goal was not reached, each contributor can withdraw | |
* the amount they contributed. | |
*/ | |
function safeWithdrawal() afterDeadline { | |
if (!fundingGoalReached) { | |
uint amount = balanceOf[msg.sender]; | |
balanceOf[msg.sender] = 0; | |
if (amount > 0) { | |
if (msg.sender.send(amount)) { | |
FundTransfer(msg.sender, amount, false); | |
} else { | |
balanceOf[msg.sender] = amount; | |
} | |
} | |
} | |
if (fundingGoalReached && beneficiary == msg.sender) { | |
if (beneficiary.send(amountRaised)) { | |
FundTransfer(beneficiary, amountRaised, false); | |
} else { | |
//If we fail to send the funds to beneficiary, unlock funders balance | |
fundingGoalReached = false; | |
} | |
} | |
} | |
function update(uint delay) payable { | |
if (oraclize_getPrice("URL") > this.balance) { | |
newOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee"); | |
} else { | |
newOraclizeQuery("Oraclize query was sent, standing by for the answer.."); | |
oraclize_query(delay, "URL", "json(https://api.kraken.com/0/public/Ticker?pair=ETHUSD).result.XETHZUSD.c.0"); | |
} | |
} | |
} | |
contract KrakenPriceTicker is usingOraclize { | |
uint public ETHUSD; | |
event newOraclizeQuery(string description); | |
event newKrakenPriceTicker(string price); | |
function KrakenPriceTicker() { | |
// FIXME: enable oraclize_setProof is production | |
// oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS); | |
update(0); | |
} | |
function __callback(bytes32 myid, string result, bytes proof) { | |
if (msg.sender != oraclize_cbAddress()) throw; | |
newKrakenPriceTicker(result); | |
ETHUSD = parseInt(result, 2); // save it in storage as $ cents | |
// do something with ETHUSD | |
// update(60); // FIXME: comment this out to enable recursive price updates | |
} | |
function update(uint delay) payable { | |
if (oraclize_getPrice("URL") > this.balance) { | |
newOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee"); | |
} else { | |
newOraclizeQuery("Oraclize query was sent, standing by for the answer.."); | |
oraclize_query(delay, "URL", "json(https://api.kraken.com/0/public/Ticker?pair=ETHUSD).result.XETHZUSD.c.0"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment