0

Compiled an "Auction contract" and got a warning regarding high gas requirements. Means: cannot run it. Below the lines with comments and that lines I included (//Part 1 Task n Line n).

// CANNOT BE EXECUTED !!! GAS REQUIREMENTS HIGH
function bid(uint _itemId, uint _count) public payable{
    //Part 1 Task 4. Implement the three conditions below. 
    //   4.1 If the number of tokens remaining with the bidder is < count of tokens bid, revert
    //   4.2 If there are no tokens remaining with the bidder, revert.
    //   4.3 If the id of the item for which bid is placed, is greater than 2, revert.
    //   Hint: "tokenDetails[msg.sender].remainingTokens" gives the details of the number of tokens remaining with the bidder.
    if (tokenDetails[msg.sender].remainingTokens < _count) return; //Part 1 Task 4 – line 1
    if (_itemId > 2) return;                                       //Part 1 Task 4 – line 2

    //Part 1 Task 5. Decrement the remainingTokens by the number of tokens bid
    //   Hint. "tokenDetails[msg.sender].remainingTokens" should be decremented by "_count".
    (tokenDetails[msg.sender].remainingTokens - _count);           //Part 1 Task 5 – 1 line

    bidders[tokenDetails[msg.sender].personId].remainingTokens= tokenDetails[msg.sender].remainingTokens; //updating the same balance in bidders map. 
    Item storage bidItem = items[_itemId]; 
    for(uint i=0; i<_count;i++) { 
        bidItem.itemTokens.push(tokenDetails[msg.sender].personId); 
    }
} 

Anybody who can help?

1

You are taking the variable _count as a parameter to the function and the function is public. Therefore anyone who runs the function can decide on how many iterations your loop goes through. It's impossible for the gas estimator to know whether there are 1 iterations or 1 million iterations and therefore it probably assumes the worst and says that gas requirements are too high.

However, I think you should be able to run it if you give a reasonable _count amount. Just ignore the warnings.

You should never give the end-user the possibility to determine how many iterations something should go through. This should be decided in your internal logic. I'm not quite sure what your loop is supposed to be doing, but you should rethink it.

P.S. You should use require instead of your if ... return statements.

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.