How to handle reject 'approve' method in metamask mobile?
Ethers Version
5.6.5
Search Terms
No response
Describe the Problem
The exception message could not be caught when the Metamask mobile user rejected 'approve',
The program will get stuck at code 2.
But the PC is normal:
MetaMask - RPC Error: Error: MetaMask Tx Signature: User denied transaction signature.
Code Snippet
try{
const contract = new ethers.Contract(TOKEN_ADDRESS, ERC20.abi, jsonRpcProvider); // code 1
const tx = await contract.approve(CONTRACT_ADDRESS, num); // code 2
await tx.wait();
}catch(err){
console.log(err); // unable to work code 3
}
Contract ABI
ERC20
Errors
No response
Environment
Altcoin - Please specify (e.g. Mumbai), node.js (v12 or newer) iPhone7P(IOS15),iPhone8P(IOS16)
same here
As long as the backend (metamask) gives an error for eth_sendTransaction, it should throw on await contract.approve(CONTRACT_ADDRESS, num); // code 2. But it seems metamask mobile does not give an error in that case.
Can you try the following code:
const ptx = await contract.populateTransaction.approve(CONTRACT_ADDRESS, num);
const resp = await contract.provider.send('eth_sendTransaction', [ptx]);
// now reject on metamask side
console.log(resp);
Please post the response here.
As long as the backend (metamask) gives an error for
eth_sendTransaction, it should throw onawait contract.approve(CONTRACT_ADDRESS, num); // code 2. But it seems metamask mobile does not give an error in that case.Can you try the following code:
const ptx = await contract.populateTransaction.approve(CONTRACT_ADDRESS, num); const resp = await contract.provider.send('eth_sendTransaction', [ptx]); // now reject on metamask side console.log(resp);Please post the response here.
The effect is the same.
Metamask click Reject.
console.log(resp); Still won't work
I am getting an issue where a simple call to signer.sendTransaction() returns a Promise as expected but if the tx signature is rejected, the Promise never rejects, perpetually remaining in a pending state. It would be better to reject with a note that the user rejected the transaction, to be able to react appropriately in the UI.
This should have been fixed in v5.7.0 and is also fixed in v6. If not, please re-open or comment.
Thanks! :)