From 8d3d3996147699ede65b72d9a24251ed5259fb19 Mon Sep 17 00:00:00 2001 From: reasje Date: Wed, 13 Sep 2023 11:16:14 +0330 Subject: [PATCH] fix: expo number control & response for unavailable network --- .../open_dapp/open_dapp_presenter.dart | 39 +++++++++++-------- packages/shared | 2 +- .../lib/js_bridge_callback_bean.dart | 13 +++---- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart b/lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart index 5c52bc23..02bfbbeb 100644 --- a/lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart +++ b/lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart @@ -55,15 +55,16 @@ class OpenDAppPresenter extends CompletePresenter { String to, EtherAmount? gasPrice, Uint8List? data, + BigInt? amountOfGas, ) async { loading = true; try { final gasFee = await _tokenContractUseCase.estimateGesFee( - from: from, - to: to, - gasPrice: gasPrice, - data: data, - ); + from: from, + to: to, + gasPrice: gasPrice, + data: data, + amountOfGas: amountOfGas); loading = false; return gasFee; @@ -106,22 +107,20 @@ class OpenDAppPresenter extends CompletePresenter { EtherAmount? gasPrice; EtherAmount? gasFee; EstimatedGasFee? estimatedGasFee; + BigInt? amountOfGas; if (bridge.gasPrice != null) { gasPrice = EtherAmount.fromBase10String(EtherUnit.wei, bridge.gasPrice!); } if (bridge.gas != null) { + amountOfGas = BigInt.parse(bridge.gas.toString()); gasPrice = gasPrice ?? await _tokenContractUseCase.getGasPrice(); - gasFee = EtherAmount.fromBigInt(EtherUnit.wei, - gasPrice.getInWei * BigInt.parse(bridge.gas.toString())); + gasFee = EtherAmount.fromBigInt( + EtherUnit.wei, gasPrice.getInWei * amountOfGas); } else { estimatedGasFee = await _estimatedFee( - bridge.from!, - bridge.to!, - gasPrice, - bridgeData, - ); + bridge.from!, bridge.to!, gasPrice, bridgeData, amountOfGas); if (estimatedGasFee == null) { cancel.call(); @@ -129,6 +128,14 @@ class OpenDAppPresenter extends CompletePresenter { } } + String finalFee = (gasFee?.getInWei != null + ? gasFee!.getValueInUnit(EtherUnit.ether) + : (estimatedGasFee?.gasFee ?? 0)) + .toString(); + + if (Validation.isExpoNumber(finalFee)) { + finalFee = '0.0'; + } try { final result = await showTransactionDialog( context!, @@ -136,8 +143,7 @@ class OpenDAppPresenter extends CompletePresenter { amount: amount, from: bridge.from!, to: bridge.to!, - estimatedFee: - '${gasFee?.getInWei != null ? gasFee!.getValueInUnit(EtherUnit.ether) : (estimatedGasFee?.gasFee ?? 0)}', + estimatedFee: finalFee, ); if (result != null && result) { @@ -171,6 +177,7 @@ class OpenDAppPresenter extends CompletePresenter { }); } else { addError(translate('network_not_found')); + state.webviewController?.sendError(translate('network_not_found')!, id); } } @@ -188,7 +195,6 @@ class OpenDAppPresenter extends CompletePresenter { _chainConfigurationUseCase.switchDefaultNetwork(toNetwork); _authUseCase.resetNetwork(toNetwork); notify(() => state.network = toNetwork); - // state.webviewController?.send("eth_chainId"); var config = """{ ethereum: { chainId: ${toNetwork.chainId}, @@ -199,7 +205,6 @@ class OpenDAppPresenter extends CompletePresenter { isMetaMask: true } }"""; - state.webviewController - ?.setChain(config, toNetwork.chainId, id); + state.webviewController?.setChain(config, toNetwork.chainId, id); } } diff --git a/packages/shared b/packages/shared index 398b14b9..a26a86d6 160000 --- a/packages/shared +++ b/packages/shared @@ -1 +1 @@ -Subproject commit 398b14b957dc481e2369604b0733da3cd5f24055 +Subproject commit a26a86d6c9d314ad7ad4a85ee7b85b037f7ae669 diff --git a/packages/web3_provider/lib/js_bridge_callback_bean.dart b/packages/web3_provider/lib/js_bridge_callback_bean.dart index 19a249ae..9a5ac3aa 100644 --- a/packages/web3_provider/lib/js_bridge_callback_bean.dart +++ b/packages/web3_provider/lib/js_bridge_callback_bean.dart @@ -40,12 +40,11 @@ extension Web3Result on InAppWebViewController { } void setChain(String config, int chainId, int id) async { - final script = "console.log(window.ethereum.setConfig($config))"; - final script2 = "console.log(window.ethereum.emitConnect($chainId))"; - final script3 = "console.log(window.ethereum.emitChainChanged($chainId))"; - await evaluateJavascript(source: script); - await evaluateJavascript(source: script2); - await evaluateJavascript(source: script3); - sendResult(chainId.toRadixString(16), id); + final setConfigScript = "console.log(window.ethereum.setConfig($config))"; + final emitChainChangeScript = + "console.log(window.ethereum.emitChainChanged($chainId))"; + await evaluateJavascript(source: setConfigScript); + await evaluateJavascript(source: emitChainChangeScript); + sendResult('null', id); } }