Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonRpcError when sending transaction using MetaMask #176

Open
devsideal opened this issue Aug 21, 2023 · 12 comments
Open

JsonRpcError when sending transaction using MetaMask #176

devsideal opened this issue Aug 21, 2023 · 12 comments

Comments

@devsideal
Copy link

When I'm calling a custom contract method to send transaction through metamask app, getting below error
Unhandled Exception: JsonRpcError(code: 5000, message: Internal JSON-RPC error.)

STR
I have three step process like:

  1. Connect (connect to Metamask wallet app) : Working
  2. Approve (approve) : Not Working
  3. Complete (minting):

Step-1 is working fine when i click on Connect button its open the Metamask and i successfully received chain id and account address.
But when i click on Approve button its open the Metamask app & show the Modal sheet with Approve and Reject buttons, when click on Approve It shows Internal JSON-RPC error alert as showing in screenshot:

Screenshot_20230821_153157

My Code is:

Future<dynamic> callApprove(String conAddressHex, int amount, BuildContext context) async {
    String transactionId = "";
    try {
      /// LOAD SMART CONTRACTOR
      var contract = await _loadContractERC20();

      /// MAKE TRANSACTION USING web3dart
      Transaction transaction = Transaction.callContract(
        from: EthereumAddress.fromHex(_accountAddress!),
        contract: contract,
        function: contract.function(_methodApprove),
        parameters: [
          EthereumAddress.fromHex(conAddressHex),
          BigInt.from(amount) * BigInt.from(1000000000000000000)
        ],
      );

      /// MAKE ETHEREUM TRANSACTION USING THE walletconnect_flutter_v2
      EthereumTransaction ethereumTransaction = EthereumTransaction(
        from: _accountAddress ?? "",
        to: conAddressHex,
        value: '0x2386F26FC10000', // 0.01 Ether (in wei)
        data: hex.encode(List<int>.from(transaction.data!)), /// ENCODE TRANSACTION USING convert LIB
      );

      /// OPEN METAMASK
      await launchUrl(Uri.parse("$wcUriPrefix$_encodedWcUrl"),
          mode: LaunchMode.externalNonBrowserApplication);

      ///  REQUEST TO WALLET FOR TRANSACTION USING walletconnect_flutter_v2
      transactionId = await _web3App?.request(
        topic: _sessionData?.topic ?? "",
        chainId: kFullChainId.toString(),
        request: SessionRequestParams(
          method: "eth_sendTransaction",
          params: [ethereumTransaction.toJson()],
        ),
      );
    } on Exception catch (_, e) {
      _log("Error:  $e");
    }
    _log("transactionId:  $transactionId");
    return transactionId;
  }

Anyone, can you help me what's going wrong?

@michael16m2
Copy link

Hi, we seem to have a similar issue. In my case the transfer of tokens via my erc721 compatible contract deployed on Polygon started to fail after having to migrate to WalletConnect v2 in our Flutter dApp.
See https://github.com/orgs/WalletConnect/discussions/3094 and https://stackoverflow.com/questions/76788918/walletconnect-migration-issue-failed-to-execute-eth-sendtransaction-requests. I am not sure about the proper way to convert the parameters to json but that does not seem to be the cause of the issue.
I would be eager to learn about your findings and progress on the matter. Perhaps we could team-up.

@vmidyllic
Copy link

+1 have the same problem from the last week.

@bobwith2bees
Copy link

@devsideal

