Repository navigation
Expand file tree
/
Copy pathAuctionContract.sol
More file actions
69 lines (55 loc) · 1.95 KB
/
Copy pathAuctionContract.sol
File metadata and controls
69 lines (55 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
k.number <= endBlock);
_;
}
function min(uint _a, uint _b)internal pure returns(uint){
if(_a > _b){
return _b;
}else{
return _a;
}
}
modifier onlyOwner(){
require(msg.sender == owner);
_;
}
function cancelAuction()public{
auctionState = State.Cancelled;
}
function placeBid()public payable afterStart beforeEnd notOwner returns(bool){
require(msg.value >= 0.01 ether);
uint currentBid = bids[msg.sender] + msg.value;
bids[msg.sender] = currentBid;
require(currentBid >= highestBidingBid);
if(currentBid >= bids[highestBidder]){
highestBidingBid = min(currentBid,bids[highestBidder]+bidIncrement);
highestBidder = msg.sender;
}else{
highestBidingBid = min(currentBid+bidIncrement,bids[highestBidder]);
}
return true;
}
function finalizeAuction()public{
require(auctionState == State.Cancelled || block.number > endBlock);//auction has been cancelled
require(bids[msg.sender]>0 || msg.sender == owner);
address payable recipient;
uint value;
if(auctionState == State.Cancelled){
recipient = msg.sender;
value = bids[msg.sender];
}else{//ended not cancelled
if(msg.sender == owner){
recipient = owner;
value = highestBidingBid;
}else{
if(msg.sender == highestBidder){
recipient = highestBidder;
value = bids[highestBidder]-highestBidingBid;
}else{//this is not the highest bidder or owner
recipient = msg.sender;
value = bids[msg.sender];
}
}
}
recipient.transfer(value);
}
}