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 "="?

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.