Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
fix: expo number control & response for unavailable network
Browse files Browse the repository at this point in the history
  • Loading branch information
reasje committed Sep 13, 2023
1 parent b0b7583 commit 8d3d399
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
39 changes: 22 additions & 17 deletions lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
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;
Expand Down Expand Up @@ -106,38 +107,43 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
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();
return;
}
}

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!,
title: translate('confirm_transaction')!,
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) {
Expand Down Expand Up @@ -171,6 +177,7 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
});
} else {
addError(translate('network_not_found'));
state.webviewController?.sendError(translate('network_not_found')!, id);
}
}

Expand All @@ -188,7 +195,6 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
_chainConfigurationUseCase.switchDefaultNetwork(toNetwork);
_authUseCase.resetNetwork(toNetwork);
notify(() => state.network = toNetwork);
// state.webviewController?.send("eth_chainId");
var config = """{
ethereum: {
chainId: ${toNetwork.chainId},
Expand All @@ -199,7 +205,6 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
isMetaMask: true
}
}""";
state.webviewController
?.setChain(config, toNetwork.chainId, id);
state.webviewController?.setChain(config, toNetwork.chainId, id);
}
}
2 changes: 1 addition & 1 deletion packages/shared
13 changes: 6 additions & 7 deletions packages/web3_provider/lib/js_bridge_callback_bean.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 8d3d399

Please sign in to comment.