1

I Designed a quiz app which simply withdraws the amount earned by user after playing the game from the smart contract.But Each time i click on the withdraw button to call the withdraw function on my smart contract i always get the above error(Transaction Error: Error: non-payable method cannot override value ) here is the withdraw code for the frontend

const {ethereum} = window;
    
    if (ethereum) {
      console.log("withdrawing")
      
      const provider = new ethers.providers.Web3Provider(ethereum, "any");
      const signer = provider.getSigner();
      const contract = new ethers.Contract(contractAddress, abi, signer);
      try {
        const tx = await contract.withdraw(userAddress, { value: earnedInWei });

        await tx.wait();
        console.log("Transaction Successful!");
      } catch (error) {
        console.error("Transaction Error: ", error);
      }
    } else {
      console.error("No web3 provider found. Please install MetaMask or use another web3 provider");
    }
  }``` 



and below is my smart contract

```//SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.8.7;

contract ethQuiz {

//address of the owner of the smart contract
address payable public owner;

//Making sure that only the owner calls the smart contract
constructor(){
    owner= payable(msg.sender);
       }  
//funding the smart contract
    receive() external payable {
    }
  
//for users to withdraw funds from the smart contract
function withdraw(uint256 amount) external {
    require(amount <= address(this).balance, "too much!");
    payable(msg.sender).transfer(amount);
    }

//to check the available balance in the smart contract
function getBalance() external view returns (uint){
    return address(this).balance;
    }
}
//initial amount on smart contract:300000000000000000 wei```


 
               
0

2 Answers 2

2

const tx = await contract.withdraw(userAddress, { value: earnedInWei });

withdraw() isnt a payable method, and you're trying to send ether to it. I believe what you're trying to do is just const tx = await contract.withdraw(earnedInWei);

1
  • yess!!!! thanks alot you are right...everything works fine now Jan 16 at 13:11
2

withdraw is not a payable method so you can not send ether to this method. @Foxxxey is correct

Do this

const tx = await contract.withdraw(earnedInWei);
await tx.wait().then(data => {
   console.log('data', data)
})
1
  • yes thanks alot,really appreciate Jan 16 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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