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

Commit

Permalink
Merge pull request #51 from MXCzkEVM/main
Browse files Browse the repository at this point in the history
fix: bugs
  • Loading branch information
sheenhx authored Sep 13, 2023
2 parents 0027727 + c7e0b5d commit 8d4802a
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 87 deletions.
5 changes: 5 additions & 0 deletions lib/common/utils/validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ class Validation {
return null;
}
}

static bool isExpoNumber(String input) {
RegExp regex = RegExp(r'^(\d+\.\d+e[-+]\d+)$');
return regex.hasMatch(input);
}
}
11 changes: 6 additions & 5 deletions lib/features/common/contract/token_contract_use_case.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,14 @@ class TokenContractUseCase extends ReactiveUseCase {
required String to,
EtherAmount? gasPrice,
Uint8List? data,
BigInt? amountOfGas,
}) async =>
await _repository.tokenContract.estimateGesFee(
from: from,
to: to,
gasPrice: gasPrice,
data: data,
);
from: from,
to: to,
gasPrice: gasPrice,
data: data,
amountOfGas: amountOfGas);

Future<String> sendTransaction({
required String privateKey,
Expand Down
85 changes: 53 additions & 32 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 All @@ -74,18 +75,18 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
}
}

Future<String?> _sendTransaction(
String to, EtherAmount amount, Uint8List? data,
Future<String?> _sendTransaction(String to, EtherAmount amount,
Uint8List? data, EstimatedGasFee? estimatedGasFee,
{String? from}) async {
loading = true;
try {
final res = await _tokenContractUseCase.sendTransaction(
privateKey: state.account!.privateKey,
to: to,
from: from,
amount: amount,
data: data,
);
privateKey: state.account!.privateKey,
to: to,
from: from,
amount: amount,
data: data,
estimatedGasFee: estimatedGasFee);

return res;
} catch (e, s) {
Expand All @@ -104,44 +105,53 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
final amount = amountEther.getValueInUnit(EtherUnit.ether).toString();
final bridgeData = hexToBytes(bridge.data ?? '');
EtherAmount? gasPrice;
EtherAmount? gasFee;
double? 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()));
final gasPriceDouble =
gasPrice.getValueInUnit(EtherUnit.ether).toDouble();
gasFee = gasPriceDouble * amountOfGas.toDouble();

estimatedGasFee =
EstimatedGasFee(gasPrice: gasPrice, gas: amountOfGas, gasFee: gasFee);
} 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 = estimatedGasFee.gasFee.toString();

if (Validation.isExpoNumber(finalFee)) {
finalFee = '0.0';
}

final symbol = state.network!.symbol;

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)}',
);
final result = await showTransactionDialog(context!,
title: translate('confirm_transaction')!,
amount: amount,
from: bridge.from!,
to: bridge.to!,
estimatedFee: finalFee,
symbol: symbol);

if (result != null && result) {
final hash = await _sendTransaction(bridge.to!, amountEther, bridgeData,
final hash = await _sendTransaction(
bridge.to!, amountEther, bridgeData, estimatedGasFee,
from: bridge.from);
if (hash != null) success.call(hash);
} else {
Expand Down Expand Up @@ -171,6 +181,7 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
});
} else {
addError(translate('network_not_found'));
state.webviewController?.sendError(translate('network_not_found')!, id);
}
}

