Created
October 16, 2017 16:37
-
-
Save vincentlg/30d84eb525412519fed324100eeea77c to your computer and use it in GitHub Desktop.
Simplified Auction Smart contract
This file contains 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
pragma solidity ^0.4.8; | |
// UNSAFE CODE, DO NOT USE! demo purpose | |
contract Auction { | |
enum Stages { | |
Initialization, | |
AuctionInprogress, | |
AuctionFailure, | |
AuctionSuccess | |
} | |
// author address | |
address public author; | |
// current stage | |
Stages public stage = Stages.Initialization; | |
// (in wei) | |
uint256 public startingPrice; | |
// (in wei) | |
uint256 public reservePrice; | |
// (in wei) | |
uint256 public highestBid; | |
// author address of highest bid | |
address public highestBidAuthor; | |
// the current campaign expiry (future block number) | |
uint256 public expiry; | |
// the contract constructor | |
function Auction(uint256 _startingPrice, uint256 _reservePrice, uint256 _expiry) public { | |
if(_startingPrice > _reservePrice){ | |
throw; | |
} | |
startingPrice = _startingPrice; | |
reservePrice = _reservePrice; | |
expiry = _expiry; | |
author = msg.sender; | |
from_initialisation_to_auctioninprogress(); | |
} | |
function from_initialisation_to_auctioninprogress() { | |
if (block.number < expiry) { | |
stage = Stages.AuctionInprogress; | |
} | |
} | |
function bid() payable atStage(Stages.AuctionInprogress) { | |
if (msg.value == 0) { | |
throw; | |
} | |
uint256 amount = msg.value; | |
if (highestBid >= amount || amount <= reservePrice || amount <= startingPrice) throw; | |
if (highestBid != 0) | |
{ | |
// UNSAFE CODE, DO NOT USE! (Favor pull over push payments) | |
if (!highestBidAuthor.send(this.balance)) { | |
throw; | |
} | |
} | |
highestBid = amount; | |
highestBidAuthor = msg.sender; | |
from_auctioninprogress_to_auctionsuccess(); | |
from_auctioninprogress_to_auctionfailure(); | |
} | |
function from_auctioninprogress_to_auctionsuccess() { | |
//if(block.number >= expiry && highestBid > reservePrice) { | |
if(highestBid > reservePrice) { | |
stage = Stages.AuctionSuccess; | |
payout(); | |
} | |
} | |
function from_auctioninprogress_to_auctionfailure() { | |
if(block.number >= expiry && highestBid < reservePrice) { | |
stage = Stages.AuctionFailure; | |
refundHighestBidAuthor(); | |
} | |
} | |
function payout() atStage(Stages.AuctionSuccess) internal{ | |
if (!author.send(this.balance)) { | |
throw; | |
} | |
} | |
function refundHighestBidAuthor() atStage(Stages.AuctionSuccess) internal{ | |
if (!highestBidAuthor.send(this.balance)) { | |
throw; | |
} | |
} | |
// check the auction state | |
modifier atStage(Stages _expectedStage) { | |
require(stage == _expectedStage); | |
_; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment