0
function AuctionNft(string memory _name) public payable {
    MyNft storage nft = fetchNft[_name];
    // bidders storage _bidders = bidderslist[msg.sender]
    require(nft.isAuctionable, "This nft is not listed for auction yet");
    require(block.timestamp <= timer,"Auction for this NFt has been ended");
    require(msg.value >= minBid, "Minimum Bid is not met");
    
    address currentBidder = msg.sender;
    bidders storage prevbidder = previousBidder[currentBidder];
    prevbidder.Address = msg.sender;
    prevbidder.bidAmount = 0;
    
    address payable recipient = payable(nft.owner);
    require(msg.value > currentBid, "Bid is lower than the previous bid");
    currentBid = msg.value * 1 ether;
    if (block.timestamp > timer) {
        recipient.transfer(currentBid);
        nft.owner = currentBidder;
        nft.isOnSale = false;
        nft.isAuctionable = false;
    }
}

In the above code I'm trying to create a function to Auction the Nft, however, when placing the bids if the current bid is higher than the previous bid what I want is to transfer the bid placed by the previous bidder back to its account.

For that reason, I want to store the address and msg. value of the previous bidder, however, I'm unable to think of any proper logic on how should I do it!!!.

I've created a struct object prevbidder having variable address and amount where I wanna save previous bidder's address and msg.value.

Please help me in understanding how can I store the address and msg.value of the previous bidder.

1 Answer 1

0

There are some logic errors in your code.

  1. currentBid = msg.value * 1 ether - Is there any reason to mutiply 1 ether on msg.value ?
  2. if (block.timestamp > timer) - you already have require(block.timestamp <= timer,"Auction for this NFt has been ended") above, another timer check here doesn't make any sense.
  3. not sure if your MyNft is ERC721 standard or not, the standard way to transfer ownership is via transferFrom or safeTransferFrom

I revise your code a little bit , you can check if this is what you want.

contract Auction {

    struct Bidder {
        address addr;
        uint256 amount;
    }

    uint256 puiblic currentBid;

    mapping(string => Bidder) public nftHighestBidders;

    uint256 private constant minBid = 0.1 ether;
    
    uint256 private constant timer = block.timestamp + 5 days;


    function AuctionNft(string memory _name) public payable {
        MyNft storage nft = fetchNft[_name];

        require(nft.isAuctionable, "This nft is not listed for auction yet");
        require(block.timestamp <= timer,"Auction for this NFt has been ended");
        require(msg.value >= minBid, "Minimum Bid is not met");
        require(msg.value > currentBid, "Bid is lower than the previous bid");

        address currentBidder = msg.sender;
        Bidder storage highestBidder = nftHighestBidders[_name];
        
        address payable recipient = payable(nft.owner);

        recipient.transfer(msg.value);
        nft.owner = currentBidder;

        currentBid = msg.value;

        highestBidder.addr = msg.sender;
        highestBidder.amount = msg.value;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.