mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Update common-patterns.rst
This commit is contained in:
committed by
chriseth
parent
00933b99cc
commit
2b4b86aa7f
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user