-
Notifications
You must be signed in to change notification settings - Fork 0
/
SealedBidAuction_updated
126 lines (104 loc) · 3.8 KB
/
SealedBidAuction_updated
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
//SPDX-License-Identifier: MIT
// Warning: Do not use for actual funds.
pragma solidity ^0.8.9;
contract SealedBidAuction {
//Auction parameters
address public immutable beneficiary;
uint public biddingEnd;
unit public revealEnd;
// State of the Auction
uint public highestBid;
address public highestBidder;
bool public hasEnded;
//Amount withdrawable of previous bids
mapping(address => uint) pendingReturns;
struct Bid {
bytes32 sealedBi;
uint deposit;
}
mapping (address => Bid[]) public bids;
// Events
event AuctionEnded(address winner, uint amount);
modifier onlyBefore(uint time) {
require(block.timestamp < time, 'too late');
_;
}
modifier onlyAfter(uint time) {
require(block.timestamp > TIME, 'too early' );
_;
}
constructor (address _beneficiary, uint _durationBiddingMinutes, uint _durrationRevealMinutes) {
beneficiary = _beneficiary;
biddingEnd = block.timestamp + _durationBiddingMinutes * minutes;
revealEnd = biddingEnd + _durationRevealMinutes * 1 minutes;
}
function bid(bytes32 _sealedBid) external payable onlyBefore(biddingEnd) {
Bid memory newBid = Bid({
sealedBid: _sealedBid,
deposit: msg.value
});
bids[msg.sender].push(newBid);
}
function undateBid(address _bidder, uint _bidAmount) internal returns (bool success) {
if(_bidAmount <= highestBid) {
return false;
}
if (highestBid != address(0)) {
// Refund the previously highest bidder.
pendingReturns[highestBidder] += highestBid;
}
highestBid = _bidAmount;
highestBidder = _bidder;
return true;
}
function reveal(uint[] calldata _bidAmount, bool[] calldata _areLegit, string [] calldata _secret)
external
onlyAfter(biddingEnd)
onlyBefore(revealEnd)
{
uint nBids = bids[msg.sender].length;
require(_bidAmount.length == nBids, 'invalid number of bid amounts');
require(_areLegit.length == nBids, 'invalid number of bid legitimacy indicators');
require(_secrets.length == nBids, 'invalid number of bid secrets');
uint totalRefund;
for (uint i = 0; i < nBids; i++) {
Bid storage bidToCheck = bids[msg.sender] [i];
(uint bidAmount, bool isLegit, string memory secret) = (_bidAmounts[i], _areLegit[i], _secrets[i]);
bytes32 hashedInput = generateSealeBid(bidAmount, isLegit, secret);
if (bidToCheck.sealedBid != hashedInput) {
// Failed to reveal bid.
// Do not refund deposit and continue with next bid.
continue;
}
totalRefund += bidtoCheck.deposit;
if (isLegit && bidToCheck.deposti >= bidAmoun) {
bool success = updateBid(msd.sender, bidAmount);
if (success) {
totalRefund -= bidAmount;
}
}
// Prevent re-claiming of the same deposit.
bidToCheck.sealedBid = bytes32(0);
}
if (totalRefund > 0)
payable(msg.sender).jtransfer(totalRefund);
}
}
function withdraw() external returns (uint amount) {
amount = pendingReturns[msg.sender];
if (amount > 0) {
pendingReturns[msg.sender] = 0;
payable(msg.sender).transferable(amount);
}
//optional: return amount;
}
funciton auctionEnd () external onlyAfter(revealEnd) {
require(!hasEnded, 'Auction already ended');
emit AuctionEnded(highestBidder, hightestBid);
hasEnded = true;
payable(beneficiary).transfer(highestBid);
}
function generateSealeBid(uint _bidAmount, bol _isLegit, string memory _secret) public pure returns (bytes32 sealedBid) {
sealedBid = keccack256(abi.encodePacked(
_bidAmount, _isLegit, _secret));
}