Skip to content

Commit

Permalink
feat: Crypto transfer issue solved #2610
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed Apr 25, 2024
1 parent af5a558 commit b0ea3af
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 182 deletions.
3 changes: 3 additions & 0 deletions lib/app/shared/extension/double_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ extension DoubleExtension on double {
String decimalNumber(int n) {
int number = 1;
for (int i = 0; i < n; i++) {
if (i > toString().split('.').toList().last.length) {
break;
}
number *= 10;
}

Expand Down
23 changes: 23 additions & 0 deletions lib/app/shared/extension/string_extension.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';

import 'package:convert/convert.dart';
import 'package:decimal/decimal.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

Expand Down Expand Up @@ -43,4 +44,26 @@ extension StringExtension on String {
final String bytes = hex.encode(encode);
return bytes;
}

bool get isEVM {
if (this == 'ETH' || this == 'MATIC' || this == 'FTM' || this == 'BNB') {
return true;
}
return false;
}

String decimalNumber(int n) {
int number = 1;
for (int i = 0; i < n; i++) {
if (i > toString().split('.').toList().last.length) {
break;
}
number *= 10;
}

final twoDecimalNumber =
(Decimal.parse(this) * Decimal.parse(number.toString())).floor() /
Decimal.parse(number.toString());
return twoDecimalNumber.toDecimal().toString();
}
}
36 changes: 20 additions & 16 deletions lib/app/shared/m_web3_client/m_web3_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:web3dart/crypto.dart';
import 'package:web3dart/json_rpc.dart';
import 'package:web3dart/web3dart.dart';

class MWeb3Client {
Expand Down Expand Up @@ -229,24 +228,22 @@ class MWeb3Client {
return txId;
} catch (e, s) {
log.e('sendToken() error: $e , stack: $s');
if (e is RPCError) rethrow;
return null;
rethrow;
}
}

static Future<BigInt> estimateEthereumFee({
// maxGas, gasPrice, feeData
static Future<(BigInt, EtherAmount, BigInt)> estimateEVMFee({
required String web3RpcURL,
required EthereumAddress sender,
required EthereumAddress reciever,
required EtherAmount amount,
String? data,
}) async {
log.i('estimateEthereumFee');
late EtherAmount gasPrice = EtherAmount.inWei(BigInt.one);
final Web3Client web3Client = Web3Client(web3RpcURL, http.Client());
final gasPrice = await web3Client.getGasPrice();
try {
final Web3Client web3Client = Web3Client(web3RpcURL, http.Client());
gasPrice = await web3Client.getGasPrice();

log.i('from: ${sender.hex}');
log.i('to: ${reciever.hex}');
log.i('gasPrice: ${gasPrice.getInWei}');
Expand All @@ -264,31 +261,35 @@ class MWeb3Client {

final fee = maxGas * gasPrice.getInWei;
log.i('maxGas * gasPrice.getInWei = $fee');
return fee;
return (maxGas, gasPrice, fee);
} catch (e, s) {
log.e('e: $e, s: $s');
final fee = BigInt.from(21000) * gasPrice.getInWei;
final maxGas = BigInt.from(21000);
final fee = maxGas * gasPrice.getInWei;
log.i('maxGas - $maxGas');
log.i('2100 * gasPrice.getInWei = $fee');
return fee;
return (maxGas, gasPrice, fee);
}
}

static Future<String> sendEthereumTransaction({
static Future<String> sendEVMTransaction({
required String web3RpcURL,
required int chainId,
required String privateKey,
required EthereumAddress sender,
required EthereumAddress reciever,
required EtherAmount amount,
BigInt? gas,
EtherAmount? gasPrice,
String? data,
}) async {
log.i('sendEthereumTransaction');
final Web3Client web3Client = Web3Client(web3RpcURL, http.Client());
final int nonce = await web3Client.getTransactionCount(sender);
final EtherAmount gasPrice = await web3Client.getGasPrice();

final maxGas = await web3Client.estimateGas(
gasPrice ??= await web3Client.getGasPrice();

gas ??= await web3Client.estimateGas(
sender: sender,
to: reciever,
gasPrice: gasPrice,
Expand All @@ -305,12 +306,15 @@ class MWeb3Client {
value: amount,
data: data != null ? hexToBytes(data) : null,
nonce: nonce,
maxGas: maxGas.toInt(),
maxGas: gas.toInt(),
);

log.i('nonce: $nonce');
log.i('maxGas: ${maxGas.toInt()}');
log.i('maxGas: ${gas.toInt()}');
log.i('chainId: $chainId');
log.i('gasPrice: $gasPrice');
final fee = gas * gasPrice.getInWei;
log.i('$gas * gasPrice.getInWei = $fee');

final transactionHash = await web3Client.sendTransaction(
credentials,
Expand Down
5 changes: 2 additions & 3 deletions lib/dashboard/connection/operation/cubit/operation_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class OperationCubit extends Cubit<OperationState> {
await fetchRpcUrl(manageNetworkCubit.state.network);
log.i('web3RpcURL - $web3RpcURL');

final feeData = await MWeb3Client.estimateEthereumFee(
final (_, _, feeData) = await MWeb3Client.estimateEVMFee(
web3RpcURL: web3RpcURL,
sender: walletConnectCubit.state.transaction!.from!,
reciever: walletConnectCubit.state.transaction!.to!,
Expand Down Expand Up @@ -353,8 +353,7 @@ class OperationCubit extends Cubit<OperationState> {

log.i('rpcUrl - $rpcUrl');

final String transactionHash =
await MWeb3Client.sendEthereumTransaction(
final String transactionHash = await MWeb3Client.sendEVMTransaction(
chainId: transactionAccountData.blockchainType.chainId,
web3RpcURL: rpcUrl,
privateKey: transactionAccountData.secretKey,
Expand Down
2 changes: 1 addition & 1 deletion lib/dashboard/home/tab_bar/nft/view/nft_details_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ class SendButton extends StatelessWidget {
? (widget.nftModel as TezosNftModel).getToken()
: (widget.nftModel as EthereumNftModel).getToken(),
withdrawalAddress: '',
amount: 1,
amount: '1',
isNFT: true,
),
);
Expand Down
Loading

0 comments on commit b0ea3af

Please sign in to comment.