trust-web3-provider icon indicating copy to clipboard operation
trust-web3-provider copied to clipboard

WebProvider does not keep connecting with Pancakeswap since today! Trust wallet is with the same issue in their Dapp.

Open ronaldoguedess opened this issue 3 years ago • 8 comments

I guess the Web provider and Trust Wallet need an update.

Today, 08/16/2022 the connection with the DApp PancakeSwap stopped working as it used to works.

The code below is responsible for maintaining the connection, but it no longer works with PancakeSwap. every time i entered in the DApp, it already authenticated me, but now it doesn't happen anymore.

And when I click on the manually connect button the function is called about 20x.

No changes were made to the code.

Take the test on TrustWallet. is having the same problem. The ISSUE just happens with the current version of the Trust Wallet: 6.27

`            DAppMethod.REQUESTACCOUNTS -> {
                val address = if (network == "solana") pubKey else publicAddress
                val setAddress = "window.$network.setAddress(\"$address\");"
                val callback = "window.$network.sendResponse($id, [\"$address\"])"
                webView.post {
                    webView.evaluateJavascript(setAddress) {    }
                    webView.evaluateJavascript(callback) { value -> println(value)  }
                }
            }`

Note: The MetaMask keeps working.

ronaldoguedess avatar Aug 16 '22 17:08 ronaldoguedess

It's better, but not perfect yet. I'm testing here, but it seems it keeps losing the connection when refreshing the page using webview.refresh()

I'm testing here yet!

ronaldoguedess avatar Aug 18 '22 13:08 ronaldoguedess

It's better, but not perfect yet. I'm testing here, but it seems it keeps losing the connection when refreshing the page using webview.refresh()

I'm testing here yet!

Are you creating a new instance of the provider on refresh? Could you also check if the address is passed to the initializer correcty?

rsrbk avatar Aug 18 '22 14:08 rsrbk

To assure that I'm not making mistake, I get the original code here to test, and to be true it needs some changes to work.

First: We need to change onPageFinished, to onPageStarted.

Replacing it

override fun onPageFinished(view: WebView?, url: String?) {
    super.onPageFinished(view, url)
    view?.evaluateJavascript(provderJs, null)
    view?.evaluateJavascript(initJs, null)
}

to it 👇

override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
    super.onPageStarted(view, url, favicon)
    view?.evaluateJavascript(provderJs, null)
    view?.evaluateJavascript(initJs, null)
}

After that, we need to open the WebAppInterface.kt and comment the materialAlertDialog on the REQUESTACCOUNTS to test the auto connection!

            DAppMethod.REQUESTACCOUNTS -> {
//                context.materialAlertDialog {
//                    title = "Request Accounts"
//                    message = "${dappUrl} requests your address"
//                    okButton {
                        val address = if (network == "solana") pubKey else addr
                        val setAddress = "window.$network.setAddress(\"$address\");"
                        val callback = "window.$network.sendResponse($id, [\"$address\"])"
                        webView.post {
                            webView.evaluateJavascript(setAddress) {
                                // ignore
                            }
                            webView.evaluateJavascript(callback) { value ->
                                println(value)
                            }
                        }
//                    }
//                    cancelButton()
//                }.show()
            }

By the way, if you let it, you will see about 20x requests... about 20 new ID, it's letting the system crazy! Just happens with PancakeSwap. It could be a Pancake issue, I don't know. Debugging it you could see it as well.

After that, I added manually this wallet. on the ethereum config.

ethereum: {
    address: "0x60ae4e7e00284bac073efd1d617fe8cc55af8098",
    chainId: $chainId,
    rpcUrl: "$rpcUrl"
},

The auto connection should be tested on PancakeSwap https://pancakeswap.finance/swap?inputCurrency=0x7d38315b92d0e7a85b35b2b7fe969b25935619d7

And to resume, it won't works 100%. Each 10 loads, just 3 works. Only works 100% if I clear the recent applications and open the Activity again... but this way I can't put it in the real APP, it's unreal.

