0

I would like to call a function in smart contract using web3. In the function, I perform few transactions.

The first method that I tried is await myContract.methods.endAuction().call();. This reverted the transaction with the error message "Auction owner is not authorized" which is strange since I succedded in calling previous function from the same smart contract which had the same restriction.

The second method I tried is await myContract.methods.endAuction().plasticBaleSC.methods.endAuction().send({from: accounts[0]}); which caused the transaction to fail with no revert message.

The function that I want to call is this:

function endAuction() onlyOwner public{
        
        require( isOpen, "Auction is not avalible.");
        require(endTime < now, "Auction duration is not up yet.");
        require(highestBidder != address(0), "No bids have been placed"); 
        
        isOpen = false; 
        
        uint halfAmount = highestBid/2;
    
        (auctionOwner).transfer(halfAmount); 

        uint contributionRate =0; 
        uint reward; 
        
        
        for(uint i=0; i < contributors.length; i++){
                uint j;
             for(j=0; j < i; j++)
                  if(contributors[i] == contributors[j])
                      break;
                if(i==j)
                tempArray.push(contributors[i]);
              }
        

        for(uint i=0; i < tempArray.length; i++){
            contribution=0;
             for(uint z=0; z < contributors.length; z++){
                  if(tempArray[i] == contributors[z])
                      contribution++;
             }
             contribution = contribution *100; 
             contributionRate = contribution / (plasticBale.length);
             reward = ((contributionRate * halfAmount)/ 100)+1;
             tempArray[i].transfer(reward); 
             rewardRecycler(tempArray[i], reward); 
    
        }
          
          for(uint i=0; i< plasticBale.length; i++)
          updateBottleStatus(highestBidder, plasticBale[i]); 
          
        emit auctionEnded(address(this), highestBidder, highestBid , now); 
    
    }


modifier onlyOwner{
        require(msg.sender == auctionOwner, "Auction owner is not authorized"); 
        _; 
    }

Any advice on what should I do?

Edit: This is a link to the full contract code although you might want to remove the modifiers for now since onlyBuyer interfaces with another smart contract EthFiddle

0

Let's begin from here - you should use .call() for contract methods which are only reading from the blockchain ( methods which are not updating the state of the smart contract ) and you should use .send() for methods which are writing on the blockchain ( methods which are updating the state of the smart contract ).

Now when we cleared this and when we know that your method endAuction() is actually updating the state of the contract, you should use await myContract.methods.endAuction().send({from: accounts[0]}). If it's still failing make sure accounts[0] has proper value and when you make sure everything looks ok with the frontend part then start inspecting the contract. You have a modifier onlyOwner and 3 different requires at the top of the method which might cause the transaction reverting.

5
  • Hello, thank you for the reply! I did try to use send({from: account[0]}) but the transaction failed. While testing the smart contract in remix, calling the function without setting a value seems to work.
    – Iman
    Jan 22 at 9:25
  • Hey, attach in the question your full JS code and the full smart contract code. Also did you debug if accounts[0] is not an empty value? Jan 22 at 9:27
  • I'm using metamask so account[0] is not empty. I will link the smart contract and the JS file shortly, although, they are very long.
    – Iman
    Jan 23 at 18:50
  • I tested the smart contract on remix using the injected web3 environment, when calling the endAuction function, I get this error message ibb.co/dG7j092
    – Iman
    Jan 23 at 19:17
  • Then your javascript part is fine, now you need to start debugging the smart contract method. It's not a simple method, it includes few requires and few for loops which might cause the problem. Jan 24 at 6:15

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.