await launchUrl(Uri.parse("$wcUriPrefix$_encodedWcUrl"

If you are sending a transaction on an established wallet connect pairing session, you only need to launch the wallet with the redirect link (i.e. native: metamask:// ) . If you include the original encoded WalletConnect pairing URI, you are attempting to create a new session with a duplicate seed. (Right before you send a transaction ... )

FYI : I am working on some interop and troubleshooting pointers here:
https://gitlab.com/graflr/flutter_web3_demo/-/wikis/WalletConnect-Interop

@devsideal
Copy link
Author

@devsideal

await launchUrl(Uri.parse("$wcUriPrefix$_encodedWcUrl"

If you are sending a transaction on an established wallet connect pairing session, you only need to launch the wallet with the redirect link (i.e. native: metamask:// ) . If you include the original encoded WalletConnect pairing URI, you are attempting to create a new session with a duplicate seed. (Right before you send a transaction ... )

FYI : I am working on some interop and troubleshooting pointers here: https://gitlab.com/graflr/flutter_web3_demo/-/wikis/WalletConnect-Interop

Not working getting same error.

@bobwith2bees
Copy link

@devsideal - I can't offer more given what you have posted.

You can look at my demo app https://gitlab.com/graflr/flutter_web3_demo and the wiki pages on that site.

If you go the route of using a class WalletConnectEip155Credentials that extends the web3dart CustomTransactionSender, you can likely avoid the need to manually create the Transaction.callContract (which I suspect is causing you trouble.)

Peek at:
lib/model/wallet_connect_eip155_credential.dart
lib/logic/token_contract.dart. // includes logs of the app running a transaction

@bobwith2bees
Copy link

@devsideal

I was able to reproduce what I think you saw. To confirm:

  • You send the transaction to Metamask.
  • Do the URI redirect to open Metamask.
  • Metamask shows a single WalletConnect Session open with Dapp
  • Metamask is able to parse the request and display a confirmation page (where you can review the data and accept)
    • I was thinking the request was not parsing correctly.
  • Once you accept, you immediately see the internal RPC error as in the screenshot.

This is going to take more context to track down. As I have only seen this once. Do you have any of these in common?

  • First run of install on IOS
  • Existing pairing session from a prior run, with an existing session (with about 5 days left)
  • The sign and send transaction was the first transaction after starting the app. (I did not pair, or ping etc.)
  • I had the same wallet address on Polygon Mainnet and Mumbai Mainnet
  • Transaction was sent to the Mumbai chainId.

Curious if this is a startup, initialization issue. But I won't have time to look at it further for a few days. There is also a good possibility this is on the Metamask side since the same message works other times.

@Luzzotica
Copy link
Contributor

I believe it's a MetaMask issue.

I created an example of signing a smart contract interaction transaction.
It fails on MetaMask, but it succeeds on TrustWallet.

@bobwith2bees
Copy link

bobwith2bees commented Sep 5, 2023

@Luzzotica - I found sending a stale session ID (i.e. expired) to signEngine.request can cause this error. (I am sure there are more ways...)

Should the app check for expired sessions or should the library catch & correct this before sending it to Metamask?

I see you can use WalletConnectUtils.isExpired(sessionData.expiry) to check expired. What do you call for an existing pairing to re-establish a new signing session?


/// 987ce182a2ad35d1899cb2af83f2aff82712a571c3571d5804814de5158279bf is the stale sessioin Topic

flutter: 09:01:48 💡 HomePage        - _onInitDone
flutter: 09:02:14 🐛 WalletConnctSvc - =Core Services wc: v2, relayUrl: wss://relay.walletconnect.com, pushUrl: https://echo.walletconnect.com, uptime: 26 seconds=
flutter: 09:02:14 🐛 WalletConnctSvc - ==Pairings (1)==
flutter: 09:02:14 🐛 WalletConnctSvc - 22.0861 days topic: 0a98bd9400fa3f84833282c3b8161943cbae80b5c60266943db86b833859cd5c, relay: irn, active: true, peer: MetaMask Wallet, redirect native: metamask://, redirect universal: https://metamask.app.link/
flutter: 09:02:19 🐛 WalletConnctSvc - =Sign Client Info - (wc v2)=
flutter: 09:02:19 🐛 WalletConnctSvc - ==Sign Client Sessions (1)==
flutter: 09:02:19 🐛 WalletConnctSvc - <expired> topic: 987ce182a2ad35d1899cb2af83f2aff82712a571c3571d5804814de5158279bf, pairing topic: 0a98bd9400fa3f84833282c3b8161943cbae80b5c60266943db86b833859cd5c ack: true, 
peer name: MetaMask Wallet accounts: [ eip155:137:0x72A4408DA42DE870499c1841D0e4a49f864e34ba, eip155:80001:0x72A4408DA42DE870499c1841D0e4a49f864e34ba ] sessionProperties: null
flutter: 09:02:19 🐛 WalletConnctSvc - ==Sign Client Proposals (0)==
flutter: 09:02:19 🐛 WalletConnctSvc - No proposals.
flutter: 09:02:29 🐛 HomePage        - Navigate to Token Test Page.
flutter: 09:02:29 🐛 TokenContract   - Token constructor called with contract address Mumbai 0xBd08...9aF1
flutter: 09:02:29 💡 TokenContract   - init - contractAddress Mumbai 0xBd08...9aF1, web3NodeProviderUrl: https://polygon-mumbai.infura.io/v3 (redacted)
flutter: 09:02:33 🐛 TokenContract   - Contract owner: 0x72a4408da42de870499c1841d0e4a49f864e34ba
flutter: 09:02:33 🐛 WalletConnctSvc - getEip155Credentials - blockchainAccount: Mumbai 0x72a4...34ba, sessionTopic: 987ce182a2ad35d1899cb2af83f2aff82712a571c3571d5804814de5158279bf
flutter: 09:02:33 🐛 TokenContract   - transfer - to: Mumbai 0x7Fd6...b0b1, amount: 10, from: 0x72a4408da42de870499c1841d0e4a49f864e34ba
flutter: WalletConnectEip155Credentials: sendTransaction - transaction: {from: null, to: 0xbd08b78ec1fe51e9b5db03657c637e8b18379af1, maxGas: null, gasPrice: null, value: null, nonce: null, maxFeePerGas: null, maxPriorityFeePerGas: null}
transaction.data in bytes: [ a9 05 9c bb 00 00 00 00 00 00 00 00 00 00 00 00 7f d6 11 af 09 be fb 5f 47 ab b1 9f cc 25 a2 29 97 ae b0 b1 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0a ]
flutter: WalletConnectEip155Credentials: sendTransaction - blockchain eip155:80001, sessionRequestParams: {method: eth_sendTransaction, params: [{from: 0x72a4408da42de870499c1841d0e4a49f864e34ba, to: 0xbd08b78ec1fe51e9b5db03657c637e8b18379af1, data: a9059cbb0000000000000000000000007fd611af09befb5f47abb19fcc25a22997aeb0b1000000000000000000000000000000000000000000000000000000000000000a}]}
flutter: 09:02:33 🐛 WalletConnctSvc - getWalletLaunchUri - for pairingTopic  or sessionTopic 987ce182a2ad35d1899cb2af83f2aff82712a571c3571d5804814de5158279bf, return universal link: false
flutter: \^[[38;5;244m┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
flutter: \^[[38;5;244m│ #0   Pairing.sendRequest (package:walletconnect_flutter_v2/apis/core/pairing/pairing.dart:388:17)<…>
flutter: \^[[38;5;244m│ #1   SignEngine.request (package:walletconnect_flutter_v2/apis/sign_api/sign_engine.dart:471:31)<…>
flutter: \^[[38;5;244m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;244m│ pairing sendResult, id: null topic: 987ce182a2ad35d1899cb2af83f2aff82712a571c3571d5804814de5158279bf, method: wc_sessionRequest, params: WcSessionRequestRequest(chainId: eip155:80001, request: SessionRequestParams(method: eth_sendTransaction, params: [{from: 0x72a4408da42de870499c1841d0e4a49f864e34ba, to: 0xbd08b78ec1fe51e9b5db03657c637e8b18379af1, data: a9059cbb0000000000000000000000007fd611af09befb5f47abb19fcc25a22997aeb0b1000000000000000000000000000000000000000000000000000000000000000a}])), ttl: null<…>
flutter: \^[[38;5;244m└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
flutter: 09:02:33 🐛 HomePage        - AppLifecycleState: AppLifecycleState.inactive.
flutter: 09:02:34 🐛 HomePage        - AppLifecycleState: AppLifecycleState.hidden.

@Mash-Woo
Copy link

I am also facing this issue

@quetool
Copy link
Collaborator

quetool commented Feb 14, 2024

Hello @Mash-Woo (and anyone else), do you still need help here?

@FelixYin66
Copy link

@devsideal - I can't offer more given what you have posted.

You can look at my demo app https://gitlab.com/graflr/flutter_web3_demo and the wiki pages on that site.

If you go the route of using a class WalletConnectEip155Credentials that extends the web3dart CustomTransactionSender, you can likely avoid the need to manually create the Transaction.callContract (which I suspect is causing you trouble.)

Peek at: lib/model/wallet_connect_eip155_credential.dart lib/logic/token_contract.dart. // includes logs of the app running a transaction

Can you update your demo? I config my projectID, But I cannot initialize the wallet normally and an Error when Running on iPhone

@bobwith2bees
Copy link

@FelixYin66 - I updated my demo project dependencies.

Note: I wasn't able to use the latest (0.1.0) web3dart_builders due to inveker/web3dart_builders#12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants