ethers.js icon indicating copy to clipboard operation
ethers.js copied to clipboard

How to handle reject 'approve' method in metamask mobile?

Open liyueran-bj opened this issue 3 years ago • 3 comments

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)

liyueran-bj avatar Jul 21 '22 06:07 liyueran-bj

same here

Sotatek-HiepNguyen2 avatar Aug 08 '22 03:08 Sotatek-HiepNguyen2

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.

zemse avatar Aug 14 '22 13:08 zemse

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.

The effect is the same. Metamask click Reject. console.log(resp); Still won't work

liyueran-bj avatar Aug 22 '22 05:08 liyueran-bj

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.

wbt avatar Sep 23 '22 23:09 wbt

This should have been fixed in v5.7.0 and is also fixed in v6. If not, please re-open or comment.

Thanks! :)

ricmoo avatar Jan 31 '23 06:01 ricmoo