-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auction.sol
47 lines (43 loc) · 1.51 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
pragma solidity ^0.4.22;
contract SimpleAuction{
address public beneficiary;
uint public auctionEnd;
address public highestBidder;
uint public highestBid;
mapping(address => uint) pendingReturns;
bool ended;
event HighestBidIncreased(address bidder, uint amount);
event AuctionEnded(address winner, uint amount);
constructor(uint _biddingTime,address _beneficiary)public payable{
beneficiary = _beneficiary;
auctionEnd = now + _biddingTime;
}
function bid() public payable{
require(now <= auctionEnd,"Auction already ended.");
require(msg.value > highestBid,"There already is a higher bid.");
if (highestBid != 0) {
pendingReturns[highestBidder] = highestBid;
}
highestBidder = msg.sender;
highestBid = msg.value;
emit HighestBidIncreased(msg.sender, msg.value);
}
function withdraw() public returns(bool){
uint amount = pendingReturns[msg.sender];
if (amount > 0) {
pendingReturns[msg.sender] = 0;
if (!msg.sender.send(amount)) {
pendingReturns[msg.sender] = amount;
return false;
}
}
return true;
}
function auctionEnd() public {
require(now <= auctionEnd, "Auction not yet ended.");
require(!ended, "auctionEnd has already been called.");
ended = true;
emit AuctionEnded(highestBidder, highestBid);
beneficiary.transfer(highestBid);
}
}