-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuction.sol
70 lines (53 loc) · 1.9 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
pragma solidity >=0.4.0 <0.9.0;
contract SimpleAuction{
// Parameters of the SimpleAuction
address payable public beneficiary;
uint public auctionEndTime;
//Current state of the auctionEndTime
address public highestBidder;
uint public highestBid;
mapping(address => uint) public pendingReturns;
bool ended = false;
event HighestBidIncrease(address bidder, uint amount);
event AuctionEnded(address winner, uint amount);
constructor(uint _biddingTime, address payable _beneficiary) {
beneficiary = _beneficiary;
auctionEndTime = block.timestamp + _biddingTime;
}
function bid() public payable{
if (block.timestamp > auctionEndTime){
revert("The auction has already 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);
}
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;
}
function auctionEnd() public{
if (block.timestamp < auctionEndTime) {
revert("The auction has not ended yet");
}
if (ended){
revert("The function auctionEnded has already been called");
}
ended = true;
emit AuctionEnded(highestBidder, highestBid);
beneficiary.transfer(highestBid);
}
}