-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauction.sol
76 lines (57 loc) · 1.71 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
//SPDX-License-Identifier: GPL-3.O
pragma solidity >=0.5.0 <0.9.0;
contract Auction{
address payable public owner;
uint public startBlock;
uint public endBlock;
string public ipfsHash;
enum State {Started, Running, Ended, Cancelled}
State public auctionState;
uint public highestBindingBid;
address payable public highestBidder;
mapping(address => uint) public bids;
uint bidIncrement;
constructor(){
owner = payable(msg.sender);
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 internal returns(uint){
if(a <= b){
return a;
} else {
return b;
}
}
function placeBid() public payable notOwner afterStart beforeEnd {
require(auctionState == State.Running);
require(msg.value >= 100);
uint currentBid = bids[msg.sender] + msg.value;
require(currentBid <= bids[highestBidder]);
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);
}
}
function cancelAuction() public onlyOwner {
auctionState = State.Cancelled;
}
}