1

This code is running but when the same user make a bid then the bid is being replaced from the first bid and event is printing twice the same record.

i want that one address make a bid and for his 2nd bid code record both of his bids along with his address.

Please suggest the rectification.

Thanks

============================================================================

My Code for Bidding Contract

pragma solidity ^0.4.25;

contract bidding {

address[] private bidderAccts;

struct bidStruct {
    uint  bid;
}
mapping (address => bidStruct) biddersMap;

event totalAccounts(address, uint );

function setBidders(address _address , uint _bid)  public {
        bidStruct a = biddersMap[_address];
        a.bid = _bid;
        bidderAccts.push(_address) - 1 ;
}

function getBidders(address _address) view public returns(uint) {
    return biddersMap[_address].bid;
}

 function countBidders() view public returns (uint) {
    return bidderAccts.length;
}

function allBidders() public  {
    for (uint i = 0; i < bidderAccts.length; i++) {
     emit totalAccounts(bidderAccts[i],biddersMap[bidderAccts[i]].bid);
}


}
}
  • 1
    What is the meaning of bidderAccts.push(_address) - 1??? – goodvibration Dec 5 '18 at 10:46
  • BTW, you might want to add an input argument in function allBidders to indicate the maximum number of iterations. Otherwise, at some point you might reach a case where this function can no longer be executed because it exceeds block size. – goodvibration Dec 5 '18 at 10:47
  • Also, you need to change bidStruct a to bidStruct storage a. I'm not sure why the compiler hasn't warned you about this. – goodvibration Dec 5 '18 at 10:49
  • @goodvibration .push() -1 returns a row. It's not assigned to anything, so useless in this case. I imagine picked up from other examples. – Rob Hitchens - B9lab Dec 5 '18 at 17:30
0

Things get awkward because the storage structure doesn't align with the requirements. I refactored with a storage structure that reflects the requirements as described.

This might be more like what you want. No warranty.

pragma solidity ^0.4.25;

contract bidding {

    address[] public bidderAccts;

    // For each address, there are 0-n bids
    mapping (address => uint[]) public bids;

    event LogNewBidder(address newBidder);
    event LogNewBid(address bidder, uint bid);

    function isBidder(address bidder) public view returns(bool isIndeed) {
        // has bid before
        return bids[bidder].length > 0;
    }

    function upsertBidder(address bidder) public {
        if(!isBidder(bidder)) {
            bidderAccts.push(bidder);
            emit LogNewBidder(bidder);
        }
    }

    function setBid(uint bid)  public {
            // there is no known rule about minimum bid, but let's say 0 is too low.
            require(bid > 0);
            upsertBidder(msg.sender);
            bids[msg.sender].push(bid);
            emit LogNewBid(msg.sender, bid);
    }

     function getUniqueBidderCount() view public returns (uint) {
        return bidderAccts.length;
    }       

}

I made a judgment decision that it is more decentralized to imagine that the bidders themselves declare their bid, so setBid attacks it from the perspective that msg.sender will be the bidder. If you have a more centralized idea in mind and a server will be processing the bids, then pass in address bidder as you were and use that in place of msg.sender.

Hope it helps.

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.