-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuction.sol
133 lines (112 loc) · 3.3 KB
/
Auction.sol
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;
contract AuctionFactory {
Auction[] public auctions;
function createAuction() public {
Auction newAuction = new Auction(msg.sender);
auctions.push(newAuction);
}
}
contract Auction {
address payable public owner;
uint256 public startBlock;
uint256 public endBlock;
string public ipfsHash;
enum State {
Started,
Running,
Ended,
Canceled
}
State public auctionState;
uint256 public highestBindingBid;
address payable public highestBidder;
mapping(address => uint256) public bids;
uint256 bidIncrement;
constructor(address eoa) {
owner = payable(eoa);
auctionState = State.Running;
startBlock = block.number;
endBlock = startBlock + 40320;
ipfsHash = "";
bidIncrement = 10;
}
modifier ownerOnly {
require(
msg.sender == owner,
"you must be the ownwer to perform this action!"
);
_;
}
modifier notOwner {
require(
msg.sender != owner,
"you must not be the ownwer to perform this action!"
);
_;
}
modifier afterStart() {
require(block.number >= startBlock);
_;
}
modifier beforeEnd() {
require(block.number <= endBlock);
_;
}
function min(uint256 a, uint256 b) internal pure returns (uint256) {
if (a < b) {
return a;
} else {
return b;
}
}
function cancelAuction() public ownerOnly {
auctionState = State.Canceled;
}
function placeBid() public payable notOwner afterStart beforeEnd {
require(auctionState == State.Running);
require(msg.value >= 100);
uint256 currentBid = bids[msg.sender] + msg.value;
require(
currentBid > highestBindingBid,
"highest bid must be higher than highestBindingBid"
);
bids[msg.sender] = currentBid;
if (currentBid <= bids[highestBidder]) {
highestBindingBid = min(
currentBid + bidIncrement,
bids[highestBidder]
);
} else {
highestBidder = payable(msg.sender);
}
}
function finalizeAuction() public {
require(auctionState == State.Canceled || block.number > endBlock);
require(msg.sender == owner || bids[msg.sender] > 0);
address payable recipient;
uint256 value;
// auction was Canceled
if (auctionState == State.Canceled) {
recipient = payable(msg.sender);
value = bids[msg.sender];
} else {
// auction not Canceled
if (msg.sender == owner) {
recipient = owner;
value = highestBindingBid;
} else {
// this is a bidder
if (msg.sender == highestBidder) {
recipient = highestBidder;
value = bids[highestBidder] - highestBindingBid;
} else {
recipient = payable(msg.sender);
value = bids[msg.sender];
}
}
}
// send money to the recipient
recipient.transfer(value);
}
}