aboutsummaryrefslogtreecommitdiffstats
path: root/docs/solidity-by-example.rst
diff options
context:
space:
mode:
authorMatt Wisniewski <contact.mattdylan@gmail.com>2017-04-20 02:12:45 +0800
committerchriseth <chris@ethereum.org>2017-05-03 18:03:02 +0800
commit2b4b86aa7f72d8f7c2c4e250953d8f9018f4fd53 (patch)
tree34b54c98a4e9f359479bbc5b73945b3c74884c98 /docs/solidity-by-example.rst
parent00933b99cc64a5af1e00ed4b119fc725ddbb3513 (diff)
downloaddexon-solidity-2b4b86aa7f72d8f7c2c4e250953d8f9018f4fd53.tar.gz
dexon-solidity-2b4b86aa7f72d8f7c2c4e250953d8f9018f4fd53.tar.zst
dexon-solidity-2b4b86aa7f72d8f7c2c4e250953d8f9018f4fd53.zip
Update common-patterns.rst
Diffstat (limited to 'docs/solidity-by-example.rst')
-rw-r--r--docs/solidity-by-example.rst104
1 files changed, 44 insertions, 60 deletions
diff --git a/docs/solidity-by-example.rst b/docs/solidity-by-example.rst
index 3d6221e4..a8e7eb2c 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,12 @@ 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;
- }
+ // `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.
+ require(msg.sender == chairperson || !voters[voter].voted);
voters[voter].weight = 1;
}
@@ -102,12 +100,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 +117,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 +140,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 +212,7 @@ activate themselves.
::
- pragma solidity ^0.4.0;
+ pragma solidity ^0.4.11;
contract SimpleAuction {
// Parameters of the auction. Times are either
@@ -269,16 +263,16 @@ 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 +321,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;
+ require(beneficiary.transfer(highestBid));
}
}
@@ -381,7 +372,7 @@ high or low invalid bids.
::
- pragma solidity ^0.4.0;
+ pragma solidity ^0.4.11;
contract BlindAuction {
struct Bid {
@@ -409,8 +400,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 +445,11 @@ 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 ||
+ _fake.length == length ||
+ _secret.length == length
+ );
uint refund;
for (uint i = 0; i < length; i++) {
@@ -481,8 +470,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 +515,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 +532,7 @@ Safe Remote Purchase
::
- pragma solidity ^0.4.0;
+ pragma solidity ^0.4.11;
contract Purchase {
uint public value;
@@ -558,26 +544,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;
+ 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 +580,7 @@ Safe Remote Purchase
{
aborted();
state = State.Inactive;
- if (!seller.send(this.balance))
- throw;
+ seller.transfer(this.balance);
}
/// Confirm the purchase as buyer.
@@ -625,8 +610,7 @@ Safe Remote Purchase
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;
+ require(buyer.transfer(value) || seller.transfer(this.balance));
}
}