-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleAuction.sol
More file actions
68 lines (48 loc) · 1.72 KB
/
SimpleAuction.sol
File metadata and controls
68 lines (48 loc) · 1.72 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract SimpleAuction {
address public owner;
uint256 public endAt;
address public highestBidder;
uint256 public highestBid;
bool public ended;
mapping(address => uint256) public refunds;
event BidPlaced(address indexed bidder, uint256 amount);
event Withdrawn(address indexed bidder, uint256 amount);
event Ended(address indexed winner, uint256 amount);
constructor(uint256 durationSeconds) {
require(durationSeconds > 0, "duration=0");
owner = msg.sender;
endAt = block.timestamp + durationSeconds;
}
modifier onlyOwner() {
require(msg.sender == owner, "not owner");
_;
}
function bid() external payable {
require(block.timestamp < endAt, "auction ended");
require(msg.value > highestBid, "bid too low");
if (highestBidder != address(0)) {
refunds[highestBidder] += highestBid;
}
highestBidder = msg.sender;
highestBid = msg.value;
emit BidPlaced(msg.sender, msg.value);
}
function withdrawRefund() external {
uint256 amount = refunds[msg.sender];
require(amount > 0, "nothing to withdraw");
refunds[msg.sender] = 0;
(bool ok, ) = msg.sender.call{value: amount}("");
require(ok, "transfer failed");
emit Withdrawn(msg.sender, amount);
}
function end() external onlyOwner {
require(block.timestamp >= endAt, "not ended yet");
require(!ended, "already ended");
ended = true;
(bool ok, ) = owner.call{value: highestBid}("");
require(ok, "transfer failed");
emit Ended(highestBidder, highestBid);
}
}