-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.txt
More file actions
131 lines (104 loc) · 4.05 KB
/
code.txt
File metadata and controls
131 lines (104 loc) · 4.05 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.0;
contract Auction {
address payable public owner;
address[] public highestBidders; // Array to store all highest bidders
address public highestBidder;
uint256 public startingTime;
uint256 public endingTime;
event LogUint(uint256 number);
mapping(address => uint256) public bids;
uint256 public highestBindingBid;
address[] public bidKeys;
function placeBid() external payable {
require(block.timestamp >= startingTime, "Auction has not started");
require(block.timestamp < endingTime, "Auction has ended");
if (msg.value >= highestBindingBid) {
// Clear the previous highest bidders if a new or equal highest bid is received
delete highestBidders;
if (msg.value > highestBindingBid) {
highestBidders.push(msg.sender);
}
}
// Refund previous bidders with lower bids
for (uint256 i = 0; i < bidKeys.length; i++) {
address payable bidder = payable(bidKeys[i]);
uint256 amount = bids[bidder];
if (amount > 0 && amount < msg.value) {
bids[bidder] = 0;
bidder.transfer(amount);
}
}
// Update highest bid and bidder information
if (msg.value >= highestBindingBid) {
highestBindingBid = msg.value;
highestBidder = msg.sender;
}
bids[msg.sender] = msg.value;
bidKeys.push(msg.sender);
}
function getCurrentHighestBid() public view returns (uint256) {
return highestBindingBid;
}
function setStartTime(uint256 time) private onlyOwner {
startingTime = time;
}
function setEndTime(uint256 time) private onlyOwner {
endingTime = time;
}
constructor(uint256 _startTime, uint256 _endTime) {
owner = payable(msg.sender);
setEndTime(_endTime);
setStartTime(_startTime);
}
modifier onlyOwner() {
require(msg.sender == owner, "Not the owner");
_;
}
function cancelAuction() external onlyOwner {
require(block.timestamp < endingTime, "Auction already ended");
for (uint256 i = 0; i < bidKeys.length; i++) {
address payable bidder = payable(bidKeys[i]);
uint256 amount = bids[bidder];
emit LogUint(amount);
if (amount > 0) {
bids[bidder] = 0;
bidder.transfer(amount);
}
}
selfdestruct(owner);
}
function finalizeAuction() external onlyOwner {
require(block.timestamp >= endingTime, "Auction not yet ended");
for (uint256 i = 0; i < bidKeys.length; i++) {
address payable bidder = payable(bidKeys[i]);
uint256 amount = bids[bidder];
if ((amount > 0) && (bidder != highestBidder)) {
bids[bidder] = 0;
bidder.transfer(amount);
}
}
selfdestruct(owner);
}
// Function to randomly select a winner from the highest bidders
function selectWinner() external onlyOwner {
require(block.timestamp >= endingTime, "Auction not yet ended");
require(highestBidders.length > 0, "No valid bidders");
require(bidKeys.length == 3,"Not 3 bidders Yet");
// Generate a random index to select a winner
uint256 randomIndex = uint256(keccak256(abi.encodePacked(block.difficulty, block.timestamp))) % highestBidders.length;
address payable winner = payable(highestBidders[randomIndex]);
// Transfer the winning amount to the owner
owner.transfer(((highestBindingBid)/ 100) * 10);
// Refund all other bidders
for (uint256 i = 0; i < bidKeys.length; i++) {
address payable bidder = payable(bidKeys[i]);
uint256 amount = bids[bidder];
if (bidder != winner && amount > 0) {
bids[bidder] = 0;
bidder.transfer(amount);
}
}
selfdestruct(owner);
}
}