From 17af49c266f1339f179ccc1a2b130b70236ce7cd Mon Sep 17 00:00:00 2001 From: x256 <153709773+xTwo56@users.noreply.github.com> Date: Tue, 2 Apr 2024 10:55:13 +0530 Subject: [PATCH] feat: modified finalizeauc and created withdraw function --- contracts/smartbid.sol | 53 ++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/contracts/smartbid.sol b/contracts/smartbid.sol index 98bb7e7..f9bffe2 100644 --- a/contracts/smartbid.sol +++ b/contracts/smartbid.sol @@ -22,6 +22,8 @@ contract EnglishAuction { mapping(address => uint256) public bids; + event bidPlaced (address bidder, uint bidAmount); + constructor() { auctioneer = payable(msg.sender); auctionState = Auc_state.Running; @@ -76,40 +78,51 @@ contract EnglishAuction { highestBidder=payable(msg.sender); } + emit bidPlaced(msg.sender, currentBid); + } function finalizeAuc() public { - require(auctionState==Auc_state.Cancelled || auctionState==Auc_state.Ended || block.number>etblock); - require(msg.sender==auctioneer || bids[msg.sender]>0); + require(auctionState==Auc_state.Ended || block.number>etblock, "Auction still running"); + require(msg.sender==auctioneer || msg.sender == highestBidder, "Call withdraw to withdraw your eth back"); address payable person; uint value; - if(auctionState==Auc_state.Cancelled) - { - person=payable(msg.sender); - value=bids[msg.sender]; - - } - else { if(msg.sender== auctioneer) { person=auctioneer; value=highestPayable; } else { - if(msg.sender ==highestBidder) - { - person=highestBidder; - value=bids[highestBidder]; - } - else{ - person=payable(msg.sender); - value=bids[msg.sender]; + //if(msg.sender == highestBidder) + // ownership of the bought item should transfer here } - } + + bids[msg.sender]=0; + person.transfer(value); } + + + + function withdraw()public{ + require(auctionState==Auc_state.Cancelled || auctionState==Auc_state.Ended || block.number>etblock, "Auction still running"); + require(bids[msg.sender]>0, "Only bidders can withdraw"); + + address payable person; + uint value; + + if(auctionState==Auc_state.Cancelled){ + person=payable(msg.sender); + value=bids[msg.sender]; + }else{ + require(msg.sender!=highestBidder, "Highest bidder cannot withdraw"); + person=payable(msg.sender); + value=bids[msg.sender]; + } bids[msg.sender]=0; - person.transfer(value); + person.transfer(value); + } + } -} +