-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuction.sol
124 lines (94 loc) · 3.29 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract Auction {
address payable public owner; // owner of this smart contract.
uint public startTime; // Start time of auction;
uint public endTime; // End time of auction;
enum State {Started, Running, Ended, Cancelled}
State public auctionState;
uint public highestPayableBid; // highest payable bid ( selling price )
uint public bidInc; // incrementing bid
address payable public highestBidder; // the person who have bidded highest amount.
mapping(address => uint) public bidders; // all bidders with their address and value.
// Constructor
constructor(){
owner = payable(msg.sender);
auctionState = State.Running;
startTime = block.number;
endTime = startTime + 240;
bidInc = 1 ether;
}
// modifier for not owner;
modifier onlyOwner {
require(msg.sender == owner, "Sorry you are not permitted to use it.");
_;
}
modifier notOwner {
require(msg.sender != owner, "Owner Can't bid");
_;
}
// modifier for the auction is started.
modifier started {
require(block.number > startTime);
_;
}
// modifier for the auction is ended.
modifier isEnded {
require(block.number <= endTime);
_;
}
// to cancel the auction
function cancelAuction() public onlyOwner {
auctionState = State.Cancelled;
}
//
function min(uint first, uint second) pure private returns(uint){
if(first <= second){
return first;
}else{
return second;
}
}
// function to bid in auction
function placeBid() payable public notOwner started isEnded {
require(auctionState == State.Running);
require(msg.value >= 1 ether);
// current bid
uint currentBid = bidders[msg.sender] + msg.value;
// The current bid should be always greater then highest person's bid.
require(currentBid > highestPayableBid);
bidders[msg.sender] = currentBid;
if(currentBid < bidders[highestBidder]){
highestPayableBid = min(currentBid+bidInc, bidders[highestBidder]);
}else {
highestPayableBid = min(currentBid, bidders[highestBidder] +bidInc);
highestBidder = payable(msg.sender);
}
}
// auction winner decision.
function finalize() public {
require(auctionState == State.Cancelled || block.number >= endTime);
require(msg.sender == owner || bidders[msg.sender] > 0);
address payable person;
uint value;
if(auctionState == State.Cancelled) {
person = payable(msg.sender);
value = bidders[msg.sender];
}else{
if(msg.sender == owner){
person = owner;
value = highestPayableBid;
}else{
if(msg.sender == highestBidder){
person = highestBidder;
value = bidders[highestBidder] - highestPayableBid;
}else{
person = payable(msg.sender);
value = bidders[msg.sender];
}
}
}
bidders[msg.sender] = 0;
person.transfer(value);
}
}