-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuction.sol
121 lines (95 loc) · 3.12 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
//SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;
contract CreateAuction{
Auction[] public auctions;
function createAuction() public{
Auction newAuction = new Auction(msg.sender);
auctions.push(newAuction);
}
}
contract Auction{
address payable public owner;
uint public startBlock;
uint public endBlock;
string ipfsHash;
mapping(address => uint) public bidders;
enum State{Running, Started, Ended, Canceled}
State public auctionState;
uint public highestBindingBid;
address payable public highestBidder;
uint incrementBid;
constructor(address eoa){
startBlock = block.number;
endBlock = startBlock + 4;
owner = payable(eoa);
ipfsHash = "";
incrementBid = 1 ether;
auctionState = State.Running;
}
modifier notOwner(){
require(msg.sender != owner);
_;
}
modifier afterStart(){
require(block.number >= startBlock);
_;
}
modifier beforeEnd(){
require(block.number <= endBlock);
_;
}
modifier onlyOwner(){
require(msg.sender == owner);
_;
}
function min(uint a, uint b) pure public returns(uint){
if(a < b){
return a;
}else
return b;
}
function placeBid() payable public notOwner afterStart beforeEnd{
require(auctionState == State.Running);
require(msg.value >= 100);
uint currentBid = bidders[msg.sender] + msg.value;
require(currentBid > highestBindingBid);
bidders[msg.sender] = currentBid;
if(currentBid <= bidders[highestBidder]){
highestBindingBid = min(currentBid + incrementBid, bidders[highestBidder]);
} else {
highestBindingBid = min(currentBid, bidders[highestBidder] + incrementBid);
highestBidder = payable(msg.sender);
}
}
function cancelAuction() public onlyOwner(){
auctionState = State.Canceled;
}
function finalizeAuction() payable public{
require(auctionState == State.Canceled || block.number > endBlock);
require(msg.sender == owner || bidders[msg.sender] > 0);
address payable recipient;
uint value;
if(auctionState == State.Canceled){
recipient = payable(msg.sender);
value = bidders[msg.sender];
}else {
if(msg.sender == owner){
recipient = owner;
value = highestBindingBid;
}else{
if(msg.sender == highestBidder){
recipient = highestBidder;
value = bidders[highestBidder] - highestBindingBid;
}else{
recipient = payable(msg.sender);
value = bidders[msg.sender];
}
}
}
bidders[msg.sender] = 0;
recipient.transfer(value);
}
function getBalance() public view returns(uint){
return address(this).balance;
}
}