Expand All @@ -188,6 +199,16 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
_chainConfigurationUseCase.switchDefaultNetwork(toNetwork);
_authUseCase.resetNetwork(toNetwork);
notify(() => state.network = toNetwork);
state.webviewController?.sendResult('null', id);
var config = """{
ethereum: {
chainId: ${toNetwork.chainId},
rpcUrl: "${toNetwork.web3RpcHttpUrl}",
address: "${state.account!.address}",
isDebug: true,
networkVersion: "${toNetwork.chainId}",
isMetaMask: true
}
}""";
state.webviewController?.setChain(config, toNetwork.chainId, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Future<bool?> showTransactionDialog(
required String to,
String? estimatedFee,
VoidCallback? onTap,
required String symbol
}) {
return showModalBottomSheet<bool>(
context: context,
Expand Down Expand Up @@ -46,6 +47,7 @@ Future<bool?> showTransactionDialog(
to: to,
estimatedFee: estimatedFee,
onTap: onTap,
symbol: symbol,
),
const SizedBox(height: 10),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ import 'package:mxc_ui/mxc_ui.dart';
import 'transaction_dialog.dart';

class TransactionInfo extends StatelessWidget {
const TransactionInfo({
Key? key,
required this.amount,
required this.from,
required this.to,
this.estimatedFee,
this.onTap,
}) : super(key: key);
const TransactionInfo(
{Key? key,
required this.amount,
required this.from,
required this.to,
this.estimatedFee,
this.onTap,
required this.symbol})
: super(key: key);

final String amount;
final String from;
final String to;
final String? estimatedFee;
final VoidCallback? onTap;
final String symbol;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -80,7 +82,7 @@ class TransactionInfo extends StatelessWidget {
),
const SizedBox(width: 4),
Text(
'MXC',
symbol,
style: FontTheme.of(context).h5.secondary(),
),
const SizedBox(height: 4),
Expand Down Expand Up @@ -110,7 +112,7 @@ class TransactionInfo extends StatelessWidget {
),
const SizedBox(width: 4),
Text(
'MXC',
symbol,
style: FontTheme.of(context).body1().copyWith(
color: ColorsTheme.of(context).grey2,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class SendNftPresenter extends CompletePresenter<SendNftState> {
late final _tokenContractUseCase = ref.read(tokenContractUseCaseProvider);
late final _nftContractUseCase = ref.read(nftContractUseCaseProvider);
late final _accountUseCase = ref.read(accountUseCaseProvider);
late final _chainConfigurationUseCase =
ref.read(chainConfigurationUseCaseProvider);
late final _nftsUseCase = ref.read(nftsUseCaseProvider);
late final TextEditingController recipientController =
TextEditingController();
Expand All @@ -36,6 +38,14 @@ class SendNftPresenter extends CompletePresenter<SendNftState> {
},
);

listen(
_chainConfigurationUseCase.selectedNetwork,
(value) {
notify(() => state.network = value);
loadPage();
},
);

listen(
_nftContractUseCase.online,
(value) => notify(() => state.online = value),
Expand All @@ -57,17 +67,18 @@ class SendNftPresenter extends CompletePresenter<SendNftState> {
}
}

final result = await showTransactionDialog(
context!,
title: _getDialogTitle(nft.name),
nft: nft,
newtork: 'MXC zkEVM',
from: state.account!.address,
to: recipient,
processType: state.processType,
estimatedFee: state.estimatedGasFee?.gasFee.toString(),
onTap: _nextTransactionStep,
);
final symbol = state.network!.symbol;

final result = await showTransactionDialog(context!,
title: _getDialogTitle(nft.name),
nft: nft,
newtork: 'MXC zkEVM',
from: state.account!.address,
to: recipient,
processType: state.processType,
estimatedFee: state.estimatedGasFee?.gasFee.toString(),
onTap: _nextTransactionStep,
symbol: symbol);

if (result != null && !result) {
notify(() => state.processType = TransactionProcessType.confirm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class SendNftState with EquatableMixin {
TransactionProcessType processType = TransactionProcessType.confirm;
Account? account;
EstimatedGasFee? estimatedGasFee;
Network? network;

@override
List<Object?> get props => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import 'transaction_info.dart';

enum TransactionProcessType { confirm, send, done }

Future<bool?> showTransactionDialog(
BuildContext context, {
String? title,
required Nft nft,
required String newtork,
required String from,
required String to,
String? estimatedFee,
TransactionProcessType? processType,
VoidCallback? onTap,
}) {
Future<bool?> showTransactionDialog(BuildContext context,
{String? title,
required Nft nft,
required String newtork,
required String from,
required String to,
String? estimatedFee,
TransactionProcessType? processType,
VoidCallback? onTap,
required String symbol}) {
return showModalBottomSheet<bool>(
context: context,
useRootNavigator: true,
Expand Down Expand Up @@ -52,6 +51,7 @@ Future<bool?> showTransactionDialog(
estimatedFee: estimatedFee,
processType: processType,
onTap: onTap,
symbol: symbol,
),
const SizedBox(height: 10),
],
Expand Down
Loading

0 comments on commit 8d4802a

Please sign in to comment.