WalletConnect / WalletConnectFlutterV2

WalletConnect v2 client made in Dart for Flutter.
https://pub.dev/packages/walletconnect_flutter_v2
Apache License 2.0
108 stars 60 forks source link

JsonRpcError when sending transaction using MetaMask #176

Open devsideal opened 1 year ago

devsideal commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

+1 have the same problem from the last week.

bobwith2bees commented 1 year ago

@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 commented 1 year ago

@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 commented 1 year ago

@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 commented 1 year ago

@devsideal

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

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?

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 commented 1 year ago

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 commented 1 year ago

@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 commented 8 months ago

I am also facing this issue

quetool commented 7 months ago

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

FelixYin66 commented 5 months ago

@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 commented 5 months ago

@FelixYin66 - I updated my demo project dependencies.

Note: I wasn't able to use the latest (0.1.0) web3dart_builders due to https://github.com/inveker/web3dart_builders/issues/12