Created
June 29, 2017 21:16
-
-
Save brynbellomy/7c769274aeb1afda837c020a24d7b85d to your computer and use it in GitHub Desktop.
medium-auction-contract-withdraw.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function withdraw() | |
| onlyEndedOrCanceled | |
| returns (bool success) | |
| { | |
| address withdrawalAccount; | |
| uint withdrawalAmount; | |
| if (canceled) { | |
| // if the auction was canceled, everyone should simply be allowed to withdraw their funds | |
| withdrawalAccount = msg.sender; | |
| withdrawalAmount = fundsByBidder[withdrawalAccount]; | |
| } else { | |
| // the auction finished without being canceled | |
| if (msg.sender == owner) { | |
| // the auction's owner should be allowed to withdraw the highestBindingBid | |
| withdrawalAccount = highestBidder; | |
| withdrawalAmount = highestBindingBid; | |
| ownerHasWithdrawn = true; | |
| } else if (msg.sender == highestBidder) { | |
| // the highest bidder should only be allowed to withdraw the difference between their | |
| // highest bid and the highestBindingBid | |
| withdrawalAccount = highestBidder; | |
| if (ownerHasWithdrawn) { | |
| withdrawalAmount = fundsByBidder[highestBidder]; | |
| } else { | |
| withdrawalAmount = fundsByBidder[highestBidder] - highestBindingBid; | |
| } | |
| } else { | |
| // anyone who participated but did not win the auction should be allowed to withdraw | |
| // the full amount of their funds | |
| withdrawalAccount = msg.sender; | |
| withdrawalAmount = fundsByBidder[withdrawalAccount]; | |
| } | |
| } | |
| if (withdrawalAmount == 0) throw; | |
| fundsByBidder[withdrawalAccount] -= withdrawalAmount; | |
| // send the funds | |
| if (!msg.sender.send(withdrawalAmount)) throw; | |
| LogWithdrawal(msg.sender, withdrawalAccount, withdrawalAmount); | |
| return true; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment