TrueFiEng / useDApp

Framework for rapid Dapp development. Simple. Robust. Extendable. Testable
https://usedapp.io
MIT License
1.59k stars 369 forks source link

Non-standard RPC error and general question on preflight validation #498

Open thebuidler opened 2 years ago

thebuidler commented 2 years ago

This is likely not a question for useDApp but truth is I don't know who it falls on (Metamask? ethers.js?), which the question will only reveal further. Hoping someone can give me a quick and straight answer. I have googled this for 30 mins now lol.

I'm submitting a transaction using useContractFunction --> send to Kovan network. I'm able to submit no problem until I hit what I'd expect to be an error within the contract itself. Instead of erroring in the contract, I get a preflight RPC error:

MetaMask - RPC Error: The execution failed due to an exception. {code: -32016, message: 'The execution failed due to an exception.', data: 'Reverted'}

For one the code doesn't appear to be a standard exit code as I can't find it anywhere. And two the error is thrown before the user is even prompted to confirm the tx through their MM wallet.

Now, I know what's causing the error. The contract has a require check for the error state, but what error is returned and at what stage of the call lifecycle (before the user confirms) have me lost.

  1. Do RPCs do client-side validation? If so, what?
  2. Who is in charge of returning the errors? The code is non standard and the message provides no useful detail.
  3. Am I able to get a more concise error?

Thank you!

rolandsaven commented 2 years ago

I’m having the same issues to be honest and there are variables that unable to validate until the blockchain call is made. I wonder how we can make the error messages more useful as it would be nice to catch them.

ItsShadowl commented 2 years ago

That's happens with Kovan, it just throws exception without the error message

FranciscoBuru commented 2 years ago

I had the exact same error. I'll reproduce the error and then the solution:

I was using a hook

const [amount, setAmount] = useState(0)

and a function to handle input changes

const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        const newAmount = event.target.value === "" ? Number(0): Number(event.target.value)
        setAmount(newAmount)
    }

Sending amout as a tx parameter will give you that -32016 error . My solution was to redeclare the hook as

const [amount, setAmount] = useState<number | string | Array<number | string>>(0

and then in the function replace Number(0) inside the ternary with "" so it looks like this.

const newAmount = event.target.value === "" ? "" : Number(event.target.value)

I would guess you have some kind of parameter type problem.

Hope it helps.

ItsShadowl commented 2 years ago

https://github.com/openethereum/parity-ethereum/issues/10147#issuecomment-462177568

Your exception is probably a kovan exception, which I think might be caused by trying to transfer zero(0) token, so amount should be anything above zero(0). Kovan doesn't go into details about exception, I think it's parity issue.