This code is running but when the same user makes 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.


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??? Dec 5, 2018 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. Dec 5, 2018 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. Dec 5, 2018 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. Dec 5, 2018 at 17:30

1 Answer 1


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)) {
            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);
            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 and acknowledge you have read our privacy policy.

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