The ethereum.org crowdsale tutorial: contributions run to a deadline against a goal, pay out a separate reward token, and are refunded if the goal is missed.
Historical Significance
The ethereum.org crowdsale tutorial was the worked example most token sales of the period started from, and its shape, a deadline, a goal, a fixed price and a refund if the goal is missed, is the one the sales of the following two years repeated. The flaws are instructive too: unchecked send() calls and an unbounded refund loop are exactly the failures auditors spent the next years writing up.
Context
By 2016 the ethereum.org tutorial had been rewritten around the transfer interface that ERC-20 was standardising, and its crowdsale took its funding goal and token price in whole ether rather than wei. Copies of it went on chain steadily through the year, most of them as trials with short deadlines and small goals rather than as real sales.
Key Facts
Description
The crowdfunding contract from the ethereum.org tutorial. Its constructor fixes the beneficiary, the funding goal in whole ether, the length of the campaign in minutes, the price of one reward token in whole ether, and the token contract that pays contributors. None of them can be changed afterwards.
Contributions arrive through the fallback function, which records the sender and amount in a public funders array and calls transfer on the reward token for the amount divided by the price. After the deadline anyone may call checkGoalReached(): if the goal was met the balance is sent to the beneficiary, otherwise the contract loops over the funders and refunds them, and in both cases whatever remains is swept to the beneficiary and the sale is marked closed.
The reward tokens must already be held by this contract when the sale opens, and nothing checks that they are. The refund loop is unbounded and no send() return value is checked, so a campaign with many contributors can leave funds it cannot pay out.
This one pays 0xa9d8f1200e17fbf94b52720684051ae77735a777 on success. It was opened with a goal of 100 ether, a window of 60 hours, and a price of 1 ether for one token, handing out the token at 0x490d77514a359c313c1206992e1981e34c217a21.
Heuristic Analysis
The following characteristics were detected through bytecode analysis and may not be accurate.
DAO Fork Era
The controversial fork to recover funds from The DAO hack.
Bytecode Overview
Verified Source Available
This contract has verified source code on Etherscan.
Show source code (Solidity)
// Submitted by EthereumHistory (ethereumhistory.com)
contract token { function transfer(address receiver, uint amount){ } }
contract Crowdsale {
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;
event GoalReached(address beneficiary, uint amountRaised);
event FundTransfer(address backer, uint amount, bool isContribution);
bool crowdsaleClosed = false;
/* data structure to hold information about campaign contributors */
/* at initialization, setup the owner */
function Crowdsale(
address ifSuccessfulSendTo,
uint fundingGoalInEthers,
uint durationInMinutes,
uint etherCostOfEachToken,
token addressOfTokenUsedAsReward
) {
beneficiary = ifSuccessfulSendTo;
fundingGoal = fundingGoalInEthers * 1 ether;
deadline = now + durationInMinutes * 1 minutes;
price = etherCostOfEachToken * 1 ether;
tokenReward = token(addressOfTokenUsedAsReward);
}
/* The function without name is the default function that is called whenever anyone sends funds to a contract */
function () {
if (crowdsaleClosed) throw;
uint amount = msg.value;
balanceOf[msg.sender] = amount;
amountRaised += amount;
tokenReward.transfer(msg.sender, amount / price);
FundTransfer(msg.sender, amount, true);
}
modifier afterDeadline() { if (now >= deadline) _ }
/* 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;
}
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;
}
}
}
}
External Links
Related contracts
ShapeShift Chain-Split Forwarder
Same eraEarliest ShapeShift on-chain contract (Jul 24, 2016). Simple ETH forwarder with an active flag — routes deposits to target when active and msg.value > 0.
0xa2d5c5...d549deJuly 24, 2016Balance Router
Same eraDAO whitehat balance-conditional router: if EF/whitehat address holds >1M ETH, route to addr1; otherwise route to addr2.
0x6e9ccd...aee851July 25, 2016ShapeShift Chain-Split Receiver
Same eraShapeShift ETH/ETC routing contract (Jul 26, 2016). Forwards ETH to target only when on-chain forked() oracle result matches stored boolean — routing deposits to the correct wallet on each chain post-DAO-fork.
0x3e7756...2e7a25July 26, 2016ShapeShift Chain-Split Receiver
Same eraShapeShift ETH/ETC routing contract (Jul 26, 2016). Twin instance of the ShapeShiftReceiver — identical bytecode, configured for the opposite chain fork state.
0x89afcc...a51456July 26, 2016CanaryV7
Same eraA liveness canary for the Ethereum Alarm Clock, deployed 28 July 2016.
0x6c3abc...4aa180July 28, 2016Migrations
Same eraTruffle's Migrations bookkeeping contract: it records the number of the last completed migration so a rerun knows where to resume.
0x765c89...e36a52July 29, 2016