2

Here's an auction code from blog.ethereum.org

contract auction {
  address highestBidder;
  uint highestBid;
  mapping(address => uint) refunds;
  function bid() {
    if (msg.value < highestBid) throw;
    if (highestBidder != 0)
      refunds[highestBidder] += highestBid;
    highestBidder = msg.sender;
    highestBid = msg.value;
  }
  function withdrawRefund() {
    uint refund = refunds[msg.sender];
    refunds[msg.sender] = 0;
    if (!msg.sender.send(refund))
     refunds[msg.sender] = refund;
  }
}

refunds[highestBidder] += highestBid;

What's the reason behind using "+=" instead of "="?

manidos
  • 4,298
  • 3
  • 31
  • 55

1 Answers1

2
  • Bidder sends 100, Bid 1.
  • Bidder gets outbid, 110, Bid 2. 100 refund due.
  • Bidder bids 120, Bid 3. This is new money, still owed 100.
  • Bidder gets outbid again, 130, Bid 4. Now owed 220.
  • Bidder withdraws 220.
Edmund Edgar
  • 16,897
  • 1
  • 29
  • 58