Skip to content

Instantly share code, notes, and snippets.

@pabloruiz55
Created November 14, 2017 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pabloruiz55/bf34bca60e92ee7495e925b9ba31fe9d to your computer and use it in GitHub Desktop.
Save pabloruiz55/bf34bca60e92ee7495e925b9ba31fe9d to your computer and use it in GitHub Desktop.
// Once the auction end has been reached, we distribute the ether.
function finalizeAuction() public {
require (now > auctionEndTime);
require (!auctionFinalized);
auctionFinalized = true;
if(highestBidder == address(0)){
//If no one bid at the auction, auctioneer can withdraw the funds.
balances[auctioneer] = auctionedEth;
}else{
// Second highest bidder gets nothing, his latest bid is lost and sent to the auctioneer
balances[secondHighestBidder] -= secondHighestBid;
balances[auctioneer] += secondHighestBid;
//Auctioneer gets the highest bid from the highest bidder.
balances[highestBidder] -= highestBid;
balances[auctioneer] += highestBid;
//winner gets the 1eth being auctioned.
balances[highestBidder] += auctionedEth;
auctionedEth = 0;
}
E_AuctionFinished(highestBidder,highestBid,secondHighestBidder,secondHighestBid,auctionEndTime);
}
//Once the auction has finished, the bidders can withdraw the eth they put
//Winner will withdraw the auctionedEth
//Auctioneer will withdraw the highest bid from the winner
//Second highest bidder will already have his balance at 0
//The rest of the bidders get their money back
function withdrawBalance() public{
require (auctionFinalized);
uint ethToWithdraw = balances[msg.sender];
if(ethToWithdraw > 0){
balances[msg.sender] = 0;
msg.sender.transfer(ethToWithdraw);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment