From 31de4885aa3230e0aa33f5f5e0300bfdec186164 Mon Sep 17 00:00:00 2001 From: javedzainab Date: Sat, 16 Dec 2023 10:08:32 +0500 Subject: [PATCH 1/3] Create hello.sol --- hello.sol | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 hello.sol diff --git a/hello.sol b/hello.sol new file mode 100644 index 0000000..424e648 --- /dev/null +++ b/hello.sol @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +contract Auction { + // State variables + address public owner; + uint256 public startTimestamp; + uint256 public endTimestamp; + address public highestBidder; + uint256 public highestBindingBid; + + mapping(address => uint256) public bids; + + // Modifiers + modifier onlyOwner() { + require(msg.sender == owner, "Not the owner"); + _; + } + + modifier onlyBeforeEnd() { + require(block.timestamp < endTimestamp, "Auction has already ended"); + _; + } + + modifier onlyAfterEnd() { + require(block.timestamp >= endTimestamp, "Auction has not ended yet"); + _; + } + + // Additional state variables + uint256 public previousHighestBid; + uint256 public numberOfBids; + uint256 public increment; + + // Constructor + constructor(uint256 minutesDuration) { + owner = msg.sender; + startTimestamp = block.timestamp; + endTimestamp = startTimestamp + (minutesDuration * 1 minutes); + } + + // Bid function + function placeBid() external payable onlyBeforeEnd { + require(msg.sender != owner, "Owner cannot bid."); + require(msg.value > 0, "Bid amount must be greater than 0"); + require(msg.value >= highestBindingBid, "Bidding amount should be greater than previous bid"); + if (msg.value >= highestBindingBid || highestBindingBid == 0) { + // Update highestBid and highestBidder + previousHighestBid = highestBindingBid; + highestBindingBid = msg.value; + highestBidder = msg.sender; + + // Update bids mapping for the bidder + bids[msg.sender] = msg.value; + + // Increment the number of bids + numberOfBids++; + } + } + + // Finalize auction function + function finalizeAuction() external onlyOwner onlyAfterEnd { + require(highestBidder != address(0), "No bidders"); + + + // Calculate the maximum allowed increment + uint256 maxIncrement = ((highestBindingBid - previousHighestBid) / 2); + + // Set the highestBindingBid with the random increment + + highestBindingBid = previousHighestBid + maxIncrement; + + // Transfer the highestBindingBid to the owner + payable(owner).transfer(highestBindingBid); +} + + // Withdraw funds function + function withdraw() external onlyAfterEnd { + uint256 amount = bids[msg.sender]; + require(amount > 0, "No funds to withdraw"); + bids[msg.sender] = 0; + + if (msg.sender == highestBidder) { + // Deduct the highestBindingBid from the withdrawal amount for the highest bidder + amount = amount - highestBindingBid; + require(amount >= 0, "Withdrawal amount cannot be negative"); + } + + // Transfer funds to the bidder + payable(msg.sender).transfer(amount); + } + + // Cancel auction function + function cancelAuction() public onlyOwner onlyBeforeEnd { + // End the auction prematurely + endTimestamp = block.timestamp; + + // Reset auction state + highestBidder = address(0); + highestBindingBid = 0; + } +} \ No newline at end of file From fe44ed01adce9392c0d5c4431986f532ac2abc84 Mon Sep 17 00:00:00 2001 From: javedzainab <138776668+javedzainab@users.noreply.github.com> Date: Sat, 16 Dec 2023 12:18:57 +0500 Subject: [PATCH 2/3] Update hello.sol --- hello.sol | 65 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/hello.sol b/hello.sol index 424e648..0e88b10 100644 --- a/hello.sol +++ b/hello.sol @@ -40,38 +40,55 @@ contract Auction { } // Bid function - function placeBid() external payable onlyBeforeEnd { - require(msg.sender != owner, "Owner cannot bid."); - require(msg.value > 0, "Bid amount must be greater than 0"); - require(msg.value >= highestBindingBid, "Bidding amount should be greater than previous bid"); - if (msg.value >= highestBindingBid || highestBindingBid == 0) { - // Update highestBid and highestBidder - previousHighestBid = highestBindingBid; - highestBindingBid = msg.value; - highestBidder = msg.sender; - - // Update bids mapping for the bidder - bids[msg.sender] = msg.value; - - // Increment the number of bids - numberOfBids++; + // Add a state variable to store addresses with the same value +// Add a state variable to store addresses with the same value +address[] private addressesWithSameValueArray; + +// Update the placeBid function +function placeBid() external payable onlyBeforeEnd { + require(msg.sender != owner, "Owner cannot bid."); + require(msg.value > 0, "Bid amount must be greater than 0"); + + if (msg.value >= highestBindingBid) { + if (msg.value == highestBindingBid) { + // Add the bidder's address to the array + addressesWithSameValueArray.push(msg.sender); } + + // Update highestBid and highestBidder + previousHighestBid = highestBindingBid; + highestBindingBid = msg.value; + highestBidder = msg.sender; } - // Finalize auction function - function finalizeAuction() external onlyOwner onlyAfterEnd { - require(highestBidder != address(0), "No bidders"); + // Update bids mapping for the bidder + bids[msg.sender] = msg.value; + // Increment the number of bids + numberOfBids++; +} + +// Add a function to randomly select a winner among bidders with the same value +function getRandomBidderWithSameValue() internal view returns (address) { + if (addressesWithSameValueArray.length > 0) { + uint256 randomIndex = uint256(keccak256(abi.encodePacked(blockhash(block.number - 1), msg.sender))) % addressesWithSameValueArray.length; + return addressesWithSameValueArray[randomIndex]; + } + + return address(0); +} +function finalizeAuction() external onlyOwner onlyAfterEnd { + require(highestBidder != address(0), "No bidders"); - // Calculate the maximum allowed increment uint256 maxIncrement = ((highestBindingBid - previousHighestBid) / 2); - // Set the highestBindingBid with the random increment - highestBindingBid = previousHighestBid + maxIncrement; - // Transfer the highestBindingBid to the owner - payable(owner).transfer(highestBindingBid); + for (uint256 i = 0; i < addressesWithSameValueArray.length; i++) { + payable(addressesWithSameValueArray[i]).transfer(bids[addressesWithSameValueArray[i]]); + } + + payable(highestBidder).transfer(highestBindingBid); } // Withdraw funds function @@ -99,4 +116,4 @@ contract Auction { highestBidder = address(0); highestBindingBid = 0; } -} \ No newline at end of file +} From 8cac48863ae134f00c1ac9ccfd6c2cb984611774 Mon Sep 17 00:00:00 2001 From: javedzainab Date: Sat, 16 Dec 2023 12:35:42 +0500 Subject: [PATCH 3/3] Update hello.sol --- hello.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hello.sol b/hello.sol index 0e88b10..168bf0e 100644 --- a/hello.sol +++ b/hello.sol @@ -116,4 +116,4 @@ function finalizeAuction() external onlyOwner onlyAfterEnd { highestBidder = address(0); highestBindingBid = 0; } -} +} \ No newline at end of file