-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauctioncontract.sol
More file actions
78 lines (44 loc) · 1.44 KB
/
auctioncontract.sol
File metadata and controls
78 lines (44 loc) · 1.44 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
69
70
71
72
73
74
75
76
77
78
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract AuctionHouse {
address public owner;
string public item;
uint public auctionEndTime;
address private highestBidder;
uint private highestBid;
bool public ended;
mapping(address => uint) public bids;
address[] public bidders;
constructor(string memory _item, uint _biddingTime) {
owner = msg.sender;
item = _item;
auctionEndTime = block.timestamp + _biddignTime;
}
function bid(uint amount) external {
require(block.timestamp > auctionEndTime, "Auc ended");
require(amount > 0, " Bid amount must be greater than zero.");
require(amount > bids[msg.sender], " new bid must be higher");
if (bids[msg.sender] == 0) {
bidders.push.(msg.sender);
}
bids[msg.sender] = amount;
if (amount > highestBid) {
highestBid = amount;
highestBidder = msg.sender;
}
}
function endAuction() external {
require(block.timestamp >= auctionEndTime, " auction");
require(!ended, "ended");
ended = true;
}
// Get a list of all bidders
function getAllBidders() external view returns (address[] memory) {
return bidders;
}
// Retrieve winner and their bid after auction ends
function getWinner() external view returns (address, uint) {
require(ended, "Auction has not ended yet.");
return (highestBidder, highestBid);
}
}