aboutsummaryrefslogtreecommitdiffstats
path: root/docs/solidity-by-example.rst
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2017-05-03 18:28:16 +0800
committerGitHub <noreply@github.com>2017-05-03 18:28:16 +0800
commit4af0451d1675c07e44120e88507fa320f21112d5 (patch)
tree2ea2e38d50a493372d08e42434d10223a81ae80e /docs/solidity-by-example.rst
parentd92fbe643d0b4006bbb84cc738646a0d08f1a9ad (diff)
parente9458be7bdebf3fdabc9a5a63e372a56deaa9f5a (diff)
downloaddexon-solidity-4af0451d1675c07e44120e88507fa320f21112d5.tar
dexon-solidity-4af0451d1675c07e44120e88507fa320f21112d5.tar.gz
dexon-solidity-4af0451d1675c07e44120e88507fa320f21112d5.tar.bz2
dexon-solidity-4af0451d1675c07e44120e88507fa320f21112d5.tar.lz
dexon-solidity-4af0451d1675c07e44120e88507fa320f21112d5.tar.xz
dexon-solidity-4af0451d1675c07e44120e88507fa320f21112d5.tar.zst
dexon-solidity-4af0451d1675c07e44120e88507fa320f21112d5.zip
Merge pull request #2207 from ethereum/wski-develop
chore(Docs): Replaced instances if - throw to require() where applicable.
Diffstat (limited to 'docs/solidity-by-example.rst')
-rw-r--r--docs/solidity-by-example.rst114
1 files changed, 50 insertions, 64 deletions
diff --git a/docs/solidity-by-example.rst b/docs/solidity-by-example.rst
index 3d6221e4..4ed474df 100644
--- a/docs/solidity-by-example.rst
+++ b/docs/solidity-by-example.rst
@@ -36,7 +36,7 @@ of votes.
::
- pragma solidity ^0.4.0;
+ pragma solidity ^0.4.11;
/// @title Voting with delegation.
contract Ballot {
@@ -87,14 +87,14 @@ of votes.
// Give `voter` the right to vote on this ballot.
// May only be called by `chairperson`.
function giveRightToVote(address voter) {
- if (msg.sender != chairperson || voters[voter].voted) {
- // `throw` terminates and reverts all changes to
- // the state and to Ether balances. It is often
- // a good idea to use this if functions are
- // called incorrectly. But watch out, this
- // will also consume all provided gas.
- throw;
- }
+ // If the argument of `require` evaluates to `false`,
+ // it terminates and reverts all changes to
+ // the state and to Ether balances. It is often
+ // a good idea to use this if functions are
+ // called incorrectly. But watch out, this
+ // will currently also consume all provided gas
+ // (this is planned to change in the future).
+ require((msg.sender == chairperson) && !voters[voter].voted);
voters[voter].weight = 1;
}
@@ -102,12 +102,10 @@ of votes.
function delegate(address to) {
// assigns reference
Voter sender = voters[msg.sender];
- if (sender.voted)
- throw;
+ require(!sender.voted);
// Self-delegation is not allowed.
- if (to == msg.sender)
- throw;
+ require(to != msg.sender);
// Forward the delegation as long as
// `to` also delegated.
@@ -121,8 +119,7 @@ of votes.
to = voters[to].delegate;
// We found a loop in the delegation, not allowed.
- if (to == msg.sender)
- throw;
+ require(to != msg.sender);
}
// Since `sender` is a reference, this
@@ -145,8 +142,7 @@ of votes.
/// to proposal `proposals[proposal].name`.
function vote(uint proposal) {
Voter sender = voters[msg.sender];
- if (sender.voted)
- throw;
+ require(!sender.voted);
sender.voted = true;
sender.vote = proposal;
@@ -218,7 +214,7 @@ activate themselves.
::
- pragma solidity ^0.4.0;
+ pragma solidity ^0.4.11;
contract SimpleAuction {
// Parameters of the auction. Times are either
@@ -269,16 +265,15 @@ activate themselves.
// the transaction. The keyword payable
// is required for the function to
// be able to receive Ether.
- if (now > auctionStart + biddingTime) {
- // Revert the call if the bidding
- // period is over.
- throw;
- }
- if (msg.value <= highestBid) {
- // If the bid is not higher, send the
- // money back.
- throw;
- }
+
+ // Revert the call if the bidding
+ // period is over.
+ require(now <= (auctionStart + biddingTime));
+
+ // If the bid is not higher, send the
+ // money back.
+ require(msg.value > highestBid);
+
if (highestBidder != 0) {
// Sending back the money by simply using
// highestBidder.send(highestBid) is a security risk
@@ -327,18 +322,15 @@ activate themselves.
// external contracts.
// 1. Conditions
- if (now <= auctionStart + biddingTime)
- throw; // auction did not yet end
- if (ended)
- throw; // this function has already been called
+ require(now >= (auctionStart + biddingTime)); // auction did not yet end
+ require(!ended); // this function has already been called
// 2. Effects
ended = true;
AuctionEnded(highestBidder, highestBid);
// 3. Interaction
- if (!beneficiary.send(highestBid))
- throw;
+ beneficiary.transfer(highestBid);
}
}
@@ -381,7 +373,7 @@ high or low invalid bids.
::
- pragma solidity ^0.4.0;
+ pragma solidity ^0.4.11;
contract BlindAuction {
struct Bid {
@@ -409,8 +401,8 @@ high or low invalid bids.
/// functions. `onlyBefore` is applied to `bid` below:
/// The new function body is the modifier's body where
/// `_` is replaced by the old function body.
- modifier onlyBefore(uint _time) { if (now >= _time) throw; _; }
- modifier onlyAfter(uint _time) { if (now <= _time) throw; _; }
+ modifier onlyBefore(uint _time) { require(now < _time); _; }
+ modifier onlyAfter(uint _time) { require(now > _time); _; }
function BlindAuction(
uint _biddingTime,
@@ -454,13 +446,9 @@ high or low invalid bids.
onlyBefore(revealEnd)
{
uint length = bids[msg.sender].length;
- if (
- _values.length != length ||
- _fake.length != length ||
- _secret.length != length
- ) {
- throw;
- }
+ require(_values.length == length);
+ require(_fake.length == length);
+ require(_secret.length == length);
uint refund;
for (uint i = 0; i < length; i++) {
@@ -481,8 +469,7 @@ high or low invalid bids.
// the same deposit.
bid.blindedBid = 0;
}
- if (!msg.sender.send(refund))
- throw;
+ msg.sender.transfer(refund);
}
// This is an "internal" function which means that it
@@ -527,14 +514,12 @@ high or low invalid bids.
function auctionEnd()
onlyAfter(revealEnd)
{
- if (ended)
- throw;
+ require(!ended);
AuctionEnded(highestBidder, highestBid);
ended = true;
// We send all the money we have, because some
// of the refunds might have failed.
- if (!beneficiary.send(this.balance))
- throw;
+ beneficiary.transfer(this.balance);
}
}
@@ -546,7 +531,7 @@ Safe Remote Purchase
::
- pragma solidity ^0.4.0;
+ pragma solidity ^0.4.11;
contract Purchase {
uint public value;
@@ -558,26 +543,26 @@ Safe Remote Purchase
function Purchase() payable {
seller = msg.sender;
value = msg.value / 2;
- if (2 * value != msg.value) throw;
+ require((2 * value) == msg.value);
}
- modifier require(bool _condition) {
- if (!_condition) throw;
+ modifier condition(bool _condition) {
+ require(_condition);
_;
}
modifier onlyBuyer() {
- if (msg.sender != buyer) throw;
+ require(msg.sender == buyer);
_;
}
modifier onlySeller() {
- if (msg.sender != seller) throw;
+ require(msg.sender == seller);
_;
}
modifier inState(State _state) {
- if (state != _state) throw;
+ require(state == _state);
_;
}
@@ -594,8 +579,7 @@ Safe Remote Purchase
{
aborted();
state = State.Inactive;
- if (!seller.send(this.balance))
- throw;
+ seller.transfer(this.balance);
}
/// Confirm the purchase as buyer.
@@ -604,7 +588,7 @@ Safe Remote Purchase
/// is called.
function confirmPurchase()
inState(State.Created)
- require(msg.value == 2 * value)
+ condition(msg.value == (2 * value))
payable
{
purchaseConfirmed();
@@ -623,10 +607,12 @@ Safe Remote Purchase
// otherwise, the contracts called using `send` below
// can call in again here.
state = State.Inactive;
- // This actually allows both the buyer and the seller to
- // block the refund.
- if (!buyer.send(value) || !seller.send(this.balance))
- throw;
+
+ // NOTE: This actually allows both the buyer and the seller to
+ // block the refund - the withdraw pattern should be used.
+
+ buyer.transfer(value);
+ seller.transfer(this.balance));
}
}