@@ -10,25 +10,27 @@ contract blindAuction is Ownable{
1010 }
1111
1212 uint256 public constant BIDDING_DURATION = 10 hours ;
13- uint256 public revealTime ;
13+ uint256 public constant REVEAL_DURATION = 1 hours ;
1414 uint256 public auctionEndTime;
1515 uint256 public revealEndTime;
1616 uint256 public highestBid;
17- uint256 public highestBidder;
17+ address public highestBidder;
1818 bool public ended;
1919 address payable public beneficiary;
20- uint256 public minimumDeposit ;
20+ uint256 public constant MINIMUM_DEPOSIT = 4 ether ;
2121
22- mapping (address => Bid[]) bids;
23- mapping (address => uint256 ) public revealedBid;
22+ mapping (address => Bid[]) public bids;
23+ mapping (address => Bid[] ) public revealedBid;
2424 mapping (address => uint256 ) public pendingReturns;
2525
2626 event bidSubmitted (address bidder );
2727 event auctionHasEnded (address winner , uint256 highestBid );
28+ event bidRevealed (address caller , uint256 bid );
2829
2930 error tooEarly ();
3031 error tooLate ();
3132 error invalidDeposit ();
33+ error lengthMismatch ();
3234
3335 constructor (
3436 address payable _beneficiary
@@ -37,19 +39,52 @@ contract blindAuction is Ownable{
3739 _transferOwnership (_beneficiary);
3840
3941 auctionEndTime = block .timestamp + BIDDING_DURATION;
40- revealEndTime = auctionEndTime + revealTime ;
42+ revealEndTime = auctionEndTime + REVEAL_DURATION ;
4143 }
4244
4345 function bid (bytes32 _blindedBid ) external payable {
4446 if (! (block .timestamp < auctionEndTime)) revert tooEarly ();
45- if ( msg .value <= 5 ether ) revert invalidDeposit ();
47+ if ( msg .value <= MINIMUM_DEPOSIT ) revert invalidDeposit ();
4648
4749 bids[msg .sender ].push (Bid ({
4850 blindedBid: _blindedBid,
4951 deposit: msg .value
5052 }));
5153 }
54+ function revealBid (
55+ uint256 [] calldata value ,
56+ bool [] calldata fake ,
57+ bytes32 [] calldata secret
58+ ) external {
59+ if (block .timestamp < auctionEndTime) revert tooEarly ();
60+ if (block .timestamp > revealEndTime) revert tooLate ();
5261
62+ uint256 length = bids[msg .sender ].length ;
63+
64+ if (value.length != length) revert lengthMismatch ();
65+ if (fake.length != length) revert lengthMismatch ();
66+ if (secret.length != length) revert lengthMismatch ();
67+
68+ for (uint i = 0 ; i < length; i++ ){
69+ Bid storage bitToReveal = bids[msg .sender ][i];
70+ bytes32 calculatedHash = keccak256 (abi.encodePacked (value[i], fake[i], secret[i]));
71+
72+ if (calculatedHash == bitToReveal.blindedBid && bitToReveal.deposit >= MINIMUM_DEPOSIT) {
73+
74+ if (! fake[i] && value[i] > highestBid){
75+ if (highestBidder != address (0 )){
76+ pendingReturns[address (uint160 (msg .sender ))] += highestBid;
77+ }
78+
79+ highestBid = value[i];
80+ highestBidder = msg .sender ;
81+ }
82+
83+ bids[msg .sender ] = revealedBid[msg .sender ];
84+ }
85+ emit bidRevealed (msg .sender , highestBid);
86+ }
87+ }
5388
5489
5590}
0 commit comments