-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auction.sol
122 lines (112 loc) · 3.82 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
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.7 <0.9.0;
contract AuctionCreator{
address[] public deployedAuctions;
// create Auction Instance
function createAuction(uint256 endBlock) public {
address newAuction = address(new Auction(msg.sender,endBlock));
deployedAuctions.push(newAuction);
}
// get All Auctions
function allAuctions() public view returns (address[] memory){
return deployedAuctions;
}
}
// errors
error Auction__FailedTosendMoney();
contract Auction{
// states
address payable public seller;
uint256 public startBlock;
uint256 public endBlock;
string public ipfsHash;
enum State{Started, Running, Ended, Canceled}
State public auctionState;
uint256 public minBid = 0.02 ether;
uint256 public highestBindingBid;
address payable public highestBidder;
mapping (address=>uint256) public bids;
uint256 bidIncrement;
// modifiers
modifier notSeller(){
require(msg.sender != seller);
_;
}
modifier afterStart(){
require(block.number >= startBlock);
_;
}
modifier beforeEnd(){
require(block.number <= endBlock);
_;
}
modifier onlySeller(){
require(seller == msg.sender);
_;
}
// constructor
constructor(address _seller,uint256 _endblock){
seller = payable(_seller);
auctionState = State.Running;
startBlock = block.timestamp;
endBlock = startBlock + _endblock; // _endblock is a time when the auction will end/ 15
ipfsHash = "";
bidIncrement = 20000000000000000; // 0.02
}
function min(uint a, uint b) pure internal returns(uint){
if(a <= b){
return a;
} else {
return b;
}
}
// cancel an auction
function cancelAuction() public onlySeller{
// change auction State to Canceled
auctionState = State.Canceled;
}
// place a bid on auction
function placeBid() public payable notSeller afterStart beforeEnd{
require(auctionState == State.Running, "The Auction state should be Running.");
require(msg.value >= minBid);
uint256 currentBid = bids[msg.sender] + msg.value;
require(currentBid > highestBindingBid);
bids[msg.sender] = currentBid;
if(currentBid <= bids[highestBidder]){
highestBindingBid = min(currentBid + bidIncrement, bids[highestBidder]);
} else {
highestBindingBid = min(currentBid, bids[highestBidder] + bidIncrement);
highestBidder = payable(msg.sender);
}
}
//finalize the auction
function finalizeAuction() public {
require(auctionState == State.Canceled || block.number > endBlock);
require(msg.sender == seller || bids[msg.sender] > 0);
address payable recipient;
uint256 value;
if(auctionState == State.Canceled) { // Auction was canceled
recipient = payable (msg.sender);
value = bids[msg.sender];
} else { // Auction was ended (not canceled)
if(msg.sender == seller){ // this is the seller
recipient = seller;
value = highestBindingBid;
} else { // this is a bidder
if(msg.sender == highestBidder) {
recipient = highestBidder;
value = bids[highestBidder] - highestBindingBid;
} else { // this is neither the seller nor the highestBidder
recipient = payable (msg.sender);
value = bids[msg.sender];
}
}
}
// resetting recipient
// send money to recipient
(bool success,) = recipient.call{value:value}("");
if (!success){
revert Auction__FailedTosendMoney();
}
}
}