Skip to content

Commit 9a3fa1b

Browse files
ADD:reveal bid function added and test passed
1 parent 0eb3a66 commit 9a3fa1b

File tree

3 files changed

+105
-8
lines changed

3 files changed

+105
-8
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.inlineSuggest.suppressSuggestions": true
3+
}

src/blindAuction.sol

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

test/blindAuction.t.sol

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,66 @@ contract testBlindAuction is Test{
1212
address public bidder2 = address(0x3);
1313

1414
function setUp() public {
15+
vm.warp(100);
16+
1517
vm.prank(beneficiary);
18+
beneficiary = payable(address(0x1));
19+
vm.deal(beneficiary, 10 ether);
1620
auction = new blindAuction( beneficiary);
1721
}
18-
}
22+
23+
function testBid() public {
24+
vm.deal(bidder1, 10 ether);
25+
vm.prank(bidder1);
26+
uint value = 7 ether;
27+
bool fake = false;
28+
bytes32 secret = keccak256(abi.encodePacked("mySecret"));
29+
bytes32 blindedBid = keccak256(abi.encodePacked(value, fake, secret));
30+
31+
auction.bid{value: 5 ether}(blindedBid);
32+
// Access the value field from the tuple (assuming bids returns array of (uint256 value, bytes32 blindedBid))
33+
(uint256 deposit, ) = auction.bids(bidder1, 0);
34+
assertEq(deposit, 5 ether, "Bid value should be 5 ether");
35+
}
36+
function testBidFail() public {
37+
vm.deal(bidder2, 10 ether);
38+
vm.prank(bidder2);
39+
uint value = 7 ether;
40+
bool fake = false;
41+
bytes32 secret = keccak256(abi.encodePacked("mySecret"));
42+
bytes32 blindedBid = keccak256(abi.encodePacked(value, fake, secret));
43+
44+
45+
vm.expectRevert(blindAuction.invalidDeposit.selector);
46+
auction.bid{value: 3 ether}(blindedBid);
47+
}
48+
49+
50+
function testRevealBid() public{
51+
vm.deal(bidder1, 15 ether);
52+
vm.prank(bidder1);
53+
54+
uint value = 7 ether;
55+
bool fake = false;
56+
bytes32 secret = keccak256(abi.encodePacked("mySecret"));
57+
bytes32 blindedBId = keccak256(abi.encodePacked(value, fake, secret));
58+
59+
auction.bid{value: 5 ether}(blindedBId);
60+
61+
vm.warp(auction.auctionEndTime() + 1);
62+
63+
uint256 [] memory values = new uint256[](1);
64+
bool [] memory fakes = new bool[](1);
65+
bytes32 [] memory secrets = new bytes32[](1);
66+
67+
values[0] = value;
68+
fakes[0] = fake;
69+
secrets[0] = secret;
70+
71+
vm.prank(bidder1);
72+
auction.revealBid(values, fakes, secrets);
73+
74+
assertEq(auction.highestBid(), 7 ether);
75+
}
76+
}
77+

0 commit comments

Comments
 (0)