@@ -133,16 +133,21 @@ contract SimpleAuction {
133133 // or time periods in seconds.
134134 address public beneficiary ;
135135 uint public auctionEnd ;
136+
136137 // Current state of the auction.
137138 address public highestBidder ;
138139 uint public highestBid ;
140+
139141 // Allowed withdrawals of previous bids
140142 mapping (address => uint ) pendingReturns ;
143+
141144 // Set to true at the end, disallows any change
142145 bool ended ;
146+
143147 // Events that will be fired on changes.
144148 event HighestBidIncreased (address bidder , uint amount );
145149 event AuctionEnded (address winner , uint amount );
150+
146151 // The following is a so-called natspec comment,
147152 // recognizable by the three slashes.
148153 // It will be shown when the user is asked to
@@ -151,16 +156,15 @@ contract SimpleAuction {
151156 // / Create a simple auction with \`_biddingTime\`
152157 // / seconds bidding time on behalf of the
153158 // / beneficiary address \`_beneficiary\`.
154-
155159 constructor (uint _biddingTime , address _beneficiary ) public {
156160 beneficiary = _beneficiary;
157161 auctionEnd = now + _biddingTime;
158162 }
163+
159164 // / Bid on the auction with the value sent
160165 // / together with this transaction.
161166 // / The value will only be refunded if the
162167 // / auction is not won.
163-
164168 function bid() public payable {
165169 // No arguments are necessary, all
166170 // information is already part of
@@ -171,9 +175,11 @@ contract SimpleAuction {
171175 // Revert the call if the bidding
172176 // period is over.
173177 require (now <= auctionEnd , " Auction already ended." );
178+
174179 // If the bid is not higher, send the
175180 // money back.
176181 require (msg .value > highestBid , " There already is a higher bid." );
182+
177183 if (highestBid != 0 ) {
178184 // Sending back the money by simply using
179185 // highestBidder.send(highestBid) is a security risk
@@ -186,15 +192,16 @@ contract SimpleAuction {
186192 highestBid = msg .value ;
187193 emit HighestBidIncreased (msg .sender , msg .value );
188194 }
189- // / Withdraw a bid that was overbid.
190195
196+ // / Withdraw a bid that was overbid.
191197 function withdraw() public returns(bool ) {
192198 uint amount = pendingReturns [msg .sender ];
193199 if (amount > 0 ) {
194200 // It is important to set this to zero because the recipient
195201 // can call this function again as part of the receiving call
196202 // before \`send\` returns.
197203 pendingReturns [msg .sender ] = 0 ;
204+
198205 if (! msg .sender .send (amount )) {
199206 // No need to call throw here, just reset the amount owing
200207 pendingReturns [msg .sender ] = amount ;
@@ -203,9 +210,9 @@ contract SimpleAuction {
203210 }
204211 return true ;
205212 }
213+
206214 // / End the auction and send the highest bid
207215 // / to the beneficiary.
208-
209216 function auctionEnd() public {
210217 // It is a good guideline to structure functions that interact
211218 // with other contracts (i.e. they call functions or send Ether)
@@ -223,9 +230,11 @@ contract SimpleAuction {
223230 // 1. Conditions
224231 require (now >= auctionEnd , " Auction not yet ended." );
225232 require (! ended , " auctionEnd has already been called." );
233+
226234 // 2. Effects
227235 ended = true ;
228236 emit AuctionEnded (highestBidder , highestBid );
237+
229238 // 3. Interaction
230239 beneficiary .transfer (highestBid );
231240 }
0 commit comments