-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuction.sol
83 lines (62 loc) · 2.08 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
pragma solidity >=0.7.0 <0.9.0;
contract Auction {
// auction parameters
// payable = this address can be used to send some coins
address payable public beneficiary;
uint public auctionEndTime;
// variables of current state of auction
address public highestBidder;
uint public highestBid;
mapping(address => uint) public pendingReturns;
// auction ended or not
bool ended = false;
// events
event HighestBidIncrease(address bidder, uint amount);
event AuctionEnded(address winner, uint amount);
constructor(uint _biddingTime, address payable _beneficiary) {
beneficiary = _beneficiary;
auctionEndTime = block.timestamp + _biddingTime;
}
// users can bid
function bid() public payable {
if(block.timestamp > auctionEndTime){
// revert stops the function from executing
revert("Auction ended");
}
if(msg.value <= highestBid){
revert("There is already a higher or equal bid");
}
if(highestBid != 0){
pendingReturns[highestBidder] += highestBid;
}
highestBidder = msg.sender;
highestBid = msg.value;
emit HighestBidIncrease(msg.sender, msg.value);
}
// overbid users can withdraw their coins
function withdraw() public returns (bool){
uint amount = pendingReturns[msg.sender];
if(amount > 0){
pendingReturns[msg.sender] = 0;
if(!payable(msg.sender).send(amount)){
pendingReturns[msg.sender] = amount;
return false;
}
}
return true;
}
// auction end
function auctionEnd() public {
if(block.timestamp < auctionEndTime){
revert("The auction not ended yet");
}
if(ended){
revert("The function auction ended has already been called");
}
ended = true;
emit AuctionEnded(highestBidder, highestBid);
// send return false if it fails
// transfer stops if fails
beneficiary.transfer(highestBid);
}
}