WalletConnect / kotlin-walletconnect-lib

library to use WalletConnect with Kotlin or Java
MIT License
156 stars 98 forks source link

Executing contract methods #58

Open anandsemtech opened 2 years ago

anandsemtech commented 2 years ago

Successfully connected Metamask with this library. Now, How do I establish web3 connection to my contract and call the contract's method. Can you please provide me some examples?

Neo-glitch commented 2 years ago

How did you connect this library pls, can't seem to do that

mendelordanza commented 2 years ago

This approach worked for me. You can try this

  1. Generate the encoded contract abi using web3j https://docs.web3j.io/4.8.7/

    val function = Function(
                "{method name}",
                listOf(
                    Address(from),
                    Address(to),
                    Uint256(BigInteger(tokenId)),
                ),  // input parameters. Change this based on the method you're using
                listOf(object : TypeReference<Utf8String>() {}) // output parameters. Change this based on the method you're using,
            )
    
            val encodedFunction = FunctionEncoder.encode(function)
  2. Peform the Send Transaction method call with WalletConnect.

    MainApplication.session?.performMethodCall(
                        Session.MethodCall.SendTransaction(
                            txRequest,
                            from = viewModel.address.value,
                            to = contractAddress,
                            nonce = nonce.transactionCount.toString(16),
                            gasPrice = DefaultGasProvider.GAS_PRICE.toString(16),
                            gasLimit = DefaultGasProvider.GAS_LIMIT.toString(16),
                            value = BigInteger.ZERO.toString(16),
                            data = endcodedFunction
                        )
                    ) { resp ->
                        // do something with callback
                    }
                    val i = Intent(Intent.ACTION_VIEW)
                    i.data = Uri.parse("wc:")
                    context.startActivity(i)

Make sure that to to parameter is the contract address you're interacting with

For some reason, this approach only works with Metamask v4.0.1 and does not work with Metamask's latest update v4.2.2. It might be a bug on their end.

Mukesh6201 commented 2 years ago

@mendelordanza Can you explain in brief . I really need to connect with the metamask wallet using my smart contract. I have successfully connected with my wallet but when I try to perform a transaction, it redirected to metamask, but nothing happens. No dialog is showing. Now when I tried your method it is not importing any of the functions above although when I tried the same method in java it works ? Can you please refer me some tutorial or anything? code