I tried call the android function recreate() that create a new instance of all... but it can't reconnect. I tried also call a finish() or finishAndRemoveTask()... but won't work. There are some failures in the code!

Please, test this Android Sample...

ronaldoguedess avatar Aug 18 '22 21:08 ronaldoguedess

Video showing the intermittent problem 👇

https://www.youtube.com/watch?v=hm7QrEv0sr8&ab_channel=RonaldoGuedes

ronaldoguedess avatar Aug 18 '22 22:08 ronaldoguedess

Can you show similar code to this one in your app? The one when you initialize the JS provider and the config that you pass there. https://github.com/trustwallet/trust-web3-provider/blob/7e7051f96c2b917d537e21bb16036120bf724942/android/app/src/main/java/com/trust/web3/demo/MainActivity.kt#L62-L84

rsrbk avatar Aug 18 '22 22:08 rsrbk

Sure, this is the full code. I'm using 95% same code.. just change the onPageFinish to onPageStarted, otherwise it wont load never.

class MainActivity : AppCompatActivity() {
    companion object {
        private const val DAPP_URL = "https://pancakeswap.finance/swap?inputCurrency=0x7d38315b92d0e7a85b35b2b7fe969b25935619d7"
        private const val CHAIN_ID = 56
        private const val RPC_URL = "https://bsc-dataseed2.binance.org"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)
        

        val provderJs = loadProviderJs()
        val initJs = loadInitJs(
            CHAIN_ID,
            RPC_URL
        )
        WebView.setWebContentsDebuggingEnabled(true)
        val webview: WebView = findViewById(R.id.webview)
        webview.settings.run {
            javaScriptEnabled = true
            domStorageEnabled = true
        }
        WebAppInterface(this, webview, DAPP_URL).run {
            webview.addJavascriptInterface(this, "_tw_")

            val webViewClient = object : WebViewClient() {

                override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                    super.onPageStarted(view, url, favicon)
                    view?.evaluateJavascript(provderJs, null)
                    view?.evaluateJavascript(initJs, null)
                }
                override fun onReceivedSslError(
                    view: WebView?,
                    handler: SslErrorHandler?,
                    error: SslError?
                ) {
                    // Ignore SSL certificate errors
                    handler?.proceed()
                    println(error.toString())
                }
            }
            webview.webViewClient = webViewClient
            webview.loadUrl(DAPP_URL)
        }
    }

    private fun loadProviderJs(): String {
        return resources.openRawResource(R.raw.trust_min).bufferedReader().use { it.readText() }
    }

    private fun loadInitJs(chainId: Int, rpcUrl: String): String {
        val source = """
        (function() {
            var config = {                
                ethereum: {
                    address: "0x60ae4e7e00284bac073efd1d617fe8cc55af8098",
                    chainId: $chainId,
                    rpcUrl: "$rpcUrl"
                },
                solana: {
                    cluster: "mainnet-beta",
                },
                isDebug: true
            };
            trustwallet.ethereum = new trustwallet.Provider(config);
            trustwallet.solana = new trustwallet.SolanaProvider(config);
            trustwallet.postMessage = (json) => {
                window._tw_.postMessage(JSON.stringify(json));
            }
            window.ethereum = trustwallet.ethereum;
        })();
        """
        return  source
    }
}

ronaldoguedess avatar Aug 18 '22 22:08 ronaldoguedess

cc @vcoolish @hewigovens to check if there is any issue with the android demo project. I don't have Android environment unfortunately. iOS demo works fine.

rsrbk avatar Aug 19 '22 09:08 rsrbk

The callback after clicking on Connect button on PancakeSwap manually it's returning over 20 results ... so crazy! big issue detected! When I'm free I will show it to you. Thanks @rsrbk . Let's wait the @hewigovens or @vcoolish to check it on the Android environment.

ronaldoguedess avatar Aug 22 '22 22:08 ronaldoguedess