@@ -128,48 +128,105 @@ contract SimpleAuction {
128128pragma solidity ^0.4.22;
129129
130130contract SimpleAuction {
131+ // Parameters of the auction. Times are either
132+ // absolute unix timestamps (seconds since 1970-01-01)
133+ // or time periods in seconds.
131134 address public beneficiary ;
132135 uint public auctionEnd ;
136+ // Current state of the auction.
133137 address public highestBidder ;
134138 uint public highestBid ;
139+ // Allowed withdrawals of previous bids
135140 mapping (address => uint ) pendingReturns ;
141+ // Set to true at the end, disallows any change
136142 bool ended ;
143+ // Events that will be fired on changes.
137144 event HighestBidIncreased (address bidder , uint amount );
138145 event AuctionEnded (address winner , uint amount );
146+ // The following is a so-called natspec comment,
147+ // recognizable by the three slashes.
148+ // It will be shown when the user is asked to
149+ // confirm a transaction.
150+
151+ // / Create a simple auction with \`_biddingTime\`
152+ // / seconds bidding time on behalf of the
153+ // / beneficiary address \`_beneficiary\`.
139154
140155 constructor (uint _biddingTime , address _beneficiary ) public {
141156 beneficiary = _beneficiary;
142157 auctionEnd = now + _biddingTime;
143158 }
159+ // / Bid on the auction with the value sent
160+ // / together with this transaction.
161+ // / The value will only be refunded if the
162+ // / auction is not won.
144163
145164 function bid() public payable {
165+ // No arguments are necessary, all
166+ // information is already part of
167+ // the transaction. The keyword payable
168+ // is required for the function to
169+ // be able to receive Ether.
170+
171+ // Revert the call if the bidding
172+ // period is over.
146173 require (now <= auctionEnd , " Auction already ended." );
174+ // If the bid is not higher, send the
175+ // money back.
147176 require (msg .value > highestBid , " There already is a higher bid." );
148177 if (highestBid != 0 ) {
178+ // Sending back the money by simply using
179+ // highestBidder.send(highestBid) is a security risk
180+ // because it could execute an untrusted contract.
181+ // It is always safer to let the recipients
182+ // withdraw their money themselves.
149183 pendingReturns [highestBidder ] += highestBid ;
150184 }
151185 highestBidder = msg .sender ;
152186 highestBid = msg .value ;
153187 emit HighestBidIncreased (msg .sender , msg .value );
154188 }
189+ // / Withdraw a bid that was overbid.
155190
156191 function withdraw() public returns(bool ) {
157192 uint amount = pendingReturns [msg .sender ];
158193 if (amount > 0 ) {
194+ // It is important to set this to zero because the recipient
195+ // can call this function again as part of the receiving call
196+ // before \`send\` returns.
159197 pendingReturns [msg .sender ] = 0 ;
160198 if (! msg .sender .send (amount )) {
199+ // No need to call throw here, just reset the amount owing
161200 pendingReturns [msg .sender ] = amount ;
162201 return false ;
163202 }
164203 }
165204 return true ;
166205 }
206+ // / End the auction and send the highest bid
207+ // / to the beneficiary.
167208
168209 function auctionEnd() public {
210+ // It is a good guideline to structure functions that interact
211+ // with other contracts (i.e. they call functions or send Ether)
212+ // into three phases:
213+ // 1. checking conditions
214+ // 2. performing actions (potentially changing conditions)
215+ // 3. interacting with other contracts
216+ // If these phases are mixed up, the other contract could call
217+ // back into the current contract and modify the state or cause
218+ // effects (ether payout) to be performed multiple times.
219+ // If functions called internally include interaction with external
220+ // contracts, they also have to be considered interaction with
221+ // external contracts.
222+
223+ // 1. Conditions
169224 require (now >= auctionEnd , " Auction not yet ended." );
170225 require (! ended , " auctionEnd has already been called." );
226+ // 2. Effects
171227 ended = true ;
172228 emit AuctionEnded (highestBidder , highestBid );
229+ // 3. Interaction
173230 beneficiary .transfer (highestBid );
174231 }
175232}
0 commit comments